LCOV - code coverage report
Current view: top level - src/script - interpreter.h (source / functions) Hit Total Coverage
Test: bitcoincash_test.info Lines: 7 13 53.8 %
Date: 2018-04-13 15:12:50 Functions: 0 9 0.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2016 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_INTERPRETER_H
       7             : #define BITCOIN_SCRIPT_INTERPRETER_H
       8             : 
       9             : #include "primitives/transaction.h"
      10             : #include "script_error.h"
      11             : #include "sighashtype.h"
      12             : 
      13             : #include <cstdint>
      14             : #include <string>
      15             : #include <vector>
      16             : 
      17             : class CPubKey;
      18             : class CScript;
      19             : class CTransaction;
      20             : class uint256;
      21             : 
      22             : /** Script verification flags */
      23             : enum {
      24             :     SCRIPT_VERIFY_NONE = 0,
      25             : 
      26             :     // Evaluate P2SH subscripts (softfork safe, BIP16).
      27             :     SCRIPT_VERIFY_P2SH = (1U << 0),
      28             : 
      29             :     // Passing a non-strict-DER signature or one with undefined hashtype to a
      30             :     // checksig operation causes script failure. Evaluating a pubkey that is not
      31             :     // (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script
      32             :     // failure.
      33             :     SCRIPT_VERIFY_STRICTENC = (1U << 1),
      34             : 
      35             :     // Passing a non-strict-DER signature to a checksig operation causes script
      36             :     // failure (softfork safe, BIP62 rule 1)
      37             :     SCRIPT_VERIFY_DERSIG = (1U << 2),
      38             : 
      39             :     // Passing a non-strict-DER signature or one with S > order/2 to a checksig
      40             :     // operation causes script failure
      41             :     // (softfork safe, BIP62 rule 5).
      42             :     SCRIPT_VERIFY_LOW_S = (1U << 3),
      43             : 
      44             :     // verify dummy stack item consumed by CHECKMULTISIG is of zero-length
      45             :     // (softfork safe, BIP62 rule 7).
      46             :     SCRIPT_VERIFY_NULLDUMMY = (1U << 4),
      47             : 
      48             :     // Using a non-push operator in the scriptSig causes script failure
      49             :     // (softfork safe, BIP62 rule 2).
      50             :     SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5),
      51             : 
      52             :     // Require minimal encodings for all push operations (OP_0... OP_16,
      53             :     // OP_1NEGATE where possible, direct pushes up to 75 bytes, OP_PUSHDATA up
      54             :     // to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating any other
      55             :     // push causes the script to fail (BIP62 rule 3). In addition, whenever a
      56             :     // stack element is interpreted as a number, it must be of minimal length
      57             :     // (BIP62 rule 4).
      58             :     // (softfork safe)
      59             :     SCRIPT_VERIFY_MINIMALDATA = (1U << 6),
      60             : 
      61             :     // Discourage use of NOPs reserved for upgrades (NOP1-10)
      62             :     //
      63             :     // Provided so that nodes can avoid accepting or mining transactions
      64             :     // containing executed NOP's whose meaning may change after a soft-fork,
      65             :     // thus rendering the script invalid; with this flag set executing
      66             :     // discouraged NOPs fails the script. This verification flag will never be a
      67             :     // mandatory flag applied to scripts in a block. NOPs that are not executed,
      68             :     // e.g.  within an unexecuted IF ENDIF block, are *not* rejected.
      69             :     SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),
      70             : 
      71             :     // Require that only a single stack element remains after evaluation. This
      72             :     // changes the success criterion from "At least one stack element must
      73             :     // remain, and when interpreted as a boolean, it must be true" to "Exactly
      74             :     // one stack element must remain, and when interpreted as a boolean, it must
      75             :     // be true".
      76             :     // (softfork safe, BIP62 rule 6)
      77             :     // Note: CLEANSTACK should never be used without P2SH or WITNESS.
      78             :     SCRIPT_VERIFY_CLEANSTACK = (1U << 8),
      79             : 
      80             :     // Verify CHECKLOCKTIMEVERIFY
      81             :     //
      82             :     // See BIP65 for details.
      83             :     SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9),
      84             : 
      85             :     // support CHECKSEQUENCEVERIFY opcode
      86             :     //
      87             :     // See BIP112 for details
      88             :     SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10),
      89             : 
      90             :     // Making v1-v16 witness program non-standard
      91             :     //
      92             :     SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM = (1U << 12),
      93             : 
      94             :     // Segwit script only: Require the argument of OP_IF/NOTIF to be exactly
      95             :     // 0x01 or empty vector
      96             :     //
      97             :     SCRIPT_VERIFY_MINIMALIF = (1U << 13),
      98             : 
      99             :     // Signature(s) must be empty vector if an CHECK(MULTI)SIG operation failed
     100             :     //
     101             :     SCRIPT_VERIFY_NULLFAIL = (1U << 14),
     102             : 
     103             :     // Public keys in scripts must be compressed
     104             :     //
     105             :     SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE = (1U << 15),
     106             : 
     107             :     // Do we accept signature using SIGHASH_FORKID
     108             :     //
     109             :     SCRIPT_ENABLE_SIGHASH_FORKID = (1U << 16),
     110             : 
     111             :     // Do we accept activate replay protection using a different fork id.
     112             :     //
     113             :     SCRIPT_ENABLE_REPLAY_PROTECTION = (1U << 17),
     114             : 
     115             :     // Enable new opcodes.
     116             :     //
     117             :     SCRIPT_ENABLE_MONOLITH_OPCODES = (1U << 18),
     118             : };
     119             : 
     120             : bool CheckSignatureEncoding(const std::vector<uint8_t> &vchSig, uint32_t flags,
     121             :                             ScriptError *serror);
     122             : 
     123             : uint256 SignatureHash(const CScript &scriptCode, const CTransaction &txTo,
     124             :                       unsigned int nIn, SigHashType sigHashType,
     125             :                       const Amount amount,
     126             :                       const PrecomputedTransactionData *cache = nullptr,
     127             :                       uint32_t flags = SCRIPT_ENABLE_SIGHASH_FORKID);
     128             : 
     129       69126 : class BaseSignatureChecker {
     130             : public:
     131           0 :     virtual bool CheckSig(const std::vector<uint8_t> &scriptSig,
     132             :                           const std::vector<uint8_t> &vchPubKey,
     133             :                           const CScript &scriptCode, uint32_t flags) const {
     134           0 :         return false;
     135             :     }
     136             : 
     137           0 :     virtual bool CheckLockTime(const CScriptNum &nLockTime) const {
     138           0 :         return false;
     139             :     }
     140             : 
     141           0 :     virtual bool CheckSequence(const CScriptNum &nSequence) const {
     142           0 :         return false;
     143             :     }
     144             : 
     145      124572 :     virtual ~BaseSignatureChecker() {}
     146             : };
     147             : 
     148        3202 : class TransactionSignatureChecker : public BaseSignatureChecker {
     149             : private:
     150             :     const CTransaction *txTo;
     151             :     unsigned int nIn;
     152             :     const Amount amount;
     153             :     const PrecomputedTransactionData *txdata;
     154             : 
     155             : protected:
     156             :     virtual bool VerifySignature(const std::vector<uint8_t> &vchSig,
     157             :                                  const CPubKey &vchPubKey,
     158             :                                  const uint256 &sighash) const;
     159             : 
     160             : public:
     161             :     TransactionSignatureChecker(const CTransaction *txToIn, unsigned int nInIn,
     162             :                                 const Amount amountIn)
     163        4329 :         : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(nullptr) {}
     164             :     TransactionSignatureChecker(const CTransaction *txToIn, unsigned int nInIn,
     165             :                                 const Amount amountIn,
     166             :                                 const PrecomputedTransactionData &txdataIn)
     167         468 :         : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
     168             :     bool CheckSig(const std::vector<uint8_t> &scriptSig,
     169             :                   const std::vector<uint8_t> &vchPubKey,
     170             :                   const CScript &scriptCode, uint32_t flags) const override;
     171             :     bool CheckLockTime(const CScriptNum &nLockTime) const override;
     172             :     bool CheckSequence(const CScriptNum &nSequence) const override;
     173             : };
     174             : 
     175        4197 : class MutableTransactionSignatureChecker : public TransactionSignatureChecker {
     176             : private:
     177             :     const CTransaction txTo;
     178             : 
     179             : public:
     180             :     MutableTransactionSignatureChecker(const CMutableTransaction *txToIn,
     181             :                                        unsigned int nInIn, const Amount amount)
     182        2798 :         : TransactionSignatureChecker(&txTo, nInIn, amount), txTo(*txToIn) {}
     183             : };
     184             : 
     185             : bool EvalScript(std::vector<std::vector<uint8_t>> &stack, const CScript &script,
     186             :                 uint32_t flags, const BaseSignatureChecker &checker,
     187             :                 ScriptError *error = nullptr);
     188             : bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey,
     189             :                   uint32_t flags, const BaseSignatureChecker &checker,
     190             :                   ScriptError *serror = nullptr);
     191             : 
     192             : #endif // BITCOIN_SCRIPT_INTERPRETER_H

Generated by: LCOV version 1.12