Line | Count | Source |
1 | | // Copyright (c) 2009-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #ifndef BITCOIN_PSBT_H |
6 | | #define BITCOIN_PSBT_H |
7 | | |
8 | | #include <common/types.h> |
9 | | #include <musig.h> |
10 | | #include <node/transaction.h> |
11 | | #include <policy/feerate.h> |
12 | | #include <primitives/transaction.h> |
13 | | #include <pubkey.h> |
14 | | #include <script/keyorigin.h> |
15 | | #include <script/sign.h> |
16 | | #include <script/signingprovider.h> |
17 | | #include <span.h> |
18 | | #include <streams.h> |
19 | | #include <uint256.h> |
20 | | #include <util/result.h> |
21 | | #include <util/expected.h> |
22 | | |
23 | | #include <optional> |
24 | | #include <bitset> |
25 | | |
26 | | namespace node { |
27 | | enum class TransactionError; |
28 | | } // namespace node |
29 | | |
30 | | using common::PSBTError; |
31 | | |
32 | | // Magic bytes |
33 | | inline constexpr uint8_t PSBT_MAGIC_BYTES[5] = {'p', 's', 'b', 't', 0xff}; |
34 | | |
35 | | // Global types |
36 | | inline constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX = 0x00; |
37 | | inline constexpr uint8_t PSBT_GLOBAL_XPUB = 0x01; |
38 | | inline constexpr uint8_t PSBT_GLOBAL_TX_VERSION = 0x02; |
39 | | inline constexpr uint8_t PSBT_GLOBAL_FALLBACK_LOCKTIME = 0x03; |
40 | | inline constexpr uint8_t PSBT_GLOBAL_INPUT_COUNT = 0x04; |
41 | | inline constexpr uint8_t PSBT_GLOBAL_OUTPUT_COUNT = 0x05; |
42 | | inline constexpr uint8_t PSBT_GLOBAL_TX_MODIFIABLE = 0x06; |
43 | | inline constexpr uint8_t PSBT_GLOBAL_VERSION = 0xFB; |
44 | | inline constexpr uint8_t PSBT_GLOBAL_PROPRIETARY = 0xFC; |
45 | | |
46 | | // Input types |
47 | | inline constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO = 0x00; |
48 | | inline constexpr uint8_t PSBT_IN_WITNESS_UTXO = 0x01; |
49 | | inline constexpr uint8_t PSBT_IN_PARTIAL_SIG = 0x02; |
50 | | inline constexpr uint8_t PSBT_IN_SIGHASH = 0x03; |
51 | | inline constexpr uint8_t PSBT_IN_REDEEMSCRIPT = 0x04; |
52 | | inline constexpr uint8_t PSBT_IN_WITNESSSCRIPT = 0x05; |
53 | | inline constexpr uint8_t PSBT_IN_BIP32_DERIVATION = 0x06; |
54 | | inline constexpr uint8_t PSBT_IN_SCRIPTSIG = 0x07; |
55 | | inline constexpr uint8_t PSBT_IN_SCRIPTWITNESS = 0x08; |
56 | | inline constexpr uint8_t PSBT_IN_RIPEMD160 = 0x0A; |
57 | | inline constexpr uint8_t PSBT_IN_SHA256 = 0x0B; |
58 | | inline constexpr uint8_t PSBT_IN_HASH160 = 0x0C; |
59 | | inline constexpr uint8_t PSBT_IN_HASH256 = 0x0D; |
60 | | inline constexpr uint8_t PSBT_IN_PREVIOUS_TXID = 0x0e; |
61 | | inline constexpr uint8_t PSBT_IN_OUTPUT_INDEX = 0x0f; |
62 | | inline constexpr uint8_t PSBT_IN_SEQUENCE = 0x10; |
63 | | inline constexpr uint8_t PSBT_IN_REQUIRED_TIME_LOCKTIME = 0x11; |
64 | | inline constexpr uint8_t PSBT_IN_REQUIRED_HEIGHT_LOCKTIME = 0x12; |
65 | | inline constexpr uint8_t PSBT_IN_TAP_KEY_SIG = 0x13; |
66 | | inline constexpr uint8_t PSBT_IN_TAP_SCRIPT_SIG = 0x14; |
67 | | inline constexpr uint8_t PSBT_IN_TAP_LEAF_SCRIPT = 0x15; |
68 | | inline constexpr uint8_t PSBT_IN_TAP_BIP32_DERIVATION = 0x16; |
69 | | inline constexpr uint8_t PSBT_IN_TAP_INTERNAL_KEY = 0x17; |
70 | | inline constexpr uint8_t PSBT_IN_TAP_MERKLE_ROOT = 0x18; |
71 | | inline constexpr uint8_t PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS = 0x1a; |
72 | | inline constexpr uint8_t PSBT_IN_MUSIG2_PUB_NONCE = 0x1b; |
73 | | inline constexpr uint8_t PSBT_IN_MUSIG2_PARTIAL_SIG = 0x1c; |
74 | | inline constexpr uint8_t PSBT_IN_PROPRIETARY = 0xFC; |
75 | | |
76 | | // Output types |
77 | | inline constexpr uint8_t PSBT_OUT_REDEEMSCRIPT = 0x00; |
78 | | inline constexpr uint8_t PSBT_OUT_WITNESSSCRIPT = 0x01; |
79 | | inline constexpr uint8_t PSBT_OUT_BIP32_DERIVATION = 0x02; |
80 | | inline constexpr uint8_t PSBT_OUT_AMOUNT = 0x03; |
81 | | inline constexpr uint8_t PSBT_OUT_SCRIPT = 0x04; |
82 | | inline constexpr uint8_t PSBT_OUT_TAP_INTERNAL_KEY = 0x05; |
83 | | inline constexpr uint8_t PSBT_OUT_TAP_TREE = 0x06; |
84 | | inline constexpr uint8_t PSBT_OUT_TAP_BIP32_DERIVATION = 0x07; |
85 | | inline constexpr uint8_t PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS = 0x08; |
86 | | inline constexpr uint8_t PSBT_OUT_PROPRIETARY = 0xFC; |
87 | | |
88 | | // The separator is 0x00. Reading this in means that the unserializer can interpret it |
89 | | // as a 0 length key which indicates that this is the separator. The separator has no value. |
90 | | inline constexpr uint8_t PSBT_SEPARATOR = 0x00; |
91 | | |
92 | | // BIP 174 does not specify a maximum file size, but we set a limit anyway |
93 | | // to prevent reading a stream indefinitely and running out of memory. |
94 | | inline constexpr std::streamsize MAX_FILE_SIZE_PSBT{100'000'000}; // 100 MB |
95 | | |
96 | | // PSBT version number |
97 | | inline constexpr uint32_t PSBT_HIGHEST_VERSION = 2; |
98 | | |
99 | | /** A structure for PSBT proprietary types */ |
100 | | struct PSBTProprietary |
101 | | { |
102 | | uint64_t subtype; |
103 | | std::vector<unsigned char> identifier; |
104 | | std::vector<unsigned char> key; |
105 | | std::vector<unsigned char> value; |
106 | | |
107 | 36 | bool operator<(const PSBTProprietary &b) const { |
108 | 36 | return key < b.key; |
109 | 36 | } |
110 | 0 | bool operator==(const PSBTProprietary &b) const { |
111 | 0 | return key == b.key; |
112 | 0 | } |
113 | | }; |
114 | | |
115 | | // Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream |
116 | | // The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other. |
117 | | template<typename Stream, typename... X> |
118 | | void SerializeToVector(Stream& s, const X&... args) |
119 | 44.8k | { |
120 | 44.8k | SizeComputer sizecomp; |
121 | 44.8k | SerializeMany(sizecomp, args...); |
122 | 44.8k | WriteCompactSize(s, sizecomp.size()); |
123 | 44.8k | SerializeMany(s, args...); |
124 | 44.8k | } void SerializeToVector<DataStream, CompactSizeWriter>(DataStream&, CompactSizeWriter const&) Line | Count | Source | 119 | 19.0k | { | 120 | 19.0k | SizeComputer sizecomp; | 121 | 19.0k | SerializeMany(sizecomp, args...); | 122 | 19.0k | WriteCompactSize(s, sizecomp.size()); | 123 | 19.0k | SerializeMany(s, args...); | 124 | 19.0k | } |
void SerializeToVector<DataStream, ParamsWrapper<TransactionSerParams, CMutableTransaction>>(DataStream&, ParamsWrapper<TransactionSerParams, CMutableTransaction> const&) Line | Count | Source | 119 | 50 | { | 120 | 50 | SizeComputer sizecomp; | 121 | 50 | SerializeMany(sizecomp, args...); | 122 | 50 | WriteCompactSize(s, sizecomp.size()); | 123 | 50 | SerializeMany(s, args...); | 124 | 50 | } |
void SerializeToVector<DataStream, unsigned char, unsigned char [78]>(DataStream&, unsigned char const&, unsigned char const (&) [78]) Line | Count | Source | 119 | 4 | { | 120 | 4 | SizeComputer sizecomp; | 121 | 4 | SerializeMany(sizecomp, args...); | 122 | 4 | WriteCompactSize(s, sizecomp.size()); | 123 | 4 | SerializeMany(s, args...); | 124 | 4 | } |
void SerializeToVector<DataStream, unsigned int>(DataStream&, unsigned int const&) Line | Count | Source | 119 | 6.19k | { | 120 | 6.19k | SizeComputer sizecomp; | 121 | 6.19k | SerializeMany(sizecomp, args...); | 122 | 6.19k | WriteCompactSize(s, sizecomp.size()); | 123 | 6.19k | SerializeMany(s, args...); | 124 | 6.19k | } |
void SerializeToVector<DataStream, unsigned char>(DataStream&, unsigned char const&) Line | Count | Source | 119 | 2.40k | { | 120 | 2.40k | SizeComputer sizecomp; | 121 | 2.40k | SerializeMany(sizecomp, args...); | 122 | 2.40k | WriteCompactSize(s, sizecomp.size()); | 123 | 2.40k | SerializeMany(s, args...); | 124 | 2.40k | } |
void SerializeToVector<DataStream, ParamsWrapper<TransactionSerParams, std::shared_ptr<CTransaction const> const>>(DataStream&, ParamsWrapper<TransactionSerParams, std::shared_ptr<CTransaction const> const> const&) Line | Count | Source | 119 | 582 | { | 120 | 582 | SizeComputer sizecomp; | 121 | 582 | SerializeMany(sizecomp, args...); | 122 | 582 | WriteCompactSize(s, sizecomp.size()); | 123 | 582 | SerializeMany(s, args...); | 124 | 582 | } |
void SerializeToVector<DataStream, CTxOut>(DataStream&, CTxOut const&) Line | Count | Source | 119 | 1.32k | { | 120 | 1.32k | SizeComputer sizecomp; | 121 | 1.32k | SerializeMany(sizecomp, args...); | 122 | 1.32k | WriteCompactSize(s, sizecomp.size()); | 123 | 1.32k | SerializeMany(s, args...); | 124 | 1.32k | } |
void SerializeToVector<DataStream, CompactSizeWriter, std::span<unsigned char const, 18446744073709551615ul>>(DataStream&, CompactSizeWriter const&, std::span<unsigned char const, 18446744073709551615ul> const&) Line | Count | Source | 119 | 1.93k | { | 120 | 1.93k | SizeComputer sizecomp; | 121 | 1.93k | SerializeMany(sizecomp, args...); | 122 | 1.93k | WriteCompactSize(s, sizecomp.size()); | 123 | 1.93k | SerializeMany(s, args...); | 124 | 1.93k | } |
void SerializeToVector<DataStream, int>(DataStream&, int const&) Line | Count | Source | 119 | 39 | { | 120 | 39 | SizeComputer sizecomp; | 121 | 39 | SerializeMany(sizecomp, args...); | 122 | 39 | WriteCompactSize(s, sizecomp.size()); | 123 | 39 | SerializeMany(s, args...); | 124 | 39 | } |
void SerializeToVector<DataStream, unsigned char, XOnlyPubKey, uint256>(DataStream&, unsigned char const&, XOnlyPubKey const&, uint256 const&) Line | Count | Source | 119 | 268 | { | 120 | 268 | SizeComputer sizecomp; | 121 | 268 | SerializeMany(sizecomp, args...); | 122 | 268 | WriteCompactSize(s, sizecomp.size()); | 123 | 268 | SerializeMany(s, args...); | 124 | 268 | } |
void SerializeToVector<DataStream, unsigned char, std::span<unsigned char const, 18446744073709551615ul>>(DataStream&, unsigned char const&, std::span<unsigned char const, 18446744073709551615ul> const&) Line | Count | Source | 119 | 1.03k | { | 120 | 1.03k | SizeComputer sizecomp; | 121 | 1.03k | SerializeMany(sizecomp, args...); | 122 | 1.03k | WriteCompactSize(s, sizecomp.size()); | 123 | 1.03k | SerializeMany(s, args...); | 124 | 1.03k | } |
void SerializeToVector<DataStream, unsigned char, XOnlyPubKey>(DataStream&, unsigned char const&, XOnlyPubKey const&) Line | Count | Source | 119 | 5.70k | { | 120 | 5.70k | SizeComputer sizecomp; | 121 | 5.70k | SerializeMany(sizecomp, args...); | 122 | 5.70k | WriteCompactSize(s, sizecomp.size()); | 123 | 5.70k | SerializeMany(s, args...); | 124 | 5.70k | } |
void SerializeToVector<DataStream, uint256>(DataStream&, uint256 const&) Line | Count | Source | 119 | 877 | { | 120 | 877 | SizeComputer sizecomp; | 121 | 877 | SerializeMany(sizecomp, args...); | 122 | 877 | WriteCompactSize(s, sizecomp.size()); | 123 | 877 | SerializeMany(s, args...); | 124 | 877 | } |
void SerializeToVector<DataStream, CompactSizeWriter, std::span<unsigned char const, 18446744073709551615ul>, std::span<unsigned char const, 18446744073709551615ul>>(DataStream&, CompactSizeWriter const&, std::span<unsigned char const, 18446744073709551615ul> const&, std::span<unsigned char const, 18446744073709551615ul> const&) Line | Count | Source | 119 | 538 | { | 120 | 538 | SizeComputer sizecomp; | 121 | 538 | SerializeMany(sizecomp, args...); | 122 | 538 | WriteCompactSize(s, sizecomp.size()); | 123 | 538 | SerializeMany(s, args...); | 124 | 538 | } |
void SerializeToVector<DataStream, CompactSizeWriter, std::span<unsigned char const, 18446744073709551615ul>, std::span<unsigned char const, 18446744073709551615ul>, uint256>(DataStream&, CompactSizeWriter const&, std::span<unsigned char const, 18446744073709551615ul> const&, std::span<unsigned char const, 18446744073709551615ul> const&, uint256 const&) Line | Count | Source | 119 | 762 | { | 120 | 762 | SizeComputer sizecomp; | 121 | 762 | SerializeMany(sizecomp, args...); | 122 | 762 | WriteCompactSize(s, sizecomp.size()); | 123 | 762 | SerializeMany(s, args...); | 124 | 762 | } |
void SerializeToVector<DataStream, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>>(DataStream&, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>> const&) Line | Count | Source | 119 | 264 | { | 120 | 264 | SizeComputer sizecomp; | 121 | 264 | SerializeMany(sizecomp, args...); | 122 | 264 | WriteCompactSize(s, sizecomp.size()); | 123 | 264 | SerializeMany(s, args...); | 124 | 264 | } |
void SerializeToVector<DataStream, transaction_identifier<false>>(DataStream&, transaction_identifier<false> const&) Line | Count | Source | 119 | 1.48k | { | 120 | 1.48k | SizeComputer sizecomp; | 121 | 1.48k | SerializeMany(sizecomp, args...); | 122 | 1.48k | WriteCompactSize(s, sizecomp.size()); | 123 | 1.48k | SerializeMany(s, args...); | 124 | 1.48k | } |
void SerializeToVector<DataStream, long>(DataStream&, long const&) Line | Count | Source | 119 | 2.31k | { | 120 | 2.31k | SizeComputer sizecomp; | 121 | 2.31k | SerializeMany(sizecomp, args...); | 122 | 2.31k | WriteCompactSize(s, sizecomp.size()); | 123 | 2.31k | SerializeMany(s, args...); | 124 | 2.31k | } |
|
125 | | |
126 | | // Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments |
127 | | template<typename Stream, typename... X> |
128 | | void UnserializeFromVector(Stream& s, X&&... args) |
129 | 23.4k | { |
130 | 23.4k | size_t expected_size = ReadCompactSize(s); |
131 | 23.4k | size_t remaining_before = s.size(); |
132 | 23.4k | UnserializeMany(s, args...); |
133 | 23.4k | size_t remaining_after = s.size(); |
134 | 23.4k | if (remaining_after + expected_size != remaining_before) { |
135 | 3 | throw std::ios_base::failure("Size of value was not the stated size"); |
136 | 3 | } |
137 | 23.4k | } void UnserializeFromVector<DataStream, ParamsWrapper<TransactionSerParams, CMutableTransaction>>(DataStream&, ParamsWrapper<TransactionSerParams, CMutableTransaction>&&) Line | Count | Source | 129 | 1 | { | 130 | 1 | size_t expected_size = ReadCompactSize(s); | 131 | 1 | size_t remaining_before = s.size(); | 132 | 1 | UnserializeMany(s, args...); | 133 | 1 | size_t remaining_after = s.size(); | 134 | 1 | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 1 | } |
Unexecuted instantiation: void UnserializeFromVector<DataStream, unsigned int&>(DataStream&, unsigned int&) Unexecuted instantiation: void UnserializeFromVector<DataStream, CompactSizeReader&>(DataStream&, CompactSizeReader&) Unexecuted instantiation: void UnserializeFromVector<DataStream, unsigned char&>(DataStream&, unsigned char&) Unexecuted instantiation: void UnserializeFromVector<DataStream, ParamsWrapper<TransactionSerParams, std::shared_ptr<CTransaction const>>>(DataStream&, ParamsWrapper<TransactionSerParams, std::shared_ptr<CTransaction const>>&&) Unexecuted instantiation: void UnserializeFromVector<DataStream, CTxOut&>(DataStream&, CTxOut&) Unexecuted instantiation: void UnserializeFromVector<DataStream, int&>(DataStream&, int&) Unexecuted instantiation: void UnserializeFromVector<DataStream, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>&>(DataStream&, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>&) Unexecuted instantiation: void UnserializeFromVector<DataStream, transaction_identifier<false>&>(DataStream&, transaction_identifier<false>&) Unexecuted instantiation: void UnserializeFromVector<DataStream, XOnlyPubKey&>(DataStream&, XOnlyPubKey&) Unexecuted instantiation: void UnserializeFromVector<DataStream, uint256&>(DataStream&, uint256&) Unexecuted instantiation: void UnserializeFromVector<DataStream, long&>(DataStream&, long&) void UnserializeFromVector<SpanReader, ParamsWrapper<TransactionSerParams, CMutableTransaction>>(SpanReader&, ParamsWrapper<TransactionSerParams, CMutableTransaction>&&) Line | Count | Source | 129 | 199 | { | 130 | 199 | size_t expected_size = ReadCompactSize(s); | 131 | 199 | size_t remaining_before = s.size(); | 132 | 199 | UnserializeMany(s, args...); | 133 | 199 | size_t remaining_after = s.size(); | 134 | 199 | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 199 | } |
void UnserializeFromVector<SpanReader, unsigned int&>(SpanReader&, unsigned int&) Line | Count | Source | 129 | 8.75k | { | 130 | 8.75k | size_t expected_size = ReadCompactSize(s); | 131 | 8.75k | size_t remaining_before = s.size(); | 132 | 8.75k | UnserializeMany(s, args...); | 133 | 8.75k | size_t remaining_after = s.size(); | 134 | 8.75k | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 8.75k | } |
void UnserializeFromVector<SpanReader, CompactSizeReader&>(SpanReader&, CompactSizeReader&) Line | Count | Source | 129 | 3.26k | { | 130 | 3.26k | size_t expected_size = ReadCompactSize(s); | 131 | 3.26k | size_t remaining_before = s.size(); | 132 | 3.26k | UnserializeMany(s, args...); | 133 | 3.26k | size_t remaining_after = s.size(); | 134 | 3.26k | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 3.26k | } |
void UnserializeFromVector<SpanReader, unsigned char&>(SpanReader&, unsigned char&) Line | Count | Source | 129 | 13 | { | 130 | 13 | size_t expected_size = ReadCompactSize(s); | 131 | 13 | size_t remaining_before = s.size(); | 132 | 13 | UnserializeMany(s, args...); | 133 | 13 | size_t remaining_after = s.size(); | 134 | 13 | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 13 | } |
void UnserializeFromVector<SpanReader, ParamsWrapper<TransactionSerParams, std::shared_ptr<CTransaction const>>>(SpanReader&, ParamsWrapper<TransactionSerParams, std::shared_ptr<CTransaction const>>&&) Line | Count | Source | 129 | 474 | { | 130 | 474 | size_t expected_size = ReadCompactSize(s); | 131 | 474 | size_t remaining_before = s.size(); | 132 | 474 | UnserializeMany(s, args...); | 133 | 474 | size_t remaining_after = s.size(); | 134 | 474 | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 474 | } |
void UnserializeFromVector<SpanReader, CTxOut&>(SpanReader&, CTxOut&) Line | Count | Source | 129 | 1.85k | { | 130 | 1.85k | size_t expected_size = ReadCompactSize(s); | 131 | 1.85k | size_t remaining_before = s.size(); | 132 | 1.85k | UnserializeMany(s, args...); | 133 | 1.85k | size_t remaining_after = s.size(); | 134 | 1.85k | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 1.85k | } |
void UnserializeFromVector<SpanReader, int&>(SpanReader&, int&) Line | Count | Source | 129 | 82 | { | 130 | 82 | size_t expected_size = ReadCompactSize(s); | 131 | 82 | size_t remaining_before = s.size(); | 132 | 82 | UnserializeMany(s, args...); | 133 | 82 | size_t remaining_after = s.size(); | 134 | 82 | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 82 | } |
void UnserializeFromVector<SpanReader, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>&>(SpanReader&, std::vector<std::vector<unsigned char, std::allocator<unsigned char>>, std::allocator<std::vector<unsigned char, std::allocator<unsigned char>>>>&) Line | Count | Source | 129 | 105 | { | 130 | 105 | size_t expected_size = ReadCompactSize(s); | 131 | 105 | size_t remaining_before = s.size(); | 132 | 105 | UnserializeMany(s, args...); | 133 | 105 | size_t remaining_after = s.size(); | 134 | 105 | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 105 | } |
void UnserializeFromVector<SpanReader, transaction_identifier<false>&>(SpanReader&, transaction_identifier<false>&) Line | Count | Source | 129 | 1.93k | { | 130 | 1.93k | size_t expected_size = ReadCompactSize(s); | 131 | 1.93k | size_t remaining_before = s.size(); | 132 | 1.93k | UnserializeMany(s, args...); | 133 | 1.93k | size_t remaining_after = s.size(); | 134 | 1.93k | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 1.93k | } |
void UnserializeFromVector<SpanReader, XOnlyPubKey&>(SpanReader&, XOnlyPubKey&) Line | Count | Source | 129 | 2.23k | { | 130 | 2.23k | size_t expected_size = ReadCompactSize(s); | 131 | 2.23k | size_t remaining_before = s.size(); | 132 | 2.23k | UnserializeMany(s, args...); | 133 | 2.23k | size_t remaining_after = s.size(); | 134 | 2.23k | if (remaining_after + expected_size != remaining_before) { | 135 | 2 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 2 | } | 137 | 2.23k | } |
void UnserializeFromVector<SpanReader, uint256&>(SpanReader&, uint256&) Line | Count | Source | 129 | 1.52k | { | 130 | 1.52k | size_t expected_size = ReadCompactSize(s); | 131 | 1.52k | size_t remaining_before = s.size(); | 132 | 1.52k | UnserializeMany(s, args...); | 133 | 1.52k | size_t remaining_after = s.size(); | 134 | 1.52k | if (remaining_after + expected_size != remaining_before) { | 135 | 1 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 1 | } | 137 | 1.52k | } |
void UnserializeFromVector<SpanReader, long&>(SpanReader&, long&) Line | Count | Source | 129 | 3.02k | { | 130 | 3.02k | size_t expected_size = ReadCompactSize(s); | 131 | 3.02k | size_t remaining_before = s.size(); | 132 | 3.02k | UnserializeMany(s, args...); | 133 | 3.02k | size_t remaining_after = s.size(); | 134 | 3.02k | if (remaining_after + expected_size != remaining_before) { | 135 | 0 | throw std::ios_base::failure("Size of value was not the stated size"); | 136 | 0 | } | 137 | 3.02k | } |
|
138 | | |
139 | | // Deserialize bytes of given length from the stream as a KeyOriginInfo |
140 | | template<typename Stream> |
141 | | KeyOriginInfo DeserializeKeyOrigin(Stream& s, uint64_t length) |
142 | 11.1k | { |
143 | | // Read in key path |
144 | 11.1k | if (length % 4 || length == 0) { |
145 | 0 | throw std::ios_base::failure("Invalid length for HD key path"); |
146 | 0 | } |
147 | | |
148 | 11.1k | KeyOriginInfo hd_keypath; |
149 | 11.1k | s >> hd_keypath.fingerprint; |
150 | 36.0k | for (unsigned int i = 4; i < length; i += sizeof(uint32_t)) { |
151 | 24.9k | uint32_t index; |
152 | 24.9k | s >> index; |
153 | 24.9k | hd_keypath.path.push_back(index); |
154 | 24.9k | } |
155 | 11.1k | return hd_keypath; |
156 | 11.1k | } Unexecuted instantiation: KeyOriginInfo DeserializeKeyOrigin<DataStream>(DataStream&, unsigned long) KeyOriginInfo DeserializeKeyOrigin<SpanReader>(SpanReader&, unsigned long) Line | Count | Source | 142 | 11.1k | { | 143 | | // Read in key path | 144 | 11.1k | if (length % 4 || length == 0) { | 145 | 0 | throw std::ios_base::failure("Invalid length for HD key path"); | 146 | 0 | } | 147 | | | 148 | 11.1k | KeyOriginInfo hd_keypath; | 149 | 11.1k | s >> hd_keypath.fingerprint; | 150 | 36.0k | for (unsigned int i = 4; i < length; i += sizeof(uint32_t)) { | 151 | 24.9k | uint32_t index; | 152 | 24.9k | s >> index; | 153 | 24.9k | hd_keypath.path.push_back(index); | 154 | 24.9k | } | 155 | 11.1k | return hd_keypath; | 156 | 11.1k | } |
|
157 | | |
158 | | // Deserialize a length prefixed KeyOriginInfo from a stream |
159 | | template<typename Stream> |
160 | | void DeserializeHDKeypath(Stream& s, KeyOriginInfo& hd_keypath) |
161 | 926 | { |
162 | 926 | hd_keypath = DeserializeKeyOrigin(s, ReadCompactSize(s)); |
163 | 926 | } Unexecuted instantiation: void DeserializeHDKeypath<DataStream>(DataStream&, KeyOriginInfo&) void DeserializeHDKeypath<SpanReader>(SpanReader&, KeyOriginInfo&) Line | Count | Source | 161 | 926 | { | 162 | 926 | hd_keypath = DeserializeKeyOrigin(s, ReadCompactSize(s)); | 163 | 926 | } |
|
164 | | |
165 | | // Deserialize HD keypaths into a map |
166 | | template<typename Stream> |
167 | | void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths) |
168 | 917 | { |
169 | | // Make sure that the key is the size of pubkey + 1 |
170 | 917 | if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) { |
171 | 2 | throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath"); |
172 | 2 | } |
173 | | // Read in the pubkey from key |
174 | 915 | CPubKey pubkey(key.begin() + 1, key.end()); |
175 | 915 | if (!pubkey.IsFullyValid()) { |
176 | 0 | throw std::ios_base::failure("Invalid pubkey"); |
177 | 0 | } |
178 | | |
179 | 915 | KeyOriginInfo keypath; |
180 | 915 | DeserializeHDKeypath(s, keypath); |
181 | | |
182 | | // Add to map |
183 | 915 | hd_keypaths.emplace(pubkey, std::move(keypath)); |
184 | 915 | } Unexecuted instantiation: void DeserializeHDKeypaths<DataStream>(DataStream&, std::vector<unsigned char, std::allocator<unsigned char>> const&, std::map<CPubKey, KeyOriginInfo, std::less<CPubKey>, std::allocator<std::pair<CPubKey const, KeyOriginInfo>>>&) void DeserializeHDKeypaths<SpanReader>(SpanReader&, std::vector<unsigned char, std::allocator<unsigned char>> const&, std::map<CPubKey, KeyOriginInfo, std::less<CPubKey>, std::allocator<std::pair<CPubKey const, KeyOriginInfo>>>&) Line | Count | Source | 168 | 917 | { | 169 | | // Make sure that the key is the size of pubkey + 1 | 170 | 917 | if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) { | 171 | 2 | throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath"); | 172 | 2 | } | 173 | | // Read in the pubkey from key | 174 | 915 | CPubKey pubkey(key.begin() + 1, key.end()); | 175 | 915 | if (!pubkey.IsFullyValid()) { | 176 | 0 | throw std::ios_base::failure("Invalid pubkey"); | 177 | 0 | } | 178 | | | 179 | 915 | KeyOriginInfo keypath; | 180 | 915 | DeserializeHDKeypath(s, keypath); | 181 | | | 182 | | // Add to map | 183 | 915 | hd_keypaths.emplace(pubkey, std::move(keypath)); | 184 | 915 | } |
|
185 | | |
186 | | // Serialize a KeyOriginInfo to a stream |
187 | | template<typename Stream> |
188 | | void SerializeKeyOrigin(Stream& s, KeyOriginInfo hd_keypath) |
189 | 6.53k | { |
190 | 6.53k | s << hd_keypath.fingerprint; |
191 | 15.2k | for (const auto& path : hd_keypath.path) { |
192 | 15.2k | s << path; |
193 | 15.2k | } |
194 | 6.53k | } void SerializeKeyOrigin<DataStream>(DataStream&, KeyOriginInfo) Line | Count | Source | 189 | 830 | { | 190 | 830 | s << hd_keypath.fingerprint; | 191 | 3.01k | for (const auto& path : hd_keypath.path) { | 192 | 3.01k | s << path; | 193 | 3.01k | } | 194 | 830 | } |
void SerializeKeyOrigin<VectorWriter>(VectorWriter&, KeyOriginInfo) Line | Count | Source | 189 | 5.70k | { | 190 | 5.70k | s << hd_keypath.fingerprint; | 191 | 12.2k | for (const auto& path : hd_keypath.path) { | 192 | 12.2k | s << path; | 193 | 12.2k | } | 194 | 5.70k | } |
|
195 | | |
196 | | // Serialize a length prefixed KeyOriginInfo to a stream |
197 | | template<typename Stream> |
198 | | void SerializeHDKeypath(Stream& s, KeyOriginInfo hd_keypath) |
199 | 830 | { |
200 | 830 | WriteCompactSize(s, (hd_keypath.path.size() + 1) * sizeof(uint32_t)); |
201 | 830 | SerializeKeyOrigin(s, hd_keypath); |
202 | 830 | } |
203 | | |
204 | | // Serialize HD keypaths to a stream from a map |
205 | | template<typename Stream> |
206 | | void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type) |
207 | 3.65k | { |
208 | 3.65k | for (const auto& keypath_pair : hd_keypaths) { |
209 | 826 | if (!keypath_pair.first.IsValid()) { |
210 | 0 | throw std::ios_base::failure("Invalid CPubKey being serialized"); |
211 | 0 | } |
212 | 826 | SerializeToVector(s, type, std::span{keypath_pair.first}); |
213 | 826 | SerializeHDKeypath(s, keypath_pair.second); |
214 | 826 | } |
215 | 3.65k | } |
216 | | |
217 | | // Deserialize a PSBT_{IN/OUT}_MUSIG2_PARTICIPANT_PUBKEYS field |
218 | | template<typename Stream> |
219 | | void DeserializeMuSig2ParticipantPubkeys(Stream& s, SpanReader& skey, std::map<CPubKey, std::vector<CPubKey>>& out, std::string context) |
220 | 1.84k | { |
221 | 1.84k | std::array<unsigned char, CPubKey::COMPRESSED_SIZE> agg_pubkey_bytes; |
222 | 1.84k | skey >> std::as_writable_bytes(std::span{agg_pubkey_bytes}); |
223 | 1.84k | CPubKey agg_pubkey(agg_pubkey_bytes); |
224 | 1.84k | if (!agg_pubkey.IsFullyValid()) { |
225 | 3 | throw std::ios_base::failure(context + " musig2 aggregate pubkey is invalid"); |
226 | 3 | } |
227 | | |
228 | 1.84k | std::vector<CPubKey> participants; |
229 | 1.84k | std::vector<unsigned char> val; |
230 | 1.84k | s >> val; |
231 | 1.84k | SpanReader s_val{val}; |
232 | 6.98k | while (s_val.size() >= CPubKey::COMPRESSED_SIZE) { |
233 | 5.14k | std::array<unsigned char, CPubKey::COMPRESSED_SIZE> part_pubkey_bytes; |
234 | 5.14k | s_val >> std::as_writable_bytes(std::span{part_pubkey_bytes}); |
235 | 5.14k | CPubKey participant(part_pubkey_bytes); |
236 | 5.14k | if (!participant.IsFullyValid()) { |
237 | 2 | throw std::ios_base::failure(context + " musig2 participant pubkey is invalid"); |
238 | 2 | } |
239 | 5.14k | participants.push_back(participant); |
240 | 5.14k | } |
241 | 1.84k | if (!s_val.empty()) { |
242 | 1 | throw std::ios_base::failure(context + " musig2 participants pubkeys value size is not a multiple of 33"); |
243 | 1 | } |
244 | | |
245 | 1.84k | out.emplace(agg_pubkey, participants); |
246 | 1.84k | } Unexecuted instantiation: void DeserializeMuSig2ParticipantPubkeys<DataStream>(DataStream&, SpanReader&, std::map<CPubKey, std::vector<CPubKey, std::allocator<CPubKey>>, std::less<CPubKey>, std::allocator<std::pair<CPubKey const, std::vector<CPubKey, std::allocator<CPubKey>>>>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>) void DeserializeMuSig2ParticipantPubkeys<SpanReader>(SpanReader&, SpanReader&, std::map<CPubKey, std::vector<CPubKey, std::allocator<CPubKey>>, std::less<CPubKey>, std::allocator<std::pair<CPubKey const, std::vector<CPubKey, std::allocator<CPubKey>>>>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>) Line | Count | Source | 220 | 1.84k | { | 221 | 1.84k | std::array<unsigned char, CPubKey::COMPRESSED_SIZE> agg_pubkey_bytes; | 222 | 1.84k | skey >> std::as_writable_bytes(std::span{agg_pubkey_bytes}); | 223 | 1.84k | CPubKey agg_pubkey(agg_pubkey_bytes); | 224 | 1.84k | if (!agg_pubkey.IsFullyValid()) { | 225 | 3 | throw std::ios_base::failure(context + " musig2 aggregate pubkey is invalid"); | 226 | 3 | } | 227 | | | 228 | 1.84k | std::vector<CPubKey> participants; | 229 | 1.84k | std::vector<unsigned char> val; | 230 | 1.84k | s >> val; | 231 | 1.84k | SpanReader s_val{val}; | 232 | 6.98k | while (s_val.size() >= CPubKey::COMPRESSED_SIZE) { | 233 | 5.14k | std::array<unsigned char, CPubKey::COMPRESSED_SIZE> part_pubkey_bytes; | 234 | 5.14k | s_val >> std::as_writable_bytes(std::span{part_pubkey_bytes}); | 235 | 5.14k | CPubKey participant(part_pubkey_bytes); | 236 | 5.14k | if (!participant.IsFullyValid()) { | 237 | 2 | throw std::ios_base::failure(context + " musig2 participant pubkey is invalid"); | 238 | 2 | } | 239 | 5.14k | participants.push_back(participant); | 240 | 5.14k | } | 241 | 1.84k | if (!s_val.empty()) { | 242 | 1 | throw std::ios_base::failure(context + " musig2 participants pubkeys value size is not a multiple of 33"); | 243 | 1 | } | 244 | | | 245 | 1.84k | out.emplace(agg_pubkey, participants); | 246 | 1.84k | } |
|
247 | | |
248 | | // Deserialize the MuSig2 participant identifiers from PSBT_MUSIG2_{PUBNONCE/PARTIAL_SIG} fields |
249 | | // Both fields contain the same data after the type byte - aggregate pubkey | participant pubkey | leaf script hash |
250 | | template<typename Stream> |
251 | | void DeserializeMuSig2ParticipantDataIdentifier(Stream& skey, CPubKey& agg_pub, CPubKey& part_pub, uint256& leaf_hash) |
252 | 2.32k | { |
253 | 2.32k | leaf_hash.SetNull(); |
254 | | |
255 | 2.32k | std::array<unsigned char, CPubKey::COMPRESSED_SIZE> part_pubkey_bytes; |
256 | 2.32k | std::array<unsigned char, CPubKey::COMPRESSED_SIZE> agg_pubkey_bytes; |
257 | | |
258 | 2.32k | skey >> std::as_writable_bytes(std::span{part_pubkey_bytes}) >> std::as_writable_bytes(std::span{agg_pubkey_bytes}); |
259 | 2.32k | agg_pub.Set(agg_pubkey_bytes.begin(), agg_pubkey_bytes.end()); |
260 | 2.32k | if (!agg_pub.IsFullyValid()) { |
261 | 2 | throw std::ios_base::failure("musig2 aggregate pubkey is invalid"); |
262 | 2 | } |
263 | | |
264 | 2.32k | part_pub.Set(part_pubkey_bytes.begin(), part_pubkey_bytes.end()); |
265 | 2.32k | if (!part_pub.IsFullyValid()) { |
266 | 2 | throw std::ios_base::failure("musig2 participant pubkey is invalid"); |
267 | 2 | } |
268 | | |
269 | 2.32k | if (!skey.empty()) { |
270 | 1.36k | skey >> leaf_hash; |
271 | 1.36k | } |
272 | 2.32k | } |
273 | | |
274 | 39.6k | static inline void ExpectedKeySize(const std::string& key_name, const std::vector<unsigned char>& key, uint64_t expected_size) { |
275 | 39.6k | if (key.size() != expected_size) { |
276 | 15 | throw std::ios_base::failure(tfm::format("Size of key was not %d for the type %s", expected_size, key_name)); |
277 | 15 | } |
278 | 39.6k | } Unexecuted instantiation: psbt_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: wallet_test_fixture.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: db_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: coinselector_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: coinselection_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: feebumper_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: group_outputs_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: ismine_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) psbt_wallet_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Line | Count | Source | 274 | 1 | static inline void ExpectedKeySize(const std::string& key_name, const std::vector<unsigned char>& key, uint64_t expected_size) { | 275 | 1 | if (key.size() != expected_size) { | 276 | 0 | throw std::ios_base::failure(tfm::format("Size of key was not %d for the type %s", expected_size, key_name)); | 277 | 0 | } | 278 | 1 | } |
Unexecuted instantiation: scriptpubkeyman_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: spend_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: wallet_interfaces_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: wallet_rpc_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: wallet_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: wallet_transaction_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: walletdb_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: walletload_tests.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: util.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: rawtransaction.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: init.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) psbt.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Line | Count | Source | 274 | 39.6k | static inline void ExpectedKeySize(const std::string& key_name, const std::vector<unsigned char>& key, uint64_t expected_size) { | 275 | 39.6k | if (key.size() != expected_size) { | 276 | 15 | throw std::ios_base::failure(tfm::format("Size of key was not %d for the type %s", expected_size, key_name)); | 277 | 15 | } | 278 | 39.6k | } |
Unexecuted instantiation: interfaces.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: load.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: receive.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: wallet.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: scan.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: scriptpubkeyman.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: spend.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: walletdb.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: export.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: external_signer_scriptpubkeyman.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: feebumper.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: fees.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: addresses.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: backup.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: coins.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: encrypt.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: signmessage.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: transactions.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) Unexecuted instantiation: external_signer.cpp:ExpectedKeySize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, unsigned long) |
279 | | |
280 | | /** A structure for PSBTs which contain per-input information */ |
281 | | class PSBTInput |
282 | | { |
283 | | private: |
284 | | uint32_t m_psbt_version; |
285 | | |
286 | | public: |
287 | | CTransactionRef non_witness_utxo; |
288 | | CTxOut witness_utxo; |
289 | | CScript redeem_script; |
290 | | CScript witness_script; |
291 | | CScript final_script_sig; |
292 | | CScriptWitness final_script_witness; |
293 | | std::map<CPubKey, KeyOriginInfo> hd_keypaths; |
294 | | std::map<CKeyID, SigPair> partial_sigs; |
295 | | std::map<uint160, std::vector<unsigned char>> ripemd160_preimages; |
296 | | std::map<uint256, std::vector<unsigned char>> sha256_preimages; |
297 | | std::map<uint160, std::vector<unsigned char>> hash160_preimages; |
298 | | std::map<uint256, std::vector<unsigned char>> hash256_preimages; |
299 | | |
300 | | Txid prev_txid; |
301 | | uint32_t prev_out; |
302 | | std::optional<uint32_t> sequence; |
303 | | std::optional<uint32_t> time_locktime; |
304 | | std::optional<uint32_t> height_locktime; |
305 | | |
306 | | // Taproot fields |
307 | | std::vector<unsigned char> m_tap_key_sig; |
308 | | std::map<std::pair<XOnlyPubKey, uint256>, std::vector<unsigned char>> m_tap_script_sigs; |
309 | | std::map<std::pair<std::vector<unsigned char>, int>, std::set<std::vector<unsigned char>, ShortestVectorFirstComparator>> m_tap_scripts; |
310 | | std::map<XOnlyPubKey, std::pair<std::set<uint256>, KeyOriginInfo>> m_tap_bip32_paths; |
311 | | XOnlyPubKey m_tap_internal_key; |
312 | | uint256 m_tap_merkle_root; |
313 | | |
314 | | // MuSig2 fields |
315 | | std::map<CPubKey, std::vector<CPubKey>> m_musig2_participants; |
316 | | // Key is the aggregate pubkey and the script leaf hash, value is a map of participant pubkey to pubnonce |
317 | | std::map<std::pair<CPubKey, uint256>, std::map<CPubKey, std::vector<uint8_t>>> m_musig2_pubnonces; |
318 | | // Key is the aggregate pubkey and the script leaf hash, value is a map of participant pubkey to partial_sig |
319 | | std::map<std::pair<CPubKey, uint256>, std::map<CPubKey, uint256>> m_musig2_partial_sigs; |
320 | | |
321 | | std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown; |
322 | | std::set<PSBTProprietary> m_proprietary; |
323 | | std::optional<int> sighash_type; |
324 | | |
325 | | void FillSignatureData(SignatureData& sigdata) const; |
326 | | void FromSignatureData(const SignatureData& sigdata); |
327 | | void Merge(const PSBTInput& input); |
328 | 37 | uint32_t GetVersion() const { return m_psbt_version; } |
329 | | COutPoint GetOutPoint() const; |
330 | | /** |
331 | | * Retrieves the UTXO for this input |
332 | | * |
333 | | * @param[out] utxo The UTXO of this input |
334 | | * @return Whether the UTXO could be retrieved |
335 | | */ |
336 | | bool GetUTXO(CTxOut& utxo) const; |
337 | | bool HasSignatures() const; |
338 | | |
339 | | explicit PSBTInput(uint32_t psbt_version, const Txid& prev_txid, uint32_t prev_out, std::optional<uint32_t> sequence = std::nullopt) |
340 | 2.02k | : m_psbt_version(psbt_version), |
341 | 2.02k | prev_txid(prev_txid), |
342 | 2.02k | prev_out(prev_out), |
343 | 2.02k | sequence(sequence) |
344 | 2.02k | { |
345 | 2.02k | assert(m_psbt_version == 0 || m_psbt_version == 2); |
346 | 2.02k | } |
347 | | |
348 | | // Construct a PSBTInput when the previous txid and output index are expected to be serialized |
349 | | template <typename Stream> |
350 | | explicit PSBTInput(deserialize_type, Stream& s, uint32_t psbt_version) |
351 | 1.93k | : m_psbt_version(psbt_version) |
352 | 1.93k | { |
353 | 1.93k | assert(m_psbt_version == 2); |
354 | 1.93k | Unserialize(s); |
355 | 1.93k | } Unexecuted instantiation: PSBTInput::PSBTInput<DataStream>(deserialize_type, DataStream&, unsigned int) PSBTInput::PSBTInput<SpanReader>(deserialize_type, SpanReader&, unsigned int) Line | Count | Source | 351 | 1.93k | : m_psbt_version(psbt_version) | 352 | 1.93k | { | 353 | 1.93k | assert(m_psbt_version == 2); | 354 | 1.93k | Unserialize(s); | 355 | 1.93k | } |
|
356 | | |
357 | | bool operator==(const PSBTInput&) const = default; |
358 | | |
359 | | template <typename Stream> |
360 | 1.56k | inline void Serialize(Stream& s) const { |
361 | | // Write the utxo |
362 | 1.56k | if (non_witness_utxo) { |
363 | 582 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_NON_WITNESS_UTXO)); |
364 | 582 | SerializeToVector(s, TX_NO_WITNESS(non_witness_utxo)); |
365 | 582 | } |
366 | 1.56k | if (!witness_utxo.IsNull()) { |
367 | 1.32k | SerializeToVector(s, CompactSizeWriter(PSBT_IN_WITNESS_UTXO)); |
368 | 1.32k | SerializeToVector(s, witness_utxo); |
369 | 1.32k | } |
370 | | |
371 | 1.56k | if (final_script_sig.empty() && final_script_witness.IsNull()) { |
372 | | // Write any partial signatures |
373 | 1.26k | for (const auto& sig_pair : partial_sigs) { |
374 | 112 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_PARTIAL_SIG), std::span{sig_pair.second.first}); |
375 | 112 | s << sig_pair.second.second; |
376 | 112 | } |
377 | | |
378 | | // Write the sighash type |
379 | 1.26k | if (sighash_type != std::nullopt) { |
380 | 39 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_SIGHASH)); |
381 | 39 | SerializeToVector(s, *sighash_type); |
382 | 39 | } |
383 | | |
384 | | // Write the redeem script |
385 | 1.26k | if (!redeem_script.empty()) { |
386 | 57 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_REDEEMSCRIPT)); |
387 | 57 | s << redeem_script; |
388 | 57 | } |
389 | | |
390 | | // Write the witness script |
391 | 1.26k | if (!witness_script.empty()) { |
392 | 70 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_WITNESSSCRIPT)); |
393 | 70 | s << witness_script; |
394 | 70 | } |
395 | | |
396 | | // Write any hd keypaths |
397 | 1.26k | SerializeHDKeypaths(s, hd_keypaths, CompactSizeWriter(PSBT_IN_BIP32_DERIVATION)); |
398 | | |
399 | | // Write any ripemd160 preimage |
400 | 1.26k | for (const auto& [hash, preimage] : ripemd160_preimages) { |
401 | 0 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_RIPEMD160), std::span{hash}); |
402 | 0 | s << preimage; |
403 | 0 | } |
404 | | |
405 | | // Write any sha256 preimage |
406 | 1.26k | for (const auto& [hash, preimage] : sha256_preimages) { |
407 | 1 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_SHA256), std::span{hash}); |
408 | 1 | s << preimage; |
409 | 1 | } |
410 | | |
411 | | // Write any hash160 preimage |
412 | 1.26k | for (const auto& [hash, preimage] : hash160_preimages) { |
413 | 0 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH160), std::span{hash}); |
414 | 0 | s << preimage; |
415 | 0 | } |
416 | | |
417 | | // Write any hash256 preimage |
418 | 1.26k | for (const auto& [hash, preimage] : hash256_preimages) { |
419 | 0 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH256), std::span{hash}); |
420 | 0 | s << preimage; |
421 | 0 | } |
422 | | |
423 | | // Write taproot key sig |
424 | 1.26k | if (!m_tap_key_sig.empty()) { |
425 | 131 | SerializeToVector(s, PSBT_IN_TAP_KEY_SIG); |
426 | 131 | s << m_tap_key_sig; |
427 | 131 | } |
428 | | |
429 | | // Write taproot script sigs |
430 | 1.26k | for (const auto& [pubkey_leaf, sig] : m_tap_script_sigs) { |
431 | 268 | const auto& [xonly, leaf_hash] = pubkey_leaf; |
432 | 268 | SerializeToVector(s, PSBT_IN_TAP_SCRIPT_SIG, xonly, leaf_hash); |
433 | 268 | s << sig; |
434 | 268 | } |
435 | | |
436 | | // Write taproot leaf scripts |
437 | 1.26k | for (const auto& [leaf, control_blocks] : m_tap_scripts) { |
438 | 860 | const auto& [script, leaf_ver] = leaf; |
439 | 1.03k | for (const auto& control_block : control_blocks) { |
440 | 1.03k | SerializeToVector(s, PSBT_IN_TAP_LEAF_SCRIPT, std::span{control_block}); |
441 | 1.03k | std::vector<unsigned char> value_v(script.begin(), script.end()); |
442 | 1.03k | value_v.push_back((uint8_t)leaf_ver); |
443 | 1.03k | s << value_v; |
444 | 1.03k | } |
445 | 860 | } |
446 | | |
447 | | // Write taproot bip32 keypaths |
448 | 3.09k | for (const auto& [xonly, leaf_origin] : m_tap_bip32_paths) { |
449 | 3.09k | const auto& [leaf_hashes, origin] = leaf_origin; |
450 | 3.09k | SerializeToVector(s, PSBT_IN_TAP_BIP32_DERIVATION, xonly); |
451 | 3.09k | std::vector<unsigned char> value; |
452 | 3.09k | VectorWriter s_value{value, 0}; |
453 | 3.09k | s_value << leaf_hashes; |
454 | 3.09k | SerializeKeyOrigin(s_value, origin); |
455 | 3.09k | s << value; |
456 | 3.09k | } |
457 | | |
458 | | // Write taproot internal key |
459 | 1.26k | if (!m_tap_internal_key.IsNull()) { |
460 | 706 | SerializeToVector(s, PSBT_IN_TAP_INTERNAL_KEY); |
461 | 706 | s << ToByteVector(m_tap_internal_key); |
462 | 706 | } |
463 | | |
464 | | // Write taproot merkle root |
465 | 1.26k | if (!m_tap_merkle_root.IsNull()) { |
466 | 571 | SerializeToVector(s, PSBT_IN_TAP_MERKLE_ROOT); |
467 | 571 | SerializeToVector(s, m_tap_merkle_root); |
468 | 571 | } |
469 | | |
470 | | // Write MuSig2 Participants |
471 | 1.26k | for (const auto& [agg_pubkey, part_pubs] : m_musig2_participants) { |
472 | 510 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS), std::span{agg_pubkey}); |
473 | 510 | std::vector<unsigned char> value; |
474 | 510 | VectorWriter s_value{value, 0}; |
475 | 1.41k | for (auto& pk : part_pubs) { |
476 | 1.41k | s_value << std::span{pk}; |
477 | 1.41k | } |
478 | 510 | s << value; |
479 | 510 | } |
480 | | |
481 | | // Write MuSig2 pubnonces |
482 | 1.26k | for (const auto& [agg_pubkey_leaf_hash, pubnonces] : m_musig2_pubnonces) { |
483 | 525 | const auto& [agg_pubkey, leaf_hash] = agg_pubkey_leaf_hash; |
484 | 994 | for (const auto& [part_pubkey, pubnonce] : pubnonces) { |
485 | 994 | if (leaf_hash.IsNull()) { |
486 | 412 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_MUSIG2_PUB_NONCE), std::span{part_pubkey}, std::span{agg_pubkey}); |
487 | 582 | } else { |
488 | 582 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_MUSIG2_PUB_NONCE), std::span{part_pubkey}, std::span{agg_pubkey}, leaf_hash); |
489 | 582 | } |
490 | 994 | s << pubnonce; |
491 | 994 | } |
492 | 525 | } |
493 | | |
494 | | // Write MuSig2 partial signatures |
495 | 1.26k | for (const auto& [agg_pubkey_leaf_hash, psigs] : m_musig2_partial_sigs) { |
496 | 208 | const auto& [agg_pubkey, leaf_hash] = agg_pubkey_leaf_hash; |
497 | 306 | for (const auto& [pubkey, psig] : psigs) { |
498 | 306 | if (leaf_hash.IsNull()) { |
499 | 126 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_MUSIG2_PARTIAL_SIG), std::span{pubkey}, std::span{agg_pubkey}); |
500 | 180 | } else { |
501 | 180 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_MUSIG2_PARTIAL_SIG), std::span{pubkey}, std::span{agg_pubkey}, leaf_hash); |
502 | 180 | } |
503 | 306 | SerializeToVector(s, psig); |
504 | 306 | } |
505 | 208 | } |
506 | 1.26k | } |
507 | | |
508 | | // Write script sig |
509 | 1.56k | if (!final_script_sig.empty()) { |
510 | 44 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_SCRIPTSIG)); |
511 | 44 | s << final_script_sig; |
512 | 44 | } |
513 | | // write script witness |
514 | 1.56k | if (!final_script_witness.IsNull()) { |
515 | 264 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_SCRIPTWITNESS)); |
516 | 264 | SerializeToVector(s, final_script_witness.stack); |
517 | 264 | } |
518 | | |
519 | | // Write PSBTv2 fields |
520 | 1.56k | if (m_psbt_version >= 2) { |
521 | | // Write prev txid, vout, sequence, and lock times |
522 | 1.48k | SerializeToVector(s, CompactSizeWriter(PSBT_IN_PREVIOUS_TXID)); |
523 | 1.48k | SerializeToVector(s, prev_txid); |
524 | | |
525 | 1.48k | SerializeToVector(s, CompactSizeWriter(PSBT_IN_OUTPUT_INDEX)); |
526 | 1.48k | SerializeToVector(s, prev_out); |
527 | | |
528 | 1.48k | if (sequence != std::nullopt) { |
529 | 1.48k | SerializeToVector(s, CompactSizeWriter(PSBT_IN_SEQUENCE)); |
530 | 1.48k | SerializeToVector(s, *sequence); |
531 | 1.48k | } |
532 | 1.48k | if (time_locktime != std::nullopt) { |
533 | 0 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_REQUIRED_TIME_LOCKTIME)); |
534 | 0 | SerializeToVector(s, *time_locktime); |
535 | 0 | } |
536 | 1.48k | if (height_locktime != std::nullopt) { |
537 | 0 | SerializeToVector(s, CompactSizeWriter(PSBT_IN_REQUIRED_HEIGHT_LOCKTIME)); |
538 | 0 | SerializeToVector(s, *height_locktime); |
539 | 0 | } |
540 | 1.48k | } |
541 | | |
542 | | // Write proprietary things |
543 | 1.56k | for (const auto& entry : m_proprietary) { |
544 | 2 | s << entry.key; |
545 | 2 | s << entry.value; |
546 | 2 | } |
547 | | |
548 | | // Write unknown things |
549 | 1.56k | for (auto& entry : unknown) { |
550 | 5 | s << entry.first; |
551 | 5 | s << entry.second; |
552 | 5 | } |
553 | | |
554 | 1.56k | s << PSBT_SEPARATOR; |
555 | 1.56k | } |
556 | | |
557 | | |
558 | | template <typename Stream> |
559 | 2.17k | inline void Unserialize(Stream& s) { |
560 | | // Used for duplicate key detection |
561 | 2.17k | std::set<std::vector<unsigned char>> key_lookup; |
562 | | // Cache whether PSBTv2 required fields were seen |
563 | 2.17k | bool found_prev_txid = false; |
564 | 2.17k | bool found_prev_out = false; |
565 | | |
566 | | // Read loop |
567 | 2.17k | bool found_sep = false; |
568 | 24.7k | while(!s.empty()) { |
569 | | // Read the key of format "<keylen><keytype><keydata>" after which |
570 | | // "key" will contain "<keytype><keydata>" |
571 | 24.7k | std::vector<unsigned char> key; |
572 | 24.7k | s >> key; |
573 | | |
574 | | // the key is empty if that was actually a separator byte |
575 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) |
576 | 24.7k | if (key.empty()) { |
577 | 2.12k | found_sep = true; |
578 | 2.12k | break; |
579 | 2.12k | } |
580 | | |
581 | | // Duplicate keys are not permitted |
582 | 22.6k | if (!key_lookup.emplace(key).second) { |
583 | 7 | throw std::ios_base::failure(tfm::format("Duplicate Key, input key \"%s\" already provided", HexStr(key))); |
584 | 7 | } |
585 | | |
586 | | // "skey" is used so that "key" is unchanged after reading keytype below |
587 | 22.5k | SpanReader skey{key}; |
588 | | // keytype is of the format compact size uint at the beginning of "key" |
589 | 22.5k | uint64_t type = ReadCompactSize(skey); |
590 | | |
591 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the |
592 | | // format "<valuelen><valuedata>" from the stream "s", and value checks |
593 | 22.5k | switch(type) { |
594 | 475 | case PSBT_IN_NON_WITNESS_UTXO: |
595 | 475 | { |
596 | 475 | ExpectedKeySize("Input Non-witness UTXO", key, 1); |
597 | | // Set the stream to unserialize with witness since this is always a valid network transaction |
598 | 475 | UnserializeFromVector(s, TX_WITH_WITNESS(non_witness_utxo)); |
599 | 475 | break; |
600 | 0 | } |
601 | 1.85k | case PSBT_IN_WITNESS_UTXO: |
602 | 1.85k | ExpectedKeySize("Input Witness UTXO", key, 1); |
603 | 1.85k | UnserializeFromVector(s, witness_utxo); |
604 | 1.85k | break; |
605 | 165 | case PSBT_IN_PARTIAL_SIG: |
606 | 165 | { |
607 | | // Make sure that the key is the size of pubkey + 1 |
608 | 165 | if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) { |
609 | 1 | throw std::ios_base::failure("Size of key was not the expected size for the type partial signature pubkey"); |
610 | 1 | } |
611 | | // Read in the pubkey from key |
612 | 164 | CPubKey pubkey(key.begin() + 1, key.end()); |
613 | 164 | if (!pubkey.IsFullyValid()) { |
614 | 0 | throw std::ios_base::failure("Invalid pubkey"); |
615 | 0 | } |
616 | | |
617 | | // Read in the signature from value |
618 | 164 | std::vector<unsigned char> sig; |
619 | 164 | s >> sig; |
620 | | |
621 | | // Check that the signature is validly encoded |
622 | 164 | if (sig.empty() || !CheckSignatureEncoding(sig, SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_STRICTENC, nullptr)) { |
623 | 0 | throw std::ios_base::failure("Signature is not a valid encoding"); |
624 | 0 | } |
625 | | |
626 | | // Add to list |
627 | 164 | partial_sigs.emplace(pubkey.GetID(), SigPair(pubkey, std::move(sig))); |
628 | 164 | break; |
629 | 164 | } |
630 | 83 | case PSBT_IN_SIGHASH: |
631 | 83 | ExpectedKeySize("Input Sighash Type", key, 1); |
632 | 83 | int sighash; |
633 | 83 | UnserializeFromVector(s, sighash); |
634 | 83 | sighash_type = sighash; |
635 | 83 | break; |
636 | 50 | case PSBT_IN_REDEEMSCRIPT: |
637 | 50 | { |
638 | 50 | ExpectedKeySize("Input redeemScript", key, 1); |
639 | 50 | s >> redeem_script; |
640 | 50 | break; |
641 | 164 | } |
642 | 109 | case PSBT_IN_WITNESSSCRIPT: |
643 | 109 | { |
644 | 109 | ExpectedKeySize("Input witnessScript", key, 1); |
645 | 109 | s >> witness_script; |
646 | 109 | break; |
647 | 164 | } |
648 | 497 | case PSBT_IN_BIP32_DERIVATION: |
649 | 497 | { |
650 | 497 | DeserializeHDKeypaths(s, key, hd_keypaths); |
651 | 497 | break; |
652 | 164 | } |
653 | 39 | case PSBT_IN_SCRIPTSIG: |
654 | 39 | { |
655 | 39 | ExpectedKeySize("Input Final scriptSig", key, 1); |
656 | 39 | s >> final_script_sig; |
657 | 39 | break; |
658 | 164 | } |
659 | 106 | case PSBT_IN_SCRIPTWITNESS: |
660 | 106 | { |
661 | 106 | ExpectedKeySize("Input Final scriptWitness", key, 1); |
662 | 106 | UnserializeFromVector(s, final_script_witness.stack); |
663 | 106 | break; |
664 | 164 | } |
665 | 3 | case PSBT_IN_RIPEMD160: |
666 | 3 | { |
667 | 3 | ExpectedKeySize("Input RIPEMD160 Preimage", key, CRIPEMD160::OUTPUT_SIZE + 1); |
668 | | // Read in the hash from key |
669 | 3 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); |
670 | 3 | uint160 hash(hash_vec); |
671 | | |
672 | | // Read in the preimage from value |
673 | 3 | std::vector<unsigned char> preimage; |
674 | 3 | s >> preimage; |
675 | | |
676 | | // Add to preimages list |
677 | 3 | ripemd160_preimages.emplace(hash, std::move(preimage)); |
678 | 3 | break; |
679 | 164 | } |
680 | 6 | case PSBT_IN_SHA256: |
681 | 6 | { |
682 | 6 | ExpectedKeySize("Input SHA256 Preimage", key, CSHA256::OUTPUT_SIZE + 1); |
683 | | // Read in the hash from key |
684 | 6 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); |
685 | 6 | uint256 hash(hash_vec); |
686 | | |
687 | | // Read in the preimage from value |
688 | 6 | std::vector<unsigned char> preimage; |
689 | 6 | s >> preimage; |
690 | | |
691 | | // Add to preimages list |
692 | 6 | sha256_preimages.emplace(hash, std::move(preimage)); |
693 | 6 | break; |
694 | 164 | } |
695 | 3 | case PSBT_IN_HASH160: |
696 | 3 | { |
697 | 3 | ExpectedKeySize("Input Hash160 Preimage", key, CHash160::OUTPUT_SIZE + 1); |
698 | | // Read in the hash from key |
699 | 3 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); |
700 | 3 | uint160 hash(hash_vec); |
701 | | |
702 | | // Read in the preimage from value |
703 | 3 | std::vector<unsigned char> preimage; |
704 | 3 | s >> preimage; |
705 | | |
706 | | // Add to preimages list |
707 | 3 | hash160_preimages.emplace(hash, std::move(preimage)); |
708 | 3 | break; |
709 | 164 | } |
710 | 3 | case PSBT_IN_HASH256: |
711 | 3 | { |
712 | 3 | ExpectedKeySize("Input Hash256 Preimage", key, CHash256::OUTPUT_SIZE + 1); |
713 | | // Read in the hash from key |
714 | 3 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); |
715 | 3 | uint256 hash(hash_vec); |
716 | | |
717 | | // Read in the preimage from value |
718 | 3 | std::vector<unsigned char> preimage; |
719 | 3 | s >> preimage; |
720 | | |
721 | | // Add to preimages list |
722 | 3 | hash256_preimages.emplace(hash, std::move(preimage)); |
723 | 3 | break; |
724 | 164 | } |
725 | 1.93k | case PSBT_IN_PREVIOUS_TXID: |
726 | 1.93k | { |
727 | 1.93k | ExpectedKeySize("Input Previous TXID", key, 1); |
728 | 1.93k | if (m_psbt_version < 2) { |
729 | 1 | throw std::ios_base::failure("Previous txid is not allowed in PSBTv0"); |
730 | 1 | } |
731 | 1.93k | UnserializeFromVector(s, prev_txid); |
732 | 1.93k | found_prev_txid = true; |
733 | 1.93k | break; |
734 | 1.93k | } |
735 | 1.93k | case PSBT_IN_OUTPUT_INDEX: |
736 | 1.93k | { |
737 | 1.93k | ExpectedKeySize("Input Previous Output's Index", key, 1); |
738 | 1.93k | if (m_psbt_version < 2) { |
739 | 1 | throw std::ios_base::failure("Previous output's index is not allowed in PSBTv0"); |
740 | 1 | } |
741 | 1.93k | UnserializeFromVector(s, prev_out); |
742 | 1.93k | found_prev_out = true; |
743 | 1.93k | break; |
744 | 1.93k | } |
745 | 1.90k | case PSBT_IN_SEQUENCE: |
746 | 1.90k | { |
747 | 1.90k | ExpectedKeySize("Input Sequence", key, 1); |
748 | 1.90k | if (m_psbt_version < 2) { |
749 | 1 | throw std::ios_base::failure("Sequence is not allowed in PSBTv0"); |
750 | 1 | } |
751 | 1.90k | sequence.emplace(); |
752 | 1.90k | UnserializeFromVector(s, *sequence); |
753 | 1.90k | break; |
754 | 1.90k | } |
755 | 15 | case PSBT_IN_REQUIRED_TIME_LOCKTIME: |
756 | 15 | { |
757 | 15 | ExpectedKeySize("Input Required Time Based Locktime", key, 1); |
758 | 15 | if (m_psbt_version < 2) { |
759 | 1 | throw std::ios_base::failure("Required time based locktime is not allowed in PSBTv0"); |
760 | 1 | } |
761 | 14 | time_locktime.emplace(); |
762 | 14 | UnserializeFromVector(s, *time_locktime); |
763 | 14 | if (*time_locktime < LOCKTIME_THRESHOLD) { |
764 | 1 | throw std::ios_base::failure("Required time based locktime is invalid (less than 500000000)"); |
765 | 1 | } |
766 | 13 | break; |
767 | 14 | } |
768 | 16 | case PSBT_IN_REQUIRED_HEIGHT_LOCKTIME: |
769 | 16 | { |
770 | 16 | ExpectedKeySize("Input Required Height Based Locktime", key, 1); |
771 | 16 | if (m_psbt_version < 2) { |
772 | 1 | throw std::ios_base::failure("Required height based locktime is not allowed in PSBTv0"); |
773 | 1 | } |
774 | 15 | height_locktime.emplace(); |
775 | 15 | UnserializeFromVector(s, *height_locktime); |
776 | 15 | if (*height_locktime >= LOCKTIME_THRESHOLD) { |
777 | 1 | throw std::ios_base::failure("Required height based locktime is invalid (greater than or equal to 500000000)"); |
778 | 14 | } else if (*height_locktime == 0) { |
779 | 1 | throw std::ios_base::failure("Required height based locktime is invalid (0)"); |
780 | 1 | } |
781 | 13 | break; |
782 | 15 | } |
783 | 187 | case PSBT_IN_TAP_KEY_SIG: |
784 | 187 | { |
785 | 187 | ExpectedKeySize("Input Taproot Key Path Signature", key, 1); |
786 | 187 | s >> m_tap_key_sig; |
787 | 187 | if (m_tap_key_sig.size() < 64) { |
788 | 1 | throw std::ios_base::failure("Input Taproot key path signature is shorter than 64 bytes"); |
789 | 186 | } else if (m_tap_key_sig.size() > 65) { |
790 | 1 | throw std::ios_base::failure("Input Taproot key path signature is longer than 65 bytes"); |
791 | 1 | } |
792 | 185 | break; |
793 | 187 | } |
794 | 383 | case PSBT_IN_TAP_SCRIPT_SIG: |
795 | 383 | { |
796 | 383 | ExpectedKeySize("Input Taproot Script Path Signature", key, 65); |
797 | 383 | SpanReader s_key{std::span{key}.subspan(1)}; |
798 | 383 | XOnlyPubKey xonly; |
799 | 383 | uint256 hash; |
800 | 383 | s_key >> xonly; |
801 | 383 | s_key >> hash; |
802 | 383 | std::vector<unsigned char> sig; |
803 | 383 | s >> sig; |
804 | 383 | if (sig.size() < 64) { |
805 | 1 | throw std::ios_base::failure("Input Taproot script path signature is shorter than 64 bytes"); |
806 | 382 | } else if (sig.size() > 65) { |
807 | 1 | throw std::ios_base::failure("Input Taproot script path signature is longer than 65 bytes"); |
808 | 1 | } |
809 | 381 | m_tap_script_sigs.emplace(std::make_pair(xonly, hash), sig); |
810 | 381 | break; |
811 | 383 | } |
812 | 1.74k | case PSBT_IN_TAP_LEAF_SCRIPT: |
813 | 1.74k | { |
814 | 1.74k | if (key.size() < 34) { |
815 | 0 | throw std::ios_base::failure("Input Taproot leaf script key is not at least 34 bytes"); |
816 | 1.74k | } else if ((key.size() - 2) % 32 != 0) { |
817 | 3 | throw std::ios_base::failure("Input Taproot leaf script key's control block size is not valid"); |
818 | 3 | } |
819 | 1.74k | std::vector<unsigned char> script_v; |
820 | 1.74k | s >> script_v; |
821 | 1.74k | if (script_v.empty()) { |
822 | 0 | throw std::ios_base::failure("Input Taproot leaf script must be at least 1 byte"); |
823 | 0 | } |
824 | 1.74k | uint8_t leaf_ver = script_v.back(); |
825 | 1.74k | script_v.pop_back(); |
826 | 1.74k | const auto leaf_script = std::make_pair(script_v, (int)leaf_ver); |
827 | 1.74k | m_tap_scripts[leaf_script].insert(std::vector<unsigned char>(key.begin() + 1, key.end())); |
828 | 1.74k | break; |
829 | 1.74k | } |
830 | 5.55k | case PSBT_IN_TAP_BIP32_DERIVATION: |
831 | 5.55k | { |
832 | 5.55k | ExpectedKeySize("Input Taproot BIP32 Keypath", key, 33); |
833 | 5.55k | SpanReader s_key{std::span{key}.subspan(1)}; |
834 | 5.55k | XOnlyPubKey xonly; |
835 | 5.55k | s_key >> xonly; |
836 | 5.55k | std::set<uint256> leaf_hashes; |
837 | 5.55k | uint64_t value_len = ReadCompactSize(s); |
838 | 5.55k | size_t before_hashes = s.size(); |
839 | 5.55k | s >> leaf_hashes; |
840 | 5.55k | size_t after_hashes = s.size(); |
841 | 5.55k | size_t hashes_len = before_hashes - after_hashes; |
842 | 5.55k | if (hashes_len > value_len) { |
843 | 1 | throw std::ios_base::failure("Input Taproot BIP32 keypath has an invalid length"); |
844 | 1 | } |
845 | 5.55k | size_t origin_len = value_len - hashes_len; |
846 | 5.55k | m_tap_bip32_paths.emplace(xonly, std::make_pair(leaf_hashes, DeserializeKeyOrigin(s, origin_len))); |
847 | 5.55k | break; |
848 | 5.55k | } |
849 | 1.22k | case PSBT_IN_TAP_INTERNAL_KEY: |
850 | 1.22k | { |
851 | 1.22k | ExpectedKeySize("Input Taproot Internal Key", key, 1); |
852 | 1.22k | UnserializeFromVector(s, m_tap_internal_key); |
853 | 1.22k | break; |
854 | 5.55k | } |
855 | 975 | case PSBT_IN_TAP_MERKLE_ROOT: |
856 | 975 | { |
857 | 975 | ExpectedKeySize("Input Taproot Merkle Root", key, 1); |
858 | 975 | UnserializeFromVector(s, m_tap_merkle_root); |
859 | 975 | break; |
860 | 5.55k | } |
861 | 979 | case PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS: |
862 | 979 | { |
863 | 979 | ExpectedKeySize("Input MuSig2 Participants Pubkeys", key, CPubKey::COMPRESSED_SIZE + 1); |
864 | 979 | DeserializeMuSig2ParticipantPubkeys(s, skey, m_musig2_participants, std::string{"Input"}); |
865 | 979 | break; |
866 | 5.55k | } |
867 | 1.77k | case PSBT_IN_MUSIG2_PUB_NONCE: |
868 | 1.77k | { |
869 | 1.77k | if (key.size() != 2 * CPubKey::COMPRESSED_SIZE + 1 && key.size() != 2 * CPubKey::COMPRESSED_SIZE + CSHA256::OUTPUT_SIZE + 1) { |
870 | 2 | throw std::ios_base::failure("Input musig2 pubnonce key is not expected size of 67 or 99 bytes"); |
871 | 2 | } |
872 | 1.77k | CPubKey agg_pub, part_pub; |
873 | 1.77k | uint256 leaf_hash; |
874 | 1.77k | DeserializeMuSig2ParticipantDataIdentifier(skey, agg_pub, part_pub, leaf_hash); |
875 | | |
876 | 1.77k | std::vector<uint8_t> pubnonce; |
877 | 1.77k | s >> pubnonce; |
878 | 1.77k | if (pubnonce.size() != MUSIG2_PUBNONCE_SIZE) { |
879 | 1 | throw std::ios_base::failure("Input musig2 pubnonce value is not 66 bytes"); |
880 | 1 | } |
881 | | |
882 | 1.77k | m_musig2_pubnonces[std::make_pair(agg_pub, leaf_hash)].emplace(part_pub, pubnonce); |
883 | 1.77k | break; |
884 | 1.77k | } |
885 | 553 | case PSBT_IN_MUSIG2_PARTIAL_SIG: |
886 | 553 | { |
887 | 553 | if (key.size() != 2 * CPubKey::COMPRESSED_SIZE + 1 && key.size() != 2 * CPubKey::COMPRESSED_SIZE + CSHA256::OUTPUT_SIZE + 1) { |
888 | 2 | throw std::ios_base::failure("Input musig2 partial sig key is not expected size of 67 or 99 bytes"); |
889 | 2 | } |
890 | 551 | CPubKey agg_pub, part_pub; |
891 | 551 | uint256 leaf_hash; |
892 | 551 | DeserializeMuSig2ParticipantDataIdentifier(skey, agg_pub, part_pub, leaf_hash); |
893 | | |
894 | 551 | uint256 partial_sig; |
895 | 551 | UnserializeFromVector(s, partial_sig); |
896 | | |
897 | 551 | m_musig2_partial_sigs[std::make_pair(agg_pub, leaf_hash)].emplace(part_pub, partial_sig); |
898 | 551 | break; |
899 | 553 | } |
900 | 5 | case PSBT_IN_PROPRIETARY: |
901 | 5 | { |
902 | 5 | PSBTProprietary this_prop; |
903 | 5 | skey >> this_prop.identifier; |
904 | 5 | this_prop.subtype = ReadCompactSize(skey); |
905 | 5 | this_prop.key = key; |
906 | | |
907 | 5 | s >> this_prop.value; |
908 | 5 | m_proprietary.insert(this_prop); |
909 | 5 | break; |
910 | 553 | } |
911 | | // Unknown stuff |
912 | 5 | default: |
913 | | // Read in the value |
914 | 5 | std::vector<unsigned char> val_bytes; |
915 | 5 | s >> val_bytes; |
916 | 5 | unknown.emplace(std::move(key), std::move(val_bytes)); |
917 | 5 | break; |
918 | 22.5k | } |
919 | 22.5k | } |
920 | | |
921 | 2.12k | if (!found_sep) { |
922 | 0 | throw std::ios_base::failure("Separator is missing at the end of an input map"); |
923 | 0 | } |
924 | | |
925 | | // Make sure required PSBTv2 fields are present |
926 | 2.12k | if (m_psbt_version >= 2) { |
927 | 1.93k | if (!found_prev_txid) { |
928 | 1 | throw std::ios_base::failure("Previous TXID is required in PSBTv2"); |
929 | 1 | } |
930 | 1.93k | if (!found_prev_out) { |
931 | 1 | throw std::ios_base::failure("Previous output's index is required in PSBTv2"); |
932 | 1 | } |
933 | 1.93k | } |
934 | 2.12k | } void PSBTInput::Unserialize<DataStream>(DataStream&) Line | Count | Source | 559 | 2 | inline void Unserialize(Stream& s) { | 560 | | // Used for duplicate key detection | 561 | 2 | std::set<std::vector<unsigned char>> key_lookup; | 562 | | // Cache whether PSBTv2 required fields were seen | 563 | 2 | bool found_prev_txid = false; | 564 | 2 | bool found_prev_out = false; | 565 | | | 566 | | // Read loop | 567 | 2 | bool found_sep = false; | 568 | 2 | while(!s.empty()) { | 569 | | // Read the key of format "<keylen><keytype><keydata>" after which | 570 | | // "key" will contain "<keytype><keydata>" | 571 | 2 | std::vector<unsigned char> key; | 572 | 2 | s >> key; | 573 | | | 574 | | // the key is empty if that was actually a separator byte | 575 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) | 576 | 2 | if (key.empty()) { | 577 | 2 | found_sep = true; | 578 | 2 | break; | 579 | 2 | } | 580 | | | 581 | | // Duplicate keys are not permitted | 582 | 0 | if (!key_lookup.emplace(key).second) { | 583 | 0 | throw std::ios_base::failure(tfm::format("Duplicate Key, input key \"%s\" already provided", HexStr(key))); | 584 | 0 | } | 585 | | | 586 | | // "skey" is used so that "key" is unchanged after reading keytype below | 587 | 0 | SpanReader skey{key}; | 588 | | // keytype is of the format compact size uint at the beginning of "key" | 589 | 0 | uint64_t type = ReadCompactSize(skey); | 590 | | | 591 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the | 592 | | // format "<valuelen><valuedata>" from the stream "s", and value checks | 593 | 0 | switch(type) { | 594 | 0 | case PSBT_IN_NON_WITNESS_UTXO: | 595 | 0 | { | 596 | 0 | ExpectedKeySize("Input Non-witness UTXO", key, 1); | 597 | | // Set the stream to unserialize with witness since this is always a valid network transaction | 598 | 0 | UnserializeFromVector(s, TX_WITH_WITNESS(non_witness_utxo)); | 599 | 0 | break; | 600 | 0 | } | 601 | 0 | case PSBT_IN_WITNESS_UTXO: | 602 | 0 | ExpectedKeySize("Input Witness UTXO", key, 1); | 603 | 0 | UnserializeFromVector(s, witness_utxo); | 604 | 0 | break; | 605 | 0 | case PSBT_IN_PARTIAL_SIG: | 606 | 0 | { | 607 | | // Make sure that the key is the size of pubkey + 1 | 608 | 0 | if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) { | 609 | 0 | throw std::ios_base::failure("Size of key was not the expected size for the type partial signature pubkey"); | 610 | 0 | } | 611 | | // Read in the pubkey from key | 612 | 0 | CPubKey pubkey(key.begin() + 1, key.end()); | 613 | 0 | if (!pubkey.IsFullyValid()) { | 614 | 0 | throw std::ios_base::failure("Invalid pubkey"); | 615 | 0 | } | 616 | | | 617 | | // Read in the signature from value | 618 | 0 | std::vector<unsigned char> sig; | 619 | 0 | s >> sig; | 620 | | | 621 | | // Check that the signature is validly encoded | 622 | 0 | if (sig.empty() || !CheckSignatureEncoding(sig, SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_STRICTENC, nullptr)) { | 623 | 0 | throw std::ios_base::failure("Signature is not a valid encoding"); | 624 | 0 | } | 625 | | | 626 | | // Add to list | 627 | 0 | partial_sigs.emplace(pubkey.GetID(), SigPair(pubkey, std::move(sig))); | 628 | 0 | break; | 629 | 0 | } | 630 | 0 | case PSBT_IN_SIGHASH: | 631 | 0 | ExpectedKeySize("Input Sighash Type", key, 1); | 632 | 0 | int sighash; | 633 | 0 | UnserializeFromVector(s, sighash); | 634 | 0 | sighash_type = sighash; | 635 | 0 | break; | 636 | 0 | case PSBT_IN_REDEEMSCRIPT: | 637 | 0 | { | 638 | 0 | ExpectedKeySize("Input redeemScript", key, 1); | 639 | 0 | s >> redeem_script; | 640 | 0 | break; | 641 | 0 | } | 642 | 0 | case PSBT_IN_WITNESSSCRIPT: | 643 | 0 | { | 644 | 0 | ExpectedKeySize("Input witnessScript", key, 1); | 645 | 0 | s >> witness_script; | 646 | 0 | break; | 647 | 0 | } | 648 | 0 | case PSBT_IN_BIP32_DERIVATION: | 649 | 0 | { | 650 | 0 | DeserializeHDKeypaths(s, key, hd_keypaths); | 651 | 0 | break; | 652 | 0 | } | 653 | 0 | case PSBT_IN_SCRIPTSIG: | 654 | 0 | { | 655 | 0 | ExpectedKeySize("Input Final scriptSig", key, 1); | 656 | 0 | s >> final_script_sig; | 657 | 0 | break; | 658 | 0 | } | 659 | 0 | case PSBT_IN_SCRIPTWITNESS: | 660 | 0 | { | 661 | 0 | ExpectedKeySize("Input Final scriptWitness", key, 1); | 662 | 0 | UnserializeFromVector(s, final_script_witness.stack); | 663 | 0 | break; | 664 | 0 | } | 665 | 0 | case PSBT_IN_RIPEMD160: | 666 | 0 | { | 667 | 0 | ExpectedKeySize("Input RIPEMD160 Preimage", key, CRIPEMD160::OUTPUT_SIZE + 1); | 668 | | // Read in the hash from key | 669 | 0 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); | 670 | 0 | uint160 hash(hash_vec); | 671 | | | 672 | | // Read in the preimage from value | 673 | 0 | std::vector<unsigned char> preimage; | 674 | 0 | s >> preimage; | 675 | | | 676 | | // Add to preimages list | 677 | 0 | ripemd160_preimages.emplace(hash, std::move(preimage)); | 678 | 0 | break; | 679 | 0 | } | 680 | 0 | case PSBT_IN_SHA256: | 681 | 0 | { | 682 | 0 | ExpectedKeySize("Input SHA256 Preimage", key, CSHA256::OUTPUT_SIZE + 1); | 683 | | // Read in the hash from key | 684 | 0 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); | 685 | 0 | uint256 hash(hash_vec); | 686 | | | 687 | | // Read in the preimage from value | 688 | 0 | std::vector<unsigned char> preimage; | 689 | 0 | s >> preimage; | 690 | | | 691 | | // Add to preimages list | 692 | 0 | sha256_preimages.emplace(hash, std::move(preimage)); | 693 | 0 | break; | 694 | 0 | } | 695 | 0 | case PSBT_IN_HASH160: | 696 | 0 | { | 697 | 0 | ExpectedKeySize("Input Hash160 Preimage", key, CHash160::OUTPUT_SIZE + 1); | 698 | | // Read in the hash from key | 699 | 0 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); | 700 | 0 | uint160 hash(hash_vec); | 701 | | | 702 | | // Read in the preimage from value | 703 | 0 | std::vector<unsigned char> preimage; | 704 | 0 | s >> preimage; | 705 | | | 706 | | // Add to preimages list | 707 | 0 | hash160_preimages.emplace(hash, std::move(preimage)); | 708 | 0 | break; | 709 | 0 | } | 710 | 0 | case PSBT_IN_HASH256: | 711 | 0 | { | 712 | 0 | ExpectedKeySize("Input Hash256 Preimage", key, CHash256::OUTPUT_SIZE + 1); | 713 | | // Read in the hash from key | 714 | 0 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); | 715 | 0 | uint256 hash(hash_vec); | 716 | | | 717 | | // Read in the preimage from value | 718 | 0 | std::vector<unsigned char> preimage; | 719 | 0 | s >> preimage; | 720 | | | 721 | | // Add to preimages list | 722 | 0 | hash256_preimages.emplace(hash, std::move(preimage)); | 723 | 0 | break; | 724 | 0 | } | 725 | 0 | case PSBT_IN_PREVIOUS_TXID: | 726 | 0 | { | 727 | 0 | ExpectedKeySize("Input Previous TXID", key, 1); | 728 | 0 | if (m_psbt_version < 2) { | 729 | 0 | throw std::ios_base::failure("Previous txid is not allowed in PSBTv0"); | 730 | 0 | } | 731 | 0 | UnserializeFromVector(s, prev_txid); | 732 | 0 | found_prev_txid = true; | 733 | 0 | break; | 734 | 0 | } | 735 | 0 | case PSBT_IN_OUTPUT_INDEX: | 736 | 0 | { | 737 | 0 | ExpectedKeySize("Input Previous Output's Index", key, 1); | 738 | 0 | if (m_psbt_version < 2) { | 739 | 0 | throw std::ios_base::failure("Previous output's index is not allowed in PSBTv0"); | 740 | 0 | } | 741 | 0 | UnserializeFromVector(s, prev_out); | 742 | 0 | found_prev_out = true; | 743 | 0 | break; | 744 | 0 | } | 745 | 0 | case PSBT_IN_SEQUENCE: | 746 | 0 | { | 747 | 0 | ExpectedKeySize("Input Sequence", key, 1); | 748 | 0 | if (m_psbt_version < 2) { | 749 | 0 | throw std::ios_base::failure("Sequence is not allowed in PSBTv0"); | 750 | 0 | } | 751 | 0 | sequence.emplace(); | 752 | 0 | UnserializeFromVector(s, *sequence); | 753 | 0 | break; | 754 | 0 | } | 755 | 0 | case PSBT_IN_REQUIRED_TIME_LOCKTIME: | 756 | 0 | { | 757 | 0 | ExpectedKeySize("Input Required Time Based Locktime", key, 1); | 758 | 0 | if (m_psbt_version < 2) { | 759 | 0 | throw std::ios_base::failure("Required time based locktime is not allowed in PSBTv0"); | 760 | 0 | } | 761 | 0 | time_locktime.emplace(); | 762 | 0 | UnserializeFromVector(s, *time_locktime); | 763 | 0 | if (*time_locktime < LOCKTIME_THRESHOLD) { | 764 | 0 | throw std::ios_base::failure("Required time based locktime is invalid (less than 500000000)"); | 765 | 0 | } | 766 | 0 | break; | 767 | 0 | } | 768 | 0 | case PSBT_IN_REQUIRED_HEIGHT_LOCKTIME: | 769 | 0 | { | 770 | 0 | ExpectedKeySize("Input Required Height Based Locktime", key, 1); | 771 | 0 | if (m_psbt_version < 2) { | 772 | 0 | throw std::ios_base::failure("Required height based locktime is not allowed in PSBTv0"); | 773 | 0 | } | 774 | 0 | height_locktime.emplace(); | 775 | 0 | UnserializeFromVector(s, *height_locktime); | 776 | 0 | if (*height_locktime >= LOCKTIME_THRESHOLD) { | 777 | 0 | throw std::ios_base::failure("Required height based locktime is invalid (greater than or equal to 500000000)"); | 778 | 0 | } else if (*height_locktime == 0) { | 779 | 0 | throw std::ios_base::failure("Required height based locktime is invalid (0)"); | 780 | 0 | } | 781 | 0 | break; | 782 | 0 | } | 783 | 0 | case PSBT_IN_TAP_KEY_SIG: | 784 | 0 | { | 785 | 0 | ExpectedKeySize("Input Taproot Key Path Signature", key, 1); | 786 | 0 | s >> m_tap_key_sig; | 787 | 0 | if (m_tap_key_sig.size() < 64) { | 788 | 0 | throw std::ios_base::failure("Input Taproot key path signature is shorter than 64 bytes"); | 789 | 0 | } else if (m_tap_key_sig.size() > 65) { | 790 | 0 | throw std::ios_base::failure("Input Taproot key path signature is longer than 65 bytes"); | 791 | 0 | } | 792 | 0 | break; | 793 | 0 | } | 794 | 0 | case PSBT_IN_TAP_SCRIPT_SIG: | 795 | 0 | { | 796 | 0 | ExpectedKeySize("Input Taproot Script Path Signature", key, 65); | 797 | 0 | SpanReader s_key{std::span{key}.subspan(1)}; | 798 | 0 | XOnlyPubKey xonly; | 799 | 0 | uint256 hash; | 800 | 0 | s_key >> xonly; | 801 | 0 | s_key >> hash; | 802 | 0 | std::vector<unsigned char> sig; | 803 | 0 | s >> sig; | 804 | 0 | if (sig.size() < 64) { | 805 | 0 | throw std::ios_base::failure("Input Taproot script path signature is shorter than 64 bytes"); | 806 | 0 | } else if (sig.size() > 65) { | 807 | 0 | throw std::ios_base::failure("Input Taproot script path signature is longer than 65 bytes"); | 808 | 0 | } | 809 | 0 | m_tap_script_sigs.emplace(std::make_pair(xonly, hash), sig); | 810 | 0 | break; | 811 | 0 | } | 812 | 0 | case PSBT_IN_TAP_LEAF_SCRIPT: | 813 | 0 | { | 814 | 0 | if (key.size() < 34) { | 815 | 0 | throw std::ios_base::failure("Input Taproot leaf script key is not at least 34 bytes"); | 816 | 0 | } else if ((key.size() - 2) % 32 != 0) { | 817 | 0 | throw std::ios_base::failure("Input Taproot leaf script key's control block size is not valid"); | 818 | 0 | } | 819 | 0 | std::vector<unsigned char> script_v; | 820 | 0 | s >> script_v; | 821 | 0 | if (script_v.empty()) { | 822 | 0 | throw std::ios_base::failure("Input Taproot leaf script must be at least 1 byte"); | 823 | 0 | } | 824 | 0 | uint8_t leaf_ver = script_v.back(); | 825 | 0 | script_v.pop_back(); | 826 | 0 | const auto leaf_script = std::make_pair(script_v, (int)leaf_ver); | 827 | 0 | m_tap_scripts[leaf_script].insert(std::vector<unsigned char>(key.begin() + 1, key.end())); | 828 | 0 | break; | 829 | 0 | } | 830 | 0 | case PSBT_IN_TAP_BIP32_DERIVATION: | 831 | 0 | { | 832 | 0 | ExpectedKeySize("Input Taproot BIP32 Keypath", key, 33); | 833 | 0 | SpanReader s_key{std::span{key}.subspan(1)}; | 834 | 0 | XOnlyPubKey xonly; | 835 | 0 | s_key >> xonly; | 836 | 0 | std::set<uint256> leaf_hashes; | 837 | 0 | uint64_t value_len = ReadCompactSize(s); | 838 | 0 | size_t before_hashes = s.size(); | 839 | 0 | s >> leaf_hashes; | 840 | 0 | size_t after_hashes = s.size(); | 841 | 0 | size_t hashes_len = before_hashes - after_hashes; | 842 | 0 | if (hashes_len > value_len) { | 843 | 0 | throw std::ios_base::failure("Input Taproot BIP32 keypath has an invalid length"); | 844 | 0 | } | 845 | 0 | size_t origin_len = value_len - hashes_len; | 846 | 0 | m_tap_bip32_paths.emplace(xonly, std::make_pair(leaf_hashes, DeserializeKeyOrigin(s, origin_len))); | 847 | 0 | break; | 848 | 0 | } | 849 | 0 | case PSBT_IN_TAP_INTERNAL_KEY: | 850 | 0 | { | 851 | 0 | ExpectedKeySize("Input Taproot Internal Key", key, 1); | 852 | 0 | UnserializeFromVector(s, m_tap_internal_key); | 853 | 0 | break; | 854 | 0 | } | 855 | 0 | case PSBT_IN_TAP_MERKLE_ROOT: | 856 | 0 | { | 857 | 0 | ExpectedKeySize("Input Taproot Merkle Root", key, 1); | 858 | 0 | UnserializeFromVector(s, m_tap_merkle_root); | 859 | 0 | break; | 860 | 0 | } | 861 | 0 | case PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS: | 862 | 0 | { | 863 | 0 | ExpectedKeySize("Input MuSig2 Participants Pubkeys", key, CPubKey::COMPRESSED_SIZE + 1); | 864 | 0 | DeserializeMuSig2ParticipantPubkeys(s, skey, m_musig2_participants, std::string{"Input"}); | 865 | 0 | break; | 866 | 0 | } | 867 | 0 | case PSBT_IN_MUSIG2_PUB_NONCE: | 868 | 0 | { | 869 | 0 | if (key.size() != 2 * CPubKey::COMPRESSED_SIZE + 1 && key.size() != 2 * CPubKey::COMPRESSED_SIZE + CSHA256::OUTPUT_SIZE + 1) { | 870 | 0 | throw std::ios_base::failure("Input musig2 pubnonce key is not expected size of 67 or 99 bytes"); | 871 | 0 | } | 872 | 0 | CPubKey agg_pub, part_pub; | 873 | 0 | uint256 leaf_hash; | 874 | 0 | DeserializeMuSig2ParticipantDataIdentifier(skey, agg_pub, part_pub, leaf_hash); | 875 | |
| 876 | 0 | std::vector<uint8_t> pubnonce; | 877 | 0 | s >> pubnonce; | 878 | 0 | if (pubnonce.size() != MUSIG2_PUBNONCE_SIZE) { | 879 | 0 | throw std::ios_base::failure("Input musig2 pubnonce value is not 66 bytes"); | 880 | 0 | } | 881 | | | 882 | 0 | m_musig2_pubnonces[std::make_pair(agg_pub, leaf_hash)].emplace(part_pub, pubnonce); | 883 | 0 | break; | 884 | 0 | } | 885 | 0 | case PSBT_IN_MUSIG2_PARTIAL_SIG: | 886 | 0 | { | 887 | 0 | if (key.size() != 2 * CPubKey::COMPRESSED_SIZE + 1 && key.size() != 2 * CPubKey::COMPRESSED_SIZE + CSHA256::OUTPUT_SIZE + 1) { | 888 | 0 | throw std::ios_base::failure("Input musig2 partial sig key is not expected size of 67 or 99 bytes"); | 889 | 0 | } | 890 | 0 | CPubKey agg_pub, part_pub; | 891 | 0 | uint256 leaf_hash; | 892 | 0 | DeserializeMuSig2ParticipantDataIdentifier(skey, agg_pub, part_pub, leaf_hash); | 893 | |
| 894 | 0 | uint256 partial_sig; | 895 | 0 | UnserializeFromVector(s, partial_sig); | 896 | |
| 897 | 0 | m_musig2_partial_sigs[std::make_pair(agg_pub, leaf_hash)].emplace(part_pub, partial_sig); | 898 | 0 | break; | 899 | 0 | } | 900 | 0 | case PSBT_IN_PROPRIETARY: | 901 | 0 | { | 902 | 0 | PSBTProprietary this_prop; | 903 | 0 | skey >> this_prop.identifier; | 904 | 0 | this_prop.subtype = ReadCompactSize(skey); | 905 | 0 | this_prop.key = key; | 906 | |
| 907 | 0 | s >> this_prop.value; | 908 | 0 | m_proprietary.insert(this_prop); | 909 | 0 | break; | 910 | 0 | } | 911 | | // Unknown stuff | 912 | 0 | default: | 913 | | // Read in the value | 914 | 0 | std::vector<unsigned char> val_bytes; | 915 | 0 | s >> val_bytes; | 916 | 0 | unknown.emplace(std::move(key), std::move(val_bytes)); | 917 | 0 | break; | 918 | 0 | } | 919 | 0 | } | 920 | | | 921 | 2 | if (!found_sep) { | 922 | 0 | throw std::ios_base::failure("Separator is missing at the end of an input map"); | 923 | 0 | } | 924 | | | 925 | | // Make sure required PSBTv2 fields are present | 926 | 2 | if (m_psbt_version >= 2) { | 927 | 0 | if (!found_prev_txid) { | 928 | 0 | throw std::ios_base::failure("Previous TXID is required in PSBTv2"); | 929 | 0 | } | 930 | 0 | if (!found_prev_out) { | 931 | 0 | throw std::ios_base::failure("Previous output's index is required in PSBTv2"); | 932 | 0 | } | 933 | 0 | } | 934 | 2 | } |
void PSBTInput::Unserialize<SpanReader>(SpanReader&) Line | Count | Source | 559 | 2.17k | inline void Unserialize(Stream& s) { | 560 | | // Used for duplicate key detection | 561 | 2.17k | std::set<std::vector<unsigned char>> key_lookup; | 562 | | // Cache whether PSBTv2 required fields were seen | 563 | 2.17k | bool found_prev_txid = false; | 564 | 2.17k | bool found_prev_out = false; | 565 | | | 566 | | // Read loop | 567 | 2.17k | bool found_sep = false; | 568 | 24.7k | while(!s.empty()) { | 569 | | // Read the key of format "<keylen><keytype><keydata>" after which | 570 | | // "key" will contain "<keytype><keydata>" | 571 | 24.7k | std::vector<unsigned char> key; | 572 | 24.7k | s >> key; | 573 | | | 574 | | // the key is empty if that was actually a separator byte | 575 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) | 576 | 24.7k | if (key.empty()) { | 577 | 2.12k | found_sep = true; | 578 | 2.12k | break; | 579 | 2.12k | } | 580 | | | 581 | | // Duplicate keys are not permitted | 582 | 22.6k | if (!key_lookup.emplace(key).second) { | 583 | 7 | throw std::ios_base::failure(tfm::format("Duplicate Key, input key \"%s\" already provided", HexStr(key))); | 584 | 7 | } | 585 | | | 586 | | // "skey" is used so that "key" is unchanged after reading keytype below | 587 | 22.5k | SpanReader skey{key}; | 588 | | // keytype is of the format compact size uint at the beginning of "key" | 589 | 22.5k | uint64_t type = ReadCompactSize(skey); | 590 | | | 591 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the | 592 | | // format "<valuelen><valuedata>" from the stream "s", and value checks | 593 | 22.5k | switch(type) { | 594 | 475 | case PSBT_IN_NON_WITNESS_UTXO: | 595 | 475 | { | 596 | 475 | ExpectedKeySize("Input Non-witness UTXO", key, 1); | 597 | | // Set the stream to unserialize with witness since this is always a valid network transaction | 598 | 475 | UnserializeFromVector(s, TX_WITH_WITNESS(non_witness_utxo)); | 599 | 475 | break; | 600 | 0 | } | 601 | 1.85k | case PSBT_IN_WITNESS_UTXO: | 602 | 1.85k | ExpectedKeySize("Input Witness UTXO", key, 1); | 603 | 1.85k | UnserializeFromVector(s, witness_utxo); | 604 | 1.85k | break; | 605 | 165 | case PSBT_IN_PARTIAL_SIG: | 606 | 165 | { | 607 | | // Make sure that the key is the size of pubkey + 1 | 608 | 165 | if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) { | 609 | 1 | throw std::ios_base::failure("Size of key was not the expected size for the type partial signature pubkey"); | 610 | 1 | } | 611 | | // Read in the pubkey from key | 612 | 164 | CPubKey pubkey(key.begin() + 1, key.end()); | 613 | 164 | if (!pubkey.IsFullyValid()) { | 614 | 0 | throw std::ios_base::failure("Invalid pubkey"); | 615 | 0 | } | 616 | | | 617 | | // Read in the signature from value | 618 | 164 | std::vector<unsigned char> sig; | 619 | 164 | s >> sig; | 620 | | | 621 | | // Check that the signature is validly encoded | 622 | 164 | if (sig.empty() || !CheckSignatureEncoding(sig, SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_STRICTENC, nullptr)) { | 623 | 0 | throw std::ios_base::failure("Signature is not a valid encoding"); | 624 | 0 | } | 625 | | | 626 | | // Add to list | 627 | 164 | partial_sigs.emplace(pubkey.GetID(), SigPair(pubkey, std::move(sig))); | 628 | 164 | break; | 629 | 164 | } | 630 | 83 | case PSBT_IN_SIGHASH: | 631 | 83 | ExpectedKeySize("Input Sighash Type", key, 1); | 632 | 83 | int sighash; | 633 | 83 | UnserializeFromVector(s, sighash); | 634 | 83 | sighash_type = sighash; | 635 | 83 | break; | 636 | 50 | case PSBT_IN_REDEEMSCRIPT: | 637 | 50 | { | 638 | 50 | ExpectedKeySize("Input redeemScript", key, 1); | 639 | 50 | s >> redeem_script; | 640 | 50 | break; | 641 | 164 | } | 642 | 109 | case PSBT_IN_WITNESSSCRIPT: | 643 | 109 | { | 644 | 109 | ExpectedKeySize("Input witnessScript", key, 1); | 645 | 109 | s >> witness_script; | 646 | 109 | break; | 647 | 164 | } | 648 | 497 | case PSBT_IN_BIP32_DERIVATION: | 649 | 497 | { | 650 | 497 | DeserializeHDKeypaths(s, key, hd_keypaths); | 651 | 497 | break; | 652 | 164 | } | 653 | 39 | case PSBT_IN_SCRIPTSIG: | 654 | 39 | { | 655 | 39 | ExpectedKeySize("Input Final scriptSig", key, 1); | 656 | 39 | s >> final_script_sig; | 657 | 39 | break; | 658 | 164 | } | 659 | 106 | case PSBT_IN_SCRIPTWITNESS: | 660 | 106 | { | 661 | 106 | ExpectedKeySize("Input Final scriptWitness", key, 1); | 662 | 106 | UnserializeFromVector(s, final_script_witness.stack); | 663 | 106 | break; | 664 | 164 | } | 665 | 3 | case PSBT_IN_RIPEMD160: | 666 | 3 | { | 667 | 3 | ExpectedKeySize("Input RIPEMD160 Preimage", key, CRIPEMD160::OUTPUT_SIZE + 1); | 668 | | // Read in the hash from key | 669 | 3 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); | 670 | 3 | uint160 hash(hash_vec); | 671 | | | 672 | | // Read in the preimage from value | 673 | 3 | std::vector<unsigned char> preimage; | 674 | 3 | s >> preimage; | 675 | | | 676 | | // Add to preimages list | 677 | 3 | ripemd160_preimages.emplace(hash, std::move(preimage)); | 678 | 3 | break; | 679 | 164 | } | 680 | 6 | case PSBT_IN_SHA256: | 681 | 6 | { | 682 | 6 | ExpectedKeySize("Input SHA256 Preimage", key, CSHA256::OUTPUT_SIZE + 1); | 683 | | // Read in the hash from key | 684 | 6 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); | 685 | 6 | uint256 hash(hash_vec); | 686 | | | 687 | | // Read in the preimage from value | 688 | 6 | std::vector<unsigned char> preimage; | 689 | 6 | s >> preimage; | 690 | | | 691 | | // Add to preimages list | 692 | 6 | sha256_preimages.emplace(hash, std::move(preimage)); | 693 | 6 | break; | 694 | 164 | } | 695 | 3 | case PSBT_IN_HASH160: | 696 | 3 | { | 697 | 3 | ExpectedKeySize("Input Hash160 Preimage", key, CHash160::OUTPUT_SIZE + 1); | 698 | | // Read in the hash from key | 699 | 3 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); | 700 | 3 | uint160 hash(hash_vec); | 701 | | | 702 | | // Read in the preimage from value | 703 | 3 | std::vector<unsigned char> preimage; | 704 | 3 | s >> preimage; | 705 | | | 706 | | // Add to preimages list | 707 | 3 | hash160_preimages.emplace(hash, std::move(preimage)); | 708 | 3 | break; | 709 | 164 | } | 710 | 3 | case PSBT_IN_HASH256: | 711 | 3 | { | 712 | 3 | ExpectedKeySize("Input Hash256 Preimage", key, CHash256::OUTPUT_SIZE + 1); | 713 | | // Read in the hash from key | 714 | 3 | std::vector<unsigned char> hash_vec(key.begin() + 1, key.end()); | 715 | 3 | uint256 hash(hash_vec); | 716 | | | 717 | | // Read in the preimage from value | 718 | 3 | std::vector<unsigned char> preimage; | 719 | 3 | s >> preimage; | 720 | | | 721 | | // Add to preimages list | 722 | 3 | hash256_preimages.emplace(hash, std::move(preimage)); | 723 | 3 | break; | 724 | 164 | } | 725 | 1.93k | case PSBT_IN_PREVIOUS_TXID: | 726 | 1.93k | { | 727 | 1.93k | ExpectedKeySize("Input Previous TXID", key, 1); | 728 | 1.93k | if (m_psbt_version < 2) { | 729 | 1 | throw std::ios_base::failure("Previous txid is not allowed in PSBTv0"); | 730 | 1 | } | 731 | 1.93k | UnserializeFromVector(s, prev_txid); | 732 | 1.93k | found_prev_txid = true; | 733 | 1.93k | break; | 734 | 1.93k | } | 735 | 1.93k | case PSBT_IN_OUTPUT_INDEX: | 736 | 1.93k | { | 737 | 1.93k | ExpectedKeySize("Input Previous Output's Index", key, 1); | 738 | 1.93k | if (m_psbt_version < 2) { | 739 | 1 | throw std::ios_base::failure("Previous output's index is not allowed in PSBTv0"); | 740 | 1 | } | 741 | 1.93k | UnserializeFromVector(s, prev_out); | 742 | 1.93k | found_prev_out = true; | 743 | 1.93k | break; | 744 | 1.93k | } | 745 | 1.90k | case PSBT_IN_SEQUENCE: | 746 | 1.90k | { | 747 | 1.90k | ExpectedKeySize("Input Sequence", key, 1); | 748 | 1.90k | if (m_psbt_version < 2) { | 749 | 1 | throw std::ios_base::failure("Sequence is not allowed in PSBTv0"); | 750 | 1 | } | 751 | 1.90k | sequence.emplace(); | 752 | 1.90k | UnserializeFromVector(s, *sequence); | 753 | 1.90k | break; | 754 | 1.90k | } | 755 | 15 | case PSBT_IN_REQUIRED_TIME_LOCKTIME: | 756 | 15 | { | 757 | 15 | ExpectedKeySize("Input Required Time Based Locktime", key, 1); | 758 | 15 | if (m_psbt_version < 2) { | 759 | 1 | throw std::ios_base::failure("Required time based locktime is not allowed in PSBTv0"); | 760 | 1 | } | 761 | 14 | time_locktime.emplace(); | 762 | 14 | UnserializeFromVector(s, *time_locktime); | 763 | 14 | if (*time_locktime < LOCKTIME_THRESHOLD) { | 764 | 1 | throw std::ios_base::failure("Required time based locktime is invalid (less than 500000000)"); | 765 | 1 | } | 766 | 13 | break; | 767 | 14 | } | 768 | 16 | case PSBT_IN_REQUIRED_HEIGHT_LOCKTIME: | 769 | 16 | { | 770 | 16 | ExpectedKeySize("Input Required Height Based Locktime", key, 1); | 771 | 16 | if (m_psbt_version < 2) { | 772 | 1 | throw std::ios_base::failure("Required height based locktime is not allowed in PSBTv0"); | 773 | 1 | } | 774 | 15 | height_locktime.emplace(); | 775 | 15 | UnserializeFromVector(s, *height_locktime); | 776 | 15 | if (*height_locktime >= LOCKTIME_THRESHOLD) { | 777 | 1 | throw std::ios_base::failure("Required height based locktime is invalid (greater than or equal to 500000000)"); | 778 | 14 | } else if (*height_locktime == 0) { | 779 | 1 | throw std::ios_base::failure("Required height based locktime is invalid (0)"); | 780 | 1 | } | 781 | 13 | break; | 782 | 15 | } | 783 | 187 | case PSBT_IN_TAP_KEY_SIG: | 784 | 187 | { | 785 | 187 | ExpectedKeySize("Input Taproot Key Path Signature", key, 1); | 786 | 187 | s >> m_tap_key_sig; | 787 | 187 | if (m_tap_key_sig.size() < 64) { | 788 | 1 | throw std::ios_base::failure("Input Taproot key path signature is shorter than 64 bytes"); | 789 | 186 | } else if (m_tap_key_sig.size() > 65) { | 790 | 1 | throw std::ios_base::failure("Input Taproot key path signature is longer than 65 bytes"); | 791 | 1 | } | 792 | 185 | break; | 793 | 187 | } | 794 | 383 | case PSBT_IN_TAP_SCRIPT_SIG: | 795 | 383 | { | 796 | 383 | ExpectedKeySize("Input Taproot Script Path Signature", key, 65); | 797 | 383 | SpanReader s_key{std::span{key}.subspan(1)}; | 798 | 383 | XOnlyPubKey xonly; | 799 | 383 | uint256 hash; | 800 | 383 | s_key >> xonly; | 801 | 383 | s_key >> hash; | 802 | 383 | std::vector<unsigned char> sig; | 803 | 383 | s >> sig; | 804 | 383 | if (sig.size() < 64) { | 805 | 1 | throw std::ios_base::failure("Input Taproot script path signature is shorter than 64 bytes"); | 806 | 382 | } else if (sig.size() > 65) { | 807 | 1 | throw std::ios_base::failure("Input Taproot script path signature is longer than 65 bytes"); | 808 | 1 | } | 809 | 381 | m_tap_script_sigs.emplace(std::make_pair(xonly, hash), sig); | 810 | 381 | break; | 811 | 383 | } | 812 | 1.74k | case PSBT_IN_TAP_LEAF_SCRIPT: | 813 | 1.74k | { | 814 | 1.74k | if (key.size() < 34) { | 815 | 0 | throw std::ios_base::failure("Input Taproot leaf script key is not at least 34 bytes"); | 816 | 1.74k | } else if ((key.size() - 2) % 32 != 0) { | 817 | 3 | throw std::ios_base::failure("Input Taproot leaf script key's control block size is not valid"); | 818 | 3 | } | 819 | 1.74k | std::vector<unsigned char> script_v; | 820 | 1.74k | s >> script_v; | 821 | 1.74k | if (script_v.empty()) { | 822 | 0 | throw std::ios_base::failure("Input Taproot leaf script must be at least 1 byte"); | 823 | 0 | } | 824 | 1.74k | uint8_t leaf_ver = script_v.back(); | 825 | 1.74k | script_v.pop_back(); | 826 | 1.74k | const auto leaf_script = std::make_pair(script_v, (int)leaf_ver); | 827 | 1.74k | m_tap_scripts[leaf_script].insert(std::vector<unsigned char>(key.begin() + 1, key.end())); | 828 | 1.74k | break; | 829 | 1.74k | } | 830 | 5.55k | case PSBT_IN_TAP_BIP32_DERIVATION: | 831 | 5.55k | { | 832 | 5.55k | ExpectedKeySize("Input Taproot BIP32 Keypath", key, 33); | 833 | 5.55k | SpanReader s_key{std::span{key}.subspan(1)}; | 834 | 5.55k | XOnlyPubKey xonly; | 835 | 5.55k | s_key >> xonly; | 836 | 5.55k | std::set<uint256> leaf_hashes; | 837 | 5.55k | uint64_t value_len = ReadCompactSize(s); | 838 | 5.55k | size_t before_hashes = s.size(); | 839 | 5.55k | s >> leaf_hashes; | 840 | 5.55k | size_t after_hashes = s.size(); | 841 | 5.55k | size_t hashes_len = before_hashes - after_hashes; | 842 | 5.55k | if (hashes_len > value_len) { | 843 | 1 | throw std::ios_base::failure("Input Taproot BIP32 keypath has an invalid length"); | 844 | 1 | } | 845 | 5.55k | size_t origin_len = value_len - hashes_len; | 846 | 5.55k | m_tap_bip32_paths.emplace(xonly, std::make_pair(leaf_hashes, DeserializeKeyOrigin(s, origin_len))); | 847 | 5.55k | break; | 848 | 5.55k | } | 849 | 1.22k | case PSBT_IN_TAP_INTERNAL_KEY: | 850 | 1.22k | { | 851 | 1.22k | ExpectedKeySize("Input Taproot Internal Key", key, 1); | 852 | 1.22k | UnserializeFromVector(s, m_tap_internal_key); | 853 | 1.22k | break; | 854 | 5.55k | } | 855 | 975 | case PSBT_IN_TAP_MERKLE_ROOT: | 856 | 975 | { | 857 | 975 | ExpectedKeySize("Input Taproot Merkle Root", key, 1); | 858 | 975 | UnserializeFromVector(s, m_tap_merkle_root); | 859 | 975 | break; | 860 | 5.55k | } | 861 | 979 | case PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS: | 862 | 979 | { | 863 | 979 | ExpectedKeySize("Input MuSig2 Participants Pubkeys", key, CPubKey::COMPRESSED_SIZE + 1); | 864 | 979 | DeserializeMuSig2ParticipantPubkeys(s, skey, m_musig2_participants, std::string{"Input"}); | 865 | 979 | break; | 866 | 5.55k | } | 867 | 1.77k | case PSBT_IN_MUSIG2_PUB_NONCE: | 868 | 1.77k | { | 869 | 1.77k | if (key.size() != 2 * CPubKey::COMPRESSED_SIZE + 1 && key.size() != 2 * CPubKey::COMPRESSED_SIZE + CSHA256::OUTPUT_SIZE + 1) { | 870 | 2 | throw std::ios_base::failure("Input musig2 pubnonce key is not expected size of 67 or 99 bytes"); | 871 | 2 | } | 872 | 1.77k | CPubKey agg_pub, part_pub; | 873 | 1.77k | uint256 leaf_hash; | 874 | 1.77k | DeserializeMuSig2ParticipantDataIdentifier(skey, agg_pub, part_pub, leaf_hash); | 875 | | | 876 | 1.77k | std::vector<uint8_t> pubnonce; | 877 | 1.77k | s >> pubnonce; | 878 | 1.77k | if (pubnonce.size() != MUSIG2_PUBNONCE_SIZE) { | 879 | 1 | throw std::ios_base::failure("Input musig2 pubnonce value is not 66 bytes"); | 880 | 1 | } | 881 | | | 882 | 1.77k | m_musig2_pubnonces[std::make_pair(agg_pub, leaf_hash)].emplace(part_pub, pubnonce); | 883 | 1.77k | break; | 884 | 1.77k | } | 885 | 553 | case PSBT_IN_MUSIG2_PARTIAL_SIG: | 886 | 553 | { | 887 | 553 | if (key.size() != 2 * CPubKey::COMPRESSED_SIZE + 1 && key.size() != 2 * CPubKey::COMPRESSED_SIZE + CSHA256::OUTPUT_SIZE + 1) { | 888 | 2 | throw std::ios_base::failure("Input musig2 partial sig key is not expected size of 67 or 99 bytes"); | 889 | 2 | } | 890 | 551 | CPubKey agg_pub, part_pub; | 891 | 551 | uint256 leaf_hash; | 892 | 551 | DeserializeMuSig2ParticipantDataIdentifier(skey, agg_pub, part_pub, leaf_hash); | 893 | | | 894 | 551 | uint256 partial_sig; | 895 | 551 | UnserializeFromVector(s, partial_sig); | 896 | | | 897 | 551 | m_musig2_partial_sigs[std::make_pair(agg_pub, leaf_hash)].emplace(part_pub, partial_sig); | 898 | 551 | break; | 899 | 553 | } | 900 | 5 | case PSBT_IN_PROPRIETARY: | 901 | 5 | { | 902 | 5 | PSBTProprietary this_prop; | 903 | 5 | skey >> this_prop.identifier; | 904 | 5 | this_prop.subtype = ReadCompactSize(skey); | 905 | 5 | this_prop.key = key; | 906 | | | 907 | 5 | s >> this_prop.value; | 908 | 5 | m_proprietary.insert(this_prop); | 909 | 5 | break; | 910 | 553 | } | 911 | | // Unknown stuff | 912 | 5 | default: | 913 | | // Read in the value | 914 | 5 | std::vector<unsigned char> val_bytes; | 915 | 5 | s >> val_bytes; | 916 | 5 | unknown.emplace(std::move(key), std::move(val_bytes)); | 917 | 5 | break; | 918 | 22.5k | } | 919 | 22.5k | } | 920 | | | 921 | 2.12k | if (!found_sep) { | 922 | 0 | throw std::ios_base::failure("Separator is missing at the end of an input map"); | 923 | 0 | } | 924 | | | 925 | | // Make sure required PSBTv2 fields are present | 926 | 2.12k | if (m_psbt_version >= 2) { | 927 | 1.93k | if (!found_prev_txid) { | 928 | 1 | throw std::ios_base::failure("Previous TXID is required in PSBTv2"); | 929 | 1 | } | 930 | 1.93k | if (!found_prev_out) { | 931 | 1 | throw std::ios_base::failure("Previous output's index is required in PSBTv2"); | 932 | 1 | } | 933 | 1.93k | } | 934 | 2.12k | } |
|
935 | | }; |
936 | | |
937 | | /** A structure for PSBTs which contains per output information */ |
938 | | class PSBTOutput |
939 | | { |
940 | | private: |
941 | | uint32_t m_psbt_version; |
942 | | |
943 | | public: |
944 | | CScript redeem_script; |
945 | | CScript witness_script; |
946 | | std::map<CPubKey, KeyOriginInfo> hd_keypaths; |
947 | | |
948 | | XOnlyPubKey m_tap_internal_key; |
949 | | std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> m_tap_tree; |
950 | | std::map<XOnlyPubKey, std::pair<std::set<uint256>, KeyOriginInfo>> m_tap_bip32_paths; |
951 | | std::map<CPubKey, std::vector<CPubKey>> m_musig2_participants; |
952 | | |
953 | | std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown; |
954 | | std::set<PSBTProprietary> m_proprietary; |
955 | | |
956 | | CAmount amount; |
957 | | CScript script; |
958 | | |
959 | | void FillSignatureData(SignatureData& sigdata) const; |
960 | | void FromSignatureData(const SignatureData& sigdata); |
961 | | void Merge(const PSBTOutput& output); |
962 | 15 | uint32_t GetVersion() const { return m_psbt_version; } |
963 | | |
964 | | explicit PSBTOutput(uint32_t psbt_version, CAmount amount, const CScript& script) |
965 | 4.42k | : m_psbt_version(psbt_version), |
966 | 4.42k | amount(amount), |
967 | 4.42k | script(script) |
968 | 4.42k | { |
969 | 4.42k | assert(m_psbt_version == 0 || m_psbt_version == 2); |
970 | 4.42k | } |
971 | | |
972 | | // Construct a PSBTOutput when the amount and script are expected to be serialized |
973 | | template <typename Stream> |
974 | | explicit PSBTOutput(deserialize_type, Stream& s, uint32_t psbt_version) |
975 | 3.02k | : m_psbt_version(psbt_version) |
976 | 3.02k | { |
977 | 3.02k | assert(m_psbt_version == 2); |
978 | 3.02k | Unserialize(s); |
979 | 3.02k | } Unexecuted instantiation: PSBTOutput::PSBTOutput<DataStream>(deserialize_type, DataStream&, unsigned int) PSBTOutput::PSBTOutput<SpanReader>(deserialize_type, SpanReader&, unsigned int) Line | Count | Source | 975 | 3.02k | : m_psbt_version(psbt_version) | 976 | 3.02k | { | 977 | 3.02k | assert(m_psbt_version == 2); | 978 | 3.02k | Unserialize(s); | 979 | 3.02k | } |
|
980 | | |
981 | | bool operator==(const PSBTOutput&) const = default; |
982 | | |
983 | | template <typename Stream> |
984 | 2.38k | inline void Serialize(Stream& s) const { |
985 | | // Write the redeem script |
986 | 2.38k | if (!redeem_script.empty()) { |
987 | 16 | SerializeToVector(s, CompactSizeWriter(PSBT_OUT_REDEEMSCRIPT)); |
988 | 16 | s << redeem_script; |
989 | 16 | } |
990 | | |
991 | | // Write the witness script |
992 | 2.38k | if (!witness_script.empty()) { |
993 | 37 | SerializeToVector(s, CompactSizeWriter(PSBT_OUT_WITNESSSCRIPT)); |
994 | 37 | s << witness_script; |
995 | 37 | } |
996 | | |
997 | | // Write any hd keypaths |
998 | 2.38k | SerializeHDKeypaths(s, hd_keypaths, CompactSizeWriter(PSBT_OUT_BIP32_DERIVATION)); |
999 | | |
1000 | 2.38k | if (m_psbt_version >= 2) { |
1001 | | // Write amount and spk |
1002 | 2.31k | SerializeToVector(s, CompactSizeWriter(PSBT_OUT_AMOUNT)); |
1003 | 2.31k | SerializeToVector(s, amount); |
1004 | | |
1005 | 2.31k | SerializeToVector(s, CompactSizeWriter(PSBT_OUT_SCRIPT)); |
1006 | 2.31k | s << script; |
1007 | 2.31k | } |
1008 | | |
1009 | | // Write proprietary things |
1010 | 2.38k | for (const auto& entry : m_proprietary) { |
1011 | 2 | s << entry.key; |
1012 | 2 | s << entry.value; |
1013 | 2 | } |
1014 | | |
1015 | | // Write taproot internal key |
1016 | 2.38k | if (!m_tap_internal_key.IsNull()) { |
1017 | 571 | SerializeToVector(s, PSBT_OUT_TAP_INTERNAL_KEY); |
1018 | 571 | s << ToByteVector(m_tap_internal_key); |
1019 | 571 | } |
1020 | | |
1021 | | // Write taproot tree |
1022 | 2.38k | if (!m_tap_tree.empty()) { |
1023 | 428 | SerializeToVector(s, PSBT_OUT_TAP_TREE); |
1024 | 428 | std::vector<unsigned char> value; |
1025 | 428 | VectorWriter s_value{value, 0}; |
1026 | 848 | for (const auto& [depth, leaf_ver, script] : m_tap_tree) { |
1027 | 848 | s_value << depth; |
1028 | 848 | s_value << leaf_ver; |
1029 | 848 | s_value << script; |
1030 | 848 | } |
1031 | 428 | s << value; |
1032 | 428 | } |
1033 | | |
1034 | | // Write taproot bip32 keypaths |
1035 | 2.61k | for (const auto& [xonly, leaf] : m_tap_bip32_paths) { |
1036 | 2.61k | const auto& [leaf_hashes, origin] = leaf; |
1037 | 2.61k | SerializeToVector(s, PSBT_OUT_TAP_BIP32_DERIVATION, xonly); |
1038 | 2.61k | std::vector<unsigned char> value; |
1039 | 2.61k | VectorWriter s_value{value, 0}; |
1040 | 2.61k | s_value << leaf_hashes; |
1041 | 2.61k | SerializeKeyOrigin(s_value, origin); |
1042 | 2.61k | s << value; |
1043 | 2.61k | } |
1044 | | |
1045 | | // Write MuSig2 Participants |
1046 | 2.38k | for (const auto& [agg_pubkey, part_pubs] : m_musig2_participants) { |
1047 | 488 | SerializeToVector(s, CompactSizeWriter(PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS), std::span{agg_pubkey}); |
1048 | 488 | std::vector<unsigned char> value; |
1049 | 488 | VectorWriter s_value{value, 0}; |
1050 | 1.37k | for (auto& pk : part_pubs) { |
1051 | 1.37k | s_value << std::span{pk}; |
1052 | 1.37k | } |
1053 | 488 | s << value; |
1054 | 488 | } |
1055 | | |
1056 | | // Write unknown things |
1057 | 2.38k | for (auto& entry : unknown) { |
1058 | 0 | s << entry.first; |
1059 | 0 | s << entry.second; |
1060 | 0 | } |
1061 | | |
1062 | 2.38k | s << PSBT_SEPARATOR; |
1063 | 2.38k | } |
1064 | | |
1065 | | |
1066 | | template <typename Stream> |
1067 | 3.21k | inline void Unserialize(Stream& s) { |
1068 | | // Used for duplicate key detection |
1069 | 3.21k | std::set<std::vector<unsigned char>> key_lookup; |
1070 | | // Cache whether PSBTv2 required fields are found |
1071 | 3.21k | bool found_amount = false; |
1072 | 3.21k | bool found_script = false; |
1073 | | |
1074 | | // Read loop |
1075 | 3.21k | bool found_sep = false; |
1076 | 17.0k | while(!s.empty()) { |
1077 | | // Read the key of format "<keylen><keytype><keydata>" after which |
1078 | | // "key" will contain "<keytype><keydata>" |
1079 | 17.0k | std::vector<unsigned char> key; |
1080 | 17.0k | s >> key; |
1081 | | |
1082 | | // the key is empty if that was actually a separator byte |
1083 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) |
1084 | 17.0k | if (key.empty()) { |
1085 | 3.20k | found_sep = true; |
1086 | 3.20k | break; |
1087 | 3.20k | } |
1088 | | |
1089 | | // Duplicate keys are not permitted |
1090 | 13.8k | if (!key_lookup.emplace(key).second) { |
1091 | 2 | throw std::ios_base::failure(tfm::format("Duplicate Key, output key \"%s\" already provided", HexStr(key))); |
1092 | 2 | } |
1093 | | |
1094 | | // "skey" is used so that "key" is unchanged after reading keytype below |
1095 | 13.8k | SpanReader skey{key}; |
1096 | | // keytype is of the format compact size uint at the beginning of "key" |
1097 | 13.8k | uint64_t type = ReadCompactSize(skey); |
1098 | | |
1099 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the |
1100 | | // format "<valuelen><valuedata>" from the stream "s", and value checks |
1101 | 13.8k | switch(type) { |
1102 | 20 | case PSBT_OUT_REDEEMSCRIPT: |
1103 | 20 | { |
1104 | 20 | ExpectedKeySize("Output redeemScript", key, 1); |
1105 | 20 | s >> redeem_script; |
1106 | 20 | break; |
1107 | 0 | } |
1108 | 52 | case PSBT_OUT_WITNESSSCRIPT: |
1109 | 52 | { |
1110 | 52 | ExpectedKeySize("Output witnessScript", key, 1); |
1111 | 52 | s >> witness_script; |
1112 | 52 | break; |
1113 | 0 | } |
1114 | 420 | case PSBT_OUT_BIP32_DERIVATION: |
1115 | 420 | { |
1116 | 420 | DeserializeHDKeypaths(s, key, hd_keypaths); |
1117 | 420 | break; |
1118 | 0 | } |
1119 | 3.02k | case PSBT_OUT_AMOUNT: |
1120 | 3.02k | { |
1121 | 3.02k | ExpectedKeySize("Output Amount", key, 1); |
1122 | 3.02k | if (m_psbt_version < 2) { |
1123 | 1 | throw std::ios_base::failure("Output amount is not allowed in PSBTv0"); |
1124 | 1 | } |
1125 | 3.02k | UnserializeFromVector(s, amount); |
1126 | 3.02k | found_amount = true; |
1127 | 3.02k | break; |
1128 | 3.02k | } |
1129 | 3.02k | case PSBT_OUT_SCRIPT: |
1130 | 3.02k | { |
1131 | 3.02k | ExpectedKeySize("Output Script", key, 1); |
1132 | 3.02k | if (m_psbt_version < 2) { |
1133 | 1 | throw std::ios_base::failure("Output script is not allowed in PSBTv0"); |
1134 | 1 | } |
1135 | 3.02k | s >> script; |
1136 | 3.02k | found_script = true; |
1137 | 3.02k | break; |
1138 | 3.02k | } |
1139 | 1.01k | case PSBT_OUT_TAP_INTERNAL_KEY: |
1140 | 1.01k | { |
1141 | 1.01k | ExpectedKeySize("Output Taproot Internal Key", key, 1); |
1142 | 1.01k | UnserializeFromVector(s, m_tap_internal_key); |
1143 | 1.01k | break; |
1144 | 3.02k | } |
1145 | 759 | case PSBT_OUT_TAP_TREE: |
1146 | 759 | { |
1147 | 759 | ExpectedKeySize("Output Taproot Tree Key", key, 1); |
1148 | 759 | std::vector<unsigned char> tree_v; |
1149 | 759 | s >> tree_v; |
1150 | 759 | SpanReader s_tree{tree_v}; |
1151 | 759 | if (s_tree.empty()) { |
1152 | 1 | throw std::ios_base::failure("Output Taproot tree must not be empty"); |
1153 | 1 | } |
1154 | 758 | TaprootBuilder builder; |
1155 | 2.25k | while (!s_tree.empty()) { |
1156 | 1.50k | uint8_t depth; |
1157 | 1.50k | uint8_t leaf_ver; |
1158 | 1.50k | std::vector<unsigned char> script; |
1159 | 1.50k | s_tree >> depth; |
1160 | 1.50k | s_tree >> leaf_ver; |
1161 | 1.50k | s_tree >> script; |
1162 | 1.50k | if (depth > TAPROOT_CONTROL_MAX_NODE_COUNT) { |
1163 | 0 | throw std::ios_base::failure("Output Taproot tree has as leaf greater than Taproot maximum depth"); |
1164 | 0 | } |
1165 | 1.50k | if ((leaf_ver & ~TAPROOT_LEAF_MASK) != 0) { |
1166 | 0 | throw std::ios_base::failure("Output Taproot tree has a leaf with an invalid leaf version"); |
1167 | 0 | } |
1168 | 1.50k | m_tap_tree.emplace_back(depth, leaf_ver, script); |
1169 | 1.50k | builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true); |
1170 | 1.50k | } |
1171 | 758 | if (!builder.IsComplete()) { |
1172 | 0 | throw std::ios_base::failure("Output Taproot tree is malformed"); |
1173 | 0 | } |
1174 | 758 | break; |
1175 | 758 | } |
1176 | 4.64k | case PSBT_OUT_TAP_BIP32_DERIVATION: |
1177 | 4.64k | { |
1178 | 4.64k | ExpectedKeySize("Output Taproot BIP32 Keypath", key, 33); |
1179 | 4.64k | XOnlyPubKey xonly(uint256(std::span<uint8_t>(key).last(32))); |
1180 | 4.64k | std::set<uint256> leaf_hashes; |
1181 | 4.64k | uint64_t value_len = ReadCompactSize(s); |
1182 | 4.64k | size_t before_hashes = s.size(); |
1183 | 4.64k | s >> leaf_hashes; |
1184 | 4.64k | size_t after_hashes = s.size(); |
1185 | 4.64k | size_t hashes_len = before_hashes - after_hashes; |
1186 | 4.64k | if (hashes_len > value_len) { |
1187 | 0 | throw std::ios_base::failure("Output Taproot BIP32 keypath has an invalid length"); |
1188 | 0 | } |
1189 | 4.64k | size_t origin_len = value_len - hashes_len; |
1190 | 4.64k | m_tap_bip32_paths.emplace(xonly, std::make_pair(leaf_hashes, DeserializeKeyOrigin(s, origin_len))); |
1191 | 4.64k | break; |
1192 | 4.64k | } |
1193 | 871 | case PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS: |
1194 | 871 | { |
1195 | 871 | ExpectedKeySize("Output MuSig2 Participants Pubkeys", key, CPubKey::COMPRESSED_SIZE + 1); |
1196 | 871 | DeserializeMuSig2ParticipantPubkeys(s, skey, m_musig2_participants, std::string{"Output"}); |
1197 | 871 | break; |
1198 | 4.64k | } |
1199 | 5 | case PSBT_OUT_PROPRIETARY: |
1200 | 5 | { |
1201 | 5 | PSBTProprietary this_prop; |
1202 | 5 | skey >> this_prop.identifier; |
1203 | 5 | this_prop.subtype = ReadCompactSize(skey); |
1204 | 5 | this_prop.key = key; |
1205 | | |
1206 | 5 | s >> this_prop.value; |
1207 | 5 | m_proprietary.insert(this_prop); |
1208 | 5 | break; |
1209 | 4.64k | } |
1210 | | // Unknown stuff |
1211 | 0 | default: { |
1212 | | // Read in the value |
1213 | 0 | std::vector<unsigned char> val_bytes; |
1214 | 0 | s >> val_bytes; |
1215 | 0 | unknown.emplace(std::move(key), std::move(val_bytes)); |
1216 | 0 | break; |
1217 | 4.64k | } |
1218 | 13.8k | } |
1219 | 13.8k | } |
1220 | | |
1221 | 3.20k | if (!found_sep) { |
1222 | 0 | throw std::ios_base::failure("Separator is missing at the end of an output map"); |
1223 | 0 | } |
1224 | | |
1225 | | // Make sure required PSBTv2 fields are present |
1226 | 3.20k | if (m_psbt_version >= 2) { |
1227 | 3.02k | if (!found_amount) { |
1228 | 1 | throw std::ios_base::failure("Output amount is required in PSBTv2"); |
1229 | 1 | } |
1230 | 3.02k | if (!found_script) { |
1231 | 1 | throw std::ios_base::failure("Output script is required in PSBTv2"); |
1232 | 1 | } |
1233 | 3.02k | } |
1234 | 3.20k | } void PSBTOutput::Unserialize<DataStream>(DataStream&) Line | Count | Source | 1067 | 2 | inline void Unserialize(Stream& s) { | 1068 | | // Used for duplicate key detection | 1069 | 2 | std::set<std::vector<unsigned char>> key_lookup; | 1070 | | // Cache whether PSBTv2 required fields are found | 1071 | 2 | bool found_amount = false; | 1072 | 2 | bool found_script = false; | 1073 | | | 1074 | | // Read loop | 1075 | 2 | bool found_sep = false; | 1076 | 2 | while(!s.empty()) { | 1077 | | // Read the key of format "<keylen><keytype><keydata>" after which | 1078 | | // "key" will contain "<keytype><keydata>" | 1079 | 2 | std::vector<unsigned char> key; | 1080 | 2 | s >> key; | 1081 | | | 1082 | | // the key is empty if that was actually a separator byte | 1083 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) | 1084 | 2 | if (key.empty()) { | 1085 | 2 | found_sep = true; | 1086 | 2 | break; | 1087 | 2 | } | 1088 | | | 1089 | | // Duplicate keys are not permitted | 1090 | 0 | if (!key_lookup.emplace(key).second) { | 1091 | 0 | throw std::ios_base::failure(tfm::format("Duplicate Key, output key \"%s\" already provided", HexStr(key))); | 1092 | 0 | } | 1093 | | | 1094 | | // "skey" is used so that "key" is unchanged after reading keytype below | 1095 | 0 | SpanReader skey{key}; | 1096 | | // keytype is of the format compact size uint at the beginning of "key" | 1097 | 0 | uint64_t type = ReadCompactSize(skey); | 1098 | | | 1099 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the | 1100 | | // format "<valuelen><valuedata>" from the stream "s", and value checks | 1101 | 0 | switch(type) { | 1102 | 0 | case PSBT_OUT_REDEEMSCRIPT: | 1103 | 0 | { | 1104 | 0 | ExpectedKeySize("Output redeemScript", key, 1); | 1105 | 0 | s >> redeem_script; | 1106 | 0 | break; | 1107 | 0 | } | 1108 | 0 | case PSBT_OUT_WITNESSSCRIPT: | 1109 | 0 | { | 1110 | 0 | ExpectedKeySize("Output witnessScript", key, 1); | 1111 | 0 | s >> witness_script; | 1112 | 0 | break; | 1113 | 0 | } | 1114 | 0 | case PSBT_OUT_BIP32_DERIVATION: | 1115 | 0 | { | 1116 | 0 | DeserializeHDKeypaths(s, key, hd_keypaths); | 1117 | 0 | break; | 1118 | 0 | } | 1119 | 0 | case PSBT_OUT_AMOUNT: | 1120 | 0 | { | 1121 | 0 | ExpectedKeySize("Output Amount", key, 1); | 1122 | 0 | if (m_psbt_version < 2) { | 1123 | 0 | throw std::ios_base::failure("Output amount is not allowed in PSBTv0"); | 1124 | 0 | } | 1125 | 0 | UnserializeFromVector(s, amount); | 1126 | 0 | found_amount = true; | 1127 | 0 | break; | 1128 | 0 | } | 1129 | 0 | case PSBT_OUT_SCRIPT: | 1130 | 0 | { | 1131 | 0 | ExpectedKeySize("Output Script", key, 1); | 1132 | 0 | if (m_psbt_version < 2) { | 1133 | 0 | throw std::ios_base::failure("Output script is not allowed in PSBTv0"); | 1134 | 0 | } | 1135 | 0 | s >> script; | 1136 | 0 | found_script = true; | 1137 | 0 | break; | 1138 | 0 | } | 1139 | 0 | case PSBT_OUT_TAP_INTERNAL_KEY: | 1140 | 0 | { | 1141 | 0 | ExpectedKeySize("Output Taproot Internal Key", key, 1); | 1142 | 0 | UnserializeFromVector(s, m_tap_internal_key); | 1143 | 0 | break; | 1144 | 0 | } | 1145 | 0 | case PSBT_OUT_TAP_TREE: | 1146 | 0 | { | 1147 | 0 | ExpectedKeySize("Output Taproot Tree Key", key, 1); | 1148 | 0 | std::vector<unsigned char> tree_v; | 1149 | 0 | s >> tree_v; | 1150 | 0 | SpanReader s_tree{tree_v}; | 1151 | 0 | if (s_tree.empty()) { | 1152 | 0 | throw std::ios_base::failure("Output Taproot tree must not be empty"); | 1153 | 0 | } | 1154 | 0 | TaprootBuilder builder; | 1155 | 0 | while (!s_tree.empty()) { | 1156 | 0 | uint8_t depth; | 1157 | 0 | uint8_t leaf_ver; | 1158 | 0 | std::vector<unsigned char> script; | 1159 | 0 | s_tree >> depth; | 1160 | 0 | s_tree >> leaf_ver; | 1161 | 0 | s_tree >> script; | 1162 | 0 | if (depth > TAPROOT_CONTROL_MAX_NODE_COUNT) { | 1163 | 0 | throw std::ios_base::failure("Output Taproot tree has as leaf greater than Taproot maximum depth"); | 1164 | 0 | } | 1165 | 0 | if ((leaf_ver & ~TAPROOT_LEAF_MASK) != 0) { | 1166 | 0 | throw std::ios_base::failure("Output Taproot tree has a leaf with an invalid leaf version"); | 1167 | 0 | } | 1168 | 0 | m_tap_tree.emplace_back(depth, leaf_ver, script); | 1169 | 0 | builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true); | 1170 | 0 | } | 1171 | 0 | if (!builder.IsComplete()) { | 1172 | 0 | throw std::ios_base::failure("Output Taproot tree is malformed"); | 1173 | 0 | } | 1174 | 0 | break; | 1175 | 0 | } | 1176 | 0 | case PSBT_OUT_TAP_BIP32_DERIVATION: | 1177 | 0 | { | 1178 | 0 | ExpectedKeySize("Output Taproot BIP32 Keypath", key, 33); | 1179 | 0 | XOnlyPubKey xonly(uint256(std::span<uint8_t>(key).last(32))); | 1180 | 0 | std::set<uint256> leaf_hashes; | 1181 | 0 | uint64_t value_len = ReadCompactSize(s); | 1182 | 0 | size_t before_hashes = s.size(); | 1183 | 0 | s >> leaf_hashes; | 1184 | 0 | size_t after_hashes = s.size(); | 1185 | 0 | size_t hashes_len = before_hashes - after_hashes; | 1186 | 0 | if (hashes_len > value_len) { | 1187 | 0 | throw std::ios_base::failure("Output Taproot BIP32 keypath has an invalid length"); | 1188 | 0 | } | 1189 | 0 | size_t origin_len = value_len - hashes_len; | 1190 | 0 | m_tap_bip32_paths.emplace(xonly, std::make_pair(leaf_hashes, DeserializeKeyOrigin(s, origin_len))); | 1191 | 0 | break; | 1192 | 0 | } | 1193 | 0 | case PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS: | 1194 | 0 | { | 1195 | 0 | ExpectedKeySize("Output MuSig2 Participants Pubkeys", key, CPubKey::COMPRESSED_SIZE + 1); | 1196 | 0 | DeserializeMuSig2ParticipantPubkeys(s, skey, m_musig2_participants, std::string{"Output"}); | 1197 | 0 | break; | 1198 | 0 | } | 1199 | 0 | case PSBT_OUT_PROPRIETARY: | 1200 | 0 | { | 1201 | 0 | PSBTProprietary this_prop; | 1202 | 0 | skey >> this_prop.identifier; | 1203 | 0 | this_prop.subtype = ReadCompactSize(skey); | 1204 | 0 | this_prop.key = key; | 1205 | |
| 1206 | 0 | s >> this_prop.value; | 1207 | 0 | m_proprietary.insert(this_prop); | 1208 | 0 | break; | 1209 | 0 | } | 1210 | | // Unknown stuff | 1211 | 0 | default: { | 1212 | | // Read in the value | 1213 | 0 | std::vector<unsigned char> val_bytes; | 1214 | 0 | s >> val_bytes; | 1215 | 0 | unknown.emplace(std::move(key), std::move(val_bytes)); | 1216 | 0 | break; | 1217 | 0 | } | 1218 | 0 | } | 1219 | 0 | } | 1220 | | | 1221 | 2 | if (!found_sep) { | 1222 | 0 | throw std::ios_base::failure("Separator is missing at the end of an output map"); | 1223 | 0 | } | 1224 | | | 1225 | | // Make sure required PSBTv2 fields are present | 1226 | 2 | if (m_psbt_version >= 2) { | 1227 | 0 | if (!found_amount) { | 1228 | 0 | throw std::ios_base::failure("Output amount is required in PSBTv2"); | 1229 | 0 | } | 1230 | 0 | if (!found_script) { | 1231 | 0 | throw std::ios_base::failure("Output script is required in PSBTv2"); | 1232 | 0 | } | 1233 | 0 | } | 1234 | 2 | } |
void PSBTOutput::Unserialize<SpanReader>(SpanReader&) Line | Count | Source | 1067 | 3.21k | inline void Unserialize(Stream& s) { | 1068 | | // Used for duplicate key detection | 1069 | 3.21k | std::set<std::vector<unsigned char>> key_lookup; | 1070 | | // Cache whether PSBTv2 required fields are found | 1071 | 3.21k | bool found_amount = false; | 1072 | 3.21k | bool found_script = false; | 1073 | | | 1074 | | // Read loop | 1075 | 3.21k | bool found_sep = false; | 1076 | 17.0k | while(!s.empty()) { | 1077 | | // Read the key of format "<keylen><keytype><keydata>" after which | 1078 | | // "key" will contain "<keytype><keydata>" | 1079 | 17.0k | std::vector<unsigned char> key; | 1080 | 17.0k | s >> key; | 1081 | | | 1082 | | // the key is empty if that was actually a separator byte | 1083 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) | 1084 | 17.0k | if (key.empty()) { | 1085 | 3.20k | found_sep = true; | 1086 | 3.20k | break; | 1087 | 3.20k | } | 1088 | | | 1089 | | // Duplicate keys are not permitted | 1090 | 13.8k | if (!key_lookup.emplace(key).second) { | 1091 | 2 | throw std::ios_base::failure(tfm::format("Duplicate Key, output key \"%s\" already provided", HexStr(key))); | 1092 | 2 | } | 1093 | | | 1094 | | // "skey" is used so that "key" is unchanged after reading keytype below | 1095 | 13.8k | SpanReader skey{key}; | 1096 | | // keytype is of the format compact size uint at the beginning of "key" | 1097 | 13.8k | uint64_t type = ReadCompactSize(skey); | 1098 | | | 1099 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the | 1100 | | // format "<valuelen><valuedata>" from the stream "s", and value checks | 1101 | 13.8k | switch(type) { | 1102 | 20 | case PSBT_OUT_REDEEMSCRIPT: | 1103 | 20 | { | 1104 | 20 | ExpectedKeySize("Output redeemScript", key, 1); | 1105 | 20 | s >> redeem_script; | 1106 | 20 | break; | 1107 | 0 | } | 1108 | 52 | case PSBT_OUT_WITNESSSCRIPT: | 1109 | 52 | { | 1110 | 52 | ExpectedKeySize("Output witnessScript", key, 1); | 1111 | 52 | s >> witness_script; | 1112 | 52 | break; | 1113 | 0 | } | 1114 | 420 | case PSBT_OUT_BIP32_DERIVATION: | 1115 | 420 | { | 1116 | 420 | DeserializeHDKeypaths(s, key, hd_keypaths); | 1117 | 420 | break; | 1118 | 0 | } | 1119 | 3.02k | case PSBT_OUT_AMOUNT: | 1120 | 3.02k | { | 1121 | 3.02k | ExpectedKeySize("Output Amount", key, 1); | 1122 | 3.02k | if (m_psbt_version < 2) { | 1123 | 1 | throw std::ios_base::failure("Output amount is not allowed in PSBTv0"); | 1124 | 1 | } | 1125 | 3.02k | UnserializeFromVector(s, amount); | 1126 | 3.02k | found_amount = true; | 1127 | 3.02k | break; | 1128 | 3.02k | } | 1129 | 3.02k | case PSBT_OUT_SCRIPT: | 1130 | 3.02k | { | 1131 | 3.02k | ExpectedKeySize("Output Script", key, 1); | 1132 | 3.02k | if (m_psbt_version < 2) { | 1133 | 1 | throw std::ios_base::failure("Output script is not allowed in PSBTv0"); | 1134 | 1 | } | 1135 | 3.02k | s >> script; | 1136 | 3.02k | found_script = true; | 1137 | 3.02k | break; | 1138 | 3.02k | } | 1139 | 1.01k | case PSBT_OUT_TAP_INTERNAL_KEY: | 1140 | 1.01k | { | 1141 | 1.01k | ExpectedKeySize("Output Taproot Internal Key", key, 1); | 1142 | 1.01k | UnserializeFromVector(s, m_tap_internal_key); | 1143 | 1.01k | break; | 1144 | 3.02k | } | 1145 | 759 | case PSBT_OUT_TAP_TREE: | 1146 | 759 | { | 1147 | 759 | ExpectedKeySize("Output Taproot Tree Key", key, 1); | 1148 | 759 | std::vector<unsigned char> tree_v; | 1149 | 759 | s >> tree_v; | 1150 | 759 | SpanReader s_tree{tree_v}; | 1151 | 759 | if (s_tree.empty()) { | 1152 | 1 | throw std::ios_base::failure("Output Taproot tree must not be empty"); | 1153 | 1 | } | 1154 | 758 | TaprootBuilder builder; | 1155 | 2.25k | while (!s_tree.empty()) { | 1156 | 1.50k | uint8_t depth; | 1157 | 1.50k | uint8_t leaf_ver; | 1158 | 1.50k | std::vector<unsigned char> script; | 1159 | 1.50k | s_tree >> depth; | 1160 | 1.50k | s_tree >> leaf_ver; | 1161 | 1.50k | s_tree >> script; | 1162 | 1.50k | if (depth > TAPROOT_CONTROL_MAX_NODE_COUNT) { | 1163 | 0 | throw std::ios_base::failure("Output Taproot tree has as leaf greater than Taproot maximum depth"); | 1164 | 0 | } | 1165 | 1.50k | if ((leaf_ver & ~TAPROOT_LEAF_MASK) != 0) { | 1166 | 0 | throw std::ios_base::failure("Output Taproot tree has a leaf with an invalid leaf version"); | 1167 | 0 | } | 1168 | 1.50k | m_tap_tree.emplace_back(depth, leaf_ver, script); | 1169 | 1.50k | builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true); | 1170 | 1.50k | } | 1171 | 758 | if (!builder.IsComplete()) { | 1172 | 0 | throw std::ios_base::failure("Output Taproot tree is malformed"); | 1173 | 0 | } | 1174 | 758 | break; | 1175 | 758 | } | 1176 | 4.64k | case PSBT_OUT_TAP_BIP32_DERIVATION: | 1177 | 4.64k | { | 1178 | 4.64k | ExpectedKeySize("Output Taproot BIP32 Keypath", key, 33); | 1179 | 4.64k | XOnlyPubKey xonly(uint256(std::span<uint8_t>(key).last(32))); | 1180 | 4.64k | std::set<uint256> leaf_hashes; | 1181 | 4.64k | uint64_t value_len = ReadCompactSize(s); | 1182 | 4.64k | size_t before_hashes = s.size(); | 1183 | 4.64k | s >> leaf_hashes; | 1184 | 4.64k | size_t after_hashes = s.size(); | 1185 | 4.64k | size_t hashes_len = before_hashes - after_hashes; | 1186 | 4.64k | if (hashes_len > value_len) { | 1187 | 0 | throw std::ios_base::failure("Output Taproot BIP32 keypath has an invalid length"); | 1188 | 0 | } | 1189 | 4.64k | size_t origin_len = value_len - hashes_len; | 1190 | 4.64k | m_tap_bip32_paths.emplace(xonly, std::make_pair(leaf_hashes, DeserializeKeyOrigin(s, origin_len))); | 1191 | 4.64k | break; | 1192 | 4.64k | } | 1193 | 871 | case PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS: | 1194 | 871 | { | 1195 | 871 | ExpectedKeySize("Output MuSig2 Participants Pubkeys", key, CPubKey::COMPRESSED_SIZE + 1); | 1196 | 871 | DeserializeMuSig2ParticipantPubkeys(s, skey, m_musig2_participants, std::string{"Output"}); | 1197 | 871 | break; | 1198 | 4.64k | } | 1199 | 5 | case PSBT_OUT_PROPRIETARY: | 1200 | 5 | { | 1201 | 5 | PSBTProprietary this_prop; | 1202 | 5 | skey >> this_prop.identifier; | 1203 | 5 | this_prop.subtype = ReadCompactSize(skey); | 1204 | 5 | this_prop.key = key; | 1205 | | | 1206 | 5 | s >> this_prop.value; | 1207 | 5 | m_proprietary.insert(this_prop); | 1208 | 5 | break; | 1209 | 4.64k | } | 1210 | | // Unknown stuff | 1211 | 0 | default: { | 1212 | | // Read in the value | 1213 | 0 | std::vector<unsigned char> val_bytes; | 1214 | 0 | s >> val_bytes; | 1215 | 0 | unknown.emplace(std::move(key), std::move(val_bytes)); | 1216 | 0 | break; | 1217 | 4.64k | } | 1218 | 13.8k | } | 1219 | 13.8k | } | 1220 | | | 1221 | 3.20k | if (!found_sep) { | 1222 | 0 | throw std::ios_base::failure("Separator is missing at the end of an output map"); | 1223 | 0 | } | 1224 | | | 1225 | | // Make sure required PSBTv2 fields are present | 1226 | 3.20k | if (m_psbt_version >= 2) { | 1227 | 3.02k | if (!found_amount) { | 1228 | 1 | throw std::ios_base::failure("Output amount is required in PSBTv2"); | 1229 | 1 | } | 1230 | 3.02k | if (!found_script) { | 1231 | 1 | throw std::ios_base::failure("Output script is required in PSBTv2"); | 1232 | 1 | } | 1233 | 3.02k | } | 1234 | 3.20k | } |
|
1235 | | }; |
1236 | | |
1237 | | /** A version of CTransaction with the PSBT format*/ |
1238 | | class PartiallySignedTransaction |
1239 | | { |
1240 | | private: |
1241 | | std::optional<uint32_t> m_version; |
1242 | | |
1243 | | public: |
1244 | | // We use a vector of CExtPubKey in the event that there happens to be the same KeyOriginInfos for different CExtPubKeys |
1245 | | // Note that this map swaps the key and values from the serialization |
1246 | | std::map<KeyOriginInfo, std::set<CExtPubKey>> m_xpubs; |
1247 | | std::optional<std::bitset<8>> m_tx_modifiable; |
1248 | | std::vector<PSBTInput> inputs; |
1249 | | std::vector<PSBTOutput> outputs; |
1250 | | std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown; |
1251 | | std::set<PSBTProprietary> m_proprietary; |
1252 | | |
1253 | | uint32_t tx_version; |
1254 | | std::optional<uint32_t> fallback_locktime; |
1255 | | |
1256 | | uint32_t GetVersion() const; |
1257 | | |
1258 | | /** Merge psbt into this. The two psbts must have the same underlying CTransaction (i.e. the |
1259 | | * same actual Bitcoin transaction.) Returns true if the merge succeeded, false otherwise. */ |
1260 | | [[nodiscard]] bool Merge(const PartiallySignedTransaction& psbt); |
1261 | | /** Merge the global xpubs of psbt into this, keeping the existing origin for an xpub |
1262 | | * seen again with a different one, as the serialized records are keyed by xpub. */ |
1263 | | void MergeGlobalXPubs(const PartiallySignedTransaction& psbt); |
1264 | | bool AddInput(const PSBTInput& psbtin); |
1265 | | bool AddOutput(const PSBTOutput& psbtout); |
1266 | | std::optional<uint32_t> ComputeTimeLock() const; |
1267 | | std::optional<CMutableTransaction> GetUnsignedTx() const; |
1268 | | std::optional<Txid> GetUniqueID() const; |
1269 | | explicit PartiallySignedTransaction(const CMutableTransaction& tx, uint32_t version = 2); |
1270 | | |
1271 | | template <typename Stream> |
1272 | 1.12k | inline void Serialize(Stream& s) const { |
1273 | | |
1274 | | // magic bytes |
1275 | 1.12k | s << PSBT_MAGIC_BYTES; |
1276 | | |
1277 | 1.12k | if (GetVersion() < 2) { |
1278 | | // unsigned tx flag |
1279 | 50 | SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_UNSIGNED_TX)); |
1280 | | |
1281 | | // Write serialized tx to a stream |
1282 | 50 | SerializeToVector(s, TX_NO_WITNESS(*GetUnsignedTx())); |
1283 | 50 | } |
1284 | | |
1285 | | // Write xpubs |
1286 | 1.12k | for (const auto& xpub_pair : m_xpubs) { |
1287 | 4 | for (const auto& xpub : xpub_pair.second) { |
1288 | 4 | unsigned char ser_xpub[BIP32_EXTKEY_WITH_VERSION_SIZE]; |
1289 | 4 | xpub.EncodeWithVersion(ser_xpub); |
1290 | | // Note that the serialization swaps the key and value |
1291 | | // The xpub is the key (for uniqueness) while the path is the value |
1292 | 4 | SerializeToVector(s, PSBT_GLOBAL_XPUB, ser_xpub); |
1293 | 4 | SerializeHDKeypath(s, xpub_pair.first); |
1294 | 4 | } |
1295 | 3 | } |
1296 | | |
1297 | 1.12k | if (GetVersion() >= 2) { |
1298 | | // Write PSBTv2 tx version, locktime, counts, etc. |
1299 | 1.07k | SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_TX_VERSION)); |
1300 | 1.07k | SerializeToVector(s, tx_version); |
1301 | 1.07k | if (fallback_locktime != std::nullopt) { |
1302 | 1.07k | SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_FALLBACK_LOCKTIME)); |
1303 | 1.07k | SerializeToVector(s, *fallback_locktime); |
1304 | 1.07k | } |
1305 | | |
1306 | 1.07k | SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_INPUT_COUNT)); |
1307 | 1.07k | SerializeToVector(s, CompactSizeWriter(inputs.size())); |
1308 | 1.07k | SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_OUTPUT_COUNT)); |
1309 | 1.07k | SerializeToVector(s, CompactSizeWriter(outputs.size())); |
1310 | | |
1311 | 1.07k | if (m_tx_modifiable != std::nullopt) { |
1312 | 0 | SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_TX_MODIFIABLE)); |
1313 | 0 | SerializeToVector(s, static_cast<uint8_t>(m_tx_modifiable->to_ulong())); |
1314 | 0 | } |
1315 | 1.07k | } |
1316 | | |
1317 | | // PSBT version |
1318 | 1.12k | if (GetVersion() > 0) { |
1319 | 1.07k | SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_VERSION)); |
1320 | 1.07k | SerializeToVector(s, *m_version); |
1321 | 1.07k | } |
1322 | | |
1323 | | // Write proprietary things |
1324 | 1.12k | for (const auto& entry : m_proprietary) { |
1325 | 6 | s << entry.key; |
1326 | 6 | s << entry.value; |
1327 | 6 | } |
1328 | | |
1329 | | // Write the unknown things |
1330 | 1.12k | for (auto& entry : unknown) { |
1331 | 0 | s << entry.first; |
1332 | 0 | s << entry.second; |
1333 | 0 | } |
1334 | | |
1335 | | // Separator |
1336 | 1.12k | s << PSBT_SEPARATOR; |
1337 | | |
1338 | | // Write inputs |
1339 | 1.56k | for (const PSBTInput& input : inputs) { |
1340 | 1.56k | s << input; |
1341 | 1.56k | } |
1342 | | // Write outputs |
1343 | 2.38k | for (const PSBTOutput& output : outputs) { |
1344 | 2.38k | s << output; |
1345 | 2.38k | } |
1346 | 1.12k | } |
1347 | | |
1348 | | |
1349 | | template <typename Stream> |
1350 | 1.83k | inline void Unserialize(Stream& s) { |
1351 | | // Read the magic bytes |
1352 | 1.83k | uint8_t magic[5]; |
1353 | 1.83k | s >> magic; |
1354 | 1.83k | if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) { |
1355 | 2 | throw std::ios_base::failure("Invalid PSBT magic bytes"); |
1356 | 2 | } |
1357 | | |
1358 | | // Used for duplicate key detection |
1359 | 1.83k | std::set<std::vector<unsigned char>> key_lookup; |
1360 | | |
1361 | | // Read global data |
1362 | 1.83k | bool found_sep = false; |
1363 | 1.83k | std::optional<CMutableTransaction> tx; |
1364 | 1.83k | uint64_t input_count = 0; |
1365 | 1.83k | uint64_t output_count = 0; |
1366 | 1.83k | bool found_input_count = false; |
1367 | 1.83k | bool found_output_count = false; |
1368 | 1.83k | bool found_tx_version = false; |
1369 | 1.83k | bool found_fallback_locktime = false; |
1370 | 10.2k | while(!s.empty()) { |
1371 | | // Read the key of format "<keylen><keytype><keydata>" after which |
1372 | | // "key" will contain "<keytype><keydata>" |
1373 | 10.2k | std::vector<unsigned char> key; |
1374 | 10.2k | s >> key; |
1375 | | |
1376 | | // the key is empty if that was actually a separator byte |
1377 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) |
1378 | 10.2k | if (key.empty()) { |
1379 | 1.83k | found_sep = true; |
1380 | 1.83k | break; |
1381 | 1.83k | } |
1382 | | |
1383 | | // Duplicate keys are not permitted |
1384 | 8.37k | if (!key_lookup.emplace(key).second) { |
1385 | 0 | throw std::ios_base::failure(tfm::format("Duplicate Key, global key \"%s\" already provided", HexStr(key))); |
1386 | 0 | } |
1387 | | |
1388 | | // "skey" is used so that "key" is unchanged after reading keytype below |
1389 | 8.37k | SpanReader skey{key}; |
1390 | | // keytype is of the format compact size uint at the beginning of "key" |
1391 | 8.37k | uint64_t type = ReadCompactSize(skey); |
1392 | | |
1393 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the |
1394 | | // format "<valuelen><valuedata>" from the stream "s", and value checks |
1395 | 8.37k | switch(type) { |
1396 | 201 | case PSBT_GLOBAL_UNSIGNED_TX: |
1397 | 201 | { |
1398 | 201 | ExpectedKeySize("Global Unsigned TX", key, 1); |
1399 | | // Set the stream to serialize with non-witness since this should always be non-witness |
1400 | 201 | tx.emplace(); |
1401 | 201 | UnserializeFromVector(s, TX_NO_WITNESS(*tx)); |
1402 | | // Make sure that all scriptSigs and scriptWitnesses are empty |
1403 | 252 | for (const CTxIn& txin : tx->vin) { |
1404 | 252 | if (!txin.scriptSig.empty() || !txin.scriptWitness.IsNull()) { |
1405 | 1 | throw std::ios_base::failure("Unsigned tx does not have empty scriptSigs and scriptWitnesses."); |
1406 | 1 | } |
1407 | 252 | } |
1408 | 200 | tx_version = tx->version; |
1409 | 200 | fallback_locktime = tx->nLockTime; |
1410 | | // Set the input and output counts |
1411 | 200 | input_count = tx->vin.size(); |
1412 | 200 | output_count = tx->vout.size(); |
1413 | 200 | break; |
1414 | 201 | } |
1415 | 1.63k | case PSBT_GLOBAL_TX_VERSION: |
1416 | 1.63k | { |
1417 | 1.63k | ExpectedKeySize("Global Transaction Version", key, 1); |
1418 | 1.63k | UnserializeFromVector(s, tx_version); |
1419 | 1.63k | found_tx_version = true; |
1420 | 1.63k | break; |
1421 | 201 | } |
1422 | 1.61k | case PSBT_GLOBAL_FALLBACK_LOCKTIME: |
1423 | 1.61k | { |
1424 | 1.61k | ExpectedKeySize("Global Fallback Locktime", key, 1); |
1425 | 1.61k | fallback_locktime.emplace(); |
1426 | 1.61k | UnserializeFromVector(s, *fallback_locktime); |
1427 | 1.61k | found_fallback_locktime = true; |
1428 | 1.61k | break; |
1429 | 201 | } |
1430 | 1.63k | case PSBT_GLOBAL_INPUT_COUNT: |
1431 | 1.63k | { |
1432 | 1.63k | ExpectedKeySize("Global Input Count", key, 1); |
1433 | 1.63k | CompactSizeReader reader(input_count); |
1434 | 1.63k | UnserializeFromVector(s, reader); |
1435 | 1.63k | found_input_count = true; |
1436 | 1.63k | break; |
1437 | 201 | } |
1438 | 1.63k | case PSBT_GLOBAL_OUTPUT_COUNT: |
1439 | 1.63k | { |
1440 | 1.63k | ExpectedKeySize("Global Output Count", key, 1); |
1441 | 1.63k | CompactSizeReader reader(output_count); |
1442 | 1.63k | UnserializeFromVector(s, reader); |
1443 | 1.63k | found_output_count = true; |
1444 | 1.63k | break; |
1445 | 201 | } |
1446 | 13 | case PSBT_GLOBAL_TX_MODIFIABLE: |
1447 | 13 | { |
1448 | 13 | ExpectedKeySize("Global TX Modifiable Flags", key, 1); |
1449 | 13 | uint8_t tx_mod; |
1450 | 13 | UnserializeFromVector(s, tx_mod); |
1451 | 13 | m_tx_modifiable.emplace(tx_mod); |
1452 | 13 | break; |
1453 | 201 | } |
1454 | 11 | case PSBT_GLOBAL_XPUB: |
1455 | 11 | { |
1456 | 11 | ExpectedKeySize("Global XPUB", key, BIP32_EXTKEY_WITH_VERSION_SIZE + 1); |
1457 | | // Read in the xpub from key |
1458 | 11 | CExtPubKey xpub; |
1459 | 11 | xpub.DecodeWithVersion(&key.data()[1]); |
1460 | 11 | if (!xpub.pubkey.IsFullyValid()) { |
1461 | 0 | throw std::ios_base::failure("Invalid pubkey"); |
1462 | 0 | } |
1463 | | // Read in the keypath from stream |
1464 | 11 | KeyOriginInfo keypath; |
1465 | 11 | DeserializeHDKeypath(s, keypath); |
1466 | | |
1467 | | // Note that we store these swapped to make searches faster. |
1468 | | // Serialization uses xpub -> keypath to enqure key uniqueness |
1469 | 11 | if (!m_xpubs.contains(keypath)) { |
1470 | | // Make a new set to put the xpub in |
1471 | 11 | m_xpubs[keypath] = {xpub}; |
1472 | 11 | } else { |
1473 | | // Insert xpub into existing set |
1474 | 0 | m_xpubs[keypath].insert(xpub); |
1475 | 0 | } |
1476 | 11 | break; |
1477 | 11 | } |
1478 | 1.63k | case PSBT_GLOBAL_VERSION: |
1479 | 1.63k | { |
1480 | 1.63k | ExpectedKeySize("Global PSBT Version", key, 1); |
1481 | 1.63k | uint32_t v; |
1482 | 1.63k | UnserializeFromVector(s, v); |
1483 | 1.63k | m_version = v; |
1484 | 1.63k | if (*m_version > PSBT_HIGHEST_VERSION) { |
1485 | 0 | throw std::ios_base::failure("Unsupported version number"); |
1486 | 0 | } |
1487 | 1.63k | break; |
1488 | 1.63k | } |
1489 | 1.63k | case PSBT_GLOBAL_PROPRIETARY: |
1490 | 11 | { |
1491 | 11 | PSBTProprietary this_prop; |
1492 | 11 | skey >> this_prop.identifier; |
1493 | 11 | this_prop.subtype = ReadCompactSize(skey); |
1494 | 11 | this_prop.key = key; |
1495 | | |
1496 | 11 | s >> this_prop.value; |
1497 | 11 | m_proprietary.insert(this_prop); |
1498 | 11 | break; |
1499 | 1.63k | } |
1500 | | // Unknown stuff |
1501 | 0 | default: { |
1502 | | // Read in the value |
1503 | 0 | std::vector<unsigned char> val_bytes; |
1504 | 0 | s >> val_bytes; |
1505 | 0 | unknown.emplace(std::move(key), std::move(val_bytes)); |
1506 | 0 | } |
1507 | 8.37k | } |
1508 | 8.37k | } |
1509 | | |
1510 | 1.83k | if (!found_sep) { |
1511 | 0 | throw std::ios_base::failure("Separator is missing at the end of the global map"); |
1512 | 0 | } |
1513 | | |
1514 | 1.83k | const uint32_t psbt_ver = GetVersion(); |
1515 | | |
1516 | | // Check PSBT version constraints |
1517 | 1.83k | if (psbt_ver == 0) { |
1518 | | // Make sure that we got an unsigned tx for PSBTv0 |
1519 | 197 | if (!tx) { |
1520 | 1 | throw std::ios_base::failure("No unsigned transaction was provided"); |
1521 | 1 | } |
1522 | | // Make sure no PSBTv2 fields are present |
1523 | 196 | if (found_tx_version) { |
1524 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_TX_VERSION is not allowed in PSBTv0"); |
1525 | 1 | } |
1526 | 195 | if (found_fallback_locktime) { |
1527 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_FALLBACK_LOCKTIME is not allowed in PSBTv0"); |
1528 | 0 | } |
1529 | 195 | if (found_input_count) { |
1530 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_INPUT_COUNT is not allowed in PSBTv0"); |
1531 | 1 | } |
1532 | 194 | if (found_output_count) { |
1533 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_OUTPUT_COUNT is not allowed in PSBTv0"); |
1534 | 1 | } |
1535 | 193 | if (m_tx_modifiable != std::nullopt) { |
1536 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_TX_MODIFIABLE is not allowed in PSBTv0"); |
1537 | 1 | } |
1538 | 193 | } |
1539 | | // Disallow v1 |
1540 | 1.82k | if (psbt_ver == 1) { |
1541 | 1 | throw std::ios_base::failure("There is no PSBT version 1"); |
1542 | 1 | } |
1543 | 1.82k | if (psbt_ver == 2) { |
1544 | | // Tx version, input, and output counts are required |
1545 | 1.63k | if (!found_tx_version) { |
1546 | 2 | throw std::ios_base::failure("PSBT_GLOBAL_TX_VERSION is required in PSBTv2"); |
1547 | 2 | } |
1548 | 1.63k | if (!found_input_count) { |
1549 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_INPUT_COUNT is required in PSBTv2"); |
1550 | 1 | } |
1551 | 1.62k | if (!found_output_count) { |
1552 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_OUTPUT_COUNT is required in PSBTv2"); |
1553 | 1 | } |
1554 | | // Unsigned tx is disallowed |
1555 | 1.62k | if (tx) { |
1556 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_UNSIGNED_TX is not allowed in PSBTv2"); |
1557 | 1 | } |
1558 | 1.62k | } |
1559 | 1.81k | if (psbt_ver > 2) { |
1560 | 0 | throw std::ios_base::failure("Unknown PSBT version"); |
1561 | 0 | } |
1562 | | |
1563 | | // Read input data |
1564 | 1.81k | unsigned int i = 0; |
1565 | 3.99k | while (!s.empty() && i < input_count) { |
1566 | 2.17k | if (psbt_ver < 2) { |
1567 | 236 | inputs.emplace_back(psbt_ver, tx->vin[i].prevout.hash, tx->vin[i].prevout.n, tx->vin[i].nSequence); |
1568 | 236 | s >> inputs.back(); |
1569 | 1.93k | } else { |
1570 | 1.93k | inputs.emplace_back(deserialize, s, psbt_ver); |
1571 | 1.93k | } |
1572 | | |
1573 | | // Make sure the non-witness utxo matches the outpoint |
1574 | 2.17k | const PSBTInput& input = inputs.back(); |
1575 | 2.17k | if (input.non_witness_utxo) { |
1576 | 462 | if (psbt_ver < 2) { |
1577 | 49 | if (input.non_witness_utxo->GetHash() != tx->vin[i].prevout.hash) { |
1578 | 1 | throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash"); |
1579 | 1 | } |
1580 | 48 | if (tx->vin[i].prevout.n >= input.non_witness_utxo->vout.size()) { |
1581 | 3 | throw std::ios_base::failure("Input specifies output index that does not exist"); |
1582 | 3 | } |
1583 | 413 | } else { |
1584 | 413 | if (input.non_witness_utxo->GetHash() != input.prev_txid) { |
1585 | 0 | throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash"); |
1586 | 0 | } |
1587 | 413 | if (input.prev_out >= input.non_witness_utxo->vout.size()) { |
1588 | 0 | throw std::ios_base::failure("Input specifies output index that does not exist"); |
1589 | 0 | } |
1590 | 413 | } |
1591 | 462 | } |
1592 | 2.17k | ++i; |
1593 | 2.17k | } |
1594 | | // Make sure that the number of inputs matches the number of inputs in the transaction |
1595 | 1.81k | if (inputs.size() != input_count) { |
1596 | 0 | throw std::ios_base::failure("Inputs provided does not match the number of inputs in transaction."); |
1597 | 0 | } |
1598 | | |
1599 | | // Read output data |
1600 | 1.81k | i = 0; |
1601 | 5.02k | while (!s.empty() && i < output_count) { |
1602 | 3.21k | if (psbt_ver < 2) { |
1603 | 186 | outputs.emplace_back(psbt_ver, tx->vout[i].nValue, tx->vout[i].scriptPubKey); |
1604 | 186 | s >> outputs.back(); |
1605 | 3.02k | } else { |
1606 | 3.02k | outputs.emplace_back(deserialize, s, psbt_ver); |
1607 | 3.02k | } |
1608 | 3.21k | ++i; |
1609 | 3.21k | } |
1610 | | // Make sure that the number of outputs matches the number of outputs in the transaction |
1611 | 1.81k | if (outputs.size() != output_count) { |
1612 | 1 | throw std::ios_base::failure("Outputs provided does not match the number of outputs in transaction."); |
1613 | 1 | } |
1614 | 1.81k | } void PartiallySignedTransaction::Unserialize<DataStream>(DataStream&) Line | Count | Source | 1350 | 1 | inline void Unserialize(Stream& s) { | 1351 | | // Read the magic bytes | 1352 | 1 | uint8_t magic[5]; | 1353 | 1 | s >> magic; | 1354 | 1 | if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) { | 1355 | 0 | throw std::ios_base::failure("Invalid PSBT magic bytes"); | 1356 | 0 | } | 1357 | | | 1358 | | // Used for duplicate key detection | 1359 | 1 | std::set<std::vector<unsigned char>> key_lookup; | 1360 | | | 1361 | | // Read global data | 1362 | 1 | bool found_sep = false; | 1363 | 1 | std::optional<CMutableTransaction> tx; | 1364 | 1 | uint64_t input_count = 0; | 1365 | 1 | uint64_t output_count = 0; | 1366 | 1 | bool found_input_count = false; | 1367 | 1 | bool found_output_count = false; | 1368 | 1 | bool found_tx_version = false; | 1369 | 1 | bool found_fallback_locktime = false; | 1370 | 2 | while(!s.empty()) { | 1371 | | // Read the key of format "<keylen><keytype><keydata>" after which | 1372 | | // "key" will contain "<keytype><keydata>" | 1373 | 2 | std::vector<unsigned char> key; | 1374 | 2 | s >> key; | 1375 | | | 1376 | | // the key is empty if that was actually a separator byte | 1377 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) | 1378 | 2 | if (key.empty()) { | 1379 | 1 | found_sep = true; | 1380 | 1 | break; | 1381 | 1 | } | 1382 | | | 1383 | | // Duplicate keys are not permitted | 1384 | 1 | if (!key_lookup.emplace(key).second) { | 1385 | 0 | throw std::ios_base::failure(tfm::format("Duplicate Key, global key \"%s\" already provided", HexStr(key))); | 1386 | 0 | } | 1387 | | | 1388 | | // "skey" is used so that "key" is unchanged after reading keytype below | 1389 | 1 | SpanReader skey{key}; | 1390 | | // keytype is of the format compact size uint at the beginning of "key" | 1391 | 1 | uint64_t type = ReadCompactSize(skey); | 1392 | | | 1393 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the | 1394 | | // format "<valuelen><valuedata>" from the stream "s", and value checks | 1395 | 1 | switch(type) { | 1396 | 1 | case PSBT_GLOBAL_UNSIGNED_TX: | 1397 | 1 | { | 1398 | 1 | ExpectedKeySize("Global Unsigned TX", key, 1); | 1399 | | // Set the stream to serialize with non-witness since this should always be non-witness | 1400 | 1 | tx.emplace(); | 1401 | 1 | UnserializeFromVector(s, TX_NO_WITNESS(*tx)); | 1402 | | // Make sure that all scriptSigs and scriptWitnesses are empty | 1403 | 2 | for (const CTxIn& txin : tx->vin) { | 1404 | 2 | if (!txin.scriptSig.empty() || !txin.scriptWitness.IsNull()) { | 1405 | 0 | throw std::ios_base::failure("Unsigned tx does not have empty scriptSigs and scriptWitnesses."); | 1406 | 0 | } | 1407 | 2 | } | 1408 | 1 | tx_version = tx->version; | 1409 | 1 | fallback_locktime = tx->nLockTime; | 1410 | | // Set the input and output counts | 1411 | 1 | input_count = tx->vin.size(); | 1412 | 1 | output_count = tx->vout.size(); | 1413 | 1 | break; | 1414 | 1 | } | 1415 | 0 | case PSBT_GLOBAL_TX_VERSION: | 1416 | 0 | { | 1417 | 0 | ExpectedKeySize("Global Transaction Version", key, 1); | 1418 | 0 | UnserializeFromVector(s, tx_version); | 1419 | 0 | found_tx_version = true; | 1420 | 0 | break; | 1421 | 1 | } | 1422 | 0 | case PSBT_GLOBAL_FALLBACK_LOCKTIME: | 1423 | 0 | { | 1424 | 0 | ExpectedKeySize("Global Fallback Locktime", key, 1); | 1425 | 0 | fallback_locktime.emplace(); | 1426 | 0 | UnserializeFromVector(s, *fallback_locktime); | 1427 | 0 | found_fallback_locktime = true; | 1428 | 0 | break; | 1429 | 1 | } | 1430 | 0 | case PSBT_GLOBAL_INPUT_COUNT: | 1431 | 0 | { | 1432 | 0 | ExpectedKeySize("Global Input Count", key, 1); | 1433 | 0 | CompactSizeReader reader(input_count); | 1434 | 0 | UnserializeFromVector(s, reader); | 1435 | 0 | found_input_count = true; | 1436 | 0 | break; | 1437 | 1 | } | 1438 | 0 | case PSBT_GLOBAL_OUTPUT_COUNT: | 1439 | 0 | { | 1440 | 0 | ExpectedKeySize("Global Output Count", key, 1); | 1441 | 0 | CompactSizeReader reader(output_count); | 1442 | 0 | UnserializeFromVector(s, reader); | 1443 | 0 | found_output_count = true; | 1444 | 0 | break; | 1445 | 1 | } | 1446 | 0 | case PSBT_GLOBAL_TX_MODIFIABLE: | 1447 | 0 | { | 1448 | 0 | ExpectedKeySize("Global TX Modifiable Flags", key, 1); | 1449 | 0 | uint8_t tx_mod; | 1450 | 0 | UnserializeFromVector(s, tx_mod); | 1451 | 0 | m_tx_modifiable.emplace(tx_mod); | 1452 | 0 | break; | 1453 | 1 | } | 1454 | 0 | case PSBT_GLOBAL_XPUB: | 1455 | 0 | { | 1456 | 0 | ExpectedKeySize("Global XPUB", key, BIP32_EXTKEY_WITH_VERSION_SIZE + 1); | 1457 | | // Read in the xpub from key | 1458 | 0 | CExtPubKey xpub; | 1459 | 0 | xpub.DecodeWithVersion(&key.data()[1]); | 1460 | 0 | if (!xpub.pubkey.IsFullyValid()) { | 1461 | 0 | throw std::ios_base::failure("Invalid pubkey"); | 1462 | 0 | } | 1463 | | // Read in the keypath from stream | 1464 | 0 | KeyOriginInfo keypath; | 1465 | 0 | DeserializeHDKeypath(s, keypath); | 1466 | | | 1467 | | // Note that we store these swapped to make searches faster. | 1468 | | // Serialization uses xpub -> keypath to enqure key uniqueness | 1469 | 0 | if (!m_xpubs.contains(keypath)) { | 1470 | | // Make a new set to put the xpub in | 1471 | 0 | m_xpubs[keypath] = {xpub}; | 1472 | 0 | } else { | 1473 | | // Insert xpub into existing set | 1474 | 0 | m_xpubs[keypath].insert(xpub); | 1475 | 0 | } | 1476 | 0 | break; | 1477 | 0 | } | 1478 | 0 | case PSBT_GLOBAL_VERSION: | 1479 | 0 | { | 1480 | 0 | ExpectedKeySize("Global PSBT Version", key, 1); | 1481 | 0 | uint32_t v; | 1482 | 0 | UnserializeFromVector(s, v); | 1483 | 0 | m_version = v; | 1484 | 0 | if (*m_version > PSBT_HIGHEST_VERSION) { | 1485 | 0 | throw std::ios_base::failure("Unsupported version number"); | 1486 | 0 | } | 1487 | 0 | break; | 1488 | 0 | } | 1489 | 0 | case PSBT_GLOBAL_PROPRIETARY: | 1490 | 0 | { | 1491 | 0 | PSBTProprietary this_prop; | 1492 | 0 | skey >> this_prop.identifier; | 1493 | 0 | this_prop.subtype = ReadCompactSize(skey); | 1494 | 0 | this_prop.key = key; | 1495 | |
| 1496 | 0 | s >> this_prop.value; | 1497 | 0 | m_proprietary.insert(this_prop); | 1498 | 0 | break; | 1499 | 0 | } | 1500 | | // Unknown stuff | 1501 | 0 | default: { | 1502 | | // Read in the value | 1503 | 0 | std::vector<unsigned char> val_bytes; | 1504 | 0 | s >> val_bytes; | 1505 | 0 | unknown.emplace(std::move(key), std::move(val_bytes)); | 1506 | 0 | } | 1507 | 1 | } | 1508 | 1 | } | 1509 | | | 1510 | 1 | if (!found_sep) { | 1511 | 0 | throw std::ios_base::failure("Separator is missing at the end of the global map"); | 1512 | 0 | } | 1513 | | | 1514 | 1 | const uint32_t psbt_ver = GetVersion(); | 1515 | | | 1516 | | // Check PSBT version constraints | 1517 | 1 | if (psbt_ver == 0) { | 1518 | | // Make sure that we got an unsigned tx for PSBTv0 | 1519 | 1 | if (!tx) { | 1520 | 0 | throw std::ios_base::failure("No unsigned transaction was provided"); | 1521 | 0 | } | 1522 | | // Make sure no PSBTv2 fields are present | 1523 | 1 | if (found_tx_version) { | 1524 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_TX_VERSION is not allowed in PSBTv0"); | 1525 | 0 | } | 1526 | 1 | if (found_fallback_locktime) { | 1527 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_FALLBACK_LOCKTIME is not allowed in PSBTv0"); | 1528 | 0 | } | 1529 | 1 | if (found_input_count) { | 1530 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_INPUT_COUNT is not allowed in PSBTv0"); | 1531 | 0 | } | 1532 | 1 | if (found_output_count) { | 1533 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_OUTPUT_COUNT is not allowed in PSBTv0"); | 1534 | 0 | } | 1535 | 1 | if (m_tx_modifiable != std::nullopt) { | 1536 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_TX_MODIFIABLE is not allowed in PSBTv0"); | 1537 | 0 | } | 1538 | 1 | } | 1539 | | // Disallow v1 | 1540 | 1 | if (psbt_ver == 1) { | 1541 | 0 | throw std::ios_base::failure("There is no PSBT version 1"); | 1542 | 0 | } | 1543 | 1 | if (psbt_ver == 2) { | 1544 | | // Tx version, input, and output counts are required | 1545 | 0 | if (!found_tx_version) { | 1546 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_TX_VERSION is required in PSBTv2"); | 1547 | 0 | } | 1548 | 0 | if (!found_input_count) { | 1549 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_INPUT_COUNT is required in PSBTv2"); | 1550 | 0 | } | 1551 | 0 | if (!found_output_count) { | 1552 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_OUTPUT_COUNT is required in PSBTv2"); | 1553 | 0 | } | 1554 | | // Unsigned tx is disallowed | 1555 | 0 | if (tx) { | 1556 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_UNSIGNED_TX is not allowed in PSBTv2"); | 1557 | 0 | } | 1558 | 0 | } | 1559 | 1 | if (psbt_ver > 2) { | 1560 | 0 | throw std::ios_base::failure("Unknown PSBT version"); | 1561 | 0 | } | 1562 | | | 1563 | | // Read input data | 1564 | 1 | unsigned int i = 0; | 1565 | 3 | while (!s.empty() && i < input_count) { | 1566 | 2 | if (psbt_ver < 2) { | 1567 | 2 | inputs.emplace_back(psbt_ver, tx->vin[i].prevout.hash, tx->vin[i].prevout.n, tx->vin[i].nSequence); | 1568 | 2 | s >> inputs.back(); | 1569 | 2 | } else { | 1570 | 0 | inputs.emplace_back(deserialize, s, psbt_ver); | 1571 | 0 | } | 1572 | | | 1573 | | // Make sure the non-witness utxo matches the outpoint | 1574 | 2 | const PSBTInput& input = inputs.back(); | 1575 | 2 | if (input.non_witness_utxo) { | 1576 | 0 | if (psbt_ver < 2) { | 1577 | 0 | if (input.non_witness_utxo->GetHash() != tx->vin[i].prevout.hash) { | 1578 | 0 | throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash"); | 1579 | 0 | } | 1580 | 0 | if (tx->vin[i].prevout.n >= input.non_witness_utxo->vout.size()) { | 1581 | 0 | throw std::ios_base::failure("Input specifies output index that does not exist"); | 1582 | 0 | } | 1583 | 0 | } else { | 1584 | 0 | if (input.non_witness_utxo->GetHash() != input.prev_txid) { | 1585 | 0 | throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash"); | 1586 | 0 | } | 1587 | 0 | if (input.prev_out >= input.non_witness_utxo->vout.size()) { | 1588 | 0 | throw std::ios_base::failure("Input specifies output index that does not exist"); | 1589 | 0 | } | 1590 | 0 | } | 1591 | 0 | } | 1592 | 2 | ++i; | 1593 | 2 | } | 1594 | | // Make sure that the number of inputs matches the number of inputs in the transaction | 1595 | 1 | if (inputs.size() != input_count) { | 1596 | 0 | throw std::ios_base::failure("Inputs provided does not match the number of inputs in transaction."); | 1597 | 0 | } | 1598 | | | 1599 | | // Read output data | 1600 | 1 | i = 0; | 1601 | 3 | while (!s.empty() && i < output_count) { | 1602 | 2 | if (psbt_ver < 2) { | 1603 | 2 | outputs.emplace_back(psbt_ver, tx->vout[i].nValue, tx->vout[i].scriptPubKey); | 1604 | 2 | s >> outputs.back(); | 1605 | 2 | } else { | 1606 | 0 | outputs.emplace_back(deserialize, s, psbt_ver); | 1607 | 0 | } | 1608 | 2 | ++i; | 1609 | 2 | } | 1610 | | // Make sure that the number of outputs matches the number of outputs in the transaction | 1611 | 1 | if (outputs.size() != output_count) { | 1612 | 0 | throw std::ios_base::failure("Outputs provided does not match the number of outputs in transaction."); | 1613 | 0 | } | 1614 | 1 | } |
void PartiallySignedTransaction::Unserialize<SpanReader>(SpanReader&) Line | Count | Source | 1350 | 1.83k | inline void Unserialize(Stream& s) { | 1351 | | // Read the magic bytes | 1352 | 1.83k | uint8_t magic[5]; | 1353 | 1.83k | s >> magic; | 1354 | 1.83k | if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) { | 1355 | 2 | throw std::ios_base::failure("Invalid PSBT magic bytes"); | 1356 | 2 | } | 1357 | | | 1358 | | // Used for duplicate key detection | 1359 | 1.83k | std::set<std::vector<unsigned char>> key_lookup; | 1360 | | | 1361 | | // Read global data | 1362 | 1.83k | bool found_sep = false; | 1363 | 1.83k | std::optional<CMutableTransaction> tx; | 1364 | 1.83k | uint64_t input_count = 0; | 1365 | 1.83k | uint64_t output_count = 0; | 1366 | 1.83k | bool found_input_count = false; | 1367 | 1.83k | bool found_output_count = false; | 1368 | 1.83k | bool found_tx_version = false; | 1369 | 1.83k | bool found_fallback_locktime = false; | 1370 | 10.2k | while(!s.empty()) { | 1371 | | // Read the key of format "<keylen><keytype><keydata>" after which | 1372 | | // "key" will contain "<keytype><keydata>" | 1373 | 10.2k | std::vector<unsigned char> key; | 1374 | 10.2k | s >> key; | 1375 | | | 1376 | | // the key is empty if that was actually a separator byte | 1377 | | // This is a special case for key lengths 0 as those are not allowed (except for separator) | 1378 | 10.2k | if (key.empty()) { | 1379 | 1.82k | found_sep = true; | 1380 | 1.82k | break; | 1381 | 1.82k | } | 1382 | | | 1383 | | // Duplicate keys are not permitted | 1384 | 8.37k | if (!key_lookup.emplace(key).second) { | 1385 | 0 | throw std::ios_base::failure(tfm::format("Duplicate Key, global key \"%s\" already provided", HexStr(key))); | 1386 | 0 | } | 1387 | | | 1388 | | // "skey" is used so that "key" is unchanged after reading keytype below | 1389 | 8.37k | SpanReader skey{key}; | 1390 | | // keytype is of the format compact size uint at the beginning of "key" | 1391 | 8.37k | uint64_t type = ReadCompactSize(skey); | 1392 | | | 1393 | | // Do stuff based on keytype "type", i.e., key checks, reading values of the | 1394 | | // format "<valuelen><valuedata>" from the stream "s", and value checks | 1395 | 8.37k | switch(type) { | 1396 | 200 | case PSBT_GLOBAL_UNSIGNED_TX: | 1397 | 200 | { | 1398 | 200 | ExpectedKeySize("Global Unsigned TX", key, 1); | 1399 | | // Set the stream to serialize with non-witness since this should always be non-witness | 1400 | 200 | tx.emplace(); | 1401 | 200 | UnserializeFromVector(s, TX_NO_WITNESS(*tx)); | 1402 | | // Make sure that all scriptSigs and scriptWitnesses are empty | 1403 | 250 | for (const CTxIn& txin : tx->vin) { | 1404 | 250 | if (!txin.scriptSig.empty() || !txin.scriptWitness.IsNull()) { | 1405 | 1 | throw std::ios_base::failure("Unsigned tx does not have empty scriptSigs and scriptWitnesses."); | 1406 | 1 | } | 1407 | 250 | } | 1408 | 199 | tx_version = tx->version; | 1409 | 199 | fallback_locktime = tx->nLockTime; | 1410 | | // Set the input and output counts | 1411 | 199 | input_count = tx->vin.size(); | 1412 | 199 | output_count = tx->vout.size(); | 1413 | 199 | break; | 1414 | 200 | } | 1415 | 1.63k | case PSBT_GLOBAL_TX_VERSION: | 1416 | 1.63k | { | 1417 | 1.63k | ExpectedKeySize("Global Transaction Version", key, 1); | 1418 | 1.63k | UnserializeFromVector(s, tx_version); | 1419 | 1.63k | found_tx_version = true; | 1420 | 1.63k | break; | 1421 | 200 | } | 1422 | 1.61k | case PSBT_GLOBAL_FALLBACK_LOCKTIME: | 1423 | 1.61k | { | 1424 | 1.61k | ExpectedKeySize("Global Fallback Locktime", key, 1); | 1425 | 1.61k | fallback_locktime.emplace(); | 1426 | 1.61k | UnserializeFromVector(s, *fallback_locktime); | 1427 | 1.61k | found_fallback_locktime = true; | 1428 | 1.61k | break; | 1429 | 200 | } | 1430 | 1.63k | case PSBT_GLOBAL_INPUT_COUNT: | 1431 | 1.63k | { | 1432 | 1.63k | ExpectedKeySize("Global Input Count", key, 1); | 1433 | 1.63k | CompactSizeReader reader(input_count); | 1434 | 1.63k | UnserializeFromVector(s, reader); | 1435 | 1.63k | found_input_count = true; | 1436 | 1.63k | break; | 1437 | 200 | } | 1438 | 1.63k | case PSBT_GLOBAL_OUTPUT_COUNT: | 1439 | 1.63k | { | 1440 | 1.63k | ExpectedKeySize("Global Output Count", key, 1); | 1441 | 1.63k | CompactSizeReader reader(output_count); | 1442 | 1.63k | UnserializeFromVector(s, reader); | 1443 | 1.63k | found_output_count = true; | 1444 | 1.63k | break; | 1445 | 200 | } | 1446 | 13 | case PSBT_GLOBAL_TX_MODIFIABLE: | 1447 | 13 | { | 1448 | 13 | ExpectedKeySize("Global TX Modifiable Flags", key, 1); | 1449 | 13 | uint8_t tx_mod; | 1450 | 13 | UnserializeFromVector(s, tx_mod); | 1451 | 13 | m_tx_modifiable.emplace(tx_mod); | 1452 | 13 | break; | 1453 | 200 | } | 1454 | 11 | case PSBT_GLOBAL_XPUB: | 1455 | 11 | { | 1456 | 11 | ExpectedKeySize("Global XPUB", key, BIP32_EXTKEY_WITH_VERSION_SIZE + 1); | 1457 | | // Read in the xpub from key | 1458 | 11 | CExtPubKey xpub; | 1459 | 11 | xpub.DecodeWithVersion(&key.data()[1]); | 1460 | 11 | if (!xpub.pubkey.IsFullyValid()) { | 1461 | 0 | throw std::ios_base::failure("Invalid pubkey"); | 1462 | 0 | } | 1463 | | // Read in the keypath from stream | 1464 | 11 | KeyOriginInfo keypath; | 1465 | 11 | DeserializeHDKeypath(s, keypath); | 1466 | | | 1467 | | // Note that we store these swapped to make searches faster. | 1468 | | // Serialization uses xpub -> keypath to enqure key uniqueness | 1469 | 11 | if (!m_xpubs.contains(keypath)) { | 1470 | | // Make a new set to put the xpub in | 1471 | 11 | m_xpubs[keypath] = {xpub}; | 1472 | 11 | } else { | 1473 | | // Insert xpub into existing set | 1474 | 0 | m_xpubs[keypath].insert(xpub); | 1475 | 0 | } | 1476 | 11 | break; | 1477 | 11 | } | 1478 | 1.63k | case PSBT_GLOBAL_VERSION: | 1479 | 1.63k | { | 1480 | 1.63k | ExpectedKeySize("Global PSBT Version", key, 1); | 1481 | 1.63k | uint32_t v; | 1482 | 1.63k | UnserializeFromVector(s, v); | 1483 | 1.63k | m_version = v; | 1484 | 1.63k | if (*m_version > PSBT_HIGHEST_VERSION) { | 1485 | 0 | throw std::ios_base::failure("Unsupported version number"); | 1486 | 0 | } | 1487 | 1.63k | break; | 1488 | 1.63k | } | 1489 | 1.63k | case PSBT_GLOBAL_PROPRIETARY: | 1490 | 11 | { | 1491 | 11 | PSBTProprietary this_prop; | 1492 | 11 | skey >> this_prop.identifier; | 1493 | 11 | this_prop.subtype = ReadCompactSize(skey); | 1494 | 11 | this_prop.key = key; | 1495 | | | 1496 | 11 | s >> this_prop.value; | 1497 | 11 | m_proprietary.insert(this_prop); | 1498 | 11 | break; | 1499 | 1.63k | } | 1500 | | // Unknown stuff | 1501 | 0 | default: { | 1502 | | // Read in the value | 1503 | 0 | std::vector<unsigned char> val_bytes; | 1504 | 0 | s >> val_bytes; | 1505 | 0 | unknown.emplace(std::move(key), std::move(val_bytes)); | 1506 | 0 | } | 1507 | 8.37k | } | 1508 | 8.37k | } | 1509 | | | 1510 | 1.82k | if (!found_sep) { | 1511 | 0 | throw std::ios_base::failure("Separator is missing at the end of the global map"); | 1512 | 0 | } | 1513 | | | 1514 | 1.82k | const uint32_t psbt_ver = GetVersion(); | 1515 | | | 1516 | | // Check PSBT version constraints | 1517 | 1.82k | if (psbt_ver == 0) { | 1518 | | // Make sure that we got an unsigned tx for PSBTv0 | 1519 | 196 | if (!tx) { | 1520 | 1 | throw std::ios_base::failure("No unsigned transaction was provided"); | 1521 | 1 | } | 1522 | | // Make sure no PSBTv2 fields are present | 1523 | 195 | if (found_tx_version) { | 1524 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_TX_VERSION is not allowed in PSBTv0"); | 1525 | 1 | } | 1526 | 194 | if (found_fallback_locktime) { | 1527 | 0 | throw std::ios_base::failure("PSBT_GLOBAL_FALLBACK_LOCKTIME is not allowed in PSBTv0"); | 1528 | 0 | } | 1529 | 194 | if (found_input_count) { | 1530 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_INPUT_COUNT is not allowed in PSBTv0"); | 1531 | 1 | } | 1532 | 193 | if (found_output_count) { | 1533 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_OUTPUT_COUNT is not allowed in PSBTv0"); | 1534 | 1 | } | 1535 | 192 | if (m_tx_modifiable != std::nullopt) { | 1536 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_TX_MODIFIABLE is not allowed in PSBTv0"); | 1537 | 1 | } | 1538 | 192 | } | 1539 | | // Disallow v1 | 1540 | 1.82k | if (psbt_ver == 1) { | 1541 | 1 | throw std::ios_base::failure("There is no PSBT version 1"); | 1542 | 1 | } | 1543 | 1.82k | if (psbt_ver == 2) { | 1544 | | // Tx version, input, and output counts are required | 1545 | 1.63k | if (!found_tx_version) { | 1546 | 2 | throw std::ios_base::failure("PSBT_GLOBAL_TX_VERSION is required in PSBTv2"); | 1547 | 2 | } | 1548 | 1.63k | if (!found_input_count) { | 1549 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_INPUT_COUNT is required in PSBTv2"); | 1550 | 1 | } | 1551 | 1.62k | if (!found_output_count) { | 1552 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_OUTPUT_COUNT is required in PSBTv2"); | 1553 | 1 | } | 1554 | | // Unsigned tx is disallowed | 1555 | 1.62k | if (tx) { | 1556 | 1 | throw std::ios_base::failure("PSBT_GLOBAL_UNSIGNED_TX is not allowed in PSBTv2"); | 1557 | 1 | } | 1558 | 1.62k | } | 1559 | 1.81k | if (psbt_ver > 2) { | 1560 | 0 | throw std::ios_base::failure("Unknown PSBT version"); | 1561 | 0 | } | 1562 | | | 1563 | | // Read input data | 1564 | 1.81k | unsigned int i = 0; | 1565 | 3.98k | while (!s.empty() && i < input_count) { | 1566 | 2.17k | if (psbt_ver < 2) { | 1567 | 234 | inputs.emplace_back(psbt_ver, tx->vin[i].prevout.hash, tx->vin[i].prevout.n, tx->vin[i].nSequence); | 1568 | 234 | s >> inputs.back(); | 1569 | 1.93k | } else { | 1570 | 1.93k | inputs.emplace_back(deserialize, s, psbt_ver); | 1571 | 1.93k | } | 1572 | | | 1573 | | // Make sure the non-witness utxo matches the outpoint | 1574 | 2.17k | const PSBTInput& input = inputs.back(); | 1575 | 2.17k | if (input.non_witness_utxo) { | 1576 | 462 | if (psbt_ver < 2) { | 1577 | 49 | if (input.non_witness_utxo->GetHash() != tx->vin[i].prevout.hash) { | 1578 | 1 | throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash"); | 1579 | 1 | } | 1580 | 48 | if (tx->vin[i].prevout.n >= input.non_witness_utxo->vout.size()) { | 1581 | 3 | throw std::ios_base::failure("Input specifies output index that does not exist"); | 1582 | 3 | } | 1583 | 413 | } else { | 1584 | 413 | if (input.non_witness_utxo->GetHash() != input.prev_txid) { | 1585 | 0 | throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash"); | 1586 | 0 | } | 1587 | 413 | if (input.prev_out >= input.non_witness_utxo->vout.size()) { | 1588 | 0 | throw std::ios_base::failure("Input specifies output index that does not exist"); | 1589 | 0 | } | 1590 | 413 | } | 1591 | 462 | } | 1592 | 2.16k | ++i; | 1593 | 2.16k | } | 1594 | | // Make sure that the number of inputs matches the number of inputs in the transaction | 1595 | 1.81k | if (inputs.size() != input_count) { | 1596 | 0 | throw std::ios_base::failure("Inputs provided does not match the number of inputs in transaction."); | 1597 | 0 | } | 1598 | | | 1599 | | // Read output data | 1600 | 1.81k | i = 0; | 1601 | 5.02k | while (!s.empty() && i < output_count) { | 1602 | 3.21k | if (psbt_ver < 2) { | 1603 | 184 | outputs.emplace_back(psbt_ver, tx->vout[i].nValue, tx->vout[i].scriptPubKey); | 1604 | 184 | s >> outputs.back(); | 1605 | 3.02k | } else { | 1606 | 3.02k | outputs.emplace_back(deserialize, s, psbt_ver); | 1607 | 3.02k | } | 1608 | 3.21k | ++i; | 1609 | 3.21k | } | 1610 | | // Make sure that the number of outputs matches the number of outputs in the transaction | 1611 | 1.81k | if (outputs.size() != output_count) { | 1612 | 1 | throw std::ios_base::failure("Outputs provided does not match the number of outputs in transaction."); | 1613 | 1 | } | 1614 | 1.81k | } |
|
1615 | | |
1616 | | template <typename Stream> |
1617 | 1.83k | PartiallySignedTransaction(deserialize_type, Stream& s) { |
1618 | 1.83k | Unserialize(s); |
1619 | 1.83k | } PartiallySignedTransaction::PartiallySignedTransaction<DataStream>(deserialize_type, DataStream&) Line | Count | Source | 1617 | 1 | PartiallySignedTransaction(deserialize_type, Stream& s) { | 1618 | 1 | Unserialize(s); | 1619 | 1 | } |
PartiallySignedTransaction::PartiallySignedTransaction<SpanReader>(deserialize_type, SpanReader&) Line | Count | Source | 1617 | 1.83k | PartiallySignedTransaction(deserialize_type, Stream& s) { | 1618 | 1.83k | Unserialize(s); | 1619 | 1.83k | } |
|
1620 | | }; |
1621 | | |
1622 | | enum class PSBTRole { |
1623 | | CREATOR, |
1624 | | UPDATER, |
1625 | | SIGNER, |
1626 | | FINALIZER, |
1627 | | EXTRACTOR |
1628 | | }; |
1629 | | |
1630 | | std::string PSBTRoleName(PSBTRole role); |
1631 | | |
1632 | | /** Compute a PrecomputedTransactionData object from a psbt. */ |
1633 | | std::optional<PrecomputedTransactionData> PrecomputePSBTData(const PartiallySignedTransaction& psbt); |
1634 | | |
1635 | | /** Checks whether a PSBTInput is already signed by checking for non-null finalized fields. */ |
1636 | | bool PSBTInputSigned(const PSBTInput& input); |
1637 | | |
1638 | | /** Checks whether a PSBTInput is already signed by doing script verification using final fields. */ |
1639 | | bool PSBTInputSignedAndVerified(const PartiallySignedTransaction& psbt, unsigned int input_index, const PrecomputedTransactionData* txdata); |
1640 | | |
1641 | | /** Signs a PSBTInput, verifying that all provided data matches what is being signed. |
1642 | | * |
1643 | | * txdata should be the output of PrecomputePSBTData (which can be shared across |
1644 | | * multiple SignPSBTInput calls). If it is nullptr, a dummy signature will be created. |
1645 | | **/ |
1646 | | [[nodiscard]] util::Expected<void, PSBTError> SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, const common::PSBTFillOptions& options, SignatureData* out_sigdata = nullptr); |
1647 | | |
1648 | | /** Reduces the size of the PSBT by dropping unnecessary `non_witness_utxos` (i.e. complete previous transactions) from a psbt when all inputs are segwit v1. */ |
1649 | | void RemoveUnnecessaryTransactions(PartiallySignedTransaction& psbtx); |
1650 | | |
1651 | | /** Counts the unsigned inputs of a PSBT. */ |
1652 | | size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt); |
1653 | | |
1654 | | /** Updates a PSBTOutput with information from provider. |
1655 | | * |
1656 | | * This fills in the redeem_script, witness_script, and hd_keypaths where possible. |
1657 | | */ |
1658 | | void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index); |
1659 | | |
1660 | | /** |
1661 | | * Finalizes a PSBT if possible, combining partial signatures. |
1662 | | * |
1663 | | * @param[in,out] psbtx PartiallySignedTransaction to finalize |
1664 | | * return True if the PSBT is now complete, false otherwise |
1665 | | */ |
1666 | | bool FinalizePSBT(PartiallySignedTransaction& psbtx); |
1667 | | |
1668 | | /** |
1669 | | * Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized. |
1670 | | * |
1671 | | * @param[in] psbtx PartiallySignedTransaction |
1672 | | * @param[out] result CMutableTransaction representing the complete transaction, if successful |
1673 | | * @return True if we successfully extracted the transaction, false otherwise |
1674 | | */ |
1675 | | bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result); |
1676 | | |
1677 | | /** |
1678 | | * Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial signatures from each input. |
1679 | | * |
1680 | | * @param[in] psbtxs the PSBTs to combine |
1681 | | * @return The combined PSBT or std::nullopt if the PSBTs cannot be combined |
1682 | | */ |
1683 | | [[nodiscard]] std::optional<PartiallySignedTransaction> CombinePSBTs(const std::vector<PartiallySignedTransaction>& psbtxs); |
1684 | | |
1685 | | //! Decode a base64ed PSBT into a PartiallySignedTransaction |
1686 | | [[nodiscard]] util::Result<PartiallySignedTransaction> DecodeBase64PSBT(const std::string& base64_tx); |
1687 | | //! Decode a raw (binary blob) PSBT into a PartiallySignedTransaction |
1688 | | [[nodiscard]] util::Result<PartiallySignedTransaction> DecodeRawPSBT(std::span<const std::byte> tx_data); |
1689 | | |
1690 | | #endif // BITCOIN_PSBT_H |