Coverage Report

Created: 2024-11-15 12:18

/root/bitcoin/src/script/sign.h
Line
Count
Source (jump to first uncovered line)
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
class BaseSignatureCreator {
29
public:
30
0
    virtual ~BaseSignatureCreator() = default;
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
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
struct SignatureData {
69
    bool complete = false; ///< Stores whether the scriptSig and scriptWitness are complete
70
    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::map<CKeyID, XOnlyPubKey> tap_pubkeys; ///< Misc Taproot pubkeys involved in this input, by hash. (Equivalent of misc_pubkeys but for Taproot.)
83
    std::vector<CKeyID> missing_pubkeys; ///< KeyIDs of pubkeys which could not be found
84
    std::vector<CKeyID> missing_sigs; ///< KeyIDs of pubkeys for signatures which could not be found
85
    uint160 missing_redeem_script; ///< ScriptID of the missing redeemScript (if any)
86
    uint256 missing_witness_script; ///< SHA256 of the missing witnessScript (if any)
87
    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
88
    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
89
    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
90
    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
91
92
0
    SignatureData() = default;
93
0
    explicit SignatureData(const CScript& script) : scriptSig(script) {}
94
    void MergeSignatureData(SignatureData sigdata);
95
};
96
97
/** Produce a script signature using a generic signature creator. */
98
bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
99
100
/** Extract signature data from a transaction input, and insert it. */
101
SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout);
102
void UpdateInput(CTxIn& input, const SignatureData& data);
103
104
/** Check whether a scriptPubKey is known to be segwit. */
105
bool IsSegWitOutput(const SigningProvider& provider, const CScript& script);
106
107
/** Sign the CMutableTransaction */
108
bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* provider, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors);
109
110
#endif // BITCOIN_SCRIPT_SIGN_H