Bitcoin ABC  0.22.12
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 {
60  return SigHashType((sigHash & ~SIGHASH_ANYONECANPAY) |
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 {
86  ::Serialize(s, getRawSigHashType());
87  }
88 
89  template <typename Stream> void Unserialize(Stream &s) {
90  ::Unserialize(s, sigHash);
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
friend constexpr bool operator!=(const SigHashType &a, const SigHashType &b)
Definition: sighashtype.h:101
bool hasForkId() const
Definition: sighashtype.h:77
bool hasAnyoneCanPay() const
Definition: sighashtype.h:79
uint32_t getRawSigHashType() const
Definition: sighashtype.h:83
bool isDefined() const
Definition: sighashtype.h:70
BaseSigHashType getBaseType() const
Definition: sighashtype.h:64
SigHashType withAnyoneCanPay(bool anyoneCanPay=true) const
Definition: sighashtype.h:59
void Unserialize(Stream &s)
Definition: sighashtype.h:89
void Serialize(Stream &s, char a)
Definition: serialize.h:261
void Serialize(Stream &s) const
Definition: sighashtype.h:85
SigHashType withForkId(bool forkId=true) const
Definition: sighashtype.h:54
SigHashType withForkValue(uint32_t forkId) const
Definition: sighashtype.h:50
uint32_t getForkValue() const
Definition: sighashtype.h:68
SigHashType withBaseType(BaseSigHashType baseSigHashType) const
Definition: sighashtype.h:46
SigHashType(uint32_t sigHashIn)
Definition: sighashtype.h:44
void Unserialize(Stream &s, char &a)
Definition: serialize.h:313
friend constexpr bool operator==(const SigHashType &a, const SigHashType &b)
Handy operators.
Definition: sighashtype.h:96
uint32_t sigHash
Definition: sighashtype.h:39
Signature hash type wrapper class.
Definition: sighashtype.h:37
BaseSigHashType
Base signature hash types Base sig hash types not defined in this enum may be used, but they will be represented as UNSUPPORTED.
Definition: sighashtype.h:29