Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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 : : #include <script/signingprovider.h>
16 : : #include <uint256.h>
17 : :
18 : : class CKey;
19 : : class CKeyID;
20 : : class CScript;
21 : : class CTransaction;
22 : : class SigningProvider;
23 : :
24 : : struct bilingual_str;
25 : : struct CMutableTransaction;
26 : :
27 : : /** Interface for signature creators. */
28 : 0 : class BaseSignatureCreator {
29 : : public:
30 : 4 : virtual ~BaseSignatureCreator() {}
31 : : virtual const BaseSignatureChecker& Checker() const =0;
32 : :
33 : : /** Create a singular (non-script) signature. */
34 : : virtual bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0;
35 : : 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;
36 : : };
37 : :
38 : : /** A signature creator for transactions. */
39 : 0 : class MutableTransactionSignatureCreator : public BaseSignatureCreator
40 : : {
41 : : const CMutableTransaction& m_txto;
42 : : unsigned int nIn;
43 : : int nHashType;
44 : : CAmount amount;
45 : : const MutableTransactionSignatureChecker checker;
46 : : const PrecomputedTransactionData* m_txdata;
47 : :
48 : : public:
49 : : MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, int hash_type);
50 : : MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, int hash_type);
51 : 0 : const BaseSignatureChecker& Checker() const override { return checker; }
52 : : bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
53 : : 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;
54 : : };
55 : :
56 : : /** A signature checker that accepts all signatures */
57 : : extern const BaseSignatureChecker& DUMMY_CHECKER;
58 : : /** A signature creator that just produces 71-byte empty signatures. */
59 : : extern const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR;
60 : : /** A signature creator that just produces 72-byte empty signatures. */
61 : : extern const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR;
62 : :
63 : : typedef std::pair<CPubKey, std::vector<unsigned char>> SigPair;
64 : :
65 : : // This struct contains information from a transaction input and also contains signatures for that input.
66 : : // The information contained here can be used to create a signature and is also filled by ProduceSignature
67 : : // in order to construct final scriptSigs and scriptWitnesses.
68 [ # # ][ # # ]: 0 : struct SignatureData {
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
69 : 0 : bool complete = false; ///< Stores whether the scriptSig and scriptWitness are complete
70 : 0 : bool witness = false; ///< Stores whether the input this SigData corresponds to is a witness input
71 : : CScript scriptSig; ///< The scriptSig of an input. Contains complete signatures or the traditional partial signatures format
72 : : CScript redeem_script; ///< The redeemScript (if any) for the input
73 : : CScript witness_script; ///< The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
74 : : CScriptWitness scriptWitness; ///< The scriptWitness of an input. Contains complete signatures or the traditional partial signatures format. scriptWitness is part of a transaction input per BIP 144.
75 : : TaprootSpendData tr_spenddata; ///< Taproot spending data.
76 : : std::optional<TaprootBuilder> tr_builder; ///< Taproot tree used to build tr_spenddata.
77 : : std::map<CKeyID, SigPair> signatures; ///< BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a final scriptSig or scriptWitness.
78 : : std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys;
79 : : std::vector<unsigned char> taproot_key_path_sig; /// Schnorr signature for key path spending
80 : : std::map<std::pair<XOnlyPubKey, uint256>, std::vector<unsigned char>> taproot_script_sigs; ///< (Partial) schnorr signatures, indexed by XOnlyPubKey and leaf_hash.
81 : : std::map<XOnlyPubKey, std::pair<std::set<uint256>, KeyOriginInfo>> taproot_misc_pubkeys; ///< Miscellaneous Taproot pubkeys involved in this input along with their leaf script hashes and key origin data. Also includes the Taproot internal key (may have no leaf script hashes).
82 : : std::vector<CKeyID> missing_pubkeys; ///< KeyIDs of pubkeys which could not be found
83 : : std::vector<CKeyID> missing_sigs; ///< KeyIDs of pubkeys for signatures which could not be found
84 : : uint160 missing_redeem_script; ///< ScriptID of the missing redeemScript (if any)
85 : : uint256 missing_witness_script; ///< SHA256 of the missing witnessScript (if any)
86 : : std::map<std::vector<uint8_t>, std::vector<uint8_t>> sha256_preimages; ///< Mapping from a SHA256 hash to its preimage provided to solve a Script
87 : : std::map<std::vector<uint8_t>, std::vector<uint8_t>> hash256_preimages; ///< Mapping from a HASH256 hash to its preimage provided to solve a Script
88 : : std::map<std::vector<uint8_t>, std::vector<uint8_t>> ripemd160_preimages; ///< Mapping from a RIPEMD160 hash to its preimage provided to solve a Script
89 : : std::map<std::vector<uint8_t>, std::vector<uint8_t>> hash160_preimages; ///< Mapping from a HASH160 hash to its preimage provided to solve a Script
90 : :
91 [ # # ][ # # ]: 0 : SignatureData() {}
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
92 [ # # ][ # # ]: 0 : explicit SignatureData(const CScript& script) : scriptSig(script) {}
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
93 : : void MergeSignatureData(SignatureData sigdata);
94 : : };
95 : :
96 : : /** Produce a script signature using a generic signature creator. */
97 : : bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
98 : :
99 : : /**
100 : : * Produce a satisfying script (scriptSig or witness).
101 : : *
102 : : * @param provider Utility containing the information necessary to solve a script.
103 : : * @param fromPubKey The script to produce a satisfaction for.
104 : : * @param txTo The spending transaction.
105 : : * @param nIn The index of the input in `txTo` referring the output being spent.
106 : : * @param amount The value of the output being spent.
107 : : * @param nHashType Signature hash type.
108 : : * @param sig_data Additional data provided to solve a script. Filled with the resulting satisfying
109 : : * script and whether the satisfaction is complete.
110 : : *
111 : : * @return True if the produced script is entirely satisfying `fromPubKey`.
112 : : **/
113 : : bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo,
114 : : unsigned int nIn, const CAmount& amount, int nHashType, SignatureData& sig_data);
115 : : bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo,
116 : : unsigned int nIn, int nHashType, SignatureData& sig_data);
117 : :
118 : : /** Extract signature data from a transaction input, and insert it. */
119 : : SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout);
120 : : void UpdateInput(CTxIn& input, const SignatureData& data);
121 : :
122 : : /** Check whether a scriptPubKey is known to be segwit. */
123 : : bool IsSegWitOutput(const SigningProvider& provider, const CScript& script);
124 : :
125 : : /** Sign the CMutableTransaction */
126 : : bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* provider, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors);
127 : :
128 : : #endif // BITCOIN_SCRIPT_SIGN_H
|