Changeset View
Changeset View
Standalone View
Standalone View
src/psbt.h
Show First 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | template <typename Stream> inline void Serialize(Stream &s) const { | ||||
s << entry.second; | s << entry.second; | ||||
} | } | ||||
s << PSBT_SEPARATOR; | s << PSBT_SEPARATOR; | ||||
} | } | ||||
template <typename Stream> inline void Unserialize(Stream &s) { | template <typename Stream> inline void Unserialize(Stream &s) { | ||||
// Read loop | // Read loop | ||||
bool found_sep = false; | |||||
while (!s.empty()) { | while (!s.empty()) { | ||||
// Read | // Read | ||||
std::vector<uint8_t> key; | std::vector<uint8_t> key; | ||||
s >> key; | s >> key; | ||||
// the key is empty if that was actually a separator byte | // the key is empty if that was actually a separator byte | ||||
// This is a special case for key lengths 0 as those are not allowed | // This is a special case for key lengths 0 as those are not allowed | ||||
// (except for separator) | // (except for separator) | ||||
if (key.empty()) { | if (key.empty()) { | ||||
return; | found_sep = true; | ||||
break; | |||||
} | } | ||||
// First byte of key is the type | // First byte of key is the type | ||||
uint8_t type = key[0]; | uint8_t type = key[0]; | ||||
// Do stuff based on type | // Do stuff based on type | ||||
switch (type) { | switch (type) { | ||||
case PSBT_IN_UTXO: | case PSBT_IN_UTXO: | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | template <typename Stream> inline void Unserialize(Stream &s) { | ||||
} | } | ||||
// Read in the value | // Read in the value | ||||
std::vector<uint8_t> val_bytes; | std::vector<uint8_t> val_bytes; | ||||
s >> val_bytes; | s >> val_bytes; | ||||
unknown.emplace(std::move(key), std::move(val_bytes)); | unknown.emplace(std::move(key), std::move(val_bytes)); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
if (!found_sep) { | |||||
throw std::ios_base::failure( | |||||
"Separator is missing at the end of an input map"); | |||||
} | |||||
} | } | ||||
template <typename Stream> PSBTInput(deserialize_type, Stream &s) { | template <typename Stream> PSBTInput(deserialize_type, Stream &s) { | ||||
Unserialize(s); | Unserialize(s); | ||||
} | } | ||||
}; | }; | ||||
/** A structure for PSBTs which contains per output information */ | /** A structure for PSBTs which contains per output information */ | ||||
Show All 25 Lines | template <typename Stream> inline void Serialize(Stream &s) const { | ||||
s << entry.second; | s << entry.second; | ||||
} | } | ||||
s << PSBT_SEPARATOR; | s << PSBT_SEPARATOR; | ||||
} | } | ||||
template <typename Stream> inline void Unserialize(Stream &s) { | template <typename Stream> inline void Unserialize(Stream &s) { | ||||
// Read loop | // Read loop | ||||
bool found_sep = false; | |||||
while (!s.empty()) { | while (!s.empty()) { | ||||
// Read | // Read | ||||
std::vector<uint8_t> key; | std::vector<uint8_t> key; | ||||
s >> key; | s >> key; | ||||
// the key is empty if that was actually a separator byte | // the key is empty if that was actually a separator byte | ||||
// This is a special case for key lengths 0 as those are not allowed | // This is a special case for key lengths 0 as those are not allowed | ||||
// (except for separator) | // (except for separator) | ||||
if (key.empty()) { | if (key.empty()) { | ||||
return; | found_sep = true; | ||||
break; | |||||
} | } | ||||
// First byte of key is the type | // First byte of key is the type | ||||
uint8_t type = key[0]; | uint8_t type = key[0]; | ||||
// Do stuff based on type | // Do stuff based on type | ||||
switch (type) { | switch (type) { | ||||
case PSBT_OUT_REDEEMSCRIPT: { | case PSBT_OUT_REDEEMSCRIPT: { | ||||
Show All 23 Lines | template <typename Stream> inline void Unserialize(Stream &s) { | ||||
// Read in the value | // Read in the value | ||||
std::vector<uint8_t> val_bytes; | std::vector<uint8_t> val_bytes; | ||||
s >> val_bytes; | s >> val_bytes; | ||||
unknown.emplace(std::move(key), std::move(val_bytes)); | unknown.emplace(std::move(key), std::move(val_bytes)); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (!found_sep) { | |||||
throw std::ios_base::failure( | |||||
"Separator is missing at the end of an output map"); | |||||
} | |||||
} | } | ||||
template <typename Stream> PSBTOutput(deserialize_type, Stream &s) { | template <typename Stream> PSBTOutput(deserialize_type, Stream &s) { | ||||
Unserialize(s); | Unserialize(s); | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | template <typename Stream> inline void Unserialize(Stream &s) { | ||||
// Read the magic bytes | // Read the magic bytes | ||||
uint8_t magic[5]; | uint8_t magic[5]; | ||||
s >> magic; | s >> magic; | ||||
if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) { | if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) { | ||||
throw std::ios_base::failure("Invalid PSBT magic bytes"); | throw std::ios_base::failure("Invalid PSBT magic bytes"); | ||||
} | } | ||||
// Read global data | // Read global data | ||||
bool found_sep = false; | |||||
while (!s.empty()) { | while (!s.empty()) { | ||||
// Read | // Read | ||||
std::vector<uint8_t> key; | std::vector<uint8_t> key; | ||||
s >> key; | s >> key; | ||||
// the key is empty if that was actually a separator byte | // the key is empty if that was actually a separator byte | ||||
// This is a special case for key lengths 0 as those are not allowed | // This is a special case for key lengths 0 as those are not allowed | ||||
// (except for separator) | // (except for separator) | ||||
if (key.empty()) { | if (key.empty()) { | ||||
found_sep = true; | |||||
break; | break; | ||||
} | } | ||||
// First byte of key is the type | // First byte of key is the type | ||||
uint8_t type = key[0]; | uint8_t type = key[0]; | ||||
// Do stuff based on type | // Do stuff based on type | ||||
switch (type) { | switch (type) { | ||||
Show All 28 Lines | template <typename Stream> inline void Unserialize(Stream &s) { | ||||
// Read in the value | // Read in the value | ||||
std::vector<uint8_t> val_bytes; | std::vector<uint8_t> val_bytes; | ||||
s >> val_bytes; | s >> val_bytes; | ||||
unknown.emplace(std::move(key), std::move(val_bytes)); | unknown.emplace(std::move(key), std::move(val_bytes)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (!found_sep) { | |||||
throw std::ios_base::failure( | |||||
"Separator is missing at the end of the global map"); | |||||
} | |||||
// Make sure that we got an unsigned tx | // Make sure that we got an unsigned tx | ||||
if (!tx) { | if (!tx) { | ||||
throw std::ios_base::failure( | throw std::ios_base::failure( | ||||
"No unsigned transcation was provided"); | "No unsigned transcation was provided"); | ||||
} | } | ||||
// Read input data | // Read input data | ||||
size_t i = 0; | size_t i = 0; | ||||
▲ Show 20 Lines • Show All 112 Lines • Show Last 20 Lines |