Branch data 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 <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 : :
# 25 : : /** Interface for signature creators. */
# 26 : : class BaseSignatureCreator {
# 27 : : public:
# 28 : 93276 : virtual ~BaseSignatureCreator() {}
# 29 : : virtual const BaseSignatureChecker& Checker() const =0;
# 30 : :
# 31 : : /** Create a singular (non-script) signature. */
# 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 : :
# 36 : : /** A signature creator for transactions. */
# 37 : : class MutableTransactionSignatureCreator : public BaseSignatureCreator {
# 38 : : const CMutableTransaction* txTo;
# 39 : : unsigned int nIn;
# 40 : : int nHashType;
# 41 : : CAmount amount;
# 42 : : const MutableTransactionSignatureChecker checker;
# 43 : : const PrecomputedTransactionData* m_txdata;
# 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 : 35616 : 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 : :
# 53 : : /** A signature creator that just produces 71-byte empty signatures. */
# 54 : : extern const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR;
# 55 : : /** A signature creator that just produces 72-byte empty signatures. */
# 56 : : extern const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR;
# 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; ///< Stores whether the scriptSig and scriptWitness are complete
# 65 : : bool witness = false; ///< Stores whether the input this SigData corresponds to is a witness input
# 66 : : CScript scriptSig; ///< The scriptSig of an input. Contains complete signatures or the traditional partial signatures format
# 67 : : CScript redeem_script; ///< The redeemScript (if any) for the input
# 68 : : CScript witness_script; ///< The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
# 69 : : 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.
# 70 : : TaprootSpendData tr_spenddata; ///< Taproot spending data.
# 71 : : 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.
# 72 : : std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys;
# 73 : : std::vector<unsigned char> taproot_key_path_sig; /// Schnorr signature for key path spending
# 74 : : std::map<std::pair<XOnlyPubKey, uint256>, std::vector<unsigned char>> taproot_script_sigs; ///< (Partial) schnorr signatures, indexed by XOnlyPubKey and leaf_hash.
# 75 : : std::vector<CKeyID> missing_pubkeys; ///< KeyIDs of pubkeys which could not be found
# 76 : : std::vector<CKeyID> missing_sigs; ///< KeyIDs of pubkeys for signatures which could not be found
# 77 : : uint160 missing_redeem_script; ///< ScriptID of the missing redeemScript (if any)
# 78 : : uint256 missing_witness_script; ///< SHA256 of the missing witnessScript (if any)
# 79 : :
# 80 : 1950863 : SignatureData() {}
# 81 : 0 : explicit SignatureData(const CScript& script) : scriptSig(script) {}
# 82 : : void MergeSignatureData(SignatureData sigdata);
# 83 : : };
# 84 : :
# 85 : : /** Produce a script signature using a generic signature creator. */
# 86 : : bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
# 87 : :
# 88 : : /** Produce a script signature for a transaction. */
# 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 : :
# 92 : : /** Extract signature data from a transaction input, and insert it. */
# 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 : :
# 102 : : /** Check whether a scriptPubKey is known to be segwit. */
# 103 : : bool IsSegWitOutput(const SigningProvider& provider, const CScript& script);
# 104 : :
# 105 : : /** Sign the CMutableTransaction */
# 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
|