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

          Line data    Source code
       1             : // Copyright (c) 2017 Bitcoin ABC developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #ifndef BITCOIN_SCRIPT_HASH_TYPE_H
       6             : #define BITCOIN_SCRIPT_HASH_TYPE_H
       7             : 
       8             : #include "serialize.h"
       9             : 
      10             : #include <cstdint>
      11             : #include <stdexcept>
      12             : 
      13             : /** Signature hash types/flags */
      14             : enum {
      15             :     SIGHASH_ALL = 1,
      16             :     SIGHASH_NONE = 2,
      17             :     SIGHASH_SINGLE = 3,
      18             :     SIGHASH_FORKID = 0x40,
      19             :     SIGHASH_ANYONECANPAY = 0x80,
      20             : };
      21             : 
      22             : /**
      23             :  * Base signature hash types
      24             :  * Base sig hash types not defined in this enum may be used, but they will be
      25             :  * represented as UNSUPPORTED.  See transaction
      26             :  * c99c49da4c38af669dea436d3e73780dfdb6c1ecf9958baa52960e8baee30e73 for an
      27             :  * example where an unsupported base sig hash of 0 was used.
      28             :  */
      29             : enum class BaseSigHashType : uint32_t {
      30             :     UNSUPPORTED = 0,
      31             :     ALL = SIGHASH_ALL,
      32             :     NONE = SIGHASH_NONE,
      33             :     SINGLE = SIGHASH_SINGLE
      34             : };
      35             : 
      36             : /** Signature hash type wrapper class */
      37             : class SigHashType {
      38             : private:
      39             :     uint32_t sigHash;
      40             : 
      41             : public:
      42        1193 :     explicit SigHashType() : sigHash(SIGHASH_ALL) {}
      43             : 
      44        1644 :     explicit SigHashType(uint32_t sigHashIn) : sigHash(sigHashIn) {}
      45             : 
      46             :     SigHashType withBaseType(BaseSigHashType baseSigHashType) const {
      47         402 :         return SigHashType((sigHash & ~0x1f) | uint32_t(baseSigHashType));
      48             :     }
      49             : 
      50             :     SigHashType withForkValue(uint32_t forkId) const {
      51        5480 :         return SigHashType((forkId << 8) | (sigHash & 0xff));
      52             :     }
      53             : 
      54             :     SigHashType withForkId(bool forkId = true) const {
      55         160 :         return SigHashType((sigHash & ~SIGHASH_FORKID) |
      56         202 :                            (forkId ? SIGHASH_FORKID : 0));
      57             :     }
      58             : 
      59             :     SigHashType withAnyoneCanPay(bool anyoneCanPay = true) const {
      60         160 :         return SigHashType((sigHash & ~SIGHASH_ANYONECANPAY) |
      61         242 :                            (anyoneCanPay ? SIGHASH_ANYONECANPAY : 0));
      62             :     }
      63             : 
      64             :     BaseSigHashType getBaseType() const {
      65        1761 :         return BaseSigHashType(sigHash & 0x1f);
      66             :     }
      67             : 
      68        1761 :     uint32_t getForkValue() const { return sigHash >> 8; }
      69             : 
      70             :     bool hasSupportedBaseType() const {
      71        1761 :         BaseSigHashType baseType = getBaseType();
      72        1761 :         return baseType >= BaseSigHashType::ALL &&
      73        1761 :                baseType <= BaseSigHashType::SINGLE;
      74             :     }
      75             : 
      76        2761 :     bool hasForkId() const { return (sigHash & SIGHASH_FORKID) != 0; }
      77             : 
      78             :     bool hasAnyoneCanPay() const {
      79        1761 :         return (sigHash & SIGHASH_ANYONECANPAY) != 0;
      80             :     }
      81             : 
      82          96 :     uint32_t getRawSigHashType() const { return sigHash; }
      83             : 
      84             :     template <typename Stream> void Serialize(Stream &s) const {
      85        1280 :         ::Serialize(s, getRawSigHashType());
      86             :     }
      87             : };
      88             : 
      89             : #endif // BITCOIN_SCRIPT_HASH_TYPE_H

Generated by: LCOV version 1.12