MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / Serialize

Method Serialize

src/psbt.h:1271–1345  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1269
1270 template <typename Stream>
1271 inline void Serialize(Stream& s) const {
1272
1273 // magic bytes
1274 s << PSBT_MAGIC_BYTES;
1275
1276 if (GetVersion() < 2) {
1277 // unsigned tx flag
1278 SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_UNSIGNED_TX));
1279
1280 // Write serialized tx to a stream
1281 SerializeToVector(s, TX_NO_WITNESS(*GetUnsignedTx()));
1282 }
1283
1284 // Write xpubs
1285 for (const auto& xpub_pair : m_xpubs) {
1286 for (const auto& xpub : xpub_pair.second) {
1287 unsigned char ser_xpub[BIP32_EXTKEY_WITH_VERSION_SIZE];
1288 xpub.EncodeWithVersion(ser_xpub);
1289 // Note that the serialization swaps the key and value
1290 // The xpub is the key (for uniqueness) while the path is the value
1291 SerializeToVector(s, PSBT_GLOBAL_XPUB, ser_xpub);
1292 SerializeHDKeypath(s, xpub_pair.first);
1293 }
1294 }
1295
1296 if (GetVersion() >= 2) {
1297 // Write PSBTv2 tx version, locktime, counts, etc.
1298 SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_TX_VERSION));
1299 SerializeToVector(s, tx_version);
1300 if (fallback_locktime != std::nullopt) {
1301 SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_FALLBACK_LOCKTIME));
1302 SerializeToVector(s, *fallback_locktime);
1303 }
1304
1305 SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_INPUT_COUNT));
1306 SerializeToVector(s, CompactSizeWriter(inputs.size()));
1307 SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_OUTPUT_COUNT));
1308 SerializeToVector(s, CompactSizeWriter(outputs.size()));
1309
1310 if (m_tx_modifiable != std::nullopt) {
1311 SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_TX_MODIFIABLE));
1312 SerializeToVector(s, static_cast<uint8_t>(m_tx_modifiable->to_ulong()));
1313 }
1314 }
1315
1316 // PSBT version
1317 if (GetVersion() > 0) {
1318 SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_VERSION));
1319 SerializeToVector(s, *m_version);
1320 }
1321
1322 // Write proprietary things
1323 for (const auto& entry : m_proprietary) {
1324 s << entry.key;
1325 s << entry.value;
1326 }
1327
1328 // Write the unknown things

Callers

nothing calls this directly

Calls 5

SerializeToVectorFunction · 0.85
CompactSizeWriterClass · 0.85
SerializeHDKeypathFunction · 0.85
EncodeWithVersionMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected