104 std::vector<unsigned char>
key;
117template<
typename Stream,
typename...
X>
127template<
typename Stream,
typename...
X>
135 throw std::ios_base::failure(
"Size of value was not the stated size");
140template<
typename Stream>
144 if (length % 4 || length == 0) {
145 throw std::ios_base::failure(
"Invalid length for HD key path");
150 for (
unsigned int i = 4; i < length; i +=
sizeof(
uint32_t)) {
159template<
typename Stream>
166template<
typename Stream>
167void DeserializeHDKeypaths(Stream& s,
const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
171 throw std::ios_base::failure(
"Size of key was not the expected size for the type BIP32 keypath");
174 CPubKey pubkey(key.begin() + 1, key.end());
176 throw std::ios_base::failure(
"Invalid pubkey");
183 hd_keypaths.emplace(pubkey, std::move(
keypath));
187template<
typename Stream>
197template<
typename Stream>
205template<
typename Stream>
210 throw std::ios_base::failure(
"Invalid CPubKey being serialized");
218template<
typename Stream>
225 throw std::ios_base::failure(
context +
" musig2 aggregate pubkey is invalid");
229 std::vector<unsigned char> val;
237 throw std::ios_base::failure(
context +
" musig2 participant pubkey is invalid");
241 if (!
s_val.empty()) {
242 throw std::ios_base::failure(
context +
" musig2 participants pubkeys value size is not a multiple of 33");
250template<
typename Stream>
261 throw std::ios_base::failure(
"musig2 aggregate pubkey is invalid");
266 throw std::ios_base::failure(
"musig2 participant pubkey is invalid");
321 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
349 template <
typename Stream>
359 template <
typename Stream>
451 std::vector<unsigned char> value;
473 std::vector<unsigned char> value;
484 for (
const auto& [
part_pubkey, pubnonce] : pubnonces) {
497 for (
const auto& [pubkey,
psig] : psigs) {
558 template <
typename Stream>
561 std::set<std::vector<unsigned char>>
key_lookup;
571 std::vector<unsigned char> key;
583 throw std::ios_base::failure(
tfm::format(
"Duplicate Key, input key \"%s\" already provided",
HexStr(key)));
609 throw std::ios_base::failure(
"Size of key was not the expected size for the type partial signature pubkey");
612 CPubKey pubkey(key.begin() + 1, key.end());
614 throw std::ios_base::failure(
"Invalid pubkey");
618 std::vector<unsigned char> sig;
623 throw std::ios_base::failure(
"Signature is not a valid encoding");
669 std::vector<unsigned char>
hash_vec(key.begin() + 1, key.end());
673 std::vector<unsigned char>
preimage;
684 std::vector<unsigned char>
hash_vec(key.begin() + 1, key.end());
688 std::vector<unsigned char>
preimage;
699 std::vector<unsigned char>
hash_vec(key.begin() + 1, key.end());
703 std::vector<unsigned char>
preimage;
714 std::vector<unsigned char>
hash_vec(key.begin() + 1, key.end());
718 std::vector<unsigned char>
preimage;
729 throw std::ios_base::failure(
"Previous txid is not allowed in PSBTv0");
739 throw std::ios_base::failure(
"Previous output's index is not allowed in PSBTv0");
749 throw std::ios_base::failure(
"Sequence is not allowed in PSBTv0");
759 throw std::ios_base::failure(
"Required time based locktime is not allowed in PSBTv0");
764 throw std::ios_base::failure(
"Required time based locktime is invalid (less than 500000000)");
772 throw std::ios_base::failure(
"Required height based locktime is not allowed in PSBTv0");
777 throw std::ios_base::failure(
"Required height based locktime is invalid (greater than or equal to 500000000)");
779 throw std::ios_base::failure(
"Required height based locktime is invalid (0)");
788 throw std::ios_base::failure(
"Input Taproot key path signature is shorter than 64 bytes");
790 throw std::ios_base::failure(
"Input Taproot key path signature is longer than 65 bytes");
802 std::vector<unsigned char> sig;
804 if (sig.size() < 64) {
805 throw std::ios_base::failure(
"Input Taproot script path signature is shorter than 64 bytes");
806 }
else if (sig.size() > 65) {
807 throw std::ios_base::failure(
"Input Taproot script path signature is longer than 65 bytes");
814 if (key.size() < 34) {
815 throw std::ios_base::failure(
"Input Taproot leaf script key is not at least 34 bytes");
816 }
else if ((key.size() - 2) % 32 != 0) {
817 throw std::ios_base::failure(
"Input Taproot leaf script key's control block size is not valid");
819 std::vector<unsigned char>
script_v;
822 throw std::ios_base::failure(
"Input Taproot leaf script must be at least 1 byte");
843 throw std::ios_base::failure(
"Input Taproot BIP32 keypath has an invalid length");
870 throw std::ios_base::failure(
"Input musig2 pubnonce key is not expected size of 67 or 99 bytes");
876 std::vector<uint8_t> pubnonce;
879 throw std::ios_base::failure(
"Input musig2 pubnonce value is not 66 bytes");
888 throw std::ios_base::failure(
"Input musig2 partial sig key is not expected size of 67 or 99 bytes");
922 throw std::ios_base::failure(
"Separator is missing at the end of an input map");
928 throw std::ios_base::failure(
"Previous TXID is required in PSBTv2");
931 throw std::ios_base::failure(
"Previous output's index is required in PSBTv2");
949 std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>>
m_tap_tree;
953 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
973 template <
typename Stream>
983 template <
typename Stream>
1024 std::vector<unsigned char> value;
1038 std::vector<unsigned char> value;
1048 std::vector<unsigned char> value;
1066 template <
typename Stream>
1069 std::set<std::vector<unsigned char>>
key_lookup;
1079 std::vector<unsigned char> key;
1091 throw std::ios_base::failure(
tfm::format(
"Duplicate Key, output key \"%s\" already provided",
HexStr(key)));
1123 throw std::ios_base::failure(
"Output amount is not allowed in PSBTv0");
1133 throw std::ios_base::failure(
"Output script is not allowed in PSBTv0");
1148 std::vector<unsigned char>
tree_v;
1152 throw std::ios_base::failure(
"Output Taproot tree must not be empty");
1155 while (!
s_tree.empty()) {
1158 std::vector<unsigned char>
script;
1163 throw std::ios_base::failure(
"Output Taproot tree has as leaf greater than Taproot maximum depth");
1166 throw std::ios_base::failure(
"Output Taproot tree has a leaf with an invalid leaf version");
1172 throw std::ios_base::failure(
"Output Taproot tree is malformed");
1187 throw std::ios_base::failure(
"Output Taproot BIP32 keypath has an invalid length");
1222 throw std::ios_base::failure(
"Separator is missing at the end of an output map");
1228 throw std::ios_base::failure(
"Output amount is required in PSBTv2");
1231 throw std::ios_base::failure(
"Output script is required in PSBTv2");
1246 std::map<KeyOriginInfo, std::set<CExtPubKey>>
m_xpubs;
1250 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
1271 template <
typename Stream>
1347 template <
typename Stream>
1353 throw std::ios_base::failure(
"Invalid PSBT magic bytes");
1357 std::set<std::vector<unsigned char>>
key_lookup;
1361 std::optional<CMutableTransaction> tx;
1371 std::vector<unsigned char> key;
1383 throw std::ios_base::failure(
tfm::format(
"Duplicate Key, global key \"%s\" already provided",
HexStr(key)));
1402 if (!
txin.scriptSig.empty() || !
txin.scriptWitness.IsNull()) {
1403 throw std::ios_base::failure(
"Unsigned tx does not have empty scriptSigs and scriptWitnesses.");
1458 if (!
xpub.pubkey.IsFullyValid()) {
1459 throw std::ios_base::failure(
"Invalid pubkey");
1483 throw std::ios_base::failure(
"Unsupported version number");
1509 throw std::ios_base::failure(
"Separator is missing at the end of the global map");
1518 throw std::ios_base::failure(
"No unsigned transaction was provided");
1522 throw std::ios_base::failure(
"PSBT_GLOBAL_TX_VERSION is not allowed in PSBTv0");
1525 throw std::ios_base::failure(
"PSBT_GLOBAL_FALLBACK_LOCKTIME is not allowed in PSBTv0");
1528 throw std::ios_base::failure(
"PSBT_GLOBAL_INPUT_COUNT is not allowed in PSBTv0");
1531 throw std::ios_base::failure(
"PSBT_GLOBAL_OUTPUT_COUNT is not allowed in PSBTv0");
1534 throw std::ios_base::failure(
"PSBT_GLOBAL_TX_MODIFIABLE is not allowed in PSBTv0");
1539 throw std::ios_base::failure(
"There is no PSBT version 1");
1544 throw std::ios_base::failure(
"PSBT_GLOBAL_TX_VERSION is required in PSBTv2");
1547 throw std::ios_base::failure(
"PSBT_GLOBAL_INPUT_COUNT is required in PSBTv2");
1550 throw std::ios_base::failure(
"PSBT_GLOBAL_OUTPUT_COUNT is required in PSBTv2");
1554 throw std::ios_base::failure(
"PSBT_GLOBAL_UNSIGNED_TX is not allowed in PSBTv2");
1558 throw std::ios_base::failure(
"Unknown PSBT version");
1565 inputs.emplace_back(
psbt_ver, tx->vin[i].prevout.hash, tx->vin[i].prevout.n, tx->vin[i].nSequence);
1576 throw std::ios_base::failure(
"Non-witness UTXO does not match outpoint hash");
1579 throw std::ios_base::failure(
"Input specifies output index that does not exist");
1583 throw std::ios_base::failure(
"Non-witness UTXO does not match outpoint hash");
1586 throw std::ios_base::failure(
"Input specifies output index that does not exist");
1594 throw std::ios_base::failure(
"Inputs provided does not match the number of inputs in transaction.");
1601 outputs.emplace_back(
psbt_ver, tx->vout[i].nValue, tx->vout[i].scriptPubKey);
1610 throw std::ios_base::failure(
"Outputs provided does not match the number of outputs in transaction.");
1614 template <
typename Stream>
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr size_t OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
An outpoint - a combination of a transaction hash and an index n into its vout.
An encapsulated public key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
static constexpr unsigned int COMPRESSED_SIZE
static constexpr unsigned int SIZE
secp256k1:
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
static constexpr size_t OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
Serialized script, used inside transaction inputs and outputs.
An input of a transaction.
An output of a transaction.
A structure for PSBTs which contains per output information.
std::map< CPubKey, std::vector< CPubKey > > m_musig2_participants
XOnlyPubKey m_tap_internal_key
std::map< XOnlyPubKey, std::pair< std::set< uint256 >, KeyOriginInfo > > m_tap_bip32_paths
bool operator==(const PSBTOutput &) const =default
void Merge(const PSBTOutput &output)
std::set< PSBTProprietary > m_proprietary
PSBTOutput(deserialize_type, Stream &s, uint32_t psbt_version)
void Serialize(Stream &s) const
uint32_t GetVersion() const
std::map< CPubKey, KeyOriginInfo > hd_keypaths
std::vector< std::tuple< uint8_t, uint8_t, std::vector< unsigned char > > > m_tap_tree
void Unserialize(Stream &s)
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
void FillSignatureData(SignatureData &sigdata) const
PSBTOutput(uint32_t psbt_version, CAmount amount, const CScript &script)
void FromSignatureData(const SignatureData &sigdata)
A version of CTransaction with the PSBT format.
std::optional< std::bitset< 8 > > m_tx_modifiable
uint32_t GetVersion() const
bool Merge(const PartiallySignedTransaction &psbt)
Merge psbt into this.
std::map< KeyOriginInfo, std::set< CExtPubKey > > m_xpubs
std::optional< uint32_t > m_version
std::optional< Txid > GetUniqueID() const
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
std::vector< PSBTInput > inputs
void MergeGlobalXPubs(const PartiallySignedTransaction &psbt)
Merge the global xpubs of psbt into this, keeping the existing origin for an xpub seen again with a d...
std::optional< CMutableTransaction > GetUnsignedTx() const
std::optional< uint32_t > ComputeTimeLock() const
std::vector< PSBTOutput > outputs
std::set< PSBTProprietary > m_proprietary
void Serialize(Stream &s) const
bool AddOutput(const PSBTOutput &psbtout)
PartiallySignedTransaction(deserialize_type, Stream &s)
std::optional< uint32_t > fallback_locktime
void Unserialize(Stream &s)
bool AddInput(const PSBTInput &psbtin)
An interface to be implemented by keystores that support signing.
Minimal stream for reading from an existing byte array by std::span.
Utility class to construct Taproot outputs from internal key and script tree.
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
constexpr bool IsNull() const
The util::Expected class provides a standard way for low-level functions to return either error value...
is a home for simple enum and struct type definitions that can be used internally by functions in the...
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, script_verify_flags flags, ScriptError *serror)
constexpr uint8_t TAPROOT_LEAF_MASK
constexpr size_t TAPROOT_CONTROL_MAX_NODE_COUNT
@ SCRIPT_VERIFY_STRICTENC
constexpr size_t MUSIG2_PUBNONCE_SIZE
constexpr TransactionSerParams TX_NO_WITNESS
constexpr TransactionSerParams TX_WITH_WITNESS
std::shared_ptr< const CTransaction > CTransactionRef
constexpr uint8_t PSBT_OUT_AMOUNT
void SerializeToVector(Stream &s, const X &... args)
constexpr uint8_t PSBT_IN_REQUIRED_HEIGHT_LOCKTIME
constexpr uint8_t PSBT_IN_RIPEMD160
void UpdatePSBTOutput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index)
Updates a PSBTOutput with information from provider.
constexpr uint8_t PSBT_IN_HASH256
constexpr uint8_t PSBT_IN_REQUIRED_TIME_LOCKTIME
constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX
constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO
util::Expected< void, PSBTError > SignPSBTInput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index, const PrecomputedTransactionData *txdata, const common::PSBTFillOptions &options, SignatureData *out_sigdata=nullptr)
Signs a PSBTInput, verifying that all provided data matches what is being signed.
constexpr uint8_t PSBT_IN_TAP_KEY_SIG
void UnserializeFromVector(Stream &s, X &&... args)
constexpr uint8_t PSBT_IN_SCRIPTSIG
constexpr uint8_t PSBT_IN_WITNESSSCRIPT
constexpr uint8_t PSBT_IN_OUTPUT_INDEX
bool PSBTInputSignedAndVerified(const PartiallySignedTransaction &psbt, unsigned int input_index, const PrecomputedTransactionData *txdata)
Checks whether a PSBTInput is already signed by doing script verification using final fields.
std::string PSBTRoleName(PSBTRole role)
util::Result< PartiallySignedTransaction > DecodeBase64PSBT(const std::string &base64_tx)
Decode a base64ed PSBT into a PartiallySignedTransaction.
constexpr uint8_t PSBT_OUT_REDEEMSCRIPT
constexpr uint8_t PSBT_GLOBAL_VERSION
constexpr uint8_t PSBT_GLOBAL_TX_VERSION
constexpr uint8_t PSBT_IN_TAP_LEAF_SCRIPT
constexpr uint8_t PSBT_OUT_PROPRIETARY
constexpr uint8_t PSBT_MAGIC_BYTES[5]
std::optional< PartiallySignedTransaction > CombinePSBTs(const std::vector< PartiallySignedTransaction > &psbtxs)
Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial sign...
constexpr uint32_t PSBT_HIGHEST_VERSION
constexpr uint8_t PSBT_SEPARATOR
constexpr uint8_t PSBT_IN_BIP32_DERIVATION
void DeserializeMuSig2ParticipantPubkeys(Stream &s, SpanReader &skey, std::map< CPubKey, std::vector< CPubKey > > &out, std::string context)
constexpr uint8_t PSBT_IN_SCRIPTWITNESS
constexpr uint8_t PSBT_OUT_TAP_TREE
constexpr uint8_t PSBT_IN_PREVIOUS_TXID
constexpr uint8_t PSBT_OUT_BIP32_DERIVATION
constexpr uint8_t PSBT_GLOBAL_PROPRIETARY
void RemoveUnnecessaryTransactions(PartiallySignedTransaction &psbtx)
Reduces the size of the PSBT by dropping unnecessary non_witness_utxos (i.e.
std::optional< PrecomputedTransactionData > PrecomputePSBTData(const PartiallySignedTransaction &psbt)
Compute a PrecomputedTransactionData object from a psbt.
constexpr uint8_t PSBT_IN_PROPRIETARY
constexpr uint8_t PSBT_GLOBAL_TX_MODIFIABLE
constexpr uint8_t PSBT_IN_SEQUENCE
KeyOriginInfo DeserializeKeyOrigin(Stream &s, uint64_t length)
void DeserializeMuSig2ParticipantDataIdentifier(Stream &skey, CPubKey &agg_pub, CPubKey &part_pub, uint256 &leaf_hash)
constexpr uint8_t PSBT_IN_TAP_SCRIPT_SIG
constexpr uint8_t PSBT_IN_MUSIG2_PUB_NONCE
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, CompactSizeWriter type)
static void ExpectedKeySize(const std::string &key_name, const std::vector< unsigned char > &key, uint64_t expected_size)
constexpr uint8_t PSBT_IN_TAP_BIP32_DERIVATION
constexpr uint8_t PSBT_IN_HASH160
constexpr uint8_t PSBT_IN_REDEEMSCRIPT
constexpr uint8_t PSBT_OUT_WITNESSSCRIPT
constexpr uint8_t PSBT_GLOBAL_XPUB
constexpr uint8_t PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS
constexpr uint8_t PSBT_GLOBAL_OUTPUT_COUNT
constexpr uint8_t PSBT_OUT_TAP_BIP32_DERIVATION
constexpr uint8_t PSBT_IN_PARTIAL_SIG
constexpr uint8_t PSBT_GLOBAL_FALLBACK_LOCKTIME
constexpr uint8_t PSBT_IN_TAP_INTERNAL_KEY
constexpr std::streamsize MAX_FILE_SIZE_PSBT
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.
constexpr uint8_t PSBT_OUT_TAP_INTERNAL_KEY
constexpr uint8_t PSBT_IN_TAP_MERKLE_ROOT
void SerializeKeyOrigin(Stream &s, KeyOriginInfo hd_keypath)
void DeserializeHDKeypaths(Stream &s, const std::vector< unsigned char > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
constexpr uint8_t PSBT_IN_SIGHASH
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
constexpr uint8_t PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS
void SerializeHDKeypath(Stream &s, KeyOriginInfo hd_keypath)
void DeserializeHDKeypath(Stream &s, KeyOriginInfo &hd_keypath)
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
constexpr uint8_t PSBT_OUT_SCRIPT
util::Result< PartiallySignedTransaction > DecodeRawPSBT(std::span< const std::byte > tx_data)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
constexpr uint8_t PSBT_GLOBAL_INPUT_COUNT
constexpr uint8_t PSBT_IN_WITNESS_UTXO
constexpr uint8_t PSBT_IN_MUSIG2_PARTIAL_SIG
constexpr uint8_t PSBT_IN_SHA256
constexpr unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE
constexpr unsigned int LOCKTIME_THRESHOLD
std::vector< unsigned char > ToByteVector(const T &in)
void SerializeMany(Stream &s, const Args &... args)
Support for (un)serializing many things at once.
constexpr deserialize_type deserialize
void UnserializeMany(Stream &s, Args &&... args)
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
std::pair< CPubKey, std::vector< unsigned char > > SigPair
A mutable version of CTransaction.
std::vector< std::vector< unsigned char > > stack
A structure for PSBT proprietary types.
std::vector< unsigned char > value
bool operator<(const PSBTProprietary &b) const
std::vector< unsigned char > identifier
std::vector< unsigned char > key
bool operator==(const PSBTProprietary &b) const
Instructions for how a PSBT should be signed or filled with information.
Dummy data type to identify deserializing constructors.
FuzzedDataProvider provider
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.