 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
68 std::vector<unsigned char>
key;
69 std::vector<unsigned char>
value;
81 template<
typename Stream,
typename...
X>
89 template<
typename Stream,
typename...
X>
93 size_t remaining_before = s.size();
95 size_t remaining_after = s.size();
96 if (remaining_after + expected_size != remaining_before) {
97 throw std::ios_base::failure(
"Size of value was not the stated size");
102 template<
typename Stream>
107 if (value_len % 4 || value_len == 0) {
108 throw std::ios_base::failure(
"Invalid length for HD key path");
112 for (
unsigned int i = 4; i < value_len; i +=
sizeof(uint32_t)) {
115 hd_keypath.
path.push_back(index);
120 template<
typename Stream>
121 void DeserializeHDKeypaths(Stream& s,
const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
125 throw std::ios_base::failure(
"Size of key was not the expected size for the type BIP32 keypath");
128 CPubKey pubkey(key.begin() + 1, key.end());
130 throw std::ios_base::failure(
"Invalid pubkey");
132 if (hd_keypaths.count(pubkey) > 0) {
133 throw std::ios_base::failure(
"Duplicate Key, pubkey derivation path already provided");
140 hd_keypaths.emplace(pubkey, std::move(keypath));
144 template<
typename Stream>
149 for (
const auto& path : hd_keypath.
path) {
155 template<
typename Stream>
158 for (
auto keypath_pair : hd_keypaths) {
159 if (!keypath_pair.first.IsValid()) {
160 throw std::ios_base::failure(
"Invalid CPubKey being serialized");
182 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
192 template <
typename Stream>
209 s << sig_pair.second.second;
285 template <
typename Stream>
288 std::set<std::vector<unsigned char>> key_lookup;
291 bool found_sep =
false;
294 std::vector<unsigned char> key;
305 SpanReader skey(s.GetType(), s.GetVersion(), key);
312 if (!key_lookup.emplace(key).second) {
313 throw std::ios_base::failure(
"Duplicate Key, input non-witness utxo already provided");
314 }
else if (key.size() != 1) {
315 throw std::ios_base::failure(
"Non-witness utxo key is more than one byte type");
323 if (!key_lookup.emplace(key).second) {
324 throw std::ios_base::failure(
"Duplicate Key, input witness utxo already provided");
325 }
else if (key.size() != 1) {
326 throw std::ios_base::failure(
"Witness utxo key is more than one byte type");
334 throw std::ios_base::failure(
"Size of key was not the expected size for the type partial signature pubkey");
337 CPubKey pubkey(key.begin() + 1, key.end());
339 throw std::ios_base::failure(
"Invalid pubkey");
342 throw std::ios_base::failure(
"Duplicate Key, input partial signature for pubkey already provided");
346 std::vector<unsigned char> sig;
354 if (!key_lookup.emplace(key).second) {
355 throw std::ios_base::failure(
"Duplicate Key, input sighash type already provided");
356 }
else if (key.size() != 1) {
357 throw std::ios_base::failure(
"Sighash type key is more than one byte type");
365 if (!key_lookup.emplace(key).second) {
366 throw std::ios_base::failure(
"Duplicate Key, input redeemScript already provided");
367 }
else if (key.size() != 1) {
368 throw std::ios_base::failure(
"Input redeemScript key is more than one byte type");
375 if (!key_lookup.emplace(key).second) {
376 throw std::ios_base::failure(
"Duplicate Key, input witnessScript already provided");
377 }
else if (key.size() != 1) {
378 throw std::ios_base::failure(
"Input witnessScript key is more than one byte type");
390 if (!key_lookup.emplace(key).second) {
391 throw std::ios_base::failure(
"Duplicate Key, input final scriptSig already provided");
392 }
else if (key.size() != 1) {
393 throw std::ios_base::failure(
"Final scriptSig key is more than one byte type");
400 if (!key_lookup.emplace(key).second) {
401 throw std::ios_base::failure(
"Duplicate Key, input final scriptWitness already provided");
402 }
else if (key.size() != 1) {
403 throw std::ios_base::failure(
"Final scriptWitness key is more than one byte type");
412 throw std::ios_base::failure(
"Size of key was not the expected size for the type ripemd160 preimage");
415 std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
418 throw std::ios_base::failure(
"Duplicate Key, input ripemd160 preimage already provided");
422 std::vector<unsigned char> preimage;
433 throw std::ios_base::failure(
"Size of key was not the expected size for the type sha256 preimage");
436 std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
439 throw std::ios_base::failure(
"Duplicate Key, input sha256 preimage already provided");
443 std::vector<unsigned char> preimage;
454 throw std::ios_base::failure(
"Size of key was not the expected size for the type hash160 preimage");
457 std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
460 throw std::ios_base::failure(
"Duplicate Key, input hash160 preimage already provided");
464 std::vector<unsigned char> preimage;
475 throw std::ios_base::failure(
"Size of key was not the expected size for the type hash256 preimage");
478 std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
481 throw std::ios_base::failure(
"Duplicate Key, input hash256 preimage already provided");
485 std::vector<unsigned char> preimage;
500 throw std::ios_base::failure(
"Duplicate Key, proprietary key already found");
502 s >> this_prop.
value;
509 throw std::ios_base::failure(
"Duplicate Key, key for unknown value already provided");
512 std::vector<unsigned char> val_bytes;
514 unknown.emplace(std::move(key), std::move(val_bytes));
520 throw std::ios_base::failure(
"Separator is missing at the end of an input map");
524 template <
typename Stream>
536 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
545 template <
typename Stream>
578 template <
typename Stream>
581 std::set<std::vector<unsigned char>> key_lookup;
584 bool found_sep =
false;
587 std::vector<unsigned char> key;
598 SpanReader skey(s.GetType(), s.GetVersion(), key);
605 if (!key_lookup.emplace(key).second) {
606 throw std::ios_base::failure(
"Duplicate Key, output redeemScript already provided");
607 }
else if (key.size() != 1) {
608 throw std::ios_base::failure(
"Output redeemScript key is more than one byte type");
615 if (!key_lookup.emplace(key).second) {
616 throw std::ios_base::failure(
"Duplicate Key, output witnessScript already provided");
617 }
else if (key.size() != 1) {
618 throw std::ios_base::failure(
"Output witnessScript key is more than one byte type");
636 throw std::ios_base::failure(
"Duplicate Key, proprietary key already found");
638 s >> this_prop.
value;
645 throw std::ios_base::failure(
"Duplicate Key, key for unknown value already provided");
648 std::vector<unsigned char> val_bytes;
650 unknown.emplace(std::move(key), std::move(val_bytes));
657 throw std::ios_base::failure(
"Separator is missing at the end of an output map");
661 template <
typename Stream>
670 std::optional<CMutableTransaction>
tx;
673 std::map<KeyOriginInfo, std::set<CExtPubKey>>
m_xpubs;
676 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
699 template <
typename Stream>
713 for (
const auto& xpub_pair :
m_xpubs) {
714 for (
const auto& xpub : xpub_pair.second) {
716 xpub.EncodeWithVersion(ser_xpub);
756 template <
typename Stream>
762 throw std::ios_base::failure(
"Invalid PSBT magic bytes");
766 std::set<std::vector<unsigned char>> key_lookup;
769 std::set<CExtPubKey> global_xpubs;
772 bool found_sep =
false;
775 std::vector<unsigned char> key;
786 SpanReader skey(s.GetType(), s.GetVersion(), key);
793 if (!key_lookup.emplace(key).second) {
794 throw std::ios_base::failure(
"Duplicate Key, unsigned tx already provided");
795 }
else if (key.size() != 1) {
796 throw std::ios_base::failure(
"Global unsigned tx key is more than one byte type");
804 for (
const CTxIn& txin :
tx->vin) {
806 throw std::ios_base::failure(
"Unsigned tx does not have empty scriptSigs and scriptWitnesses.");
814 throw std::ios_base::failure(
"Size of key was not the expected size for the type global xpub");
820 throw std::ios_base::failure(
"Invalid pubkey");
822 if (global_xpubs.count(xpub) > 0) {
823 throw std::ios_base::failure(
"Duplicate key, global xpub already provided");
825 global_xpubs.insert(xpub);
832 if (
m_xpubs.count(keypath) == 0) {
844 throw std::ios_base::failure(
"Duplicate Key, version already provided");
845 }
else if (key.size() != 1) {
846 throw std::ios_base::failure(
"Global version key is more than one byte type");
852 throw std::ios_base::failure(
"Unsupported version number");
864 throw std::ios_base::failure(
"Duplicate Key, proprietary key already found");
866 s >> this_prop.
value;
873 throw std::ios_base::failure(
"Duplicate Key, key for unknown value already provided");
876 std::vector<unsigned char> val_bytes;
878 unknown.emplace(std::move(key), std::move(val_bytes));
884 throw std::ios_base::failure(
"Separator is missing at the end of the global map");
889 throw std::ios_base::failure(
"No unsigned transcation was provided");
894 while (!s.empty() && i < tx->vin.size()) {
900 if (input.non_witness_utxo && input.non_witness_utxo->GetHash() !=
tx->vin[i].prevout.hash) {
901 throw std::ios_base::failure(
"Non-witness UTXO does not match outpoint hash");
906 if (
inputs.size() !=
tx->vin.size()) {
907 throw std::ios_base::failure(
"Inputs provided does not match the number of inputs in transaction.");
912 while (!s.empty() && i < tx->vout.size()) {
920 throw std::ios_base::failure(
"Outputs provided does not match the number of outputs in transaction.");
924 template <
typename Stream>
993 #endif // BITCOIN_PSBT_H
An input of a transaction.
Dummy data type to identify deserializing constructors.
uint32_t GetVersion() const
static constexpr uint8_t PSBT_IN_SCRIPTWITNESS
bool DecodeRawPSBT(PartiallySignedTransaction &decoded_psbt, const std::string &raw_psbt, std::string &error)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
static constexpr uint8_t PSBT_IN_PROPRIETARY
std::vector< unsigned char > key
void FillSignatureData(SignatureData &sigdata) const
TransactionError CombinePSBTs(PartiallySignedTransaction &out, const std::vector< PartiallySignedTransaction > &psbtxs)
Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial sign...
static constexpr unsigned int SIZE
secp256k1:
static constexpr uint8_t PSBT_IN_REDEEMSCRIPT
std::vector< PSBTInput > inputs
size_t GetSerializeSizeMany(int nVersion, const T &... t)
bool AddInput(const CTxIn &txin, PSBTInput &psbtin)
static constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX
An interface to be implemented by keystores that support signing.
static constexpr uint8_t PSBT_IN_HASH256
static const size_t OUTPUT_SIZE
void Unserialize(Stream &s)
static constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO
static constexpr uint8_t PSBT_IN_HASH160
void DeserializeHDKeypaths(Stream &s, const std::vector< unsigned char > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed.
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
std::optional< uint32_t > m_version
std::shared_ptr< const CTransaction > CTransactionRef
PSBTOutput(deserialize_type, Stream &s)
static constexpr uint8_t PSBT_IN_SIGHASH
void UpdatePSBTOutput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index)
Updates a PSBTOutput with information from provider.
void DeserializeHDKeypath(Stream &s, KeyOriginInfo &hd_keypath)
bool DecodeBase64PSBT(PartiallySignedTransaction &decoded_psbt, const std::string &base64_psbt, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
CScriptWitness scriptWitness
Only serialized through CTransaction.
A Span is an object that can refer to a contiguous sequence of objects.
std::pair< CPubKey, std::vector< unsigned char > > SigPair
static constexpr uint8_t PSBT_MAGIC_BYTES[5]
bool SignPSBTInput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index, const PrecomputedTransactionData *txdata, int sighash=SIGHASH_ALL, SignatureData *out_sigdata=nullptr, bool finalize=true)
Signs a PSBTInput, verifying that all provided data matches what is being signed.
PartiallySignedTransaction()
std::set< PSBTProprietary > m_proprietary
static constexpr uint32_t PSBT_HIGHEST_VERSION
bool Merge(const PartiallySignedTransaction &psbt)
Merge psbt into this.
const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE
An output of a transaction.
void Unserialize(Stream &s)
static const size_t OUTPUT_SIZE
static constexpr uint8_t PSBT_IN_PARTIAL_SIG
bool GetInputUTXO(CTxOut &utxo, int input_index) const
Finds the UTXO for a given input index.
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
std::string PSBTRoleName(PSBTRole role)
static constexpr uint8_t PSBT_IN_WITNESS_UTXO
A structure for PSBT proprietary types.
std::optional< CMutableTransaction > tx
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
Minimal stream for reading from an existing byte array by Span.
std::vector< unsigned char > identifier
A structure for PSBTs which contains per output information.
std::map< KeyOriginInfo, std::set< CExtPubKey > > m_xpubs
void UnserializeMany(Stream &s)
void SerializeMany(Stream &s)
static constexpr unsigned int COMPRESSED_SIZE
static constexpr uint8_t PSBT_OUT_BIP32_DERIVATION
static constexpr uint8_t PSBT_GLOBAL_VERSION
Serialized script, used inside transaction inputs and outputs.
bool AddOutput(const CTxOut &txout, const PSBTOutput &psbtout)
static constexpr uint8_t PSBT_IN_BIP32_DERIVATION
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction &psbt)
Counts the unsigned inputs of a PSBT.
bool FinalizeAndExtractPSBT(PartiallySignedTransaction &psbtx, CMutableTransaction &result)
Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized.
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, CompactSizeWriter type)
static const size_t OUTPUT_SIZE
static constexpr uint8_t PSBT_IN_SHA256
static constexpr uint8_t PSBT_IN_SCRIPTSIG
void Merge(const PSBTOutput &output)
void SerializeHDKeypath(Stream &s, KeyOriginInfo hd_keypath)
An encapsulated public key.
void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE])
static constexpr uint8_t PSBT_OUT_WITNESSSCRIPT
static constexpr uint8_t PSBT_GLOBAL_PROPRIETARY
A version of CTransaction with the PSBT format.
void Serialize(Stream &s) const
static constexpr uint8_t PSBT_OUT_PROPRIETARY
static constexpr uint8_t PSBT_OUT_REDEEMSCRIPT
std::vector< PSBTOutput > outputs
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
std::set< PSBTProprietary > m_proprietary
PartiallySignedTransaction(deserialize_type, Stream &s)
const std::streamsize MAX_FILE_SIZE_PSBT
std::vector< uint32_t > path
bool operator==(const PSBTProprietary &b) const
bool operator<(const PSBTProprietary &b) const
static constexpr uint8_t PSBT_SEPARATOR
std::vector< unsigned char > value
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
static constexpr uint8_t PSBT_GLOBAL_XPUB
bool error(const char *fmt, const Args &... args)
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction &psbt)
Compute a PrecomputedTransactionData object from a psbt.
A mutable version of CTransaction.
void FromSignatureData(const SignatureData &sigdata)
unsigned char fingerprint[4]
First 32 bits of the Hash160 of the public key at the root of the path.
static const size_t OUTPUT_SIZE
std::map< CPubKey, KeyOriginInfo > hd_keypaths
void UnserializeFromVector(Stream &s, X &... args)
void Serialize(Stream &s) const
std::vector< std::vector< unsigned char > > stack
static constexpr uint8_t PSBT_IN_WITNESSSCRIPT
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
void SerializeToVector(Stream &s, const X &... args)
static constexpr uint8_t PSBT_IN_RIPEMD160