6 #ifndef BITCOIN_SCRIPT_SIGN_H 7 #define BITCOIN_SCRIPT_SIGN_H 9 #include <boost/optional.hpp> 26 std::vector<uint32_t>
path;
62 std::map<CKeyID, CKey>
keys;
101 typedef std::pair<CPubKey, std::vector<unsigned char>>
SigPair;
122 static constexpr uint8_t PSBT_MAGIC_BYTES[5] = {
'p',
's',
'b',
't', 0xff};
125 static constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX = 0x00;
128 static constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO = 0x00;
129 static constexpr uint8_t PSBT_IN_WITNESS_UTXO = 0x01;
130 static constexpr uint8_t PSBT_IN_PARTIAL_SIG = 0x02;
131 static constexpr uint8_t PSBT_IN_SIGHASH = 0x03;
132 static constexpr uint8_t PSBT_IN_REDEEMSCRIPT = 0x04;
133 static constexpr uint8_t PSBT_IN_WITNESSSCRIPT = 0x05;
134 static constexpr uint8_t PSBT_IN_BIP32_DERIVATION = 0x06;
135 static constexpr uint8_t PSBT_IN_SCRIPTSIG = 0x07;
136 static constexpr uint8_t PSBT_IN_SCRIPTWITNESS = 0x08;
139 static constexpr uint8_t PSBT_OUT_REDEEMSCRIPT = 0x00;
140 static constexpr uint8_t PSBT_OUT_WITNESSSCRIPT = 0x01;
141 static constexpr uint8_t PSBT_OUT_BIP32_DERIVATION = 0x02;
145 static constexpr uint8_t PSBT_SEPARATOR = 0x00;
149 template<
typename Stream,
typename...
X>
157 template<
typename Stream,
typename...
X>
161 size_t remaining_before = s.size();
163 size_t remaining_after = s.size();
164 if (remaining_after + expected_size != remaining_before) {
165 throw std::ios_base::failure(
"Size of value was not the stated size");
170 template<
typename Stream>
171 void DeserializeHDKeypaths(Stream& s,
const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
175 throw std::ios_base::failure(
"Size of key was not the expected size for the type BIP32 keypath");
178 CPubKey pubkey(key.begin() + 1, key.end());
179 if (!pubkey.IsFullyValid()) {
180 throw std::ios_base::failure(
"Invalid pubkey");
182 if (hd_keypaths.count(pubkey) > 0) {
183 throw std::ios_base::failure(
"Duplicate Key, pubkey derivation path already provided");
188 if (value_len % 4 || value_len == 0) {
189 throw std::ios_base::failure(
"Invalid length for HD key path");
194 for (
unsigned int i = 4; i < value_len; i +=
sizeof(uint32_t)) {
197 keypath.
path.push_back(index);
201 hd_keypaths.emplace(pubkey, std::move(keypath));
205 template<
typename Stream>
208 for (
auto keypath_pair : hd_keypaths) {
210 WriteCompactSize(s, (keypath_pair.second.path.size() + 1) *
sizeof(uint32_t));
211 s << keypath_pair.second.fingerprint;
212 for (
const auto& path : keypath_pair.second.path) {
229 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
239 template <
typename Stream>
256 s << sig_pair.second.second;
302 template <
typename Stream>
305 bool found_sep =
false;
308 std::vector<unsigned char> key;
319 unsigned char type = key[0];
323 case PSBT_IN_NON_WITNESS_UTXO:
326 throw std::ios_base::failure(
"Duplicate Key, input non-witness utxo already provided");
327 }
else if (key.size() != 1) {
328 throw std::ios_base::failure(
"Non-witness utxo key is more than one byte type");
335 case PSBT_IN_WITNESS_UTXO:
337 throw std::ios_base::failure(
"Duplicate Key, input witness utxo already provided");
338 }
else if (key.size() != 1) {
339 throw std::ios_base::failure(
"Witness utxo key is more than one byte type");
343 case PSBT_IN_PARTIAL_SIG:
347 throw std::ios_base::failure(
"Size of key was not the expected size for the type partial signature pubkey");
350 CPubKey pubkey(key.begin() + 1, key.end());
351 if (!pubkey.IsFullyValid()) {
352 throw std::ios_base::failure(
"Invalid pubkey");
355 throw std::ios_base::failure(
"Duplicate Key, input partial signature for pubkey already provided");
359 std::vector<unsigned char> sig;
366 case PSBT_IN_SIGHASH:
368 throw std::ios_base::failure(
"Duplicate Key, input sighash type already provided");
369 }
else if (key.size() != 1) {
370 throw std::ios_base::failure(
"Sighash type key is more than one byte type");
374 case PSBT_IN_REDEEMSCRIPT:
377 throw std::ios_base::failure(
"Duplicate Key, input redeemScript already provided");
378 }
else if (key.size() != 1) {
379 throw std::ios_base::failure(
"Input redeemScript key is more than one byte type");
384 case PSBT_IN_WITNESSSCRIPT:
387 throw std::ios_base::failure(
"Duplicate Key, input witnessScript already provided");
388 }
else if (key.size() != 1) {
389 throw std::ios_base::failure(
"Input witnessScript key is more than one byte type");
394 case PSBT_IN_BIP32_DERIVATION:
399 case PSBT_IN_SCRIPTSIG:
402 throw std::ios_base::failure(
"Duplicate Key, input final scriptSig already provided");
403 }
else if (key.size() != 1) {
404 throw std::ios_base::failure(
"Final scriptSig key is more than one byte type");
409 case PSBT_IN_SCRIPTWITNESS:
412 throw std::ios_base::failure(
"Duplicate Key, input final scriptWitness already provided");
413 }
else if (key.size() != 1) {
414 throw std::ios_base::failure(
"Final scriptWitness key is more than one byte type");
422 throw std::ios_base::failure(
"Duplicate Key, key for unknown value already provided");
425 std::vector<unsigned char> val_bytes;
427 unknown.emplace(std::move(key), std::move(val_bytes));
433 throw std::ios_base::failure(
"Separator is missing at the end of an input map");
437 template <
typename Stream>
449 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
458 template <
typename Stream>
485 template <
typename Stream>
488 bool found_sep =
false;
491 std::vector<unsigned char> key;
502 unsigned char type = key[0];
506 case PSBT_OUT_REDEEMSCRIPT:
509 throw std::ios_base::failure(
"Duplicate Key, output redeemScript already provided");
510 }
else if (key.size() != 1) {
511 throw std::ios_base::failure(
"Output redeemScript key is more than one byte type");
516 case PSBT_OUT_WITNESSSCRIPT:
519 throw std::ios_base::failure(
"Duplicate Key, output witnessScript already provided");
520 }
else if (key.size() != 1) {
521 throw std::ios_base::failure(
"Output witnessScript key is more than one byte type");
526 case PSBT_OUT_BIP32_DERIVATION:
534 throw std::ios_base::failure(
"Duplicate Key, key for unknown value already provided");
537 std::vector<unsigned char> val_bytes;
539 unknown.emplace(std::move(key), std::move(val_bytes));
546 throw std::ios_base::failure(
"Separator is missing at the end of an output map");
550 template <
typename Stream>
559 boost::optional<CMutableTransaction>
tx;
562 std::map<std::vector<unsigned char>, std::vector<unsigned char>>
unknown;
573 return a.
tx->GetHash() == b.
tx->GetHash();
580 template <
typename Stream>
584 s << PSBT_MAGIC_BYTES;
613 template <
typename Stream>
618 if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) {
619 throw std::ios_base::failure(
"Invalid PSBT magic bytes");
623 bool found_sep =
false;
626 std::vector<unsigned char> key;
637 unsigned char type = key[0];
641 case PSBT_GLOBAL_UNSIGNED_TX:
644 throw std::ios_base::failure(
"Duplicate Key, unsigned tx already provided");
645 }
else if (key.size() != 1) {
646 throw std::ios_base::failure(
"Global unsigned tx key is more than one byte type");
654 for (
const CTxIn& txin :
tx->vin) {
656 throw std::ios_base::failure(
"Unsigned tx does not have empty scriptSigs and scriptWitnesses.");
664 throw std::ios_base::failure(
"Duplicate Key, key for unknown value already provided");
667 std::vector<unsigned char> val_bytes;
669 unknown.emplace(std::move(key), std::move(val_bytes));
675 throw std::ios_base::failure(
"Separator is missing at the end of the global map");
680 throw std::ios_base::failure(
"No unsigned transcation was provided");
685 while (!s.empty() && i <
tx->vin.size()) {
691 if (input.non_witness_utxo && input.non_witness_utxo->GetHash() !=
tx->vin[i].prevout.hash) {
692 throw std::ios_base::failure(
"Non-witness UTXO does not match outpoint hash");
697 if (
inputs.size() !=
tx->vin.size()) {
698 throw std::ios_base::failure(
"Inputs provided does not match the number of inputs in transaction.");
703 while (!s.empty() && i <
tx->vout.size()) {
711 throw std::ios_base::failure(
"Outputs provided does not match the number of outputs in transaction.");
715 throw std::ios_base::failure(
"PSBT is not sane.");
719 template <
typename Stream>
745 #endif // BITCOIN_SCRIPT_SIGN_H
virtual ~SigningProvider()
bool IsSolvable(const SigningProvider &provider, const CScript &script)
unsigned char fingerprint[4]
friend bool operator==(const PartiallySignedTransaction &a, const PartiallySignedTransaction &b)
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
friend bool operator!=(const PartiallySignedTransaction &a, const PartiallySignedTransaction &b)
boost::optional< CMutableTransaction > tx
void SerializeToVector(Stream &s, const X &... args)
const SigningProvider * m_provider
uint64_t ReadCompactSize(Stream &is)
virtual bool CreateSig(const SigningProvider &provider, std::vector< unsigned char > &vchSig, const CKeyID &keyid, const CScript &scriptCode, SigVersion sigversion) const =0
Create a singular (non-script) signature.
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
const MutableTransactionSignatureChecker checker
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format...
std::map< CKeyID, CKey > keys
CScriptWitness scriptWitness
Only serialized through CTransaction.
static constexpr unsigned int PUBLIC_KEY_SIZE
secp256k1:
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Interface for signature creators.
const BaseSignatureChecker & Checker() const override
std::map< CKeyID, KeyOriginInfo > origins
std::vector< std::vector< unsigned char > > stack
void Merge(const PSBTOutput &output)
void Serialize(Stream &s) const
A version of CTransaction with the PSBT format.
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
A signature creator for transactions.
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
void Serialize(Stream &s) const
void DeserializeHDKeypaths(Stream &s, const std::vector< unsigned char > &key, std::map< CPubKey, KeyOriginInfo > &hd_keypaths)
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
std::shared_ptr< const CTransaction > CTransactionRef
void UnserializeMany(Stream &s)
Dummy data type to identify deserializing constructors.
void UnserializeFromVector(Stream &s, X &... args)
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
void FromSignatureData(const SignatureData &sigdata)
bool CreateSig(const SigningProvider &provider, std::vector< unsigned char > &vchSig, const CKeyID &keyid, const CScript &scriptCode, SigVersion sigversion) const override
Create a singular (non-script) signature.
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &scriptPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
int64_t CAmount
Amount in satoshis (Can be negative)
SignatureData(const CScript &script)
A structure for PSBTs which contains per output information.
std::vector< PSBTOutput > outputs
std::map< CPubKey, KeyOriginInfo > hd_keypaths
void UpdateInput(CTxIn &input, const SignatureData &data)
bool SignPSBTInput(const SigningProvider &provider, const CMutableTransaction &tx, PSBTInput &input, int index, int sighash=SIGHASH_ALL)
Signs a PSBTInput, verifying that all provided data matches what is being signed. ...
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
std::map< CScriptID, CScript > scripts
An input of a transaction.
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
An encapsulated public key.
bool GetKey(const CKeyID &keyid, CKey &key) const override
std::map< CKeyID, CPubKey > pubkeys
std::pair< CPubKey, std::vector< unsigned char > > SigPair
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
void Unserialize(Stream &s)
An output of a transaction.
void MergeSignatureData(SignatureData sigdata)
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
void SerializeMany(Stream &s)
std::vector< PSBTInput > inputs
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
virtual bool GetKey(const CKeyID &address, CKey &key) const
const SigningProvider & DUMMY_SIGNING_PROVIDER
bool GetCScript(const CScriptID &scriptid, CScript &script) const override
bool GetKey(const CKeyID &keyid, CKey &key) const override
void FillSignatureData(SignatureData &sigdata) const
An interface to be implemented by keystores that support signing.
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType)
Produce a script signature for a transaction.
void SerializeHDKeypaths(Stream &s, const std::map< CPubKey, KeyOriginInfo > &hd_keypaths, uint8_t type)
virtual ~BaseSignatureCreator()
PartiallySignedTransaction(const PartiallySignedTransaction &psbt_in)
Serialized script, used inside transaction inputs and outputs.
bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const override
A reference to a CKey: the Hash360 of its serialized public key.
size_t GetSerializeSizeMany(int nVersion, const T &... t)
void Unserialize(Stream &s)
A reference to a CScript: the Hash360 of its serialization (see script.h)
A mutable version of CTransaction.
HidingSigningProvider(const SigningProvider *provider, bool hide_secret, bool hide_origin)
PSBTOutput(deserialize_type, Stream &s)
An encapsulated private key.
The basic transaction that is broadcasted on the network and contained in blocks. ...
void Merge(const PartiallySignedTransaction &psbt)
MutableTransactionSignatureCreator(const CMutableTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn, int nHashTypeIn=SIGHASH_ALL)
bool complete
Stores whether the scriptSig and scriptWitness are complete.
PartiallySignedTransaction(deserialize_type, Stream &s)
std::vector< uint32_t > path
bool GetPubKey(const CKeyID &keyid, CPubKey &pubkey) const override
CScript redeem_script
The redeemScript (if any) for the input.
PartiallySignedTransaction()
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
bool witness
Stores whether the input this SigData corresponds to is a witness input.
FlatSigningProvider Merge(const FlatSigningProvider &a, const FlatSigningProvider &b)
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
virtual const BaseSignatureChecker & Checker() const =0
const CMutableTransaction * txTo