LCOV - code coverage report
Current view: top level - src/script - sign.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 14 15 93.3 %
Date: 2025-02-23 09:33:43 Functions: 1 2 50.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2014 The Bitcoin developers
       3             : // Copyright (c) 2016-2022 The PIVX Core developers
       4             : // Distributed under the MIT software license, see the accompanying
       5             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       6             : 
       7             : #ifndef PIVX_SCRIPT_SIGN_H
       8             : #define PIVX_SCRIPT_SIGN_H
       9             : 
      10             : #include "script/interpreter.h"
      11             : 
      12             : class CKey;
      13             : class CKeyID;
      14             : class CKeyStore;
      15             : class CScript;
      16             : class CScriptID;
      17             : class CTransaction;
      18             : 
      19             : struct CMutableTransaction;
      20             : 
      21             : /** An interface to be implemented by keystores that support signing. */
      22          37 : class SigningProvider
      23             : {
      24             : public:
      25             :     virtual ~SigningProvider() {}
      26             :     virtual bool GetCScript(const CScriptID &scriptid, CScript& script) const { return false; }
      27             :     virtual bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const { return false; }
      28           0 :     virtual bool GetKey(const CKeyID &address, CKey& key) const { return false; }
      29             : };
      30             : 
      31             : extern const SigningProvider& DUMMY_SIGNING_PROVIDER;
      32             : 
      33             : class PublicOnlySigningProvider : public SigningProvider
      34             : {
      35             : private:
      36             :     const SigningProvider* m_provider;
      37             : 
      38             : public:
      39             :     explicit PublicOnlySigningProvider(const SigningProvider* provider) : m_provider(provider) {}
      40             :     bool GetCScript(const CScriptID &scriptid, CScript& script) const;
      41             :     bool GetPubKey(const CKeyID &address, CPubKey& pubkey) const;
      42             : };
      43             : 
      44          37 : struct FlatSigningProvider final : public SigningProvider
      45             : {
      46             :     std::map<CScriptID, CScript> scripts;
      47             :     std::map<CKeyID, CPubKey> pubkeys;
      48             :     std::map<CKeyID, CKey> keys;
      49             : 
      50             :     bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
      51             :     bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
      52             :     bool GetKey(const CKeyID& keyid, CKey& key) const override;
      53             : };
      54             : 
      55             : FlatSigningProvider Merge(const FlatSigningProvider& a, const FlatSigningProvider& b);
      56             : 
      57             : /** Virtual base class for signature creators. */
      58             : class BaseSignatureCreator {
      59             : protected:
      60             :     const CKeyStore* keystore;
      61             : 
      62             : public:
      63     3533219 :     explicit BaseSignatureCreator(const CKeyStore* keystoreIn) : keystore(keystoreIn) {}
      64     3524823 :     const CKeyStore& KeyStore() const { return *keystore; };
      65      211417 :     virtual ~BaseSignatureCreator() {}
      66             :     virtual const BaseSignatureChecker& Checker() const =0;
      67             : 
      68             :     /** Create a singular (non-script) signature. */
      69             :     virtual bool CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0;
      70             : };
      71             : 
      72             : /** A signature creator for transactions. */
      73      211377 : class TransactionSignatureCreator : public BaseSignatureCreator {
      74             :     const CTransaction* txTo;
      75             :     unsigned int nIn;
      76             :     int nHashType;
      77             :     CAmount amount;
      78             :     const TransactionSignatureChecker checker;
      79             : 
      80             : public:
      81             :     TransactionSignatureCreator(const CKeyStore* keystoreIn, const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, int nHashTypeIn=SIGHASH_ALL);
      82      211369 :     const BaseSignatureChecker& Checker() const { return checker; }
      83             :     bool CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const;
      84             : };
      85             : 
      86      202978 : class MutableTransactionSignatureCreator : public TransactionSignatureCreator {
      87             :     const CTransaction tx;
      88             : 
      89             : public:
      90      202978 :     MutableTransactionSignatureCreator(const CKeyStore* keystoreIn, const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amount, int nHashTypeIn) : TransactionSignatureCreator(keystoreIn, &tx, nInIn, amount, nHashTypeIn), tx(*txToIn) {}
      91             : };
      92             : 
      93             : /** A signature creator that just produces 72-byte empty signatyres. */
      94     3321838 : class DummySignatureCreator : public BaseSignatureCreator {
      95             : public:
      96     3321838 :     explicit DummySignatureCreator(const CKeyStore* keystoreIn) : BaseSignatureCreator(keystoreIn) {}
      97             :     const BaseSignatureChecker& Checker() const;
      98             :     bool CreateSig(std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const;
      99             : };
     100             : 
     101     7472024 : struct SignatureData {
     102             :     CScript scriptSig;
     103             : 
     104     3938970 :     SignatureData() {}
     105          18 :     explicit SignatureData(const CScript& script) : scriptSig(script) {}
     106             : };
     107             : 
     108             : /** Produce a script signature using a generic signature creator. */
     109             : bool ProduceSignature(const BaseSignatureCreator& creator, const CScript& fromPubKey, SignatureData& sigdata, SigVersion sigversion, bool fColdStake, ScriptError* serror = nullptr);
     110             : 
     111             : /** Produce a script signature for a transaction. */
     112             : bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType, bool fColdStake = false);
     113             : bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType, bool fColdStake = false);
     114             : 
     115             : /** Combine two script signatures using a generic signature checker, intelligently, possibly with OP_0 placeholders. */
     116             : SignatureData CombineSignatures(const CScript& scriptPubKey, const BaseSignatureChecker& checker, const SignatureData& scriptSig1, const SignatureData& scriptSig2);
     117             : 
     118             : /** Extract signature data from a transaction, and insert it. */
     119             : SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn);
     120             : void UpdateTransaction(CMutableTransaction& tx, unsigned int nIn, const SignatureData& data);
     121             : 
     122             : /* Check whether we know how to sign for an output like this, assuming we
     123             :   * have all private keys. While this function does not need private keys, the passed
     124             :   * keystore is used to look up public keys and redeemscripts by hash.
     125             :   * Solvability is unrelated to whether we consider this output to be ours. */
     126             : bool IsSolvable(const CKeyStore& store, const CScript& script, bool fColdStaking);
     127             : 
     128             : #endif // PIVX_SCRIPT_SIGN_H

Generated by: LCOV version 1.14