9
9
#include < pubkey.h>
10
10
#include < script/standard.h>
11
11
12
- bool CScriptCompressor::IsToKeyID (CKeyID &hash) const
12
+ /*
13
+ * These check for scripts for which a special case with a shorter encoding is defined.
14
+ * They are implemented separately from the CScript test, as these test for exact byte
15
+ * sequence correspondences, and are more strict. For example, IsToPubKey also verifies
16
+ * whether the public key is valid (as invalid ones cannot be represented in compressed
17
+ * form).
18
+ */
19
+
20
+ static bool IsToKeyID (const CScript& script, CKeyID &hash)
13
21
{
14
22
if (script.size () == 25 && script[0 ] == OP_DUP && script[1 ] == OP_HASH160
15
23
&& script[2 ] == 20 && script[23 ] == OP_EQUALVERIFY
@@ -20,7 +28,7 @@ bool CScriptCompressor::IsToKeyID(CKeyID &hash) const
20
28
return false ;
21
29
}
22
30
23
- bool CScriptCompressor:: IsToScriptID (CScriptID &hash) const
31
+ static bool IsToScriptID (const CScript& script, CScriptID &hash)
24
32
{
25
33
if (script.size () == 23 && script[0 ] == OP_HASH160 && script[1 ] == 20
26
34
&& script[22 ] == OP_EQUAL) {
@@ -30,7 +38,7 @@ bool CScriptCompressor::IsToScriptID(CScriptID &hash) const
30
38
return false ;
31
39
}
32
40
33
- bool CScriptCompressor:: IsToPubKey (CPubKey &pubkey) const
41
+ static bool IsToPubKey (const CScript& script, CPubKey &pubkey)
34
42
{
35
43
if (script.size () == 35 && script[0 ] == 33 && script[34 ] == OP_CHECKSIG
36
44
&& (script[1 ] == 0x02 || script[1 ] == 0x03 )) {
@@ -45,24 +53,24 @@ bool CScriptCompressor::IsToPubKey(CPubKey &pubkey) const
45
53
return false ;
46
54
}
47
55
48
- bool CScriptCompressor::Compress ( std::vector<unsigned char > &out) const
56
+ bool CompressScript ( const CScript& script, std::vector<unsigned char > &out)
49
57
{
50
58
CKeyID keyID;
51
- if (IsToKeyID (keyID)) {
59
+ if (IsToKeyID (script, keyID)) {
52
60
out.resize (21 );
53
61
out[0 ] = 0x00 ;
54
62
memcpy (&out[1 ], &keyID, 20 );
55
63
return true ;
56
64
}
57
65
CScriptID scriptID;
58
- if (IsToScriptID (scriptID)) {
66
+ if (IsToScriptID (script, scriptID)) {
59
67
out.resize (21 );
60
68
out[0 ] = 0x01 ;
61
69
memcpy (&out[1 ], &scriptID, 20 );
62
70
return true ;
63
71
}
64
72
CPubKey pubkey;
65
- if (IsToPubKey (pubkey)) {
73
+ if (IsToPubKey (script, pubkey)) {
66
74
out.resize (33 );
67
75
memcpy (&out[1 ], &pubkey[1 ], 32 );
68
76
if (pubkey[0 ] == 0x02 || pubkey[0 ] == 0x03 ) {
@@ -76,7 +84,7 @@ bool CScriptCompressor::Compress(std::vector<unsigned char> &out) const
76
84
return false ;
77
85
}
78
86
79
- unsigned int CScriptCompressor::GetSpecialSize (unsigned int nSize) const
87
+ unsigned int GetSpecialScriptSize (unsigned int nSize)
80
88
{
81
89
if (nSize == 0 || nSize == 1 )
82
90
return 20 ;
@@ -85,7 +93,7 @@ unsigned int CScriptCompressor::GetSpecialSize(unsigned int nSize) const
85
93
return 0 ;
86
94
}
87
95
88
- bool CScriptCompressor::Decompress ( unsigned int nSize, const std::vector<unsigned char > &in)
96
+ bool DecompressScript (CScript& script, unsigned int nSize, const std::vector<unsigned char > &in)
89
97
{
90
98
switch (nSize) {
91
99
case 0x00 :
@@ -139,7 +147,7 @@ bool CScriptCompressor::Decompress(unsigned int nSize, const std::vector<unsigne
139
147
// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
140
148
// (this is decodable, as d is in [1-9] and e is in [0-9])
141
149
142
- uint64_t CTxOutCompressor:: CompressAmount (uint64_t n)
150
+ uint64_t CompressAmount (uint64_t n)
143
151
{
144
152
if (n == 0 )
145
153
return 0 ;
@@ -158,7 +166,7 @@ uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
158
166
}
159
167
}
160
168
161
- uint64_t CTxOutCompressor:: DecompressAmount (uint64_t x)
169
+ uint64_t DecompressAmount (uint64_t x)
162
170
{
163
171
// x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
164
172
if (x == 0 )
0 commit comments