aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--src/psbt.h77
-rw-r--r--src/script/sign.cpp1
-rw-r--r--src/script/sign.h76
-rw-r--r--src/script/signingprovider.h3
-rw-r--r--src/test/fuzz/script_sign.cpp1
5 files changed, 80 insertions, 78 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
{
diff --git a/src/script/sign.cpp b/src/script/sign.cpp
index 3f7060879c..d33c847d98 100644
--- a/src/script/sign.cpp
+++ b/src/script/sign.cpp
@@ -9,6 +9,7 @@
#include <key.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
+#include <script/keyorigin.h>
#include <script/signingprovider.h>
#include <script/standard.h>
#include <uint256.h>
diff --git a/src/script/sign.h b/src/script/sign.h
index 622147cd95..7e3d5e80e4 100644
--- a/src/script/sign.h
+++ b/src/script/sign.h
@@ -12,8 +12,6 @@
#include <script/interpreter.h>
#include <script/keyorigin.h>
#include <script/standard.h>
-#include <span.h>
-#include <streams.h>
class CKey;
class CKeyID;
@@ -84,80 +82,6 @@ struct SignatureData {
void MergeSignatureData(SignatureData sigdata);
};
-// 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;
- }
- }
-}
-
/** Produce a script signature using a generic signature creator. */
bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
diff --git a/src/script/signingprovider.h b/src/script/signingprovider.h
index fbce61c6a9..b8b3e03dd3 100644
--- a/src/script/signingprovider.h
+++ b/src/script/signingprovider.h
@@ -8,12 +8,11 @@
#include <key.h>
#include <pubkey.h>
+#include <script/keyorigin.h>
#include <script/script.h>
#include <script/standard.h>
#include <sync.h>
-struct KeyOriginInfo;
-
/** An interface to be implemented by keystores that support signing. */
class SigningProvider
{
diff --git a/src/test/fuzz/script_sign.cpp b/src/test/fuzz/script_sign.cpp
index 205f6c8061..1a42179724 100644
--- a/src/test/fuzz/script_sign.cpp
+++ b/src/test/fuzz/script_sign.cpp
@@ -5,6 +5,7 @@
#include <chainparams.h>
#include <chainparamsbase.h>
#include <key.h>
+#include <psbt.h>
#include <pubkey.h>
#include <script/keyorigin.h>
#include <script/sign.h>