Bitcoin ABC  0.26.3
P2P Digital Currency
sighashtype.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2018 Bitcoin 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_SIGHASHTYPE_H
6 #define BITCOIN_SCRIPT_SIGHASHTYPE_H
7 
8 #include <serialize.h>
9 
10 #include <cstdint>
11 #include <stdexcept>
12 
14 enum {
20 };
21 
29 enum class BaseSigHashType : uint8_t {
30  UNSUPPORTED = 0,
31  ALL = SIGHASH_ALL,
34 };
35 
37 class SigHashType {
38 private:
39  uint32_t sigHash;
40 
41 public:
42  explicit SigHashType() : sigHash(SIGHASH_ALL) {}
43 
44  explicit SigHashType(uint32_t sigHashIn) : sigHash(sigHashIn) {}
45 
46  SigHashType withBaseType(BaseSigHashType baseSigHashType) const {
47  return SigHashType((sigHash & ~0x1f) | uint32_t(baseSigHashType));
48  }
49 
50  SigHashType withForkValue(uint32_t forkId) const {
51  return SigHashType((forkId << 8) | (sigHash & 0xff));
52  }
53 
54  SigHashType withForkId(bool forkId = true) const {
55  return SigHashType((sigHash & ~SIGHASH_FORKID) |
56  (forkId ? SIGHASH_FORKID : 0));
57  }
58 
59  SigHashType withAnyoneCanPay(bool anyoneCanPay = true) const {
61  (anyoneCanPay ? SIGHASH_ANYONECANPAY : 0));
62  }
63 
65  return BaseSigHashType(sigHash & 0x1f);
66  }
67 
68  uint32_t getForkValue() const { return sigHash >> 8; }
69 
70  bool isDefined() const {
71  auto baseType =
73  return baseType >= BaseSigHashType::ALL &&
74  baseType <= BaseSigHashType::SINGLE;
75  }
76 
77  bool hasForkId() const { return (sigHash & SIGHASH_FORKID) != 0; }
78 
79  bool hasAnyoneCanPay() const {
80  return (sigHash & SIGHASH_ANYONECANPAY) != 0;
81  }
82 
83  uint32_t getRawSigHashType() const { return sigHash; }
84 
85  template <typename Stream> void Serialize(Stream &s) const {
87  }
88 
89  template <typename Stream> void Unserialize(Stream &s) {
91  }
92 
96  friend constexpr bool operator==(const SigHashType &a,
97  const SigHashType &b) {
98  return a.sigHash == b.sigHash;
99  }
100 
101  friend constexpr bool operator!=(const SigHashType &a,
102  const SigHashType &b) {
103  return !(a == b);
104  }
105 };
106 
107 #endif // BITCOIN_SCRIPT_SIGHASHTYPE_H
Signature hash type wrapper class.
Definition: sighashtype.h:37
uint32_t getRawSigHashType() const
Definition: sighashtype.h:83
void Unserialize(Stream &s)
Definition: sighashtype.h:89
BaseSigHashType getBaseType() const
Definition: sighashtype.h:64
constexpr friend bool operator==(const SigHashType &a, const SigHashType &b)
Handy operators.
Definition: sighashtype.h:96
SigHashType withForkId(bool forkId=true) const
Definition: sighashtype.h:54
SigHashType withAnyoneCanPay(bool anyoneCanPay=true) const
Definition: sighashtype.h:59
bool hasAnyoneCanPay() const
Definition: sighashtype.h:79
bool isDefined() const
Definition: sighashtype.h:70
SigHashType(uint32_t sigHashIn)
Definition: sighashtype.h:44
void Serialize(Stream &s) const
Definition: sighashtype.h:85
SigHashType withForkValue(uint32_t forkId) const
Definition: sighashtype.h:50
bool hasForkId() const
Definition: sighashtype.h:77
constexpr friend bool operator!=(const SigHashType &a, const SigHashType &b)
Definition: sighashtype.h:101
uint32_t sigHash
Definition: sighashtype.h:39
SigHashType withBaseType(BaseSigHashType baseSigHashType) const
Definition: sighashtype.h:46
uint32_t getForkValue() const
Definition: sighashtype.h:68
@ SIGHASH_FORKID
Definition: sighashtype.h:18
@ SIGHASH_ANYONECANPAY
Definition: sighashtype.h:19
@ SIGHASH_ALL
Definition: sighashtype.h:15
@ SIGHASH_NONE
Definition: sighashtype.h:16
@ SIGHASH_SINGLE
Definition: sighashtype.h:17
BaseSigHashType
Base signature hash types Base sig hash types not defined in this enum may be used,...
Definition: sighashtype.h:29