Line data Source code
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 <attributes.h> 10 : #include <coins.h> 11 : #include <hash.h> 12 : #include <pubkey.h> 13 : #include <script/interpreter.h> 14 : #include <script/keyorigin.h> 15 : 16 : class CKey; 17 : class CKeyID; 18 : class CScript; 19 : class CScriptID; 20 : class CTransaction; 21 : class SigningProvider; 22 : 23 : struct bilingual_str; 24 : struct CMutableTransaction; 25 : 26 : /** Interface for signature creators. */ 27 : class BaseSignatureCreator { 28 : public: 29 1001 : virtual ~BaseSignatureCreator() {} 30 : virtual const BaseSignatureChecker& Checker() const =0; 31 : 32 : /** Create a singular (non-script) signature. */ 33 : virtual bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0; 34 : }; 35 : 36 : /** A signature creator for transactions. */ 37 : class MutableTransactionSignatureCreator : public BaseSignatureCreator 38 : { 39 : const CMutableTransaction& m_txto; 40 : unsigned int nIn; 41 : int nHashType; 42 : CAmount amount; 43 : const MutableTransactionSignatureChecker checker; 44 : const PrecomputedTransactionData* m_txdata; 45 : 46 : public: 47 : MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amountIn, int nHashTypeIn = SIGHASH_ALL); 48 : MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amountIn, const PrecomputedTransactionData* txdata, int nHashTypeIn = SIGHASH_ALL); 49 966 : const BaseSignatureChecker& Checker() const override { return checker; } 50 : bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override; 51 : }; 52 : 53 : /** A signature checker that accepts all signatures */ 54 : extern const BaseSignatureChecker& DUMMY_CHECKER; 55 : /** A signature creator that just produces 71-byte empty signatures. */ 56 : extern const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR; 57 : /** A signature creator that just produces 72-byte empty signatures. */ 58 : extern const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR; 59 : 60 : typedef std::pair<CPubKey, std::vector<unsigned char>> SigPair; 61 : 62 : // This struct contains information from a transaction input and also contains signatures for that input. 63 : // The information contained here can be used to create a signature and is also filled by ProduceSignature 64 : // in order to construct final scriptSigs and scriptWitnesses. 65 35 : struct SignatureData { 66 119167 : bool complete = false; ///< Stores whether the scriptSig is complete 67 : CScript scriptSig; ///< The scriptSig of an input. Contains complete signatures or the traditional partial signatures format 68 : CScript redeem_script; ///< The redeemScript (if any) for the input 69 : std::map<CKeyID, SigPair> signatures; ///< BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a final scriptSig. 70 : std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys; 71 : std::vector<CKeyID> missing_pubkeys; ///< KeyIDs of pubkeys which could not be found 72 : std::vector<CKeyID> missing_sigs; ///< KeyIDs of pubkeys for signatures which could not be found 73 : uint160 missing_redeem_script; ///< ScriptID of the missing redeemScript (if any) 74 : 75 357501 : SignatureData() {} 76 : explicit SignatureData(const CScript& script) : scriptSig(script) {} 77 : void MergeSignatureData(SignatureData sigdata); 78 : }; 79 : 80 : /** Produce a script signature using a generic signature creator. */ 81 : bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata); 82 : 83 : /** Produce a script signature for a transaction. */ 84 : bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType); 85 : bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType); 86 : 87 : /** Extract signature data from a transaction input, and insert it. */ 88 : SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout); 89 : void UpdateInput(CTxIn& input, const SignatureData& data); 90 : 91 : /* Check whether we know how to sign for an output like this, assuming we 92 : * have all private keys. While this function does not need private keys, the passed 93 : * provider is used to look up public keys and redeemscripts by hash. 94 : * Solvability is unrelated to whether we consider this output to be ours. */ 95 : bool IsSolvable(const SigningProvider& provider, const CScript& script); 96 : 97 : /** Sign the CMutableTransaction */ 98 : bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* provider, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors); 99 : 100 : #endif // BITCOIN_SCRIPT_SIGN_H