22 : m_txto(tx), nIn(input_idx), amount(amount), sigHashType(sig_hash_type),
23 checker(&m_txto, nIn, amount) {}
29 if (!provider.
GetKey(address, key)) {
69 const auto it = sigdata.
signatures.find(address);
71 pubkey = it->second.first;
77 pubkey = pk_it->second.first;
81 return provider.
GetPubKey(address, pubkey);
86 std::vector<uint8_t> &sig_out,
const CPubKey &pubkey,
89 const auto it = sigdata.
signatures.find(keyid);
91 sig_out = it->second.second;
97 std::make_pair(pubkey, std::move(info)));
99 if (creator.
CreateSig(provider, sig_out, keyid, scriptcode)) {
118 const CScript &scriptPubKey, std::vector<valtype> &ret,
123 std::vector<uint8_t>
sig;
125 std::vector<valtype> vSolutions;
126 whichTypeRet =
Solver(scriptPubKey, vSolutions);
128 switch (whichTypeRet) {
134 CPubKey(vSolutions[0]), scriptPubKey)) {
137 ret.push_back(std::move(
sig));
142 if (!
GetPubKey(provider, sigdata, keyID, pubkey)) {
151 ret.push_back(std::move(
sig));
159 std::vector<uint8_t>(scriptRet.
begin(), scriptRet.
end()));
166 size_t required = vSolutions.front()[0];
169 for (
size_t i = 1; i < vSolutions.size() - 1; ++i) {
177 if (ret.size() < required + 1) {
178 ret.push_back(std::move(
sig));
182 bool ok = ret.size() == required + 1;
183 for (
size_t i = 0; i + ret.size() < required + 1; ++i) {
195 for (
const valtype &v : values) {
198 }
else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
200 }
else if (v.size() == 1 && v[0] == 0x81) {
217 std::vector<valtype> result;
220 SignStep(provider, creator, fromPubKey, result, whichType, sigdata);
227 subscript =
CScript(result[0].begin(), result[0].end());
231 SignStep(provider, creator, subscript, result, whichType,
235 std::vector<uint8_t>(subscript.
begin(), subscript.
end()));
256 : sigdata(sigdata_), checker(checker_) {}
257 bool CheckSig(
const std::vector<uint8_t> &scriptSig,
258 const std::vector<uint8_t> &vchPubKey,
259 const CScript &scriptCode, uint32_t
flags)
const override {
260 if (checker.
CheckSig(scriptSig, vchPubKey, scriptCode,
flags)) {
272 std::vector<valtype>
script;
275 Stacks(
const Stacks &) =
delete;
288 unsigned int nIn,
const CTxOut &txout) {
296 SignatureExtractorChecker extractor_checker(data, tx_checker);
304 std::vector<std::vector<uint8_t>> solutions;
309 !stack.script.back().empty()) {
311 CScript redeem_script(stack.script.back().begin(),
312 stack.script.back().end());
314 next_script = std::move(redeem_script);
317 script_type =
Solver(next_script, solutions);
318 stack.script.pop_back();
322 assert(solutions.size() > 1);
323 unsigned int num_pubkeys = solutions.size() - 2;
324 unsigned int last_success_key = 0;
326 for (
unsigned int i = last_success_key; i < num_pubkeys; ++i) {
327 const valtype &pubkey = solutions[i + 1];
331 extractor_checker.CheckSig(
sig, pubkey, next_script,
333 last_success_key = i + 1;
352 *
this = std::move(sigdata);
359 std::make_move_iterator(sigdata.
signatures.end()));
391 DummySignatureChecker() =
default;
392 bool CheckSig(
const std::vector<uint8_t> &scriptSig,
393 const std::vector<uint8_t> &vchPubKey,
394 const CScript &scriptCode, uint32_t
flags)
const override {
398const DummySignatureChecker DUMMY_CHECKER;
406 DummySignatureCreator(
char r_len,
char s_len)
407 : m_r_len(r_len), m_s_len(s_len) {}
409 return DUMMY_CHECKER;
412 std::vector<uint8_t> &vchSig,
const CKeyID &keyid,
413 const CScript &scriptCode)
const override {
415 vchSig.assign(m_r_len + m_s_len + 7,
'\000');
417 vchSig[1] = m_r_len + m_s_len + 4;
421 vchSig[4 + m_r_len] = 0x02;
422 vchSig[5 + m_r_len] = m_s_len;
423 vchSig[6 + m_r_len] = 0x01;
432 DummySignatureCreator(32, 32);
434 DummySignatureCreator(33, 32);
454 const std::map<COutPoint, Coin> &coins,
456 std::map<int, std::string> &input_errors) {
461 for (
size_t i = 0; i < mtx.
vin.size(); i++) {
463 auto coin = coins.find(txin.
prevout);
464 if (coin == coins.end() || coin->second.IsSpent()) {
465 input_errors[i] =
"Input not found or already spent";
468 const CScript &prevPubKey = coin->second.GetTxOut().scriptPubKey;
469 const Amount amount = coin->second.GetTxOut().nValue;
476 prevPubKey, sigdata);
482 input_errors[i] =
"Missing amount";
493 input_errors[i] =
"Unable to sign input, invalid stack size "
494 "(possibly missing key)";
500 input_errors.erase(i);
503 return input_errors.empty();
std::vector< uint8_t > valtype
static constexpr Amount MAX_MONEY
No amount larger than this (in satoshi) is valid.
virtual bool CheckSig(const std::vector< uint8_t > &vchSigIn, const std::vector< uint8_t > &vchPubKey, const CScript &scriptCode, uint32_t flags) const
Interface for signature creators.
virtual bool CreateSig(const SigningProvider &provider, std::vector< uint8_t > &vchSig, const CKeyID &keyid, const CScript &scriptCode) const =0
Create a singular (non-script) signature.
virtual const BaseSignatureChecker & Checker() const =0
An encapsulated secp256k1 private key.
bool SignECDSA(const uint256 &hash, std::vector< uint8_t > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized ECDSA signature.
A reference to a CKey: the Hash160 of its serialized public key.
A mutable version of CTransaction.
std::vector< CTxOut > vout
An encapsulated public key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Serialized script, used inside transaction inputs and outputs.
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
static opcodetype EncodeOP_N(int n)
A reference to a CScript: the Hash160 of its serialization.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
An input of a transaction.
An output of a transaction.
A signature creator for transactions.
MutableTransactionSignatureCreator(const CMutableTransaction &tx LIFETIMEBOUND, unsigned int input_idx, const Amount &amount, SigHashType sig_hash_type=SigHashType())
bool CreateSig(const SigningProvider &provider, std::vector< uint8_t > &vchSig, const CKeyID &keyid, const CScript &scriptCode) const override
Create a singular (non-script) signature.
const CMutableTransaction & m_txto
Signature hash type wrapper class.
uint32_t getRawSigHashType() const
BaseSigHashType getBaseType() const
An interface to be implemented by keystores that support signing.
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) 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
bool EvalScript(std::vector< valtype > &stack, const CScript &script, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics, ScriptError *serror)
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, SigHashType sigHashType, const Amount amount, const PrecomputedTransactionData *cache, uint32_t flags, SigHashCache *sighash_cache)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metricsOut, ScriptError *serror)
Execute an unlocking and locking script together.
static constexpr uint32_t STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
std::vector< uint8_t > ToByteVector(const T &in)
std::string ScriptErrorString(const ScriptError serror)
@ INVALID_STACK_OPERATION
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
std::vector< uint8_t > valtype
void UpdateInput(CTxIn &input, const SignatureData &data)
static bool CreateSig(const BaseSignatureCreator &creator, SignatureData &sigdata, const SigningProvider &provider, std::vector< uint8_t > &sig_out, const CPubKey &pubkey, const CScript &scriptcode)
static bool SignStep(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &scriptPubKey, std::vector< valtype > &ret, TxoutType &whichTypeRet, SignatureData &sigdata)
Sign scriptPubKey using signature made with creator.
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Check whether we know how to sign for an output like this, assuming we have all private keys.
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)
bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *keystore, const std::map< COutPoint, Coin > &coins, SigHashType sigHashType, std::map< int, std::string > &input_errors)
Sign the CMutableTransaction.
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
static CScript PushAll(const std::vector< valtype > &values)
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const Amount amount, SigHashType sigHashType)
Produce a script signature for a transaction.
static bool GetCScript(const SigningProvider &provider, const SignatureData &sigdata, const CScriptID &scriptid, CScript &script)
std::pair< CPubKey, std::vector< uint8_t > > SigPair
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< uint8_t > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
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.
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
CScript scriptSig
The scriptSig of an input.
CScript redeem_script
The redeemScript (if any) for the input.
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
bool complete
Stores whether the scriptSig are complete.