Bitcoin Core  22.99.0
P2P Digital Currency
sign.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_SCRIPT_SIGN_H
7 #define BITCOIN_SCRIPT_SIGN_H
8 
9 #include <coins.h>
10 #include <hash.h>
11 #include <pubkey.h>
12 #include <script/interpreter.h>
13 #include <script/keyorigin.h>
14 #include <script/standard.h>
15 
16 class CKey;
17 class CKeyID;
18 class CScript;
19 class CTransaction;
20 class SigningProvider;
21 
22 struct bilingual_str;
23 struct CMutableTransaction;
24 
27 public:
28  virtual ~BaseSignatureCreator() {}
29  virtual const BaseSignatureChecker& Checker() const =0;
30 
32  virtual bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0;
33  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;
34 };
35 
39  unsigned int nIn;
40  int nHashType;
44 
45 public:
46  MutableTransactionSignatureCreator(const CMutableTransaction* tx, unsigned int input_idx, const CAmount& amount, int hash_type);
47  MutableTransactionSignatureCreator(const CMutableTransaction* tx, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, int hash_type);
48  const BaseSignatureChecker& Checker() const override { return checker; }
49  bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
50  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;
51 };
52 
57 
58 typedef std::pair<CPubKey, std::vector<unsigned char>> SigPair;
59 
60 // This struct contains information from a transaction input and also contains signatures for that input.
61 // The information contained here can be used to create a signature and is also filled by ProduceSignature
62 // in order to construct final scriptSigs and scriptWitnesses.
63 struct SignatureData {
64  bool complete = false;
65  bool witness = false;
71  std::map<CKeyID, SigPair> signatures;
72  std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys;
73  std::vector<unsigned char> taproot_key_path_sig;
74  std::map<std::pair<XOnlyPubKey, uint256>, std::vector<unsigned char>> taproot_script_sigs;
75  std::vector<CKeyID> missing_pubkeys;
76  std::vector<CKeyID> missing_sigs;
79 
81  explicit SignatureData(const CScript& script) : scriptSig(script) {}
82  void MergeSignatureData(SignatureData sigdata);
83 };
84 
86 bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
87 
89 bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType);
90 bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType);
91 
93 SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout);
94 void UpdateInput(CTxIn& input, const SignatureData& data);
95 
96 /* Check whether we know how to sign for an output like this, assuming we
97  * have all private keys. While this function does not need private keys, the passed
98  * provider is used to look up public keys and redeemscripts by hash.
99  * Solvability is unrelated to whether we consider this output to be ours. */
100 bool IsSolvable(const SigningProvider& provider, const CScript& script);
101 
103 bool IsSegWitOutput(const SigningProvider& provider, const CScript& script);
104 
106 bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* provider, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors);
107 
108 #endif // BITCOIN_SCRIPT_SIGN_H
SigVersion
SigVersion
Definition: interpreter.h:187
CTxIn
An input of a transaction.
Definition: transaction.h:65
SignatureData::missing_redeem_script
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
Definition: sign.h:77
BaseSignatureCreator::CreateSig
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.
SignatureData::scriptWitness
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
Definition: sign.h:69
SignatureData::MergeSignatureData
void MergeSignatureData(SignatureData sigdata)
Definition: sign.cpp:500
bilingual_str
Bilingual messages:
Definition: translation.h:16
GenericTransactionSignatureChecker
Definition: interpreter.h:278
IsSolvable
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:583
SigningProvider
An interface to be implemented by keystores that support signing.
Definition: signingprovider.h:17
DUMMY_MAXIMUM_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_MAXIMUM_SIGNATURE_CREATOR
A signature creator that just produces 72-byte empty signatures.
Definition: sign.cpp:581
MutableTransactionSignatureCreator
A signature creator for transactions.
Definition: sign.h:37
interpreter.h
MutableTransactionSignatureCreator::nIn
unsigned int nIn
Definition: sign.h:39
BaseSignatureChecker
Definition: interpreter.h:239
XOnlyPubKey
Definition: pubkey.h:224
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
SignatureData::SignatureData
SignatureData()
Definition: sign.h:80
pubkey.h
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
CScriptWitness
Definition: script.h:557
SigPair
std::pair< CPubKey, std::vector< unsigned char > > SigPair
Definition: sign.h:58
MutableTransactionSignatureCreator::CreateSig
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.
Definition: sign.cpp:35
SignatureData
Definition: sign.h:63
MutableTransactionSignatureCreator::amount
CAmount amount
Definition: sign.h:41
CTxOut
An output of a transaction.
Definition: transaction.h:128
SignTransaction
bool SignTransaction(CMutableTransaction &mtx, const SigningProvider *provider, const std::map< COutPoint, Coin > &coins, int sighash, std::map< int, bilingual_str > &input_errors)
Sign the CMutableTransaction.
Definition: sign.cpp:621
TaprootSpendData
Definition: standard.h:207
SignSignature
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.
Definition: sign.cpp:516
SignatureData::redeem_script
CScript redeem_script
The redeemScript (if any) for the input.
Definition: sign.h:67
SignatureData::scriptSig
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format.
Definition: sign.h:66
SignatureData::witness
bool witness
Stores whether the input this SigData corresponds to is a witness input.
Definition: sign.h:65
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
standard.h
MutableTransactionSignatureCreator::m_txdata
const PrecomputedTransactionData * m_txdata
Definition: sign.h:43
SignatureData::signatures
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
Definition: sign.h:71
SignatureData::missing_sigs
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
Definition: sign.h:76
SignatureData::witness_script
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
Definition: sign.h:68
PrecomputedTransactionData
Definition: interpreter.h:150
SignatureData::taproot_key_path_sig
std::vector< unsigned char > taproot_key_path_sig
Definition: sign.h:73
SignatureData::complete
bool complete
Stores whether the scriptSig and scriptWitness are complete.
Definition: sign.h:64
uint256
256-bit opaque blob.
Definition: uint256.h:124
MutableTransactionSignatureCreator::MutableTransactionSignatureCreator
MutableTransactionSignatureCreator(const CMutableTransaction *tx, unsigned int input_idx, const CAmount &amount, int hash_type)
Definition: sign.cpp:21
MutableTransactionSignatureCreator::nHashType
int nHashType
Definition: sign.h:40
DUMMY_SIGNATURE_CREATOR
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
Definition: sign.cpp:580
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
coins.h
SignatureData::tr_spenddata
TaprootSpendData tr_spenddata
Taproot spending data.
Definition: sign.h:70
BaseSignatureCreator::CreateSchnorrSig
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
MutableTransactionSignatureCreator::txTo
const CMutableTransaction * txTo
Definition: sign.h:38
SignatureData::missing_pubkeys
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
Definition: sign.h:75
MutableTransactionSignatureCreator::CreateSchnorrSig
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
Definition: sign.cpp:60
UpdateInput
void UpdateInput(CTxIn &input, const SignatureData &data)
Definition: sign.cpp:494
BaseSignatureCreator::~BaseSignatureCreator
virtual ~BaseSignatureCreator()
Definition: sign.h:28
uint160
160-bit opaque blob.
Definition: uint256.h:113
CKey
An encapsulated private key.
Definition: key.h:26
SignatureData::misc_pubkeys
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
Definition: sign.h:72
IsSegWitOutput
bool IsSegWitOutput(const SigningProvider &provider, const CScript &script)
Check whether a scriptPubKey is known to be segwit.
Definition: sign.cpp:602
ProduceSignature
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &scriptPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
Definition: sign.cpp:333
BaseSignatureCreator::Checker
virtual const BaseSignatureChecker & Checker() const =0
MutableTransactionSignatureCreator::Checker
const BaseSignatureChecker & Checker() const override
Definition: sign.h:48
SignatureData::SignatureData
SignatureData(const CScript &script)
Definition: sign.h:81
DataFromTransaction
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
Definition: sign.cpp:429
SignatureData::missing_witness_script
uint256 missing_witness_script
SHA256 of the missing witnessScript (if any)
Definition: sign.h:78
keyorigin.h
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
BaseSignatureCreator
Interface for signature creators.
Definition: sign.h:26
SignatureData::taproot_script_sigs
std::map< std::pair< XOnlyPubKey, uint256 >, std::vector< unsigned char > > taproot_script_sigs
Schnorr signature for key path spending.
Definition: sign.h:74
MutableTransactionSignatureCreator::checker
const MutableTransactionSignatureChecker checker
Definition: sign.h:42