sign.h raw

   1  // Copyright (c) 2009-2010 Satoshi Nakamoto
   2  // Copyright (c) 2009-2022 The Limenka 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 LIMENKA_SCRIPT_SIGN_H
   7  #define LIMENKA_SCRIPT_SIGN_H
   8  
   9  #include <attributes.h>
  10  #include <coins.h>
  11  #include <hash.h>
  12  #include <pubkey.h>
  13  #include <consensus/params.h>
  14  #include <script/interpreter.h>
  15  #include <script/keyorigin.h>
  16  #include <script/signingprovider.h>
  17  #include <uint256.h>
  18  
  19  #include <optional>
  20  
  21  class CKey;
  22  class CKeyID;
  23  class CScript;
  24  class CTransaction;
  25  class SigningProvider;
  26  
  27  struct bilingual_str;
  28  struct CMutableTransaction;
  29  
  30  /** Interface for signature creators. */
  31  class BaseSignatureCreator {
  32  public:
  33      virtual ~BaseSignatureCreator() = default;
  34      virtual const BaseSignatureChecker& Checker() const =0;
  35  
  36      /** Create a singular (non-script) signature. */
  37      virtual bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0;
  38      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;
  39  };
  40  
  41  /** A signature creator for transactions. */
  42  class MutableTransactionSignatureCreator : public BaseSignatureCreator
  43  {
  44      const CMutableTransaction& m_txto;
  45      unsigned int nIn;
  46      int nHashType;
  47      CAmount amount;
  48      const MutableTransactionSignatureChecker checker;
  49      const PrecomputedTransactionData* m_txdata;
  50  
  51  public:
  52      MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, int hash_type);
  53      MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, int hash_type);
  54      const BaseSignatureChecker& Checker() const override { return checker; }
  55      bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override;
  56      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;
  57  };
  58  
  59  /** A signature checker that accepts all signatures */
  60  extern const BaseSignatureChecker& DUMMY_CHECKER;
  61  /** A signature creator that just produces 71-byte empty signatures. */
  62  extern const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR;
  63  /** A signature creator that just produces 72-byte empty signatures. */
  64  extern const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR;
  65  
  66  typedef std::pair<CPubKey, std::vector<unsigned char>> SigPair;
  67  
  68  // This struct contains information from a transaction input and also contains signatures for that input.
  69  // The information contained here can be used to create a signature and is also filled by ProduceSignature
  70  // in order to construct final scriptSigs and scriptWitnesses.
  71  struct SignatureData {
  72      bool complete = false; ///< Stores whether the scriptSig and scriptWitness are complete
  73      bool witness = false; ///< Stores whether the input this SigData corresponds to is a witness input
  74      CScript scriptSig; ///< The scriptSig of an input. Contains complete signatures or the traditional partial signatures format
  75      CScript redeem_script; ///< The redeemScript (if any) for the input
  76      CScript witness_script; ///< The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
  77      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.
  78      TaprootSpendData tr_spenddata; ///< Taproot spending data.
  79      std::optional<TaprootBuilder> tr_builder; ///< Taproot tree used to build tr_spenddata.
  80      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.
  81      std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>> misc_pubkeys;
  82      std::vector<unsigned char> taproot_key_path_sig; /// Schnorr signature for key path spending
  83      std::map<std::pair<XOnlyPubKey, uint256>, std::vector<unsigned char>> taproot_script_sigs; ///< (Partial) schnorr signatures, indexed by XOnlyPubKey and leaf_hash.
  84      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).
  85      std::map<CKeyID, XOnlyPubKey> tap_pubkeys; ///< Misc Taproot pubkeys involved in this input, by hash. (Equivalent of misc_pubkeys but for Taproot.)
  86      std::vector<CKeyID> missing_pubkeys; ///< KeyIDs of pubkeys which could not be found
  87      std::vector<CKeyID> missing_sigs; ///< KeyIDs of pubkeys for signatures which could not be found
  88      uint160 missing_redeem_script; ///< ScriptID of the missing redeemScript (if any)
  89      uint256 missing_witness_script; ///< SHA256 of the missing witnessScript (if any)
  90      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
  91      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
  92      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
  93      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
  94  
  95      SignatureData() = default;
  96      explicit SignatureData(const CScript& script) : scriptSig(script) {}
  97      void MergeSignatureData(SignatureData sigdata);
  98  };
  99  
 100  /** Produce a script signature using a generic signature creator. */
 101  bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
 102  
 103  /** Extract signature data from a transaction input, and insert it. */
 104  SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout);
 105  void UpdateInput(CTxIn& input, const SignatureData& data);
 106  
 107  /** Check whether a scriptPubKey is known to be segwit. */
 108  bool IsSegWitOutput(const SigningProvider& provider, const CScript& script);
 109  
 110  /** Sign the CMutableTransaction */
 111  bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* provider, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors, std::optional<CAmount>* inputs_amount_sum = nullptr);
 112  
 113  #endif // LIMENKA_SCRIPT_SIGN_H
 114