diff --git a/src/script/sign.h b/src/script/sign.h --- a/src/script/sign.h +++ b/src/script/sign.h @@ -158,27 +158,25 @@ // 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. The resulting output into the stream has the total -// serialized length of all of the objects followed by all objects concatenated -// with each other. +// 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 void SerializeToVector(Stream &s, const X &... args) { - std::vector 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 +// then each object individually in the order provided in the arguments. template void UnserializeFromVector(Stream &s, X &... args) { - std::vector 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"); } } diff --git a/src/serialize.h b/src/serialize.h --- a/src/serialize.h +++ b/src/serialize.h @@ -993,4 +993,11 @@ return (CSizeComputer(s.GetVersion()) << t).size(); } +template +size_t GetSerializeSizeMany(const S &s, const T &... t) { + CSizeComputer sc(s.GetVersion()); + SerializeMany(sc, t...); + return sc.size(); +} + #endif // BITCOIN_SERIALIZE_H