18typedef std::vector<unsigned char>
valtype;
21 : txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn,
MissingDataBehavior::
FAIL),
27 : txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn),
39 if (!provider.
GetKey(address, key))
53 if (!key.
Sign(hash, vchSig))
55 vchSig.push_back((
unsigned char)hashtype);
77 if (!leaf_hash)
return false;
84 if (!key.
SignSchnorr(hash, sig, merkle_root,
nullptr))
return false;
108 const auto it = sigdata.
signatures.find(address);
110 pubkey = it->second.first;
116 pubkey = pk_it->second.first;
120 return provider.
GetPubKey(address, pubkey);
126 const auto it = sigdata.
signatures.find(keyid);
128 sig_out = it->second.second;
133 sigdata.
misc_pubkeys.emplace(keyid, std::make_pair(pubkey, std::move(info)));
135 if (creator.
CreateSig(provider, sig_out, keyid, scriptcode, sigversion)) {
147 auto lookup_key = std::make_pair(pubkey, leaf_hash);
150 sig_out = it->second;
152 if (creator.
CreateSchnorrSig(provider, sig_out, pubkey, &leaf_hash,
nullptr, sigversion)) {
168 if (script.
size() == 34 && script[33] ==
OP_CHECKSIG && script[0] == 0x20) {
170 std::vector<unsigned char> sig;
172 result =
Vector(std::move(sig));
191 std::vector<unsigned char> sig;
204 std::vector<std::vector<unsigned char>> smallest_result_stack;
206 const auto& [script, leaf_ver] = key;
207 std::vector<std::vector<unsigned char>> result_stack;
208 if (
SignTaprootScript(provider, creator, sigdata, leaf_ver, script, result_stack)) {
209 result_stack.emplace_back(std::begin(script), std::end(script));
210 result_stack.push_back(*control_blocks.begin());
211 if (smallest_result_stack.size() == 0 ||
213 smallest_result_stack = std::move(result_stack);
217 if (smallest_result_stack.size() != 0) {
218 result = std::move(smallest_result_stack);
237 std::vector<unsigned char> sig;
239 std::vector<valtype> vSolutions;
240 whichTypeRet =
Solver(scriptPubKey, vSolutions);
242 switch (whichTypeRet) {
248 if (!
CreateSig(creator, sigdata, provider, sig,
CPubKey(vSolutions[0]), scriptPubKey, sigversion))
return false;
249 ret.push_back(std::move(sig));
254 if (!
GetPubKey(provider, sigdata, keyID, pubkey)) {
259 if (!
CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion))
return false;
260 ret.push_back(std::move(sig));
267 ret.push_back(std::vector<unsigned char>(scriptRet.
begin(), scriptRet.
end()));
275 size_t required = vSolutions.front()[0];
277 for (
size_t i = 1; i < vSolutions.size() - 1; ++i) {
282 if (
CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) {
283 if (ret.size() < required + 1) {
284 ret.push_back(std::move(sig));
288 bool ok = ret.size() == required + 1;
289 for (
size_t i = 0; i + ret.size() < required + 1; ++i) {
295 ret.push_back(vSolutions[0]);
301 ret.push_back(std::vector<unsigned char>(scriptRet.
begin(), scriptRet.
end()));
320 }
else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
322 }
else if (v.size() == 1 && v[0] == 0x81) {
335 std::vector<valtype> result;
346 subscript =
CScript(result[0].begin(), result[0].end());
364 CScript witnessscript(result[0].begin(), result[0].end());
368 result.push_back(std::vector<unsigned char>(witnessscript.
begin(), witnessscript.
end()));
384 result.push_back(std::vector<unsigned char>(subscript.
begin(), subscript.
end()));
402 bool CheckECDSASignature(
const std::vector<unsigned char>& scriptSig,
const std::vector<unsigned char>& vchPubKey,
const CScript& scriptCode,
SigVersion sigversion)
const override
404 if (m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion)) {
415 std::vector<valtype> script;
416 std::vector<valtype> witness;
419 Stacks(
const Stacks&) =
delete;
420 explicit Stacks(
const SignatureData& data) : witness(data.scriptWitness.stack) {
437 SignatureExtractorChecker extractor_checker(data, tx_checker);
444 std::vector<std::vector<unsigned char>> solutions;
451 CScript redeem_script(stack.script.back().begin(), stack.script.back().end());
453 next_script = std::move(redeem_script);
456 script_type =
Solver(next_script, solutions);
457 stack.script.pop_back();
461 CScript witness_script(stack.witness.back().begin(), stack.witness.back().end());
463 next_script = std::move(witness_script);
466 script_type =
Solver(next_script, solutions);
467 stack.witness.pop_back();
468 stack.script = std::move(stack.witness);
469 stack.witness.clear();
474 assert(solutions.size() > 1);
475 unsigned int num_pubkeys = solutions.size()-2;
476 unsigned int last_success_key = 0;
477 for (
const valtype& sig : stack.script) {
478 for (
unsigned int i = last_success_key; i < num_pubkeys; ++i) {
479 const valtype& pubkey = solutions[i+1];
481 if (data.
signatures.count(
CPubKey(pubkey).GetID()) || extractor_checker.CheckECDSASignature(sig, pubkey, next_script, sigversion)) {
482 last_success_key = i + 1;
502 *
this = std::move(sigdata);
541 DummySignatureChecker() {}
542 bool CheckECDSASignature(
const std::vector<unsigned char>& scriptSig,
const std::vector<unsigned char>& vchPubKey,
const CScript& scriptCode,
SigVersion sigversion)
const override {
return true; }
545const DummySignatureChecker DUMMY_CHECKER;
552 DummySignatureCreator(
char r_len,
char s_len) : m_r_len(r_len), m_s_len(s_len) {}
557 vchSig.assign(m_r_len + m_s_len + 7,
'\000');
559 vchSig[1] = m_r_len + m_s_len + 4;
563 vchSig[4 + m_r_len] = 0x02;
564 vchSig[5 + m_r_len] = m_s_len;
565 vchSig[6 + m_r_len] = 0x01;
571 sig.assign(64,
'\000');
606 std::vector<valtype> solutions;
607 auto whichtype =
Solver(script, solutions);
609 auto h160 =
uint160(solutions[0]);
621 bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) ==
SIGHASH_SINGLE);
628 std::vector<CTxOut> spent_outputs;
629 for (
unsigned int i = 0; i < mtx.
vin.size(); ++i) {
631 auto coin = coins.find(txin.
prevout);
632 if (coin == coins.end() || coin->second.IsSpent()) {
633 txdata.
Init(txConst, {},
true);
636 spent_outputs.emplace_back(coin->second.out.nValue, coin->second.out.scriptPubKey);
639 if (spent_outputs.size() == mtx.
vin.size()) {
640 txdata.
Init(txConst, std::move(spent_outputs),
true);
644 for (
unsigned int i = 0; i < mtx.
vin.size(); ++i) {
646 auto coin = coins.find(txin.
prevout);
647 if (coin == coins.end() || coin->second.IsSpent()) {
648 input_errors[i] =
_(
"Input not found or already spent");
651 const CScript& prevPubKey = coin->second.out.scriptPubKey;
652 const CAmount& amount = coin->second.out.nValue;
656 if (!fHashSingle || (i < mtx.
vout.size())) {
664 input_errors[i] =
_(
"Missing amount");
672 input_errors[i] =
Untranslated(
"Unable to sign input, invalid stack size (possibly missing key)");
675 input_errors[i] =
Untranslated(
"CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)");
681 input_errors.erase(i);
684 return input_errors.empty();
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
bool MoneyRange(const CAmount &nValue)
int64_t CAmount
Amount in satoshis (Can be negative)
virtual bool CheckSchnorrSignature(Span< const unsigned char > sig, Span< const unsigned char > pubkey, SigVersion sigversion, const ScriptExecutionData &execdata, ScriptError *serror=nullptr) const
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Interface for signature creators.
virtual const BaseSignatureChecker & Checker() const =0
virtual bool CreateSchnorrSig(const SigningProvider &provider, std::vector< unsigned char > &sig, const XOnlyPubKey &pubkey, const uint256 *leaf_hash, const uint256 *merkle_root, SigVersion sigversion) const =0
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.
A writer stream (for serialization) that computes a 256-bit hash.
An encapsulated private key.
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
bool SignSchnorr(const uint256 &hash, Span< unsigned char > sig, const uint256 *merkle_root=nullptr, const uint256 *aux=nullptr) const
Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this, optionally tweaked b...
A reference to a CKey: the Hash160 of its serialized public key.
An encapsulated public key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
A hasher class for RIPEMD-160.
CRIPEMD160 & Write(const unsigned char *data, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
Serialized script, used inside transaction inputs and outputs.
bool IsPayToScriptHash() const
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
static opcodetype EncodeOP_N(int n)
A reference to a CScript: the Hash160 of its serialization (see script.h)
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
An input of a transaction.
CScriptWitness scriptWitness
Only serialized through CTransaction.
An output of a transaction.
bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const override
A signature creator for transactions.
bool CreateSchnorrSig(const SigningProvider &provider, std::vector< unsigned char > &sig, const XOnlyPubKey &pubkey, const uint256 *leaf_hash, const uint256 *merkle_root, SigVersion sigversion) const override
MutableTransactionSignatureCreator(const CMutableTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn, int nHashTypeIn)
const CMutableTransaction * txTo
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.
const PrecomputedTransactionData * m_txdata
An interface to be implemented by keystores that support signing.
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
bool GetKeyByXOnly(const XOnlyPubKey &pubkey, CKey &key) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
A Span is an object that can refer to a contiguous sequence of objects.
bool SignatureHashSchnorr(uint256 &hash_out, const ScriptExecutionData &execdata, const T &tx_to, uint32_t in_pos, uint8_t hash_type, SigVersion sigversion, const PrecomputedTransactionData &cache, MissingDataBehavior mdb)
const CHashWriter HASHER_TAPLEAF
Hasher with tag "TapLeaf" pre-fed to it.
std::vector< unsigned char > valtype
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ TAPROOT
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, key path spending; see BIP 341.
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ TAPSCRIPT
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, script path spending, leaf version 0xc0; see...
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
@ SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
@ SCRIPT_VERIFY_STRICTENC
static constexpr uint8_t TAPROOT_LEAF_TAPSCRIPT
@ SIGHASH_DEFAULT
Taproot only; implied when sighash byte is missing, and equivalent to SIGHASH_ALL.
MissingDataBehavior
Enum to specify what *TransactionSignatureChecker's behavior should be when dealing with missing tran...
@ FAIL
Just act as if the signature was invalid.
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
std::vector< unsigned char > ToByteVector(const T &in)
std::string ScriptErrorString(const ScriptError serror)
enum ScriptError_t ScriptError
@ SCRIPT_ERR_INVALID_STACK_OPERATION
@ SCRIPT_ERR_SIG_NULLFAIL
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
size_t GetSerializeSize(const T &t, int nVersion=0)
static bool SignStep(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &scriptPubKey, std::vector< valtype > &ret, TxoutType &whichTypeRet, SigVersion sigversion, SignatureData &sigdata)
Sign scriptPubKey using signature made with creator.
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
static bool CreateTaprootScriptSig(const BaseSignatureCreator &creator, SignatureData &sigdata, const SigningProvider &provider, std::vector< unsigned char > &sig_out, const XOnlyPubKey &pubkey, const uint256 &leaf_hash, SigVersion sigversion)
void UpdateInput(CTxIn &input, const SignatureData &data)
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
static bool SignTaprootScript(const SigningProvider &provider, const BaseSignatureCreator &creator, SignatureData &sigdata, int leaf_version, const CScript &script, std::vector< valtype > &result)
static bool CreateSig(const BaseSignatureCreator &creator, SignatureData &sigdata, const SigningProvider &provider, std::vector< unsigned char > &sig_out, const CPubKey &pubkey, const CScript &scriptcode, SigVersion sigversion)
bool IsSolvable(const SigningProvider &provider, const CScript &script)
std::vector< unsigned char > valtype
static bool SignTaproot(const SigningProvider &provider, const BaseSignatureCreator &creator, const WitnessV1Taproot &output, SignatureData &sigdata, std::vector< valtype > &result)
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.
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, int nHashType, std::map< int, bilingual_str > &input_errors)
Sign the CMutableTransaction.
static CScript PushAll(const std::vector< valtype > &values)
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
static bool GetCScript(const SigningProvider &provider, const SignatureData &sigdata, const CScriptID &scriptid, CScript &script)
std::pair< CPubKey, std::vector< unsigned char > > SigPair
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
@ WITNESS_UNKNOWN
Only for Witness versions not already defined above.
@ NULL_DATA
unspendable OP_RETURN script that carries data
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< std::vector< unsigned char > > stack
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
bool m_bip341_taproot_ready
Whether the 5 fields above are initialized.
bool m_spent_outputs_ready
Whether m_spent_outputs is initialized.
uint256 m_tapleaf_hash
The tapleaf hash.
bool m_annex_present
Whether an annex is present.
bool m_annex_init
Whether m_annex_present and (when needed) m_annex_hash are initialized.
bool m_codeseparator_pos_init
Whether m_codeseparator_pos is initialized.
bool m_tapleaf_hash_init
Whether m_tapleaf_hash is initialized.
uint32_t m_codeseparator_pos
Opcode position of the last executed OP_CODESEPARATOR (or 0xFFFFFFFF if none executed).
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
void MergeSignatureData(SignatureData sigdata)
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
TaprootSpendData tr_spenddata
Taproot spending data.
bool witness
Stores whether the input this SigData corresponds to is a witness input.
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format.
std::vector< unsigned char > taproot_key_path_sig
std::map< std::pair< XOnlyPubKey, uint256 >, std::vector< unsigned char > > taproot_script_sigs
Schnorr signature for key path spending.
CScript redeem_script
The redeemScript (if any) for the input.
uint256 missing_witness_script
SHA256 of the missing witnessScript (if any)
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
bool complete
Stores whether the scriptSig and scriptWitness are complete.
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
std::map< std::pair< CScript, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
XOnlyPubKey internal_key
The BIP341 internal key.
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
static const int PROTOCOL_VERSION
network protocol versioning