diff options
Diffstat (limited to 'src/script/sign.h')
-rw-r--r-- | src/script/sign.h | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/src/script/sign.h b/src/script/sign.h index e3a6196b28..d12d0b5874 100644 --- a/src/script/sign.h +++ b/src/script/sign.h @@ -116,26 +116,24 @@ static constexpr uint8_t PSBT_OUT_BIP32_DERIVATION = 0x02; // as a 0 length key which indicates that this is the separator. The separator has no value. static constexpr uint8_t PSBT_SEPARATOR = 0x00; -// Takes a stream and multiple arguments and serializes them into a vector and then into the stream +// Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream // The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other. template<typename Stream, typename... X> void SerializeToVector(Stream& s, const X&... args) { - std::vector<unsigned char> ret; - CVectorWriter ss(SER_NETWORK, PROTOCOL_VERSION, ret, 0); - SerializeMany(ss, args...); - s << ret; + WriteCompactSize(s, GetSerializeSizeMany(s, args...)); + SerializeMany(s, args...); } // Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments template<typename Stream, typename... X> void UnserializeFromVector(Stream& s, X&... args) { - std::vector<unsigned char> data; - s >> data; - CDataStream ss(data, SER_NETWORK, PROTOCOL_VERSION); - UnserializeMany(ss, args...); - if (!ss.eof()) { + size_t expected_size = ReadCompactSize(s); + size_t remaining_before = s.size(); + UnserializeMany(s, args...); + size_t remaining_after = s.size(); + if (remaining_after + expected_size != remaining_before) { throw std::ios_base::failure("Size of value was not the stated size"); } } @@ -286,12 +284,16 @@ struct PSBTInput case PSBT_IN_NON_WITNESS_UTXO: if (non_witness_utxo) { throw std::ios_base::failure("Duplicate Key, input non-witness utxo already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Non-witness utxo key is more than one byte type"); } UnserializeFromVector(s, non_witness_utxo); break; case PSBT_IN_WITNESS_UTXO: if (!witness_utxo.IsNull()) { throw std::ios_base::failure("Duplicate Key, input witness utxo already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Witness utxo key is more than one byte type"); } UnserializeFromVector(s, witness_utxo); break; @@ -321,6 +323,8 @@ struct PSBTInput case PSBT_IN_SIGHASH: if (sighash_type > 0) { throw std::ios_base::failure("Duplicate Key, input sighash type already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Sighash type key is more than one byte type"); } UnserializeFromVector(s, sighash_type); break; @@ -328,6 +332,8 @@ struct PSBTInput { if (!redeem_script.empty()) { throw std::ios_base::failure("Duplicate Key, input redeemScript already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Input redeemScript key is more than one byte type"); } s >> redeem_script; break; @@ -336,6 +342,8 @@ struct PSBTInput { if (!witness_script.empty()) { throw std::ios_base::failure("Duplicate Key, input witnessScript already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Input witnessScript key is more than one byte type"); } s >> witness_script; break; @@ -349,6 +357,8 @@ struct PSBTInput { if (!final_script_sig.empty()) { throw std::ios_base::failure("Duplicate Key, input final scriptSig already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Final scriptSig key is more than one byte type"); } s >> final_script_sig; break; @@ -357,6 +367,8 @@ struct PSBTInput { if (!final_script_witness.IsNull()) { throw std::ios_base::failure("Duplicate Key, input final scriptWitness already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Final scriptWitness key is more than one byte type"); } UnserializeFromVector(s, final_script_witness.stack); break; @@ -444,6 +456,8 @@ struct PSBTOutput { if (!redeem_script.empty()) { throw std::ios_base::failure("Duplicate Key, output redeemScript already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Output redeemScript key is more than one byte type"); } s >> redeem_script; break; @@ -452,6 +466,8 @@ struct PSBTOutput { if (!witness_script.empty()) { throw std::ios_base::failure("Duplicate Key, output witnessScript already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Output witnessScript key is more than one byte type"); } s >> witness_script; break; @@ -566,6 +582,8 @@ struct PartiallySignedTransaction { if (tx) { throw std::ios_base::failure("Duplicate Key, unsigned tx already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Global unsigned tx key is more than one byte type"); } CMutableTransaction mtx; UnserializeFromVector(s, mtx); |