This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
script.js
1101 lines (1016 loc) · 31.7 KB
/
script.js
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
'use strict';
var Address = require('../address');
var BufferReader = require('../encoding/bufferreader');
var BufferWriter = require('../encoding/bufferwriter');
var Hash = require('../crypto/hash');
var Opcode = require('../opcode');
var PublicKey = require('../publickey');
var Signature = require('../crypto/signature');
var Networks = require('../networks');
var $ = require('../util/preconditions');
var _ = require('lodash');
var errors = require('../errors');
var buffer = require('buffer');
var BufferUtil = require('../util/buffer');
var JSUtil = require('../util/js');
/**
* A bitcoin transaction script. Each transaction's inputs and outputs
* has a script that is evaluated to validate it's spending.
*
* See https://en.bitcoin.it/wiki/Script
*
* @constructor
* @param {Object|string|Buffer=} from optional data to populate script
*/
var Script = function Script(from) {
if (!(this instanceof Script)) {
return new Script(from);
}
this.chunks = [];
if (BufferUtil.isBuffer(from)) {
return Script.fromBuffer(from);
} else if (from instanceof Address) {
return Script.fromAddress(from);
} else if (from instanceof Script) {
return Script.fromBuffer(from.toBuffer());
} else if (_.isString(from)) {
return Script.fromString(from);
} else if (_.isObject(from) && _.isArray(from.chunks)) {
this.set(from);
}
};
Script.prototype.set = function(obj) {
$.checkArgument(_.isObject(obj));
$.checkArgument(_.isArray(obj.chunks));
this.chunks = obj.chunks;
return this;
};
Script.fromBuffer = function(buffer) {
var script = new Script();
script.chunks = [];
var br = new BufferReader(buffer);
while (!br.finished()) {
try {
var opcodenum = br.readUInt8();
var len, buf;
if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
len = opcodenum;
script.chunks.push({
buf: br.read(len),
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.OP_PUSHDATA1) {
len = br.readUInt8();
buf = br.read(len);
script.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.OP_PUSHDATA2) {
len = br.readUInt16LE();
buf = br.read(len);
script.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.OP_PUSHDATA4) {
len = br.readUInt32LE();
buf = br.read(len);
script.chunks.push({
buf: buf,
len: len,
opcodenum: opcodenum
});
} else {
script.chunks.push({
opcodenum: opcodenum
});
}
} catch (e) {
if (e instanceof RangeError) {
throw new errors.Script.InvalidBuffer(buffer.toString('hex'));
}
throw e;
}
}
return script;
};
Script.prototype.toBuffer = function() {
var bw = new BufferWriter();
for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i];
var opcodenum = chunk.opcodenum;
bw.writeUInt8(chunk.opcodenum);
if (chunk.buf) {
if (opcodenum < Opcode.OP_PUSHDATA1) {
bw.write(chunk.buf);
} else if (opcodenum === Opcode.OP_PUSHDATA1) {
bw.writeUInt8(chunk.len);
bw.write(chunk.buf);
} else if (opcodenum === Opcode.OP_PUSHDATA2) {
bw.writeUInt16LE(chunk.len);
bw.write(chunk.buf);
} else if (opcodenum === Opcode.OP_PUSHDATA4) {
bw.writeUInt32LE(chunk.len);
bw.write(chunk.buf);
}
}
}
return bw.concat();
};
Script.fromASM = function(str) {
var script = new Script();
script.chunks = [];
var tokens = str.split(' ');
var i = 0;
while (i < tokens.length) {
var token = tokens[i];
var opcode = Opcode(token);
var opcodenum = opcode.toNumber();
if (_.isUndefined(opcodenum)) {
var buf = Buffer.from(tokens[i], 'hex');
script.chunks.push({
buf: buf,
len: buf.length,
opcodenum: buf.length
});
i = i + 1;
} else if (opcodenum === Opcode.OP_PUSHDATA1 ||
opcodenum === Opcode.OP_PUSHDATA2 ||
opcodenum === Opcode.OP_PUSHDATA4) {
script.chunks.push({
buf: Buffer.from(tokens[i + 2], 'hex'),
len: parseInt(tokens[i + 1]),
opcodenum: opcodenum
});
i = i + 3;
} else {
script.chunks.push({
opcodenum: opcodenum
});
i = i + 1;
}
}
return script;
};
Script.fromHex = function(str) {
return new Script(new buffer.Buffer(str, 'hex'));
};
Script.fromString = function(str) {
if (JSUtil.isHexa(str) || str.length === 0) {
return new Script(new buffer.Buffer(str, 'hex'));
}
var script = new Script();
script.chunks = [];
var tokens = str.split(' ');
var i = 0;
while (i < tokens.length) {
var token = tokens[i];
var opcode = Opcode(token);
var opcodenum = opcode.toNumber();
if (_.isUndefined(opcodenum)) {
opcodenum = parseInt(token);
if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
script.chunks.push({
buf: Buffer.from(tokens[i + 1].slice(2), 'hex'),
len: opcodenum,
opcodenum: opcodenum
});
i = i + 2;
} else {
throw new Error('Invalid script: ' + JSON.stringify(str));
}
} else if (opcodenum === Opcode.OP_PUSHDATA1 ||
opcodenum === Opcode.OP_PUSHDATA2 ||
opcodenum === Opcode.OP_PUSHDATA4) {
if (tokens[i + 2].slice(0, 2) !== '0x') {
throw new Error('Pushdata data must start with 0x');
}
script.chunks.push({
buf: Buffer.from(tokens[i + 2].slice(2), 'hex'),
len: parseInt(tokens[i + 1]),
opcodenum: opcodenum
});
i = i + 3;
} else {
script.chunks.push({
opcodenum: opcodenum
});
i = i + 1;
}
}
return script;
};
Script.prototype._chunkToString = function(chunk, type) {
var opcodenum = chunk.opcodenum;
var asm = (type === 'asm');
var str = '';
if (!chunk.buf) {
// no data chunk
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
if (asm) {
// A few cases where the opcode name differs from reverseMap
// aside from 1 to 16 data pushes.
if (opcodenum === 0) {
// OP_0 -> 0
str = str + ' 0';
} else if(opcodenum === 79) {
// OP_1NEGATE -> 1
str = str + ' -1';
} else {
str = str + ' ' + Opcode(opcodenum).toString();
}
} else {
str = str + ' ' + Opcode(opcodenum).toString();
}
} else {
var numstr = opcodenum.toString(16);
if (numstr.length % 2 !== 0) {
numstr = '0' + numstr;
}
if (asm) {
str = str + ' ' + numstr;
} else {
str = str + ' ' + '0x' + numstr;
}
}
} else {
// data chunk
if (!asm && opcodenum === Opcode.OP_PUSHDATA1 ||
opcodenum === Opcode.OP_PUSHDATA2 ||
opcodenum === Opcode.OP_PUSHDATA4) {
str = str + ' ' + Opcode(opcodenum).toString();
}
if (chunk.len > 0) {
if (asm) {
str = str + ' ' + chunk.buf.toString('hex');
} else {
str = str + ' ' + chunk.len + ' ' + '0x' + chunk.buf.toString('hex');
}
}
}
return str;
};
Script.prototype.toASM = function() {
var str = '';
for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i];
str += this._chunkToString(chunk, 'asm');
}
return str.substr(1);
};
Script.prototype.toString = function() {
var str = '';
for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i];
str += this._chunkToString(chunk);
}
return str.substr(1);
};
Script.prototype.toHex = function() {
return this.toBuffer().toString('hex');
};
Script.prototype.inspect = function() {
return '<Script: ' + this.toString() + '>';
};
// script classification methods
/**
* @returns {boolean} if this is a pay to pubkey hash output script
*/
Script.prototype.isPublicKeyHashOut = function() {
return !!(this.chunks.length === 5 &&
this.chunks[0].opcodenum === Opcode.OP_DUP &&
this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
this.chunks[2].buf &&
this.chunks[2].buf.length === 20 &&
this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
this.chunks[4].opcodenum === Opcode.OP_CHECKSIG);
};
/**
* @returns {boolean} if this is a pay to public key hash input script
*/
Script.prototype.isPublicKeyHashIn = function() {
if (this.chunks.length === 2) {
var signatureBuf = this.chunks[0].buf;
var pubkeyBuf = this.chunks[1].buf;
if (signatureBuf &&
signatureBuf.length &&
signatureBuf[0] === 0x30 &&
pubkeyBuf &&
pubkeyBuf.length
) {
var version = pubkeyBuf[0];
if ((version === 0x04 ||
version === 0x06 ||
version === 0x07) && pubkeyBuf.length === 65) {
return true;
} else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) {
return true;
}
}
}
return false;
};
Script.prototype.getPublicKey = function() {
$.checkState(this.isPublicKeyOut(), 'Can\'t retrieve PublicKey from a non-PK output');
return this.chunks[0].buf;
};
Script.prototype.getPublicKeyHash = function() {
$.checkState(this.isPublicKeyHashOut(), 'Can\'t retrieve PublicKeyHash from a non-PKH output');
return this.chunks[2].buf;
};
/**
* @returns {boolean} if this is a public key output script
*/
Script.prototype.isPublicKeyOut = function() {
if (this.chunks.length === 2 &&
this.chunks[0].buf &&
this.chunks[0].buf.length &&
this.chunks[1].opcodenum === Opcode.OP_CHECKSIG) {
var pubkeyBuf = this.chunks[0].buf;
var version = pubkeyBuf[0];
var isVersion = false;
if ((version === 0x04 ||
version === 0x06 ||
version === 0x07) && pubkeyBuf.length === 65) {
isVersion = true;
} else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) {
isVersion = true;
}
if (isVersion) {
return PublicKey.isValid(pubkeyBuf);
}
}
return false;
};
/**
* @returns {boolean} if this is a pay to public key input script
*/
Script.prototype.isPublicKeyIn = function() {
if (this.chunks.length === 1) {
var signatureBuf = this.chunks[0].buf;
if (signatureBuf &&
signatureBuf.length &&
signatureBuf[0] === 0x30) {
return true;
}
}
return false;
};
/**
* @returns {boolean} if this is a p2sh output script
*/
Script.prototype.isScriptHashOut = function() {
var buf = this.toBuffer();
return (buf.length === 23 &&
buf[0] === Opcode.OP_HASH160 &&
buf[1] === 0x14 &&
buf[buf.length - 1] === Opcode.OP_EQUAL);
};
/**
* @returns {boolean} if this is a p2sh input script
* Note that these are frequently indistinguishable from pubkeyhashin
*/
Script.prototype.isScriptHashIn = function() {
if (this.chunks.length <= 1) {
return false;
}
var redeemChunk = this.chunks[this.chunks.length - 1];
var redeemBuf = redeemChunk.buf;
if (!redeemBuf) {
return false;
}
var redeemScript;
try {
redeemScript = Script.fromBuffer(redeemBuf);
} catch (e) {
if (e instanceof errors.Script.InvalidBuffer) {
return false;
}
throw e;
}
var type = redeemScript.classify();
return type !== Script.types.UNKNOWN;
};
/**
* @returns {boolean} if this is a mutlsig output script
*/
Script.prototype.isMultisigOut = function() {
return (this.chunks.length > 3 &&
Opcode.isSmallIntOp(this.chunks[0].opcodenum) &&
this.chunks.slice(1, this.chunks.length - 2).every(function(obj) {
return obj.buf && BufferUtil.isBuffer(obj.buf);
}) &&
Opcode.isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) &&
this.chunks[this.chunks.length - 1].opcodenum === Opcode.OP_CHECKMULTISIG);
};
/**
* @returns {boolean} if this is a multisig input script
*/
Script.prototype.isMultisigIn = function() {
return this.chunks.length >= 2 &&
this.chunks[0].opcodenum === 0 &&
this.chunks.slice(1, this.chunks.length).every(function(obj) {
return obj.buf &&
BufferUtil.isBuffer(obj.buf) &&
Signature.isTxDER(obj.buf);
});
};
/**
* @returns {boolean} true if this is a valid standard OP_RETURN output
*/
Script.prototype.isDataOut = function() {
return this.chunks.length >= 1 &&
this.chunks[0].opcodenum === Opcode.OP_RETURN &&
(this.chunks.length === 1 ||
(this.chunks.length === 2 &&
this.chunks[1].buf &&
this.chunks[1].buf.length <= Script.OP_RETURN_STANDARD_SIZE &&
this.chunks[1].length === this.chunks.len));
};
/**
* Retrieve the associated data for this script.
* In the case of a pay to public key hash or P2SH, return the hash.
* In the case of a standard OP_RETURN, return the data
* @returns {Buffer}
*/
Script.prototype.getData = function() {
if (this.isDataOut() || this.isScriptHashOut()) {
if (_.isUndefined(this.chunks[1])) {
return Buffer.alloc(0);
} else {
return Buffer.from(this.chunks[1].buf);
}
}
if (this.isPublicKeyHashOut()) {
return Buffer.from(this.chunks[2].buf);
}
throw new Error('Unrecognized script type to get data from');
};
/**
* @returns {boolean} if the script is only composed of data pushing
* opcodes or small int opcodes (OP_0, OP_1, ..., OP_16)
*/
Script.prototype.isPushOnly = function() {
return _.every(this.chunks, function(chunk) {
return chunk.opcodenum <= Opcode.OP_16;
});
};
Script.types = {};
Script.types.UNKNOWN = 'Unknown';
Script.types.PUBKEY_OUT = 'Pay to public key';
Script.types.PUBKEY_IN = 'Spend from public key';
Script.types.PUBKEYHASH_OUT = 'Pay to public key hash';
Script.types.PUBKEYHASH_IN = 'Spend from public key hash';
Script.types.SCRIPTHASH_OUT = 'Pay to script hash';
Script.types.SCRIPTHASH_IN = 'Spend from script hash';
Script.types.MULTISIG_OUT = 'Pay to multisig';
Script.types.MULTISIG_IN = 'Spend from multisig';
Script.types.DATA_OUT = 'Data push';
Script.OP_RETURN_STANDARD_SIZE = 220;
/**
* @returns {object} The Script type if it is a known form,
* or Script.UNKNOWN if it isn't
*/
Script.prototype.classify = function() {
if (this._isInput) {
return this.classifyInput();
} else if (this._isOutput) {
return this.classifyOutput();
} else {
var outputType = this.classifyOutput();
return outputType != Script.types.UNKNOWN ? outputType : this.classifyInput();
}
};
Script.outputIdentifiers = {};
Script.outputIdentifiers.PUBKEY_OUT = Script.prototype.isPublicKeyOut;
Script.outputIdentifiers.PUBKEYHASH_OUT = Script.prototype.isPublicKeyHashOut;
Script.outputIdentifiers.MULTISIG_OUT = Script.prototype.isMultisigOut;
Script.outputIdentifiers.SCRIPTHASH_OUT = Script.prototype.isScriptHashOut;
Script.outputIdentifiers.DATA_OUT = Script.prototype.isDataOut;
/**
* @returns {object} The Script type if it is a known form,
* or Script.UNKNOWN if it isn't
*/
Script.prototype.classifyOutput = function() {
for (var type in Script.outputIdentifiers) {
if (Script.outputIdentifiers[type].bind(this)()) {
return Script.types[type];
}
}
return Script.types.UNKNOWN;
};
Script.inputIdentifiers = {};
Script.inputIdentifiers.PUBKEY_IN = Script.prototype.isPublicKeyIn;
Script.inputIdentifiers.PUBKEYHASH_IN = Script.prototype.isPublicKeyHashIn;
Script.inputIdentifiers.MULTISIG_IN = Script.prototype.isMultisigIn;
Script.inputIdentifiers.SCRIPTHASH_IN = Script.prototype.isScriptHashIn;
/**
* @returns {object} The Script type if it is a known form,
* or Script.UNKNOWN if it isn't
*/
Script.prototype.classifyInput = function() {
for (var type in Script.inputIdentifiers) {
if (Script.inputIdentifiers[type].bind(this)()) {
return Script.types[type];
}
}
return Script.types.UNKNOWN;
};
/**
* @returns {boolean} if script is one of the known types
*/
Script.prototype.isStandard = function() {
// TODO: Add BIP62 compliance
return this.classify() !== Script.types.UNKNOWN;
};
// Script construction methods
/**
* Adds a script element at the start of the script.
* @param {*} obj a string, number, Opcode, Buffer, or object to add
* @returns {Script} this script instance
*/
Script.prototype.prepend = function(obj) {
this._addByType(obj, true);
return this;
};
/**
* Compares a script with another script
*/
Script.prototype.equals = function(script) {
$.checkState(script instanceof Script, 'Must provide another script');
if (this.chunks.length !== script.chunks.length) {
return false;
}
var i;
for (i = 0; i < this.chunks.length; i++) {
if (BufferUtil.isBuffer(this.chunks[i].buf) && !BufferUtil.isBuffer(script.chunks[i].buf)) {
return false;
}
if (BufferUtil.isBuffer(this.chunks[i].buf) && !BufferUtil.equals(this.chunks[i].buf, script.chunks[i].buf)) {
return false;
} else if (this.chunks[i].opcodenum !== script.chunks[i].opcodenum) {
return false;
}
}
return true;
};
/**
* Adds a script element to the end of the script.
*
* @param {*} obj a string, number, Opcode, Buffer, or object to add
* @returns {Script} this script instance
*
*/
Script.prototype.add = function(obj) {
this._addByType(obj, false);
return this;
};
Script.prototype._addByType = function(obj, prepend) {
if (typeof obj === 'string') {
this._addOpcode(obj, prepend);
} else if (typeof obj === 'number') {
this._addOpcode(obj, prepend);
} else if (obj instanceof Opcode) {
this._addOpcode(obj, prepend);
} else if (BufferUtil.isBuffer(obj)) {
this._addBuffer(obj, prepend);
} else if (obj instanceof Script) {
this.chunks = this.chunks.concat(obj.chunks);
} else if (typeof obj === 'object') {
this._insertAtPosition(obj, prepend);
} else {
throw new Error('Invalid script chunk');
}
};
Script.prototype._insertAtPosition = function(op, prepend) {
if (prepend) {
this.chunks.unshift(op);
} else {
this.chunks.push(op);
}
};
Script.prototype._addOpcode = function(opcode, prepend) {
var op;
if (typeof opcode === 'number') {
op = opcode;
} else if (opcode instanceof Opcode) {
op = opcode.toNumber();
} else {
op = Opcode(opcode).toNumber();
}
this._insertAtPosition({
opcodenum: op
}, prepend);
return this;
};
Script.prototype._addBuffer = function(buf, prepend) {
var opcodenum;
var len = buf.length;
if (len >= 0 && len < Opcode.OP_PUSHDATA1) {
opcodenum = len;
} else if (len < Math.pow(2, 8)) {
opcodenum = Opcode.OP_PUSHDATA1;
} else if (len < Math.pow(2, 16)) {
opcodenum = Opcode.OP_PUSHDATA2;
} else if (len < Math.pow(2, 32)) {
opcodenum = Opcode.OP_PUSHDATA4;
} else {
throw new Error('You can\'t push that much data');
}
this._insertAtPosition({
buf: buf,
len: len,
opcodenum: opcodenum
}, prepend);
return this;
};
Script.prototype.removeCodeseparators = function() {
var chunks = [];
for (var i = 0; i < this.chunks.length; i++) {
if (this.chunks[i].opcodenum !== Opcode.OP_CODESEPARATOR) {
chunks.push(this.chunks[i]);
}
}
this.chunks = chunks;
return this;
};
// high level script builder methods
/**
* @returns {Script} a new Multisig output script for given public keys,
* requiring m of those public keys to spend
* @param {PublicKey[]} publicKeys - list of all public keys controlling the output
* @param {number} threshold - amount of required signatures to spend the output
* @param {Object=} opts - Several options:
* - noSorting: defaults to false, if true, don't sort the given
* public keys before creating the script
*/
Script.buildMultisigOut = function(publicKeys, threshold, opts) {
$.checkArgument(threshold <= publicKeys.length,
'Number of required signatures must be less than or equal to the number of public keys');
opts = opts || {};
var script = new Script();
script.add(Opcode.smallInt(threshold));
publicKeys = _.map(publicKeys, PublicKey);
var sorted = publicKeys;
if (!opts.noSorting) {
sorted = _.sortBy(publicKeys, function(publicKey) {
return publicKey.toString('hex');
});
}
for (var i = 0; i < sorted.length; i++) {
var publicKey = sorted[i];
script.add(publicKey.toBuffer());
}
script.add(Opcode.smallInt(publicKeys.length));
script.add(Opcode.OP_CHECKMULTISIG);
return script;
};
/**
* A new Multisig input script for the given public keys, requiring m of those public keys to spend
*
* @param {PublicKey[]} pubkeys list of all public keys controlling the output
* @param {number} threshold amount of required signatures to spend the output
* @param {Array} signatures and array of signature buffers to append to the script
* @param {Object=} opts
* @param {boolean=} opts.noSorting don't sort the given public keys before creating the script (false by default)
* @param {Script=} opts.cachedMultisig don't recalculate the redeemScript
*
* @returns {Script}
*/
Script.buildMultisigIn = function(pubkeys, threshold, signatures, opts) {
$.checkArgument(_.isArray(pubkeys));
$.checkArgument(_.isNumber(threshold));
$.checkArgument(_.isArray(signatures));
opts = opts || {};
var s = new Script();
s.add(Opcode.OP_0);
_.each(signatures, function(signature) {
$.checkArgument(BufferUtil.isBuffer(signature), 'Signatures must be an array of Buffers');
// TODO: allow signatures to be an array of Signature objects
s.add(signature);
});
return s;
};
/**
* A new P2SH Multisig input script for the given public keys, requiring m of those public keys to spend
*
* @param {PublicKey[]} pubkeys list of all public keys controlling the output
* @param {number} threshold amount of required signatures to spend the output
* @param {Array} signatures and array of signature buffers to append to the script
* @param {Object=} opts
* @param {boolean=} opts.noSorting don't sort the given public keys before creating the script (false by default)
* @param {Script=} opts.cachedMultisig don't recalculate the redeemScript
*
* @returns {Script}
*/
Script.buildP2SHMultisigIn = function(pubkeys, threshold, signatures, opts) {
$.checkArgument(_.isArray(pubkeys));
$.checkArgument(_.isNumber(threshold));
$.checkArgument(_.isArray(signatures));
opts = opts || {};
var s = new Script();
s.add(Opcode.OP_0);
_.each(signatures, function(signature) {
$.checkArgument(BufferUtil.isBuffer(signature), 'Signatures must be an array of Buffers');
// TODO: allow signatures to be an array of Signature objects
s.add(signature);
});
s.add((opts.cachedMultisig || Script.buildMultisigOut(pubkeys, threshold, opts)).toBuffer());
return s;
};
/**
* @returns {Script} a new pay to public key hash output for the given
* address or public key
* @param {(Address|PublicKey)} to - destination address or public key
*/
Script.buildPublicKeyHashOut = function(to) {
$.checkArgument(!_.isUndefined(to));
$.checkArgument(to instanceof PublicKey || to instanceof Address || _.isString(to));
if (to instanceof PublicKey) {
to = to.toAddress();
} else if (_.isString(to)) {
to = new Address(to);
}
var s = new Script();
s.add(Opcode.OP_DUP)
.add(Opcode.OP_HASH160)
.add(to.hashBuffer)
.add(Opcode.OP_EQUALVERIFY)
.add(Opcode.OP_CHECKSIG);
s._network = to.network;
return s;
};
/**
* @returns {Script} a new pay to public key output for the given
* public key
*/
Script.buildPublicKeyOut = function(pubkey) {
$.checkArgument(pubkey instanceof PublicKey);
var s = new Script();
s.add(pubkey.toBuffer())
.add(Opcode.OP_CHECKSIG);
return s;
};
/**
* @returns {Script} a new OP_RETURN script with data
* @param {(string|Buffer)} data - the data to embed in the output
* @param {(string)} encoding - the type of encoding of the string
*/
Script.buildDataOut = function(data, encoding) {
$.checkArgument(_.isUndefined(data) || _.isString(data) || BufferUtil.isBuffer(data));
if (_.isString(data)) {
data = Buffer.from(data, encoding);
}
var s = new Script();
s.add(Opcode.OP_RETURN);
if (!_.isUndefined(data)) {
s.add(data);
}
return s;
};
/**
* @param {Script|Address} script - the redeemScript for the new p2sh output.
* It can also be a p2sh address
* @returns {Script} new pay to script hash script for given script
*/
Script.buildScriptHashOut = function(script) {
$.checkArgument(script instanceof Script ||
(script instanceof Address && script.isPayToScriptHash()));
var s = new Script();
s.add(Opcode.OP_HASH160)
.add(script instanceof Address ? script.hashBuffer : Hash.sha256ripemd160(script.toBuffer()))
.add(Opcode.OP_EQUAL);
s._network = script._network || script.network;
return s;
};
/**
* Builds a scriptSig (a script for an input) that signs a public key output script.
*
* @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding
* @param {number=} sigtype - the type of the signature (defaults to SIGHASH_ALL)
*/
Script.buildPublicKeyIn = function(signature, sigtype) {
$.checkArgument(signature instanceof Signature || BufferUtil.isBuffer(signature));
$.checkArgument(_.isUndefined(sigtype) || _.isNumber(sigtype));
if (signature instanceof Signature) {
signature = signature.toBuffer();
}
var script = new Script();
script.add(BufferUtil.concat([
signature,
BufferUtil.integerAsSingleByteBuffer(sigtype || Signature.SIGHASH_ALL)
]));
return script;
};
/**
* Builds a scriptSig (a script for an input) that signs a public key hash
* output script.
*
* @param {Buffer|string|PublicKey} publicKey
* @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding
* @param {number=} sigtype - the type of the signature (defaults to SIGHASH_ALL)
*/
Script.buildPublicKeyHashIn = function(publicKey, signature, sigtype) {
$.checkArgument(signature instanceof Signature || BufferUtil.isBuffer(signature));
$.checkArgument(_.isUndefined(sigtype) || _.isNumber(sigtype));
if (signature instanceof Signature) {
signature = signature.toBuffer();
}
var script = new Script()
.add(BufferUtil.concat([
signature,
BufferUtil.integerAsSingleByteBuffer(sigtype || Signature.SIGHASH_ALL)
]))
.add(new PublicKey(publicKey).toBuffer());
return script;
};
/**
* @returns {Script} an empty script
*/
Script.empty = function() {
return new Script();
};
/**
* @returns {Script} a new pay to script hash script that pays to this script
*/
Script.prototype.toScriptHashOut = function() {
return Script.buildScriptHashOut(this);
};
/**
* @return {Script} an output script built from the address
*/
Script.fromAddress = function(address) {
address = Address(address);
if (address.isPayToScriptHash()) {
return Script.buildScriptHashOut(address);
} else if (address.isPayToPublicKeyHash()) {
return Script.buildPublicKeyHashOut(address);
}
throw new errors.Script.UnrecognizedAddress(address);
};
/**
* Will return the associated address information object
* @return {Address|boolean}
*/
Script.prototype.getAddressInfo = function(opts) {
if (this._isInput) {
return this._getInputAddressInfo();
} else if (this._isOutput) {
return this._getOutputAddressInfo();
} else {
var info = this._getOutputAddressInfo();
if (!info) {
return this._getInputAddressInfo();
}
return info;
}
};
/**
* Will return the associated output scriptPubKey address information object
* @return {Address|boolean}
* @private
*/
Script.prototype._getOutputAddressInfo = function() {
var info = {};
if (this.isScriptHashOut()) {
info.hashBuffer = this.getData();
info.type = Address.PayToScriptHash;
} else if (this.isPublicKeyHashOut()) {
info.hashBuffer = this.getData();
info.type = Address.PayToPublicKeyHash;
} else {
return false;
}
return info;
};
/**
* Will return the associated input scriptSig address information object
* @return {Address|boolean}
* @private
*/
Script.prototype._getInputAddressInfo = function() {
var info = {};
if (this.isPublicKeyHashIn()) {
// hash the publickey found in the scriptSig
info.hashBuffer = Hash.sha256ripemd160(this.chunks[1].buf);
info.type = Address.PayToPublicKeyHash;
} else if (this.isScriptHashIn()) {
// hash the redeemscript found at the end of the scriptSig
info.hashBuffer = Hash.sha256ripemd160(this.chunks[this.chunks.length - 1].buf);
info.type = Address.PayToScriptHash;
} else {
return false;
}
return info;
};
/**
* @param {Network=} network
* @return {Address|boolean} the associated address for this script if possible, or false
*/
Script.prototype.toAddress = function(network) {
var info = this.getAddressInfo();
if (!info) {
return false;
}
info.network = Networks.get(network) || this._network || Networks.defaultNetwork;
return new Address(info);