forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard.go
1442 lines (1260 loc) · 42.1 KB
/
standard.go
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txscript
import (
"encoding/binary"
"fmt"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/chaincfg/chainec"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil"
)
const (
// MaxDataCarrierSize is the maximum number of bytes allowed in pushed
// data to be considered a nulldata transaction.
MaxDataCarrierSize = 256
)
// ScriptClass is an enumeration for the list of standard types of script.
type ScriptClass byte
// Classes of script payment known about in the blockchain.
const (
NonStandardTy ScriptClass = iota // None of the recognized forms.
PubKeyTy // Pay pubkey.
PubKeyHashTy // Pay pubkey hash.
ScriptHashTy // Pay to script hash.
MultiSigTy // Multi signature.
NullDataTy // Empty data-only (provably prunable).
StakeSubmissionTy // Stake submission.
StakeGenTy // Stake generation
StakeRevocationTy // Stake revocation.
StakeSubChangeTy // Change for stake submission tx.
PubkeyAltTy // Alternative signature pubkey.
PubkeyHashAltTy // Alternative signature pubkey hash.
)
// scriptClassToName houses the human-readable strings which describe each
// script class.
var scriptClassToName = []string{
NonStandardTy: "nonstandard",
PubKeyTy: "pubkey",
PubkeyAltTy: "pubkeyalt",
PubKeyHashTy: "pubkeyhash",
PubkeyHashAltTy: "pubkeyhashalt",
ScriptHashTy: "scripthash",
MultiSigTy: "multisig",
NullDataTy: "nulldata",
StakeSubmissionTy: "stakesubmission",
StakeGenTy: "stakegen",
StakeRevocationTy: "stakerevoke",
StakeSubChangeTy: "sstxchange",
}
// String implements the Stringer interface by returning the name of
// the enum script class. If the enum is invalid then "Invalid" will be
// returned.
func (t ScriptClass) String() string {
if int(t) > len(scriptClassToName) || int(t) < 0 {
return "Invalid"
}
return scriptClassToName[t]
}
// isPubkey returns true if the script passed is a pay-to-pubkey transaction,
// false otherwise.
func isPubkey(pops []parsedOpcode) bool {
// Valid pubkeys are either 33 or 65 bytes.
return len(pops) == 2 &&
(len(pops[0].data) == 33 || len(pops[0].data) == 65) &&
pops[1].opcode.value == OP_CHECKSIG
}
// isOneByteMaxDataPush returns true if the parsed opcode pushes exactly one
// byte to the stack.
func isOneByteMaxDataPush(po parsedOpcode) bool {
return po.opcode.value == OP_1 ||
po.opcode.value == OP_2 ||
po.opcode.value == OP_3 ||
po.opcode.value == OP_4 ||
po.opcode.value == OP_5 ||
po.opcode.value == OP_6 ||
po.opcode.value == OP_7 ||
po.opcode.value == OP_8 ||
po.opcode.value == OP_9 ||
po.opcode.value == OP_10 ||
po.opcode.value == OP_11 ||
po.opcode.value == OP_12 ||
po.opcode.value == OP_13 ||
po.opcode.value == OP_14 ||
po.opcode.value == OP_15 ||
po.opcode.value == OP_16 ||
po.opcode.value == OP_DATA_1
}
// isPubkey returns true if the script passed is an alternative pay-to-pubkey
// transaction, false otherwise.
func isPubkeyAlt(pops []parsedOpcode) bool {
// An alternative pubkey must be less than 512 bytes.
return len(pops) == 3 &&
len(pops[0].data) < 512 &&
isOneByteMaxDataPush(pops[1]) &&
pops[2].opcode.value == OP_CHECKSIGALT
}
// isPubkeyHash returns true if the script passed is a pay-to-pubkey-hash
// transaction, false otherwise.
func isPubkeyHash(pops []parsedOpcode) bool {
return len(pops) == 5 &&
pops[0].opcode.value == OP_DUP &&
pops[1].opcode.value == OP_HASH160 &&
pops[2].opcode.value == OP_DATA_20 &&
pops[3].opcode.value == OP_EQUALVERIFY &&
pops[4].opcode.value == OP_CHECKSIG
}
// isPubkeyHashAlt returns true if the script passed is a pay-to-pubkey-hash
// transaction, false otherwise.
func isPubkeyHashAlt(pops []parsedOpcode) bool {
return len(pops) == 6 &&
pops[0].opcode.value == OP_DUP &&
pops[1].opcode.value == OP_HASH160 &&
pops[2].opcode.value == OP_DATA_20 &&
pops[3].opcode.value == OP_EQUALVERIFY &&
isOneByteMaxDataPush(pops[4]) &&
pops[5].opcode.value == OP_CHECKSIGALT
}
// isScriptHash returns true if the script passed is a pay-to-script-hash
// transaction, false otherwise.
func isScriptHash(pops []parsedOpcode) bool {
return len(pops) == 3 &&
pops[0].opcode.value == OP_HASH160 &&
pops[1].opcode.value == OP_DATA_20 &&
pops[2].opcode.value == OP_EQUAL
}
// isAnyKindOfScriptHash returns true if the script passed is a pay-to-script-hash
// or stake pay-to-script-hash transaction, false otherwise. Used to make the
// engine have the correct behaviour.
func isAnyKindOfScriptHash(pops []parsedOpcode) bool {
standardP2SH := len(pops) == 3 &&
pops[0].opcode.value == OP_HASH160 &&
pops[1].opcode.value == OP_DATA_20 &&
pops[2].opcode.value == OP_EQUAL
if standardP2SH {
return true
}
return len(pops) == 4 &&
(pops[0].opcode.value >= 186 && pops[0].opcode.value <= 189) &&
pops[1].opcode.value == OP_HASH160 &&
pops[2].opcode.value == OP_DATA_20 &&
pops[3].opcode.value == OP_EQUAL
}
// isMultiSig returns true if the passed script is a multisig transaction, false
// otherwise.
func isMultiSig(pops []parsedOpcode) bool {
// The absolute minimum is 1 pubkey:
// OP_0/OP_1-16 <pubkey> OP_1 OP_CHECKMULTISIG
l := len(pops)
if l < 4 {
return false
}
if !isSmallInt(pops[0].opcode) {
return false
}
if !isSmallInt(pops[l-2].opcode) {
return false
}
if pops[l-1].opcode.value != OP_CHECKMULTISIG {
return false
}
// Verify the number of pubkeys specified matches the actual number
// of pubkeys provided.
if l-2-1 != asSmallInt(pops[l-2].opcode) {
return false
}
for _, pop := range pops[1 : l-2] {
// Valid pubkeys are either 33 or 65 bytes.
if len(pop.data) != 33 && len(pop.data) != 65 {
return false
}
}
return true
}
// IsMultisigScript takes a script, parses it, then returns whether or
// not it is a multisignature script.
func IsMultisigScript(script []byte) (bool, error) {
pops, err := parseScript(script)
if err != nil {
return false, err
}
return isMultiSig(pops), nil
}
// IsMultisigSigScript takes a script, parses it, then returns whether or
// not it is a multisignature script.
func IsMultisigSigScript(script []byte) bool {
if len(script) == 0 || script == nil {
return false
}
pops, err := parseScript(script)
if err != nil {
return false
}
subPops, err := parseScript(pops[len(pops)-1].data)
if err != nil {
return false
}
return isMultiSig(subPops)
}
// isNullData returns true if the passed script is a null data transaction,
// false otherwise.
func isNullData(pops []parsedOpcode) bool {
// A nulldata transaction is either a single OP_RETURN or an
// OP_RETURN SMALLDATA (where SMALLDATA is a data push up to
// MaxDataCarrierSize bytes).
l := len(pops)
if l == 1 && pops[0].opcode.value == OP_RETURN {
return true
}
return l == 2 &&
pops[0].opcode.value == OP_RETURN &&
(isSmallInt(pops[1].opcode) || pops[1].opcode.value <=
OP_PUSHDATA4) &&
len(pops[1].data) <= MaxDataCarrierSize
}
// isStakeSubmission returns true if the script passed is a stake submission tx,
// false otherwise.
func isStakeSubmission(pops []parsedOpcode) bool {
if len(pops) == 6 &&
pops[0].opcode.value == OP_SSTX &&
pops[1].opcode.value == OP_DUP &&
pops[2].opcode.value == OP_HASH160 &&
pops[3].opcode.value == OP_DATA_20 &&
pops[4].opcode.value == OP_EQUALVERIFY &&
pops[5].opcode.value == OP_CHECKSIG {
return true
}
if len(pops) == 4 &&
pops[0].opcode.value == OP_SSTX &&
pops[1].opcode.value == OP_HASH160 &&
pops[2].opcode.value == OP_DATA_20 &&
pops[3].opcode.value == OP_EQUAL {
return true
}
return false
}
// isStakeGen returns true if the script passed is a stake generation tx,
// false otherwise.
func isStakeGen(pops []parsedOpcode) bool {
if len(pops) == 6 &&
pops[0].opcode.value == OP_SSGEN &&
pops[1].opcode.value == OP_DUP &&
pops[2].opcode.value == OP_HASH160 &&
pops[3].opcode.value == OP_DATA_20 &&
pops[4].opcode.value == OP_EQUALVERIFY &&
pops[5].opcode.value == OP_CHECKSIG {
return true
}
if len(pops) == 4 &&
pops[0].opcode.value == OP_SSGEN &&
pops[1].opcode.value == OP_HASH160 &&
pops[2].opcode.value == OP_DATA_20 &&
pops[3].opcode.value == OP_EQUAL {
return true
}
return false
}
// isStakeRevocation returns true if the script passed is a stake submission
// revocation tx, false otherwise.
func isStakeRevocation(pops []parsedOpcode) bool {
if len(pops) == 6 &&
pops[0].opcode.value == OP_SSRTX &&
pops[1].opcode.value == OP_DUP &&
pops[2].opcode.value == OP_HASH160 &&
pops[3].opcode.value == OP_DATA_20 &&
pops[4].opcode.value == OP_EQUALVERIFY &&
pops[5].opcode.value == OP_CHECKSIG {
return true
}
if len(pops) == 4 &&
pops[0].opcode.value == OP_SSRTX &&
pops[1].opcode.value == OP_HASH160 &&
pops[2].opcode.value == OP_DATA_20 &&
pops[3].opcode.value == OP_EQUAL {
return true
}
return false
}
// isSStxChange returns true if the script passed is a stake submission
// change tx, false otherwise.
func isSStxChange(pops []parsedOpcode) bool {
if len(pops) == 6 &&
pops[0].opcode.value == OP_SSTXCHANGE &&
pops[1].opcode.value == OP_DUP &&
pops[2].opcode.value == OP_HASH160 &&
pops[3].opcode.value == OP_DATA_20 &&
pops[4].opcode.value == OP_EQUALVERIFY &&
pops[5].opcode.value == OP_CHECKSIG {
return true
}
if len(pops) == 4 &&
pops[0].opcode.value == OP_SSTXCHANGE &&
pops[1].opcode.value == OP_HASH160 &&
pops[2].opcode.value == OP_DATA_20 &&
pops[3].opcode.value == OP_EQUAL {
return true
}
return false
}
// scriptType returns the type of the script being inspected from the known
// standard types.
func typeOfScript(pops []parsedOpcode) ScriptClass {
if isPubkey(pops) {
return PubKeyTy
} else if isPubkeyAlt(pops) {
return PubkeyAltTy
} else if isPubkeyHash(pops) {
return PubKeyHashTy
} else if isPubkeyHashAlt(pops) {
return PubkeyHashAltTy
} else if isScriptHash(pops) {
return ScriptHashTy
} else if isMultiSig(pops) {
return MultiSigTy
} else if isNullData(pops) {
return NullDataTy
} else if isStakeSubmission(pops) {
return StakeSubmissionTy
} else if isStakeGen(pops) {
return StakeGenTy
} else if isStakeRevocation(pops) {
return StakeRevocationTy
} else if isSStxChange(pops) {
return StakeSubChangeTy
}
return NonStandardTy
}
// GetScriptClass returns the class of the script passed.
//
// NonStandardTy will be returned when the script does not parse.
func GetScriptClass(version uint16, script []byte) ScriptClass {
// NullDataTy outputs are allowed to have non-default script
// versions. However, other types are not.
if version != DefaultScriptVersion {
return NonStandardTy
}
pops, err := parseScript(script)
if err != nil {
return NonStandardTy
}
return typeOfScript(pops)
}
// expectedInputs returns the number of arguments required by a script.
// If the script is of unknown type such that the number can not be determined
// then -1 is returned. We are an internal function and thus assume that class
// is the real class of pops (and we can thus assume things that were determined
// while finding out the type).
func expectedInputs(pops []parsedOpcode, class ScriptClass,
subclass ScriptClass) int {
switch class {
case PubKeyTy:
return 1
case PubKeyHashTy:
return 2
case StakeSubmissionTy:
if subclass == PubKeyHashTy {
return 2
}
return 1 // P2SH
case StakeGenTy:
if subclass == PubKeyHashTy {
return 2
}
return 1 // P2SH
case StakeRevocationTy:
if subclass == PubKeyHashTy {
return 2
}
return 1 // P2SH
case StakeSubChangeTy:
if subclass == PubKeyHashTy {
return 2
}
return 1 // P2SH
case ScriptHashTy:
// Not including script, handled below.
return 1
case MultiSigTy:
// Standard multisig has a push a small number for the number
// of sigs and number of keys. Check the first push instruction
// to see how many arguments are expected. typeOfScript already
// checked this so we know it'll be a small int. Also, due to
// the original bitcoind bug where OP_CHECKMULTISIG pops an
// additional item from the stack, add an extra expected input
// for the extra push that is required to compensate.
return asSmallInt(pops[0].opcode)
case NullDataTy:
fallthrough
default:
return -1
}
}
// ScriptInfo houses information about a script pair that is determined by
// CalcScriptInfo.
type ScriptInfo struct {
// PkScriptClass is the class of the public key script and is equivalent
// to calling GetScriptClass on it.
PkScriptClass ScriptClass
// NumInputs is the number of inputs provided by the public key script.
NumInputs int
// ExpectedInputs is the number of outputs required by the signature
// script and any pay-to-script-hash scripts. The number will be -1 if
// unknown.
ExpectedInputs int
// SigOps is the number of signature operations in the script pair.
SigOps int
}
// IsStakeOutput returns true is a script output is a stake type.
func IsStakeOutput(pkScript []byte) bool {
pkPops, err := parseScript(pkScript)
if err != nil {
return false
}
class := typeOfScript(pkPops)
return class == StakeSubmissionTy ||
class == StakeGenTy ||
class == StakeRevocationTy ||
class == StakeSubChangeTy
}
// GetStakeOutSubclass extracts the subclass (P2PKH or P2SH)
// from a stake output.
func GetStakeOutSubclass(pkScript []byte) (ScriptClass, error) {
pkPops, err := parseScript(pkScript)
if err != nil {
return 0, err
}
class := typeOfScript(pkPops)
isStake := class == StakeSubmissionTy ||
class == StakeGenTy ||
class == StakeRevocationTy ||
class == StakeSubChangeTy
subClass := ScriptClass(0)
if isStake {
var stakeSubscript []parsedOpcode
for _, pop := range pkPops {
if pop.opcode.value >= 186 && pop.opcode.value <= 189 {
continue
}
stakeSubscript = append(stakeSubscript, pop)
}
subClass = typeOfScript(stakeSubscript)
} else {
return 0, fmt.Errorf("not a stake output")
}
return subClass, nil
}
// getStakeOutSubscript extracts the subscript (P2PKH or P2SH)
// from a stake output.
func getStakeOutSubscript(pkScript []byte) []byte {
return pkScript[1:]
}
// ContainsStakeOpCodes returns whether or not a pkScript contains stake tagging
// OP codes.
func ContainsStakeOpCodes(pkScript []byte) (bool, error) {
shPops, err := parseScript(pkScript)
if err != nil {
return false, err
}
for _, pop := range shPops {
if pop.opcode.value >= 186 && pop.opcode.value <= 189 {
return true, nil
}
}
return false, nil
}
// CalcScriptInfo returns a structure providing data about the provided script
// pair. It will error if the pair is in someway invalid such that they can not
// be analysed, i.e. if they do not parse or the pkScript is not a push-only
// script
func CalcScriptInfo(sigScript, pkScript []byte, bip16 bool) (*ScriptInfo, error) {
sigPops, err := parseScript(sigScript)
if err != nil {
return nil, err
}
pkPops, err := parseScript(pkScript)
if err != nil {
return nil, err
}
// Push only sigScript makes little sense.
si := new(ScriptInfo)
si.PkScriptClass = typeOfScript(pkPops)
// Can't have a pkScript that doesn't just push data.
if !isPushOnly(sigPops) {
return nil, ErrStackNonPushOnly
}
subClass := ScriptClass(0)
if si.PkScriptClass == StakeSubmissionTy ||
si.PkScriptClass == StakeGenTy ||
si.PkScriptClass == StakeRevocationTy ||
si.PkScriptClass == StakeSubChangeTy {
subClass, err = GetStakeOutSubclass(pkScript)
if err != nil {
return nil, err
}
}
si.ExpectedInputs = expectedInputs(pkPops, si.PkScriptClass, subClass)
// All entries pushed to stack (or are OP_RESERVED and exec will fail).
si.NumInputs = len(sigPops)
// Count sigops taking into account pay-to-script-hash.
if (si.PkScriptClass == ScriptHashTy || subClass == ScriptHashTy) && bip16 {
// The pay-to-hash-script is the final data push of the
// signature script.
script := sigPops[len(sigPops)-1].data
shPops, err := parseScript(script)
if err != nil {
return nil, err
}
shInputs := expectedInputs(shPops, typeOfScript(shPops), 0)
if shInputs == -1 {
si.ExpectedInputs = -1
} else {
si.ExpectedInputs += shInputs
}
si.SigOps = getSigOpCount(shPops, true)
} else {
si.SigOps = getSigOpCount(pkPops, true)
}
return si, nil
}
// CalcMultiSigStats returns the number of public keys and signatures from
// a multi-signature transaction script. The passed script MUST already be
// known to be a multi-signature script.
func CalcMultiSigStats(script []byte) (int, int, error) {
pops, err := parseScript(script)
if err != nil {
return 0, 0, err
}
// A multi-signature script is of the pattern:
// NUM_SIGS PUBKEY PUBKEY PUBKEY... NUM_PUBKEYS OP_CHECKMULTISIG
// Therefore the number of signatures is the oldest item on the stack
// and the number of pubkeys is the 2nd to last. Also, the absolute
// minimum for a multi-signature script is 1 pubkey, so at least 4
// items must be on the stack per:
// OP_1 PUBKEY OP_1 OP_CHECKMULTISIG
if len(pops) < 4 {
return 0, 0, ErrStackUnderflow
}
numSigs := asSmallInt(pops[0].opcode)
numPubKeys := asSmallInt(pops[len(pops)-2].opcode)
return numPubKeys, numSigs, nil
}
// MultisigRedeemScriptFromScriptSig attempts to extract a multi-
// signature redeem script from a P2SH-redeeming input. It returns
// nil if the signature script is not a multisignature script.
func MultisigRedeemScriptFromScriptSig(script []byte) ([]byte, error) {
pops, err := parseScript(script)
if err != nil {
return nil, err
}
// The redeemScript is always the last item on the stack of
// the script sig.
return pops[len(pops)-1].data, nil
}
// payToPubKeyHashScript creates a new script to pay a transaction
// output to a 20-byte pubkey hash. It is expected that the input is a valid
// hash.
func payToPubKeyHashScript(pubKeyHash []byte) ([]byte, error) {
return NewScriptBuilder().AddOp(OP_DUP).AddOp(OP_HASH160).
AddData(pubKeyHash).AddOp(OP_EQUALVERIFY).AddOp(OP_CHECKSIG).
Script()
}
// payToPubKeyHashEdwardsScript creates a new script to pay a transaction
// output to a 20-byte pubkey hash of an Edwards public key. It is expected
// that the input is a valid hash.
func payToPubKeyHashEdwardsScript(pubKeyHash []byte) ([]byte, error) {
edwardsData := []byte{byte(edwards)}
return NewScriptBuilder().AddOp(OP_DUP).AddOp(OP_HASH160).
AddData(pubKeyHash).AddOp(OP_EQUALVERIFY).AddData(edwardsData).
AddOp(OP_CHECKSIGALT).Script()
}
// payToPubKeyHashSchnorrScript creates a new script to pay a transaction
// output to a 20-byte pubkey hash of a secp256k1 public key, but expecting
// a schnorr signature instead of a classic secp256k1 signature. It is
// expected that the input is a valid hash.
func payToPubKeyHashSchnorrScript(pubKeyHash []byte) ([]byte, error) {
schnorrData := []byte{byte(secSchnorr)}
return NewScriptBuilder().AddOp(OP_DUP).AddOp(OP_HASH160).
AddData(pubKeyHash).AddOp(OP_EQUALVERIFY).AddData(schnorrData).
AddOp(OP_CHECKSIGALT).Script()
}
// payToScriptHashScript creates a new script to pay a transaction output to a
// script hash. It is expected that the input is a valid hash.
func payToScriptHashScript(scriptHash []byte) ([]byte, error) {
return NewScriptBuilder().AddOp(OP_HASH160).AddData(scriptHash).
AddOp(OP_EQUAL).Script()
}
// GetScriptHashFromP2SHScript extracts the script hash from a valid
// P2SH pkScript.
func GetScriptHashFromP2SHScript(pkScript []byte) ([]byte, error) {
pops, err := parseScript(pkScript)
if err != nil {
return nil, err
}
var sh []byte
reachedHash160DataPush := false
for _, p := range pops {
if p.opcode.value == OP_HASH160 {
reachedHash160DataPush = true
continue
}
if reachedHash160DataPush {
sh = p.data
break
}
}
return sh, nil
}
// PayToScriptHashScript is the exported version of payToScriptHashScript.
func PayToScriptHashScript(scriptHash []byte) ([]byte, error) {
return payToScriptHashScript(scriptHash)
}
// payToPubkeyScript creates a new script to pay a transaction output to a
// public key. It is expected that the input is a valid pubkey.
func payToPubKeyScript(serializedPubKey []byte) ([]byte, error) {
return NewScriptBuilder().AddData(serializedPubKey).
AddOp(OP_CHECKSIG).Script()
}
// payToEdwardsPubKeyScript creates a new script to pay a transaction output
// to an Ed25519 public key. It is expected that the input is a valid pubkey.
func payToEdwardsPubKeyScript(serializedPubKey []byte) ([]byte, error) {
edwardsData := []byte{byte(edwards)}
return NewScriptBuilder().AddData(serializedPubKey).AddData(edwardsData).
AddOp(OP_CHECKSIGALT).Script()
}
// payToSchnorrPubKeyScript creates a new script to pay a transaction output
// to a secp256k1 public key, but to be signed by Schnorr type signature. It
// is expected that the input is a valid pubkey.
func payToSchnorrPubKeyScript(serializedPubKey []byte) ([]byte, error) {
schnorrData := []byte{byte(secSchnorr)}
return NewScriptBuilder().AddData(serializedPubKey).AddData(schnorrData).
AddOp(OP_CHECKSIGALT).Script()
}
// PayToSStx creates a new script to pay a transaction output to a script hash or
// public key hash, but tags the output with OP_SSTX. For use in constructing
// valid SStxs.
func PayToSStx(addr dcrutil.Address) ([]byte, error) {
if addr == nil {
return nil, ErrUnsupportedAddress
}
// Only pay to pubkey hash and pay to script hash are
// supported.
scriptType := PubKeyHashTy
switch addr := addr.(type) {
case *dcrutil.AddressPubKeyHash:
if addr.DSA(addr.Net()) != chainec.ECTypeSecp256k1 {
return nil, ErrUnsupportedAddress
}
case *dcrutil.AddressScriptHash:
scriptType = ScriptHashTy
default:
return nil, ErrUnsupportedAddress
}
hash := addr.ScriptAddress()
if scriptType == PubKeyHashTy {
return NewScriptBuilder().AddOp(OP_SSTX).AddOp(OP_DUP).
AddOp(OP_HASH160).AddData(hash).AddOp(OP_EQUALVERIFY).
AddOp(OP_CHECKSIG).Script()
}
return NewScriptBuilder().AddOp(OP_SSTX).AddOp(OP_HASH160).
AddData(hash).AddOp(OP_EQUAL).Script()
}
// PayToSStxChange creates a new script to pay a transaction output to a
// public key hash, but tags the output with OP_SSTXCHANGE. For use in constructing
// valid SStxs.
func PayToSStxChange(addr dcrutil.Address) ([]byte, error) {
if addr == nil {
return nil, ErrUnsupportedAddress
}
// Only pay to pubkey hash and pay to script hash are
// supported.
scriptType := PubKeyHashTy
switch addr := addr.(type) {
case *dcrutil.AddressPubKeyHash:
if addr.DSA(addr.Net()) != chainec.ECTypeSecp256k1 {
return nil, ErrUnsupportedAddress
}
case *dcrutil.AddressScriptHash:
scriptType = ScriptHashTy
default:
return nil, ErrUnsupportedAddress
}
hash := addr.ScriptAddress()
if scriptType == PubKeyHashTy {
return NewScriptBuilder().AddOp(OP_SSTXCHANGE).AddOp(OP_DUP).
AddOp(OP_HASH160).AddData(hash).AddOp(OP_EQUALVERIFY).
AddOp(OP_CHECKSIG).Script()
}
return NewScriptBuilder().AddOp(OP_SSTXCHANGE).AddOp(OP_HASH160).
AddData(hash).AddOp(OP_EQUAL).Script()
}
// PayToSSGen creates a new script to pay a transaction output to a public key
// hash or script hash, but tags the output with OP_SSGEN. For use in constructing
// valid SSGen txs.
func PayToSSGen(addr dcrutil.Address) ([]byte, error) {
if addr == nil {
return nil, ErrUnsupportedAddress
}
// Only pay to pubkey hash and pay to script hash are
// supported.
scriptType := PubKeyHashTy
switch addr := addr.(type) {
case *dcrutil.AddressPubKeyHash:
if addr.DSA(addr.Net()) != chainec.ECTypeSecp256k1 {
return nil, ErrUnsupportedAddress
}
case *dcrutil.AddressScriptHash:
scriptType = ScriptHashTy
default:
return nil, ErrUnsupportedAddress
}
hash := addr.ScriptAddress()
if scriptType == PubKeyHashTy {
return NewScriptBuilder().AddOp(OP_SSGEN).AddOp(OP_DUP).
AddOp(OP_HASH160).AddData(hash).AddOp(OP_EQUALVERIFY).
AddOp(OP_CHECKSIG).Script()
}
return NewScriptBuilder().AddOp(OP_SSGEN).AddOp(OP_HASH160).
AddData(hash).AddOp(OP_EQUAL).Script()
}
// PayToSSGenPKHDirect creates a new script to pay a transaction output to a
// public key hash, but tags the output with OP_SSGEN. For use in constructing
// valid SSGen txs. Unlike PayToSSGen, this function directly uses the HASH160
// pubkeyhash (instead of an address).
func PayToSSGenPKHDirect(pkh []byte) ([]byte, error) {
if pkh == nil {
return nil, ErrUnsupportedAddress
}
return NewScriptBuilder().AddOp(OP_SSGEN).AddOp(OP_DUP).
AddOp(OP_HASH160).AddData(pkh).AddOp(OP_EQUALVERIFY).
AddOp(OP_CHECKSIG).Script()
}
// PayToSSGenSHDirect creates a new script to pay a transaction output to a
// script hash, but tags the output with OP_SSGEN. For use in constructing
// valid SSGen txs. Unlike PayToSSGen, this function directly uses the HASH160
// script hash (instead of an address).
func PayToSSGenSHDirect(sh []byte) ([]byte, error) {
if sh == nil {
return nil, ErrUnsupportedAddress
}
return NewScriptBuilder().AddOp(OP_SSGEN).AddOp(OP_HASH160).
AddData(sh).AddOp(OP_EQUAL).Script()
}
// PayToSSRtx creates a new script to pay a transaction output to a
// public key hash, but tags the output with OP_SSRTX. For use in constructing
// valid SSRtx.
func PayToSSRtx(addr dcrutil.Address) ([]byte, error) {
if addr == nil {
return nil, ErrUnsupportedAddress
}
// Only pay to pubkey hash and pay to script hash are
// supported.
scriptType := PubKeyHashTy
switch addr := addr.(type) {
case *dcrutil.AddressPubKeyHash:
if addr.DSA(addr.Net()) != chainec.ECTypeSecp256k1 {
return nil, ErrUnsupportedAddress
}
case *dcrutil.AddressScriptHash:
scriptType = ScriptHashTy
default:
return nil, ErrUnsupportedAddress
}
hash := addr.ScriptAddress()
if scriptType == PubKeyHashTy {
return NewScriptBuilder().AddOp(OP_SSRTX).AddOp(OP_DUP).
AddOp(OP_HASH160).AddData(hash).AddOp(OP_EQUALVERIFY).
AddOp(OP_CHECKSIG).Script()
}
return NewScriptBuilder().AddOp(OP_SSRTX).AddOp(OP_HASH160).
AddData(hash).AddOp(OP_EQUAL).Script()
}
// PayToSSRtxPKHDirect creates a new script to pay a transaction output to a
// public key hash, but tags the output with OP_SSRTX. For use in constructing
// valid SSRtx. Unlike PayToSSRtx, this function directly uses the HASH160
// pubkeyhash (instead of an address).
func PayToSSRtxPKHDirect(pkh []byte) ([]byte, error) {
if pkh == nil {
return nil, ErrUnsupportedAddress
}
return NewScriptBuilder().AddOp(OP_SSRTX).AddOp(OP_DUP).
AddOp(OP_HASH160).AddData(pkh).AddOp(OP_EQUALVERIFY).
AddOp(OP_CHECKSIG).Script()
}
// PayToSSRtxSHDirect creates a new script to pay a transaction output to a
// script hash, but tags the output with OP_SSRTX. For use in constructing
// valid SSRtx. Unlike PayToSSRtx, this function directly uses the HASH160
// script hash (instead of an address).
func PayToSSRtxSHDirect(sh []byte) ([]byte, error) {
if sh == nil {
return nil, ErrUnsupportedAddress
}
return NewScriptBuilder().AddOp(OP_SSRTX).AddOp(OP_HASH160).
AddData(sh).AddOp(OP_EQUAL).Script()
}
// GenerateSStxAddrPush generates an OP_RETURN push for SSGen payment addresses in
// an SStx.
func GenerateSStxAddrPush(addr dcrutil.Address, amount dcrutil.Amount,
limits uint16) ([]byte, error) {
if addr == nil {
return nil, ErrUnsupportedAddress
}
// Only pay to pubkey hash and pay to script hash are
// supported.
scriptType := PubKeyHashTy
switch addr := addr.(type) {
case *dcrutil.AddressPubKeyHash:
if addr.DSA(addr.Net()) != chainec.ECTypeSecp256k1 {
return nil, ErrUnsupportedAddress
}
case *dcrutil.AddressScriptHash:
scriptType = ScriptHashTy
default:
return nil, ErrUnsupportedAddress
}
// Prefix
dataPushes := []byte{
0x6a, // OP_RETURN
0x1e, // OP_DATA_30
}
hash := addr.ScriptAddress()
amountBuffer := make([]byte, 8)
binary.LittleEndian.PutUint64(amountBuffer, uint64(amount))
// Set the bit flag indicating pay to script hash.
if scriptType == ScriptHashTy {
amountBuffer[7] |= 1 << 7
}
limitsBuffer := make([]byte, 2)
binary.LittleEndian.PutUint16(limitsBuffer, limits)
// Concatenate the prefix, pubkeyhash, and amount.
addrOut := append(dataPushes, hash...)
addrOut = append(addrOut, amountBuffer...)
addrOut = append(addrOut, limitsBuffer...)
return addrOut, nil
}
// GenerateSSGenBlockRef generates an OP_RETURN push for the block header hash and
// height which the block votes on.
func GenerateSSGenBlockRef(blockHash chainhash.Hash, height uint32) ([]byte,
error) {
// Prefix
dataPushes := []byte{
0x6a, // OP_RETURN
0x24, // OP_DATA_36
}
// Serialize the block hash and height
blockHeightBytes := make([]byte, 4)
binary.LittleEndian.PutUint32(blockHeightBytes, height)
blockData := append(blockHash[:], blockHeightBytes...)
// Concatenate the prefix and block data
blockDataOut := append(dataPushes, blockData...)
return blockDataOut, nil
}
// GenerateSSGenVotes generates an OP_RETURN push for the vote bits in an SSGen tx.
func GenerateSSGenVotes(votebits uint16) ([]byte, error) {
// Prefix
dataPushes := []byte{
0x6a, // OP_RETURN
0x02, // OP_DATA_2
}
// Serialize the votebits
voteBitsBytes := make([]byte, 2)
binary.LittleEndian.PutUint16(voteBitsBytes, votebits)
// Concatenate the prefix and vote bits
voteBitsOut := append(dataPushes, voteBitsBytes...)
return voteBitsOut, nil
}