15 typedef std::vector<unsigned char>
valtype;
22 if (!provider.
GetKey(address, key))
30 if (!key.
Sign(hash, vchSig))
32 vchSig.push_back((
unsigned char)
nHashType);
57 pubkey =
it->second.first;
63 pubkey = pk_it->second.first;
67 return provider.
GetPubKey(address, pubkey);
75 sig_out =
it->second.second;
80 sigdata.
misc_pubkeys.emplace(keyid, std::make_pair(pubkey, std::move(info)));
82 if (creator.
CreateSig(provider, sig_out, keyid, scriptcode, sigversion)) {
104 std::vector<unsigned char> sig;
106 std::vector<valtype> vSolutions;
107 whichTypeRet =
Solver(scriptPubKey, vSolutions);
109 switch (whichTypeRet)
117 if (!
CreateSig(creator, sigdata, provider, sig,
CPubKey(vSolutions[0]), scriptPubKey, sigversion))
return false;
118 ret.push_back(std::move(sig));
123 if (!
GetPubKey(provider, sigdata, keyID, pubkey)) {
128 if (!
CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion))
return false;
129 ret.push_back(std::move(sig));
136 ret.push_back(std::vector<unsigned char>(scriptRet.
begin(), scriptRet.
end()));
144 size_t required = vSolutions.front()[0];
146 for (
size_t i = 1; i < vSolutions.size() - 1; ++i) {
151 if (
CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) {
152 if (ret.size() < required + 1) {
153 ret.push_back(std::move(sig));
157 bool ok = ret.size() == required + 1;
158 for (
size_t i = 0; i + ret.size() < required + 1; ++i) {
164 ret.push_back(vSolutions[0]);
170 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 277 std::vector<valtype> script;
278 std::vector<valtype> witness;
281 Stacks(
const Stacks&) =
delete;
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;
362 if (complete)
return;
364 *
this = std::move(sigdata);
393 const CTxOut& txout = txFrom.
vout[txin.prevout.n];
395 return SignSignature(provider, txout.scriptPubKey, txTo, nIn, txout.nValue, nHashType);
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;
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();
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Witness v0 (P2WPKH and P2WSH); see BIP 141.
std::deque< CInv >::iterator it
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.
enum ScriptError_t ScriptError
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
iterator insert(iterator pos, const T &value)
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.
const MutableTransactionSignatureChecker checker
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format...
CScriptWitness scriptWitness
Only serialized through CTransaction.
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
Interface for signature creators.
static CScript PushAll(const std::vector< valtype > &values)
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
const BaseSignatureChecker & Checker() const override
std::vector< std::vector< unsigned char > > stack
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
A signature creator for transactions.
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.
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
unspendable OP_RETURN script that carries data
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
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 SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType)
Produce a script signature for a transaction.
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.
int64_t CAmount
Amount in satoshis (Can be negative)
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
An input of a transaction.
virtual bool GetKeyOrigin(const CKeyID &keyid, KeyOriginInfo &info) const
An encapsulated public key.
std::string ScriptErrorString(const ScriptError serror)
std::pair< CPubKey, std::vector< unsigned char > > SigPair
const std::vector< CTxOut > vout
static bool GetCScript(const SigningProvider &provider, const SignatureData &sigdata, const CScriptID &scriptid, CScript &script)
An output of a transaction.
void MergeSignatureData(SignatureData sigdata)
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
std::vector< CTxOut > vout
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
bool IsSolvable(const SigningProvider &provider, const CScript &script)
CRIPEMD160 & Write(const unsigned char *data, size_t len)
An interface to be implemented by keystores that support signing.
static opcodetype EncodeOP_N(int n)
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Serialized script, used inside transaction inputs and outputs.
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
A reference to a CKey: the Hash160 of its serialized public key.
void UpdateInput(CTxIn &input, const SignatureData &data)
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
Only for Witness versions not already defined above.
std::vector< unsigned char > valtype
std::vector< unsigned char > valtype
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
A reference to a CScript: the Hash160 of its serialization (see script.h)
A mutable version of CTransaction.
An encapsulated private key.
The basic transaction that is broadcasted on the network and contained in blocks. ...
MutableTransactionSignatureCreator(const CMutableTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn, int nHashTypeIn=SIGHASH_ALL)
void Finalize(unsigned char hash[OUTPUT_SIZE])
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 complete
Stores whether the scriptSig and scriptWitness are complete.
CScript redeem_script
The redeemScript (if any) for the input.
bool witness
Stores whether the input this SigData corresponds to is a witness input.
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
A hasher class for RIPEMD-160.
virtual const BaseSignatureChecker & Checker() const =0
const CMutableTransaction * txTo
std::vector< unsigned char > ToByteVector(const T &in)