forked from David4860/navcoin-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.cpp
More file actions
594 lines (523 loc) · 18.4 KB
/
script.cpp
File metadata and controls
594 lines (523 loc) · 18.4 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 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/script.h>
#include "consensus/dao/flags.h"
#include "tinyformat.h"
#include "utilstrencodings.h"
using namespace std;
const char* GetOpName(opcodetype opcode)
{
switch (opcode)
{
// push value
case OP_0 : return "0";
case OP_PUSHDATA1 : return "OP_PUSHDATA1";
case OP_PUSHDATA2 : return "OP_PUSHDATA2";
case OP_PUSHDATA4 : return "OP_PUSHDATA4";
case OP_1NEGATE : return "-1";
case OP_RESERVED : return "OP_RESERVED";
case OP_1 : return "1";
case OP_2 : return "2";
case OP_3 : return "3";
case OP_4 : return "4";
case OP_5 : return "5";
case OP_6 : return "6";
case OP_7 : return "7";
case OP_8 : return "8";
case OP_9 : return "9";
case OP_10 : return "10";
case OP_11 : return "11";
case OP_12 : return "12";
case OP_13 : return "13";
case OP_14 : return "14";
case OP_15 : return "15";
case OP_16 : return "16";
// control
case OP_NOP : return "OP_NOP";
case OP_VER : return "OP_VER";
case OP_IF : return "OP_IF";
case OP_NOTIF : return "OP_NOTIF";
case OP_VERIF : return "OP_VERIF";
case OP_VERNOTIF : return "OP_VERNOTIF";
case OP_ELSE : return "OP_ELSE";
case OP_ENDIF : return "OP_ENDIF";
case OP_VERIFY : return "OP_VERIFY";
case OP_RETURN : return "OP_RETURN";
// stack ops
case OP_TOALTSTACK : return "OP_TOALTSTACK";
case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
case OP_2DROP : return "OP_2DROP";
case OP_2DUP : return "OP_2DUP";
case OP_3DUP : return "OP_3DUP";
case OP_2OVER : return "OP_2OVER";
case OP_2ROT : return "OP_2ROT";
case OP_2SWAP : return "OP_2SWAP";
case OP_IFDUP : return "OP_IFDUP";
case OP_DEPTH : return "OP_DEPTH";
case OP_DROP : return "OP_DROP";
case OP_DUP : return "OP_DUP";
case OP_NIP : return "OP_NIP";
case OP_OVER : return "OP_OVER";
case OP_PICK : return "OP_PICK";
case OP_ROLL : return "OP_ROLL";
case OP_ROT : return "OP_ROT";
case OP_SWAP : return "OP_SWAP";
case OP_TUCK : return "OP_TUCK";
// splice ops
case OP_CAT : return "OP_CAT";
case OP_SUBSTR : return "OP_SUBSTR";
case OP_LEFT : return "OP_LEFT";
case OP_RIGHT : return "OP_RIGHT";
case OP_SIZE : return "OP_SIZE";
// bit logic
case OP_INVERT : return "OP_INVERT";
case OP_AND : return "OP_AND";
case OP_OR : return "OP_OR";
case OP_XOR : return "OP_XOR";
case OP_EQUAL : return "OP_EQUAL";
case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
case OP_RESERVED1 : return "OP_RESERVED1";
case OP_RESERVED2 : return "OP_RESERVED2";
// numeric
case OP_1ADD : return "OP_1ADD";
case OP_1SUB : return "OP_1SUB";
case OP_2MUL : return "OP_2MUL";
case OP_2DIV : return "OP_2DIV";
case OP_NEGATE : return "OP_NEGATE";
case OP_ABS : return "OP_ABS";
case OP_NOT : return "OP_NOT";
case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
case OP_ADD : return "OP_ADD";
case OP_SUB : return "OP_SUB";
case OP_MUL : return "OP_MUL";
case OP_DIV : return "OP_DIV";
case OP_MOD : return "OP_MOD";
case OP_LSHIFT : return "OP_LSHIFT";
case OP_RSHIFT : return "OP_RSHIFT";
case OP_BOOLAND : return "OP_BOOLAND";
case OP_BOOLOR : return "OP_BOOLOR";
case OP_NUMEQUAL : return "OP_NUMEQUAL";
case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
case OP_LESSTHAN : return "OP_LESSTHAN";
case OP_GREATERTHAN : return "OP_GREATERTHAN";
case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
case OP_MIN : return "OP_MIN";
case OP_MAX : return "OP_MAX";
case OP_WITHIN : return "OP_WITHIN";
// crypto
case OP_RIPEMD160 : return "OP_RIPEMD160";
case OP_SHA1 : return "OP_SHA1";
case OP_SHA256 : return "OP_SHA256";
case OP_HASH160 : return "OP_HASH160";
case OP_HASH256 : return "OP_HASH256";
case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
case OP_CHECKSIG : return "OP_CHECKSIG";
case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
// expanson
case OP_NOP1 : return "OP_NOP1";
case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";
case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";
case OP_NOP4 : return "OP_NOP4";
case OP_NOP5 : return "OP_NOP5";
case OP_NOP6 : return "OP_NOP6";
case OP_NOP7 : return "OP_NOP7";
case OP_NOP8 : return "OP_NOP8";
case OP_NOP9 : return "OP_NOP9";
case OP_NOP10 : return "OP_NOP10";
case OP_CFUND : return "OP_CFUND";
case OP_PROP : return "OP_PROP";
case OP_PREQ : return "OP_PREQ";
case OP_YES : return "OP_YES";
case OP_NO : return "OP_NO";
case OP_ABSTAIN : return "OP_ABSTAIN";
case OP_REMOVE : return "OP_REMOVE";
case OP_DAO : return "OP_DAO";
case OP_ANSWER : return "OP_ANSWER";
case OP_CONSULTATION : return "OP_CONSULTATION";
case OP_COINSTAKE : return "OP_COINSTAKE";
case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
case OP_POOL : return "OP_POOL";
// Note:
// The template matching params OP_SMALLDATA/etc are defined in opcodetype enum
// as kind of implementation hack, they are *NOT* real opcodes. If found in real
// Script, just let the default: case deal with them.
default:
return "OP_UNKNOWN";
}
}
unsigned int CScript::GetSigOpCount(bool fAccurate) const
{
unsigned int n = 0;
const_iterator pc = begin();
opcodetype lastOpcode = OP_INVALIDOPCODE;
while (pc < end())
{
opcodetype opcode;
if (!GetOp(pc, opcode))
break;
if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
n++;
else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
{
if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
n += DecodeOP_N(lastOpcode);
else
n += MAX_PUBKEYS_PER_MULTISIG;
}
lastOpcode = opcode;
}
return n;
}
unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
{
if (!IsPayToScriptHash())
return GetSigOpCount(true);
// This is a pay-to-script-hash scriptPubKey;
// get the last item that the scriptSig
// pushes onto the stack:
const_iterator pc = scriptSig.begin();
vector<unsigned char> data;
while (pc < scriptSig.end())
{
opcodetype opcode;
if (!scriptSig.GetOp(pc, opcode, data))
return 0;
if (opcode > OP_16)
return 0;
}
/// ... and return its opcount:
CScript subscript(data.begin(), data.end());
return subscript.GetSigOpCount(true);
}
bool CScript::IsColdStaking() const
{
return (this->size() == 1+1+25+1+25+1 &&
(*this)[0] == OP_COINSTAKE &&
(*this)[1] == OP_IF &&
(*this)[2] == OP_DUP &&
(*this)[3] == OP_HASH160 &&
(*this)[4] == 0x14 &&
(*this)[25] == OP_EQUALVERIFY &&
(*this)[26] == OP_CHECKSIG &&
(*this)[27] == OP_ELSE &&
(*this)[28] == OP_DUP &&
(*this)[29] == OP_HASH160 &&
(*this)[30] == 0x14 &&
(*this)[51] == OP_EQUALVERIFY &&
(*this)[52] == OP_CHECKSIG &&
(*this)[53] == OP_ENDIF);
}
bool CScript::IsColdStakingv2() const
{
return (this->size() == 1+1+25+1+25+1+22 &&
(*this)[0] == 0x14 &&
(*this)[21] == OP_DROP &&
(*this)[22] == OP_COINSTAKE &&
(*this)[23] == OP_IF &&
(*this)[24] == OP_DUP &&
(*this)[25] == OP_HASH160 &&
(*this)[26] == 0x14 &&
(*this)[47] == OP_EQUALVERIFY &&
(*this)[48] == OP_CHECKSIG &&
(*this)[49] == OP_ELSE &&
(*this)[50] == OP_DUP &&
(*this)[51] == OP_HASH160 &&
(*this)[52] == 0x14 &&
(*this)[73] == OP_EQUALVERIFY &&
(*this)[74] == OP_CHECKSIG &&
(*this)[75] == OP_ENDIF);
}
bool CScript::IsPayToPublicKeyHash() const
{
// Extra-fast test for pay-to-pubkey CScripts:
return (this->size() == 25 &&
(*this)[0] == OP_DUP &&
(*this)[1] == OP_HASH160 &&
(*this)[2] == 0x14 &&
(*this)[23] == OP_EQUALVERIFY &&
(*this)[24] == OP_CHECKSIG);
}
bool CScript::IsPayToPublicKey() const
{
// Extra-fast test for pay-to-pubkey-hash CScripts:
return (this->size() == 35 &&
(*this)[0] == 0x21 &&
(*this)[34] == OP_CHECKSIG);
}
bool CScript::IsCommunityFundContribution() const
{
return (this->size() == 2 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND);
}
bool CScript::IsSupportVote() const
{
return IsSupportVoteYes() || IsSupportVoteRemove();
}
bool CScript::IsConsultationVote() const
{
return IsConsultationVoteAnswer() || IsConsultationVoteAbstention() || IsConsultationVoteRemove();
}
bool CScript::IsConsultationVoteAnswer() const
{
return (this->size() >= 36 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CONSULTATION &&
(*this)[2] == OP_ANSWER &&
(*this)[3] == 0x20);
}
bool CScript::IsConsultationVoteAbstention() const
{
return (this->size() == 36 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CONSULTATION &&
(*this)[2] == OP_ABSTAIN &&
(*this)[3] == 0x20);
}
bool CScript::IsConsultationVoteRemove() const
{
return (this->size() == 36 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CONSULTATION &&
(*this)[2] == OP_REMOVE &&
(*this)[3] == 0x20);
}
bool CScript::IsSupportVoteYes() const
{
return (this->size() == 36 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_DAO &&
(*this)[2] == OP_YES &&
(*this)[3] == 0x20);
}
bool CScript::IsSupportVoteRemove() const
{
return (this->size() == 36 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_DAO &&
(*this)[2] == OP_REMOVE &&
(*this)[3] == 0x20);
}
bool CScript::IsProposalVote() const
{
return IsProposalVoteYes() || IsProposalVoteNo() || IsProposalVoteAbs() || IsProposalVoteRemove();
}
bool CScript::IsProposalVoteYes() const
{
return (this->size() == 37 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND &&
(*this)[2] == OP_PROP &&
(*this)[3] == OP_YES &&
(*this)[4] == 0x20);
}
bool CScript::IsProposalVoteNo() const
{
return (this->size() == 37 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND &&
(*this)[2] == OP_PROP &&
(*this)[3] == OP_NO &&
(*this)[4] == 0x20);
}
bool CScript::IsProposalVoteAbs() const
{
return (this->size() == 37 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND &&
(*this)[2] == OP_PROP &&
(*this)[3] == OP_ABSTAIN &&
(*this)[4] == 0x20);
}
bool CScript::IsProposalVoteRemove() const
{
return (this->size() == 37 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND &&
(*this)[2] == OP_PROP &&
(*this)[3] == OP_REMOVE &&
(*this)[4] == 0x20);
}
bool CScript::IsPaymentRequestVote() const
{
return IsPaymentRequestVoteYes() || IsPaymentRequestVoteNo() || IsPaymentRequestVoteAbs() || IsPaymentRequestVoteRemove();
}
bool CScript::IsPaymentRequestVoteYes() const
{
return (this->size() == 37 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND &&
(*this)[2] == OP_PREQ &&
(*this)[3] == OP_YES &&
(*this)[4] == 0x20);
}
bool CScript::IsPaymentRequestVoteNo() const
{
return (this->size() == 37 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND &&
(*this)[2] == OP_PREQ &&
(*this)[3] == OP_NO &&
(*this)[4] == 0x20);
}
bool CScript::IsPaymentRequestVoteAbs() const
{
return (this->size() == 37 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND &&
(*this)[2] == OP_PREQ &&
(*this)[3] == OP_ABSTAIN &&
(*this)[4] == 0x20);
}
bool CScript::IsPaymentRequestVoteRemove() const
{
return (this->size() == 37 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_CFUND &&
(*this)[2] == OP_PREQ &&
(*this)[3] == OP_REMOVE &&
(*this)[4] == 0x20);
}
bool CScript::IsPool() const
{
return (this->size() == 2 &&
(*this)[0] == OP_RETURN &&
(*this)[1] == OP_POOL);
}
bool CScript::ExtractVote(uint256 &hash, int64_t &vote) const
{
if(!IsPaymentRequestVote() && !IsProposalVote())
return false;
vector<unsigned char> vHash(this->begin()+5, this->begin()+37);
hash = uint256(vHash);
vote = (*this)[3] == OP_YES ? VoteFlags::VOTE_YES : VoteFlags::VOTE_NO;
if ((*this)[3] == OP_ABSTAIN)
vote = VoteFlags::VOTE_ABSTAIN;
if ((*this)[3] == OP_REMOVE)
vote = VoteFlags::VOTE_REMOVE;
return true;
}
bool CScript::ExtractSupportVote(uint256 &hash, int64_t &vote) const
{
if (!IsSupportVote())
return false;
vector<unsigned char> vHash(this->begin()+4, this->begin()+36);
hash = uint256(vHash);
vote = 0;
if ((*this)[2] == OP_REMOVE)
vote = VoteFlags::SUPPORT_REMOVE;
if ((*this)[2] == OP_YES)
vote = VoteFlags::SUPPORT;
return true;
}
bool CScript::ExtractConsultationVote(uint256 &hash, int64_t &vote) const
{
if (!IsConsultationVote())
return false;
vector<unsigned char> vHash(this->begin()+4, this->begin()+36);
hash = uint256(vHash);
vote = 0;
if ((*this)[2] == OP_REMOVE)
vote = VoteFlags::VOTE_REMOVE;
else if ((*this)[2] == OP_ABSTAIN)
vote = VoteFlags::VOTE_ABSTAIN;
else if (this->size() > 36)
{
if (this->size() == 37 && (*this)[36] == 0x00)
vote = 0;
else {
vector<unsigned char> vVote(this->begin()+37, this->end());
CScriptNum nVote(vVote, false);
vote = nVote.getint();
}
}
else
{
vote = true;
}
return true;
}
bool CScript::IsPayToScriptHash() const
{
// Extra-fast test for pay-to-script-hash CScripts:
return (this->size() == 23 &&
(*this)[0] == OP_HASH160 &&
(*this)[1] == 0x14 &&
(*this)[22] == OP_EQUAL);
}
bool CScript::IsPayToWitnessScriptHash() const
{
// Extra-fast test for pay-to-witness-script-hash CScripts:
return (this->size() == 34 &&
(*this)[0] == OP_0 &&
(*this)[1] == 0x20);
}
// A witness program is any valid CScript that consists of a 1-byte push opcode
// followed by a data push between 2 and 40 bytes.
bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
{
if (this->size() < 4 || this->size() > 42) {
return false;
}
if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
return false;
}
if ((size_t)((*this)[1] + 2) == this->size()) {
version = DecodeOP_N((opcodetype)(*this)[0]);
program = std::vector<unsigned char>(this->begin() + 2, this->end());
return true;
}
return false;
}
bool CScript::IsPushOnly(const_iterator pc) const
{
while (pc < end())
{
opcodetype opcode;
if (!GetOp(pc, opcode))
return false;
// Note that IsPushOnly() *does* consider OP_RESERVED to be a
// push-type opcode, however execution of OP_RESERVED fails, so
// it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
// the P2SH special validation code being executed.
if (opcode > OP_16)
return false;
}
return true;
}
bool CScript::IsPushOnly() const
{
return this->IsPushOnly(begin());
}
bool CScript::GetStakerScript(std::vector<unsigned char>& script) const
{
if (IsColdStakingv2())
{
script = std::vector<unsigned char>(this->begin(), this->begin()+20);
return true;
}
else if (IsPayToPublicKeyHash())
{
script = std::vector<unsigned char>(this->begin()+2, this->begin()+22);
return true;
}
script = std::vector<unsigned char>(this->begin(),this->end());
return true;
}
std::string CScriptWitness::ToString() const
{
std::string ret = "CScriptWitness(";
for (unsigned int i = 0; i < stack.size(); i++) {
if (i) {
ret += ", ";
}
ret += HexStr(stack[i]);
}
return ret + ")";
}