 |
Bitcoin Core
22.99.0
P2P Digital Currency
|
Go to the documentation of this file.
19 typedef std::vector<unsigned char>
valtype;
28 : txTo{tx}, nIn{input_idx}, nHashType{hash_type}, amount{amount},
40 if (!provider.
GetKey(address, key))
54 if (!key.
Sign(hash, vchSig))
56 vchSig.push_back((
unsigned char)hashtype);
78 if (!leaf_hash)
return false;
86 if (!key.
SignSchnorr(hash, sig, merkle_root, {}))
return false;
110 const auto it = sigdata.
signatures.find(address);
112 pubkey = it->second.first;
118 pubkey = pk_it->second.first;
122 return provider.
GetPubKey(address, pubkey);
128 const auto it = sigdata.
signatures.find(keyid);
130 sig_out = it->second.second;
135 sigdata.
misc_pubkeys.emplace(keyid, std::make_pair(pubkey, std::move(info)));
137 if (creator.
CreateSig(provider, sig_out, keyid, scriptcode, sigversion)) {
149 auto lookup_key = std::make_pair(pubkey, leaf_hash);
152 sig_out = it->second;
154 if (creator.
CreateSchnorrSig(provider, sig_out, pubkey, &leaf_hash,
nullptr, sigversion)) {
170 if (script.
size() == 34 && script[33] ==
OP_CHECKSIG && script[0] == 0x20) {
172 std::vector<unsigned char> sig;
174 result =
Vector(std::move(sig));
193 std::vector<unsigned char> sig;
206 std::vector<std::vector<unsigned char>> smallest_result_stack;
208 const auto& [script, leaf_ver] = key;
209 std::vector<std::vector<unsigned char>> result_stack;
210 if (
SignTaprootScript(provider, creator, sigdata, leaf_ver, script, result_stack)) {
211 result_stack.emplace_back(std::begin(script), std::end(script));
212 result_stack.push_back(*control_blocks.begin());
213 if (smallest_result_stack.size() == 0 ||
215 smallest_result_stack = std::move(result_stack);
219 if (smallest_result_stack.size() != 0) {
220 result = std::move(smallest_result_stack);
239 std::vector<unsigned char> sig;
241 std::vector<valtype> vSolutions;
242 whichTypeRet =
Solver(scriptPubKey, vSolutions);
244 switch (whichTypeRet) {
250 if (!
CreateSig(creator, sigdata, provider, sig,
CPubKey(vSolutions[0]), scriptPubKey, sigversion))
return false;
251 ret.push_back(std::move(sig));
256 if (!
GetPubKey(provider, sigdata, keyID, pubkey)) {
261 if (!
CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion))
return false;
262 ret.push_back(std::move(sig));
269 ret.push_back(std::vector<unsigned char>(scriptRet.
begin(), scriptRet.
end()));
277 size_t required = vSolutions.front()[0];
279 for (
size_t i = 1; i < vSolutions.size() - 1; ++i) {
284 if (
CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) {
285 if (ret.size() < required + 1) {
286 ret.push_back(std::move(sig));
290 bool ok = ret.size() == required + 1;
291 for (
size_t i = 0; i + ret.size() < required + 1; ++i) {
297 ret.push_back(vSolutions[0]);
303 ret.push_back(std::vector<unsigned char>(scriptRet.
begin(), scriptRet.
end()));
322 }
else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
324 }
else if (v.size() == 1 && v[0] == 0x81) {
337 std::vector<valtype> result;
348 subscript =
CScript(result[0].begin(), result[0].end());
366 CScript witnessscript(result[0].begin(), result[0].end());
370 result.push_back(std::vector<unsigned char>(witnessscript.
begin(), witnessscript.
end()));
386 result.push_back(std::vector<unsigned char>(subscript.
begin(), subscript.
end()));
404 bool CheckECDSASignature(
const std::vector<unsigned char>& scriptSig,
const std::vector<unsigned char>& vchPubKey,
const CScript& scriptCode,
SigVersion sigversion)
const override
406 if (m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion)) {
417 std::vector<valtype> script;
418 std::vector<valtype> witness;
421 Stacks(
const Stacks&) =
delete;
422 explicit Stacks(
const SignatureData& data) : witness(data.scriptWitness.stack) {
439 SignatureExtractorChecker extractor_checker(data, tx_checker);
446 std::vector<std::vector<unsigned char>> solutions;
453 CScript redeem_script(stack.script.back().begin(), stack.script.back().end());
455 next_script = std::move(redeem_script);
458 script_type =
Solver(next_script, solutions);
459 stack.script.pop_back();
463 CScript witness_script(stack.witness.back().begin(), stack.witness.back().end());
465 next_script = std::move(witness_script);
468 script_type =
Solver(next_script, solutions);
469 stack.witness.pop_back();
470 stack.script = std::move(stack.witness);
471 stack.witness.clear();
476 assert(solutions.size() > 1);
477 unsigned int num_pubkeys = solutions.size()-2;
478 unsigned int last_success_key = 0;
479 for (
const valtype& sig : stack.script) {
480 for (
unsigned int i = last_success_key; i < num_pubkeys; ++i) {
481 const valtype& pubkey = solutions[i+1];
483 if (data.
signatures.count(
CPubKey(pubkey).GetID()) || extractor_checker.CheckECDSASignature(sig, pubkey, next_script, sigversion)) {
484 last_success_key = i + 1;
504 *
this = std::move(sigdata);
543 DummySignatureChecker() {}
544 bool CheckECDSASignature(
const std::vector<unsigned char>& scriptSig,
const std::vector<unsigned char>& vchPubKey,
const CScript& scriptCode,
SigVersion sigversion)
const override {
return true; }
547 const DummySignatureChecker DUMMY_CHECKER;
554 DummySignatureCreator(
char r_len,
char s_len) : m_r_len(r_len), m_s_len(s_len) {}
559 vchSig.assign(m_r_len + m_s_len + 7,
'\000');
561 vchSig[1] = m_r_len + m_s_len + 4;
565 vchSig[4 + m_r_len] = 0x02;
566 vchSig[5 + m_r_len] = m_s_len;
567 vchSig[6 + m_r_len] = 0x01;
573 sig.assign(64,
'\000');
608 std::vector<valtype> solutions;
609 auto whichtype =
Solver(script, solutions);
611 auto h160 =
uint160(solutions[0]);
630 std::vector<CTxOut> spent_outputs;
631 for (
unsigned int i = 0; i < mtx.
vin.size(); ++i) {
633 auto coin = coins.find(txin.
prevout);
634 if (coin == coins.end() || coin->second.IsSpent()) {
635 txdata.
Init(txConst, {},
true);
638 spent_outputs.emplace_back(coin->second.out.nValue, coin->second.out.scriptPubKey);
641 if (spent_outputs.size() == mtx.
vin.size()) {
642 txdata.
Init(txConst, std::move(spent_outputs),
true);
646 for (
unsigned int i = 0; i < mtx.
vin.size(); ++i) {
648 auto coin = coins.find(txin.
prevout);
649 if (coin == coins.end() || coin->second.IsSpent()) {
650 input_errors[i] =
_(
"Input not found or already spent");
653 const CScript& prevPubKey = coin->second.out.scriptPubKey;
654 const CAmount& amount = coin->second.out.nValue;
658 if (!fHashSingle || (i < mtx.
vout.size())) {
666 input_errors[i] =
_(
"Missing amount");
674 input_errors[i] =
Untranslated(
"Unable to sign input, invalid stack size (possibly missing key)");
677 input_errors[i] =
Untranslated(
"CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)");
683 input_errors.erase(i);
686 return input_errors.empty();
std::string ScriptErrorString(const ScriptError serror)
size_t GetSerializeSize(const T &t, int nVersion=0)
An input of a transaction.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
bool GetKeyByXOnly(const XOnlyPubKey &pubkey, CKey &key) const
bilingual_str _(const char *psz)
Translation function.
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
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.
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
A hasher class for RIPEMD-160.
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.
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
std::map< std::pair< CScript, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
@ WITNESS_UNKNOWN
Only for Witness versions not already defined above.
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
void MergeSignatureData(SignatureData sigdata)
void Finalize(unsigned char hash[OUTPUT_SIZE])
@ SCRIPT_ERR_SIG_NULLFAIL
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
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.
uint32_t m_codeseparator_pos
Opcode position of the last executed OP_CODESEPARATOR (or 0xFFFFFFFF if none executed).
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
An interface to be implemented by keystores that support signing.
A signature creator for transactions.
static bool SignTaprootScript(const SigningProvider &provider, const BaseSignatureCreator &creator, SignatureData &sigdata, int leaf_version, const CScript &script, std::vector< valtype > &result)
bool MoneyRange(const CAmount &nValue)
static opcodetype EncodeOP_N(int n)
bool m_annex_present
Whether an annex is present.
A reference to a CKey: the Hash160 of its serialized public key.
bool IsPayToScriptHash() const
@ FAIL
Just act as if the signature was invalid.
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
@ TAPROOT
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, key path spending; see BIP 341.
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
The basic transaction that is broadcasted on the network and contained in blocks.
virtual bool CheckSchnorrSignature(Span< const unsigned char > sig, Span< const unsigned char > pubkey, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror=nullptr) const
enum ScriptError_t ScriptError
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 CScript PushAll(const std::vector< valtype > &values)
std::vector< unsigned char > ToByteVector(const T &in)
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
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.
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
An output of a transaction.
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.
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
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)
const std::vector< CTxOut > vout
uint256 m_tapleaf_hash
The tapleaf hash.
bool m_annex_init
Whether m_annex_present and (when needed) m_annex_hash are initialized.
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
bool m_codeseparator_pos_init
Whether m_codeseparator_pos is initialized.
CScript redeem_script
The redeemScript (if any) for the input.
CRIPEMD160 & Write(const unsigned char *data, size_t len)
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format.
bool witness
Stores whether the input this SigData corresponds to is a witness input.
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
int64_t CAmount
Amount in satoshis (Can be negative)
bool m_spent_outputs_ready
Whether m_spent_outputs is initialized.
const PrecomputedTransactionData * m_txdata
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
@ SCRIPT_ERR_INVALID_STACK_OPERATION
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
std::vector< unsigned char > taproot_key_path_sig
virtual bool GetTaprootSpendData(const XOnlyPubKey &output_key, TaprootSpendData &spenddata) const
bool complete
Stores whether the scriptSig and scriptWitness are complete.
MutableTransactionSignatureCreator(const CMutableTransaction *tx, unsigned int input_idx, const CAmount &amount, int hash_type)
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
Serialized script, used inside transaction inputs and outputs.
@ SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
static bool SignTaproot(const SigningProvider &provider, const BaseSignatureCreator &creator, const WitnessV1Taproot &output, SignatureData &sigdata, std::vector< valtype > &result)
XOnlyPubKey internal_key
The BIP341 internal key.
@ NULL_DATA
unspendable OP_RETURN script that carries data
bool m_tapleaf_hash_init
Whether m_tapleaf_hash is initialized.
TaprootSpendData tr_spenddata
Taproot spending data.
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
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
const CMutableTransaction * txTo
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
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
std::vector< CTxOut > vout
An encapsulated public key.
const CHashWriter HASHER_TAPLEAF
Hasher with tag "TapLeaf" pre-fed to it.
An encapsulated private key.
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
bool IsSolvable(const SigningProvider &provider, const CScript &script)
bool SignSchnorr(const uint256 &hash, Span< unsigned char > sig, const uint256 *merkle_root, const uint256 &aux) const
Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this, optionally tweaked b...
static bool GetCScript(const SigningProvider &provider, const SignatureData &sigdata, const CScriptID &scriptid, CScript &script)
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
std::vector< typename std::common_type< Args... >::type > Vector(Args &&... args)
Construct a vector with the specified elements.
@ TAPSCRIPT
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, script path spending, leaf version 0xc0; see...
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 CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const override
A writer stream (for serialization) that computes a 256-bit hash.
static constexpr uint8_t TAPROOT_LEAF_TAPSCRIPT
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual const BaseSignatureChecker & Checker() const =0
std::vector< unsigned char > valtype
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
void UpdateInput(CTxIn &input, const SignatureData &data)
bool m_bip341_taproot_ready
Whether the 5 fields above are initialized.
uint256 missing_witness_script
SHA256 of the missing witnessScript (if any)
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
A mutable version of CTransaction.
bool SignatureHashSchnorr(uint256 &hash_out, ScriptExecutionData &execdata, const T &tx_to, uint32_t in_pos, uint8_t hash_type, SigVersion sigversion, const PrecomputedTransactionData &cache, MissingDataBehavior mdb)
Interface for signature creators.
std::map< std::pair< XOnlyPubKey, uint256 >, std::vector< unsigned char > > taproot_script_sigs
Schnorr signature for key path spending.
A reference to a CScript: the Hash160 of its serialization (see script.h)
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
std::vector< std::vector< unsigned char > > stack
std::vector< unsigned char > valtype
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
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)
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
@ SCRIPT_VERIFY_STRICTENC
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
static const int PROTOCOL_VERSION
network protocol versioning
@ SIGHASH_DEFAULT
Taproot only; implied when sighash byte is missing, and equivalent to SIGHASH_ALL.