@@ -20,13 +20,74 @@ class CKeyID;
20
20
class CScript ;
21
21
struct ScriptHash ;
22
22
23
+ template <typename HashType>
24
+ class BaseHash
25
+ {
26
+ protected:
27
+ HashType m_hash;
28
+
29
+ public:
30
+ BaseHash () : m_hash() {}
31
+ BaseHash (const HashType& in) : m_hash(in) {}
32
+
33
+ unsigned char * begin ()
34
+ {
35
+ return m_hash.begin ();
36
+ }
37
+
38
+ const unsigned char * begin () const
39
+ {
40
+ return m_hash.begin ();
41
+ }
42
+
43
+ unsigned char * end ()
44
+ {
45
+ return m_hash.end ();
46
+ }
47
+
48
+ const unsigned char * end () const
49
+ {
50
+ return m_hash.end ();
51
+ }
52
+
53
+ operator std::vector<unsigned char >() const
54
+ {
55
+ return std::vector<unsigned char >{m_hash.begin (), m_hash.end ()};
56
+ }
57
+
58
+ std::string ToString () const
59
+ {
60
+ return m_hash.ToString ();
61
+ }
62
+
63
+ bool operator ==(const BaseHash<HashType>& other) const noexcept
64
+ {
65
+ return m_hash == other.m_hash ;
66
+ }
67
+
68
+ bool operator !=(const BaseHash<HashType>& other) const noexcept
69
+ {
70
+ return !(m_hash == other.m_hash );
71
+ }
72
+
73
+ bool operator <(const BaseHash<HashType>& other) const noexcept
74
+ {
75
+ return m_hash < other.m_hash ;
76
+ }
77
+
78
+ size_t size () const
79
+ {
80
+ return m_hash.size ();
81
+ }
82
+ };
83
+
23
84
/* * A reference to a CScript: the Hash160 of its serialization (see script.h) */
24
- class CScriptID : public uint160
85
+ class CScriptID : public BaseHash < uint160>
25
86
{
26
87
public:
27
- CScriptID () : uint160 () {}
88
+ CScriptID () : BaseHash () {}
28
89
explicit CScriptID (const CScript& in);
29
- explicit CScriptID (const uint160& in) : uint160 (in) {}
90
+ explicit CScriptID (const uint160& in) : BaseHash (in) {}
30
91
explicit CScriptID (const ScriptHash& in);
31
92
};
32
93
@@ -75,39 +136,40 @@ class CNoDestination {
75
136
friend bool operator <(const CNoDestination &a, const CNoDestination &b) { return true ; }
76
137
};
77
138
78
- struct PKHash : public uint160
139
+ struct PKHash : public BaseHash < uint160>
79
140
{
80
- PKHash () : uint160 () {}
81
- explicit PKHash (const uint160& hash) : uint160 (hash) {}
141
+ PKHash () : BaseHash () {}
142
+ explicit PKHash (const uint160& hash) : BaseHash (hash) {}
82
143
explicit PKHash (const CPubKey& pubkey);
83
144
explicit PKHash (const CKeyID& pubkey_id);
84
145
};
85
146
CKeyID ToKeyID (const PKHash& key_hash);
86
147
87
148
struct WitnessV0KeyHash ;
88
- struct ScriptHash : public uint160
149
+ struct ScriptHash : public BaseHash < uint160>
89
150
{
90
- ScriptHash () : uint160 () {}
151
+ ScriptHash () : BaseHash () {}
91
152
// These don't do what you'd expect.
92
153
// Use ScriptHash(GetScriptForDestination(...)) instead.
93
154
explicit ScriptHash (const WitnessV0KeyHash& hash) = delete;
94
155
explicit ScriptHash (const PKHash& hash) = delete;
95
- explicit ScriptHash (const uint160& hash) : uint160(hash) {}
156
+
157
+ explicit ScriptHash (const uint160& hash) : BaseHash(hash) {}
96
158
explicit ScriptHash (const CScript& script);
97
159
explicit ScriptHash (const CScriptID& script);
98
160
};
99
161
100
- struct WitnessV0ScriptHash : public uint256
162
+ struct WitnessV0ScriptHash : public BaseHash < uint256>
101
163
{
102
- WitnessV0ScriptHash () : uint256 () {}
103
- explicit WitnessV0ScriptHash (const uint256& hash) : uint256 (hash) {}
164
+ WitnessV0ScriptHash () : BaseHash () {}
165
+ explicit WitnessV0ScriptHash (const uint256& hash) : BaseHash (hash) {}
104
166
explicit WitnessV0ScriptHash (const CScript& script);
105
167
};
106
168
107
- struct WitnessV0KeyHash : public uint160
169
+ struct WitnessV0KeyHash : public BaseHash < uint160>
108
170
{
109
- WitnessV0KeyHash () : uint160 () {}
110
- explicit WitnessV0KeyHash (const uint160& hash) : uint160 (hash) {}
171
+ WitnessV0KeyHash () : BaseHash () {}
172
+ explicit WitnessV0KeyHash (const uint160& hash) : BaseHash (hash) {}
111
173
explicit WitnessV0KeyHash (const CPubKey& pubkey);
112
174
explicit WitnessV0KeyHash (const PKHash& pubkey_hash);
113
175
};
0 commit comments