 |
Bitcoin Core
21.99.0
P2P Digital Currency
|
Go to the documentation of this file.
15 typedef std::vector<unsigned char>
valtype;
17 MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(
const CMutableTransaction* txToIn,
unsigned int nInIn,
const CAmount& amountIn,
int nHashTypeIn) : txTo(txToIn), nIn(nInIn), nHashType(nHashTypeIn), amount(amountIn), checker(txTo, nIn, amountIn,
MissingDataBehavior::
FAIL) {}
22 if (!provider.
GetKey(address, key))
33 if (!key.
Sign(hash, vchSig))
35 vchSig.push_back((
unsigned char)
nHashType);
60 pubkey =
it->second.first;
66 pubkey = pk_it->second.first;
70 return provider.
GetPubKey(address, pubkey);
78 sig_out =
it->second.second;
83 sigdata.
misc_pubkeys.emplace(keyid, std::make_pair(pubkey, std::move(info)));
85 if (creator.
CreateSig(provider, sig_out, keyid, scriptcode, sigversion)) {
107 std::vector<unsigned char> sig;
109 std::vector<valtype> vSolutions;
110 whichTypeRet =
Solver(scriptPubKey, vSolutions);
112 switch (whichTypeRet) {
119 if (!
CreateSig(creator, sigdata, provider, sig,
CPubKey(vSolutions[0]), scriptPubKey, sigversion))
return false;
120 ret.push_back(std::move(sig));
125 if (!
GetPubKey(provider, sigdata, keyID, pubkey)) {
130 if (!
CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion))
return false;
131 ret.push_back(std::move(sig));
138 ret.push_back(std::vector<unsigned char>(scriptRet.
begin(), scriptRet.
end()));
146 size_t required = vSolutions.front()[0];
148 for (
size_t i = 1; i < vSolutions.size() - 1; ++i) {
153 if (
CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) {
154 if (ret.size() < required + 1) {
155 ret.push_back(std::move(sig));
159 bool ok = ret.size() == required + 1;
160 for (
size_t i = 0; i + ret.size() < required + 1; ++i) {
166 ret.push_back(vSolutions[0]);
172 ret.push_back(std::vector<unsigned char>(scriptRet.
begin(), scriptRet.
end()));
185 for (
const valtype& v : values) {
188 }
else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
190 }
else if (v.size() == 1 && v[0] == 0x81) {
203 std::vector<valtype> result;
215 subscript =
CScript(result[0].begin(), result[0].end());
233 CScript witnessscript(result[0].begin(), result[0].end());
237 result.push_back(std::vector<unsigned char>(witnessscript.
begin(), witnessscript.
end()));
246 result.push_back(std::vector<unsigned char>(subscript.
begin(), subscript.
end()));
264 bool CheckECDSASignature(
const std::vector<unsigned char>& scriptSig,
const std::vector<unsigned char>& vchPubKey,
const CScript& scriptCode,
SigVersion sigversion)
const override
266 if (m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion)) {
277 std::vector<valtype> script;
278 std::vector<valtype> witness;
281 Stacks(
const Stacks&) =
delete;
282 explicit Stacks(
const SignatureData& data) : witness(data.scriptWitness.stack) {
299 SignatureExtractorChecker extractor_checker(data, tx_checker);
306 std::vector<std::vector<unsigned char>> solutions;
313 CScript redeem_script(stack.script.back().begin(), stack.script.back().end());
315 next_script = std::move(redeem_script);
318 script_type =
Solver(next_script, solutions);
319 stack.script.pop_back();
323 CScript witness_script(stack.witness.back().begin(), stack.witness.back().end());
325 next_script = std::move(witness_script);
328 script_type =
Solver(next_script, solutions);
329 stack.witness.pop_back();
330 stack.script = std::move(stack.witness);
331 stack.witness.clear();
336 assert(solutions.size() > 1);
337 unsigned int num_pubkeys = solutions.size()-2;
338 unsigned int last_success_key = 0;
339 for (
const valtype& sig : stack.script) {
340 for (
unsigned int i = last_success_key; i < num_pubkeys; ++i) {
341 const valtype& pubkey = solutions[i+1];
343 if (data.
signatures.count(
CPubKey(pubkey).GetID()) || extractor_checker.CheckECDSASignature(sig, pubkey, next_script, sigversion)) {
344 last_success_key = i + 1;
364 *
this = std::move(sigdata);
403 DummySignatureChecker() {}
404 bool CheckECDSASignature(
const std::vector<unsigned char>& scriptSig,
const std::vector<unsigned char>& vchPubKey,
const CScript& scriptCode,
SigVersion sigversion)
const override {
return true; }
406 const DummySignatureChecker DUMMY_CHECKER;
413 DummySignatureCreator(
char r_len,
char s_len) : m_r_len(r_len), m_s_len(s_len) {}
418 vchSig.assign(m_r_len + m_s_len + 7,
'\000');
420 vchSig[1] = m_r_len + m_s_len + 4;
424 vchSig[4 + m_r_len] = 0x02;
425 vchSig[5 + m_r_len] = m_s_len;
426 vchSig[6 + m_r_len] = 0x01;
458 std::vector<valtype> solutions;
459 auto whichtype =
Solver(script, solutions);
462 auto h160 =
uint160(solutions[0]);
465 whichtype =
Solver(subscript, solutions);
480 for (
unsigned int i = 0; i < mtx.
vin.size(); i++) {
482 auto coin = coins.find(txin.
prevout);
483 if (coin == coins.end() || coin->second.IsSpent()) {
484 input_errors[i] =
"Input not found or already spent";
487 const CScript& prevPubKey = coin->second.out.scriptPubKey;
488 const CAmount& amount = coin->second.out.nValue;
492 if (!fHashSingle || (i < mtx.
vout.size())) {
500 input_errors[i] =
"Missing amount";
508 input_errors[i] =
"Unable to sign input, invalid stack size (possibly missing key)";
511 input_errors[i] =
"CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)";
517 input_errors.erase(i);
520 return input_errors.empty();
std::string ScriptErrorString(const ScriptError serror)
An input of a transaction.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
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.
@ SCRIPT_VERIFY_STRICTENC
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
MissingDataBehavior
Enum to specify what *TransactionSignatureChecker's behavior should be when dealing with missing tran...
@ 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)
@ SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
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.
std::vector< unsigned char > ToByteVector(const T &in)
An interface to be implemented by keystores that support signing.
A signature creator for transactions.
static opcodetype EncodeOP_N(int n)
A reference to a CKey: the Hash160 of its serialized public key.
@ FAIL
Just act as if the signature was invalid.
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
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.
enum ScriptError_t ScriptError
CScriptWitness scriptWitness
Only serialized through CTransaction.
std::pair< CPubKey, std::vector< unsigned char > > SigPair
static CScript PushAll(const std::vector< valtype > &values)
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
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 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
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
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.
int64_t CAmount
Amount in satoshis (Can be negative)
@ 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.
bool complete
Stores whether the scriptSig and scriptWitness are complete.
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
Serialized script, used inside transaction inputs and outputs.
@ NULL_DATA
unspendable OP_RETURN script that carries data
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.
std::vector< CTxOut > vout
An encapsulated public key.
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)
static bool GetCScript(const SigningProvider &provider, const SignatureData &sigdata, const CScriptID &scriptid, CScript &script)
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
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
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
virtual bool GetKey(const CKeyID &address, CKey &key) const
virtual const BaseSignatureChecker & Checker() const =0
std::vector< unsigned char > valtype
void UpdateInput(CTxIn &input, const SignatureData &data)
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.
MutableTransactionSignatureCreator(const CMutableTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn, int nHashTypeIn=SIGHASH_ALL)
Interface for signature creators.
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)
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
std::vector< std::vector< unsigned char > > stack
std::vector< unsigned char > valtype
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, int nHashType, std::map< int, std::string > &input_errors)
Sign the CMutableTransaction.