-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstandard.cpp
More file actions
394 lines (337 loc) · 12.3 KB
/
standard.cpp
File metadata and controls
394 lines (337 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <script/standard.h>
#include <pubkey.h>
#include <script/script.h>
#include <util/strencodings.h>
#include <util/system.h>
using valtype = std::vector<uint8_t>;
bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
CScriptID::CScriptID(const CScript &in)
: uint160(Hash160(in.begin(), in.end())) {}
const char *GetTxnOutputType(txnouttype t) {
switch (t) {
case TX_NONSTANDARD:
return "nonstandard";
case TX_PUBKEY:
return "pubkey";
case TX_BLSPUBKEY:
return "blspubkey";
case TX_PUBKEYHASH:
return "pubkeyhash";
case TX_BLSKEYHASH:
return "blskeyhash";
case TX_SCRIPTHASH:
return "scripthash";
case TX_MULTISIG:
return "multisig";
case TX_NULL_DATA:
return "nulldata";
}
return nullptr;
}
static bool MatchPayToPubkey(const CScript &script, valtype &pubkey) {
if (script.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 2 &&
script[0] == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE &&
script.back() == OP_CHECKSIG) {
pubkey =
valtype(script.begin() + 1,
script.begin() + CPubKey::COMPRESSED_PUBLIC_KEY_SIZE + 1);
return CPubKey::ValidSize(pubkey);
}
return false;
}
bool MatchPayToBLSPubkey(const CScript &script, valtype &pubkey) {
if (script.size() == CPubKey::BLS_PUBLIC_KEY_SIZE + 2 &&
script[0] == CPubKey::BLS_PUBLIC_KEY_SIZE &&
script.back() == OP_CHECKSIG) {
pubkey =
valtype(script.begin() + 1,
script.begin() + CPubKey::BLS_PUBLIC_KEY_SIZE + 1);
return true;
}
//std::cout << "Script size in MatchPay = " << script.size() << "\n";
return false;
}
static bool MatchPayToPubkeyHash(const CScript &script, valtype &pubkeyhash) {
if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160 &&
script[2] == 20 && script[23] == OP_EQUALVERIFY &&
script[24] == OP_CHECKSIG) {
pubkeyhash = valtype(script.begin() + 3, script.begin() + 23);
return true;
}
return false;
}
bool MatchPayToBLSkeyHash(const CScript &script, valtype &pubkeyhash) {
if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_BLSKEYHASH &&
script[2] == 20 && script[23] == OP_EQUALVERIFY &&
script[24] == OP_CHECKSIG) {
pubkeyhash = valtype(script.begin() + 3, script.begin() + 23);
return true;
}
return false;
}
/** Test for "small positive integer" script opcodes - OP_1 through OP_16. */
static constexpr bool IsSmallInteger(opcodetype opcode) {
return opcode >= OP_1 && opcode <= OP_16;
}
static bool MatchMultisig(const CScript &script, unsigned int &required,
std::vector<valtype> &pubkeys) {
opcodetype opcode;
valtype data;
CScript::const_iterator it = script.begin();
if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) {
return false;
}
if (!script.GetOp(it, opcode, data) || !IsSmallInteger(opcode)) {
return false;
}
required = CScript::DecodeOP_N(opcode);
while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
pubkeys.emplace_back(std::move(data));
}
if (!IsSmallInteger(opcode)) {
return false;
}
unsigned int keys = CScript::DecodeOP_N(opcode);
if (pubkeys.size() != keys || keys < required) {
return false;
}
return (it + 1 == script.end());
}
// Just used for debug stuff. BLS P2PKH (25) would be same size as a legacy P2PKH
bool IsValidBLSScriptSize(const CScript &script) {
// for all inputs except final, should only be either 0, BLS_PUBLIC_KEY_SIZE + 1, BLS_PUBLIC_KEY_SIZE + 2
// Last input will be BLS_SIGNATURE_SIZE + 1 + n*(BLS_PUBLIC_KEY_SIZE+2)
auto script_size = script.size();
//std::cout << "Script size = << " << script_size << "\n";
if (script_size > CPubKey::BLS_PUBLIC_KEY_SIZE+2) {
script_size -= (CPubKey::BLS_SIGNATURE_SIZE + 3);
return ((script_size % (CPubKey::BLS_PUBLIC_KEY_SIZE+1) == 0));
} else {
return ((script.size() == CPubKey::BLS_PUBLIC_KEY_SIZE+1) ||
(script.size() == CPubKey::BLS_PUBLIC_KEY_SIZE+2) ||
(script.size() == CPubKey::BLS_PUBLIC_KEY_SIZE) ||
(script.size() == 25) || // B2PKH
(script.size() == 0));
}
}
bool IsValidBLSPubKeySize(const CScript &script) {
return ((script.size() == CPubKey::BLS_PUBLIC_KEY_SIZE+2) ||
(script.size() == CPubKey::BLS_PUBLIC_KEY_SIZE+1) ||
(script.size() == CPubKey::BLS_PUBLIC_KEY_SIZE+CPubKey::BLS_SIGNATURE_SIZE+4));
}
CPubKey ExtractBLSPubKey(const CScript & scr) {
CPubKey pubkey(scr.begin()+1,scr.begin()+CPubKey::BLS_PUBLIC_KEY_SIZE+1);
return pubkey;
}
std::vector<uint8_t> ExtractBLSSig(const CScript & scr) {
// Handle Agg Sig
std::vector<uint8_t> aggSig = std::vector<uint8_t>(scr.begin()+2, scr.begin()+2+CPubKey::BLS_SIGNATURE_SIZE);
return aggSig;
}
std::tuple<CPubKey,std::vector<uint8_t> > ExtractBLSPubKeyAndSig(const CScript & scr) {
// Handle Grabbing PubKey + Agg Sig
std::vector<uint8_t> aggSig = std::vector<uint8_t>(scr.begin()+2, scr.begin()+2+CPubKey::BLS_SIGNATURE_SIZE);
CPubKey pubkey(scr.begin()+4+CPubKey::BLS_SIGNATURE_SIZE,
scr.begin()+4+CPubKey::BLS_SIGNATURE_SIZE+CPubKey::BLS_PUBLIC_KEY_SIZE);
return std::tuple(pubkey,aggSig);
}
std::tuple<std::vector<std::vector<uint8_t>>,std::vector<uint8_t> > ExtractBLSPubKeysAndSig(const CScript & scr, int rand_keys) {
std::vector<std::vector<uint8_t> > pubkeys;
int start = 1;
for (int i=0;i < rand_keys+1 ; i++) {
std::vector<uint8_t> p = std::vector<uint8_t>(scr.begin()+start, scr.begin()+start+CPubKey::BLS_PUBLIC_KEY_SIZE);
start += CPubKey::BLS_PUBLIC_KEY_SIZE + 1;
pubkeys.push_back(p);
}
start++;
std::vector<uint8_t> aggSig = std::vector<uint8_t>(scr.begin()+start, scr.begin()+start+CPubKey::BLS_SIGNATURE_SIZE+1);
return std::tuple(pubkeys,aggSig);
}
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet,
std::vector<std::vector<uint8_t>> &vSolutionsRet) {
vSolutionsRet.clear();
// Shortcut for pay-to-script-hash, which are more constrained than the
// other types:
// it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
if (scriptPubKey.IsPayToScriptHash()) {
typeRet = TX_SCRIPTHASH;
std::vector<uint8_t> hashBytes(scriptPubKey.begin() + 2,
scriptPubKey.begin() + 22);
vSolutionsRet.push_back(hashBytes);
return true;
}
// Provably prunable, data-carrying output
//
// So long as script passes the IsUnspendable() test and all but the first
// byte passes the IsPushOnly() test we don't care what exactly is in the
// script.
if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN &&
scriptPubKey.IsPushOnly(scriptPubKey.begin() + 1)) {
typeRet = TX_NULL_DATA;
return true;
}
std::vector<uint8_t> data;
if (MatchPayToPubkey(scriptPubKey, data)) {
typeRet = TX_PUBKEY;
vSolutionsRet.push_back(std::move(data));
return true;
}
if (MatchPayToBLSPubkey(scriptPubKey, data)) {
typeRet = TX_BLSPUBKEY;
vSolutionsRet.push_back(std::move(data));
return true;
}
if (MatchPayToPubkeyHash(scriptPubKey, data)) {
typeRet = TX_PUBKEYHASH;
vSolutionsRet.push_back(std::move(data));
return true;
}
// just checking for BLS..
if (MatchPayToBLSkeyHash(scriptPubKey, data)) {
typeRet = TX_BLSKEYHASH;
vSolutionsRet.push_back(std::move(data));
return true;
}
unsigned int required;
std::vector<std::vector<uint8_t>> keys;
if (MatchMultisig(scriptPubKey, required, keys)) {
typeRet = TX_MULTISIG;
// safe as required is in range 1..16
vSolutionsRet.push_back({static_cast<uint8_t>(required)});
vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
// safe as size is in range 1..16
vSolutionsRet.push_back({static_cast<uint8_t>(keys.size())});
return true;
}
vSolutionsRet.clear();
typeRet = TX_NONSTANDARD;
return false;
}
bool ExtractDestination(const CScript &scriptPubKey,
CTxDestination &addressRet) {
std::vector<valtype> vSolutions;
txnouttype whichType;
if (!Solver(scriptPubKey, whichType, vSolutions)) {
return false;
}
if (whichType == TX_PUBKEY) {
CPubKey pubKey(vSolutions[0]);
if (!pubKey.IsValid()) {
return false;
}
addressRet = pubKey.GetKeyID();
return true;
}
if (whichType == TX_BLSPUBKEY) {
CPubKey pubKey(vSolutions[0]);
if (!pubKey.IsValid()) {
return false;
}
addressRet = pubKey.GetBLSKeyID();
return true;
}
if (whichType == TX_PUBKEYHASH) {
addressRet = CKeyID(uint160(vSolutions[0]));
return true;
}
if (whichType == TX_BLSKEYHASH) {
addressRet = BKeyID(uint160(vSolutions[0]));
return true;
}
if (whichType == TX_SCRIPTHASH) {
addressRet = CScriptID(uint160(vSolutions[0]));
return true;
}
// Multisig txns have more than one address...
return false;
}
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet,
std::vector<CTxDestination> &addressRet,
int &nRequiredRet) {
addressRet.clear();
typeRet = TX_NONSTANDARD;
std::vector<valtype> vSolutions;
if (!Solver(scriptPubKey, typeRet, vSolutions)) {
return false;
}
if (typeRet == TX_NULL_DATA) {
// This is data, not addresses
return false;
}
if (typeRet == TX_MULTISIG) {
nRequiredRet = vSolutions.front()[0];
for (size_t i = 1; i < vSolutions.size() - 1; i++) {
CPubKey pubKey(vSolutions[i]);
if (!pubKey.IsValid()) {
continue;
}
CTxDestination address = pubKey.GetKeyID();
addressRet.push_back(address);
}
if (addressRet.empty()) {
return false;
}
} else {
nRequiredRet = 1;
CTxDestination address;
if (!ExtractDestination(scriptPubKey, address)) {
return false;
}
addressRet.push_back(address);
}
return true;
}
namespace {
class CScriptVisitor : public std::variant<bool> {
private:
CScript *script;
public:
explicit CScriptVisitor(CScript *scriptin) { script = scriptin; }
bool operator()(const CNoDestination &dest) const {
script->clear();
return false;
}
bool operator()(const CKeyID &keyID) const {
script->clear();
*script << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY
<< OP_CHECKSIG;
return true;
}
bool operator()(const BKeyID &keyID) const {
script->clear();
*script << OP_DUP << OP_BLSKEYHASH << ToByteVector(keyID) << OP_EQUALVERIFY
<< OP_CHECKSIG;
return true;
}
bool operator()(const CScriptID &scriptID) const {
script->clear();
*script << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
return true;
}
};
} // namespace
CScript GetScriptForDestination(const CTxDestination &dest) {
CScript script;
std::visit(CScriptVisitor(&script), dest);
return script;
}
CScript GetScriptForRawPubKey(const CPubKey &pubKey) {
return CScript() << std::vector<uint8_t>(pubKey.begin(), pubKey.end())
<< OP_CHECKSIG;
}
CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey> &keys) {
CScript script;
script << CScript::EncodeOP_N(nRequired);
for (const CPubKey &key : keys) {
script << ToByteVector(key);
}
script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
return script;
}
bool IsValidDestination(const CTxDestination &dest) {
return !std::holds_alternative<CNoDestination>(dest);
}