aboutsummaryrefslogtreecommitdiff
path: root/src/psbt.h
diff options
context:
space:
mode:
authorAndrew Chow <achow101-github@achow101.com>2019-07-25 11:26:20 -0400
committerAndrew Chow <achow101-github@achow101.com>2021-12-10 08:29:47 -0500
commit5fdaf6a2adbf99c4ab2c2863fba35a0baa559fb5 (patch)
tree169fe8aeafa7b2bff2d8cb4be96bd55e836d1e7d /src/psbt.h
parent94065cc6c5a087a5657519202a4ca08db7c1d861 (diff)
downloadbitcoin-5fdaf6a2adbf99c4ab2c2863fba35a0baa559fb5.tar.xz
moveonly: Move (Un)Serialize(To/From)Vector, (De)SerializeHDKeypaths to psbt module
SerializeToVector, UnserializeFromVector, DeserializeHDKeypaths, and SerializeHDKeypaths were in sign.h where PSBT was originally implemented. Since all of the PSBT serialization has moved to its own file, these functions should follow.
Diffstat (limited to 'src/psbt.h')
-rw-r--r--src/psbt.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/psbt.h b/src/psbt.h
index 9390b38eee..36a3d2ca16 100644
--- a/src/psbt.h
+++ b/src/psbt.h
@@ -10,8 +10,11 @@
#include <policy/feerate.h>
#include <primitives/transaction.h>
#include <pubkey.h>
+#include <script/keyorigin.h>
#include <script/sign.h>
#include <script/signingprovider.h>
+#include <span.h>
+#include <streams.h>
#include <optional>
@@ -68,6 +71,80 @@ struct PSBTProprietary
}
};
+// Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream
+// The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other.
+template<typename Stream, typename... X>
+void SerializeToVector(Stream& s, const X&... args)
+{
+ WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
+ SerializeMany(s, args...);
+}
+
+// Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments
+template<typename Stream, typename... X>
+void UnserializeFromVector(Stream& s, X&... args)
+{
+ size_t expected_size = ReadCompactSize(s);
+ size_t remaining_before = s.size();
+ UnserializeMany(s, args...);
+ size_t remaining_after = s.size();
+ if (remaining_after + expected_size != remaining_before) {
+ throw std::ios_base::failure("Size of value was not the stated size");
+ }
+}
+
+// Deserialize HD keypaths into a map
+template<typename Stream>
+void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
+{
+ // Make sure that the key is the size of pubkey + 1
+ if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
+ throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
+ }
+ // Read in the pubkey from key
+ CPubKey pubkey(key.begin() + 1, key.end());
+ if (!pubkey.IsFullyValid()) {
+ throw std::ios_base::failure("Invalid pubkey");
+ }
+ if (hd_keypaths.count(pubkey) > 0) {
+ throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided");
+ }
+
+ // Read in key path
+ uint64_t value_len = ReadCompactSize(s);
+ if (value_len % 4 || value_len == 0) {
+ throw std::ios_base::failure("Invalid length for HD key path");
+ }
+
+ KeyOriginInfo keypath;
+ s >> keypath.fingerprint;
+ for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
+ uint32_t index;
+ s >> index;
+ keypath.path.push_back(index);
+ }
+
+ // Add to map
+ hd_keypaths.emplace(pubkey, std::move(keypath));
+}
+
+// Serialize HD keypaths to a stream from a map
+template<typename Stream>
+void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type)
+{
+ for (auto keypath_pair : hd_keypaths) {
+ if (!keypath_pair.first.IsValid()) {
+ throw std::ios_base::failure("Invalid CPubKey being serialized");
+ }
+ SerializeToVector(s, type, Span{keypath_pair.first});
+ WriteCompactSize(s, (keypath_pair.second.path.size() + 1) * sizeof(uint32_t));
+ s << keypath_pair.second.fingerprint;
+ for (const auto& path : keypath_pair.second.path) {
+ s << path;
+ }
+ }
+}
+
/** A structure for PSBTs which contain per-input information */
struct PSBTInput
{