-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
script.js
782 lines (714 loc) · 22.5 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
'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 (typeof from === 'string') {
return Script.fromString(from);
} else if (typeof from !== 'undefined') {
this.set(from);
}
};
Script.prototype.set = function(obj) {
this.chunks = obj.chunks || this.chunks;
return this;
};
Script.fromBuffer = function(buffer) {
var script = new Script();
script.chunks = [];
var br = new BufferReader(buffer);
while (!br.eof()) {
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
});
}
}
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.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: new Buffer(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: new Buffer(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.toString = function() {
var str = '';
for (var i = 0; i < this.chunks.length; i++) {
var chunk = this.chunks[i];
var opcodenum = chunk.opcodenum;
if (!chunk.buf) {
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
str = str + ' ' + Opcode(opcodenum).toString();
} else {
var numstr = opcodenum.toString(16);
if (numstr.length % 2 !== 0) {
numstr = '0' + numstr;
}
str = str + ' ' + '0x' + numstr;
}
} else {
if (opcodenum === Opcode.OP_PUSHDATA1 ||
opcodenum === Opcode.OP_PUSHDATA2 ||
opcodenum === Opcode.OP_PUSHDATA4) {
str = str + ' ' + Opcode(opcodenum).toString();
}
str = str + ' ' + chunk.len;
if (chunk.len > 0) {
str = str + ' ' + '0x' + chunk.buf.toString('hex');
}
}
}
return str.substr(1);
};
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[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() {
return this.chunks.length === 2 &&
this.chunks[0].buf &&
this.chunks[0].buf.length >= 0x47 &&
this.chunks[0].buf.length <= 0x49 &&
PublicKey.isValid(this.chunks[1].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() {
return this.chunks.length === 2 &&
BufferUtil.isBuffer(this.chunks[0].buf) &&
PublicKey.isValid(this.chunks[0].buf) &&
this.chunks[1].opcodenum === Opcode.OP_CHECKSIG;
};
/**
* @returns {boolean} if this is a pay to public key input script
*/
Script.prototype.isPublicKeyIn = function() {
return this.chunks.length === 1 &&
BufferUtil.isBuffer(this.chunks[0].buf) &&
this.chunks[0].buf.length === 0x47;
};
/**
* @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 === 0) {
return false;
}
var chunk = this.chunks[this.chunks.length - 1];
if (!chunk) {
return false;
}
var scriptBuf = chunk.buf;
if (!scriptBuf) {
return false;
}
var redeemScript = new Script(scriptBuf);
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) &&
obj.buf.length === 0x47;
});
};
/**
* @returns {boolean} if this is an OP_RETURN data script
*/
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 <= 40 &&
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()) {
return new Buffer(this.chunks[1].buf);
}
if (this.isPublicKeyHashOut()) {
return new Buffer(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.identifiers = {};
Script.identifiers.PUBKEY_OUT = Script.prototype.isPublicKeyOut;
Script.identifiers.PUBKEY_IN = Script.prototype.isPublicKeyIn;
Script.identifiers.PUBKEYHASH_OUT = Script.prototype.isPublicKeyHashOut;
Script.identifiers.PUBKEYHASH_IN = Script.prototype.isPublicKeyHashIn;
Script.identifiers.MULTISIG_OUT = Script.prototype.isMultisigOut;
Script.identifiers.MULTISIG_IN = Script.prototype.isMultisigIn;
Script.identifiers.SCRIPTHASH_OUT = Script.prototype.isScriptHashOut;
Script.identifiers.SCRIPTHASH_IN = Script.prototype.isScriptHashIn;
Script.identifiers.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.classify = function() {
for (var type in Script.identifiers) {
if (Script.identifiers[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, Bufer, 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]) && !BufferUtil.isBuffer(script.chunks[i])) {
return false;
} else if (this.chunks[i] instanceof Opcode && !(script.chunks[i] instanceof Opcode)) {
return false;
}
if (BufferUtil.isBuffer(this.chunks[i]) && !BufferUtil.equals(this.chunks[i], script.chunks[i])) {
return false;
} else if (this.chunks[i].num !== script.chunks[i].num) {
return false;
}
}
return true;
};
/**
* Adds a script element to the end of the script.
*
* @param {*} obj a string, number, Opcode, Bufer, 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 (typeof obj === 'object') {
this._insertAtPosition(obj, prepend);
} else if (obj instanceof Script) {
this.chunks = this.chunks.concat(obj.chunks);
} 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) {
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 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 signatures 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) {
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)} to - the data to embed in the output
*/
Script.buildDataOut = function(data) {
$.checkArgument(_.isUndefined(data) || _.isString(data) || BufferUtil.isBuffer(data));
if (typeof data === 'string') {
data = new Buffer(data);
}
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 hash
* output script.
*
* @param {Buffer|string|PublicKey} publicKey
* @param {Signature|Buffer} signature - a Signature object, or the signature in DER cannonical 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} a 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);
};
/**
* @param {Network} [network]
* @return {Address} the associated address for this script
*/
Script.prototype.toAddress = function(network) {
network = Networks.get(network) || this._network || Networks.defaultNetwork;
if (this.isPublicKeyHashOut() || this.isScriptHashOut()) {
return new Address(this, network);
}
throw new Error('The script type needs to be PayToPublicKeyHash or PayToScriptHash');
};
/**
* @return {Script}
*/
Script.prototype.toScriptHashOut = function() {
return Script.buildScriptHashOut(this);
};
/**
* Analagous to bitcoind's FindAndDelete. Find and delete equivalent chunks,
* typically used with push data chunks. Note that this will find and delete
* not just the same data, but the same data with the same push data op as
* produced by default. i.e., if a pushdata in a tx does not use the minimal
* pushdata op, then when you try to remove the data it is pushing, it will not
* be removed, because they do not use the same pushdata op.
*/
Script.prototype.findAndDelete = function(script) {
var buf = script.toBuffer();
var hex = buf.toString('hex');
for (var i = 0; i < this.chunks.length; i++) {
var script2 = Script({
chunks: [this.chunks[i]]
});
var buf2 = script2.toBuffer();
var hex2 = buf2.toString('hex');
if (hex === hex2) {
this.chunks.splice(i, 1);
}
}
return this;
};
/**
* Comes from bitcoind's script interpreter CheckMinimalPush function
* @returns {boolean} if the chunk {i} is the smallest way to push that particular data.
*/
Script.prototype.checkMinimalPush = function(i) {
var chunk = this.chunks[i];
var buf = chunk.buf;
var opcodenum = chunk.opcodenum;
if (!buf) {
return true;
}
if (buf.length === 0) {
// Could have used OP_0.
return opcodenum === Opcode.OP_0;
} else if (buf.length === 1 && buf[0] >= 1 && buf[0] <= 16) {
// Could have used OP_1 .. OP_16.
return opcodenum === Opcode.OP_1 + (buf[0] - 1);
} else if (buf.length === 1 && buf[0] === 0x81) {
// Could have used OP_1NEGATE
return opcodenum === Opcode.OP_1NEGATE;
} else if (buf.length <= 75) {
// Could have used a direct push (opcode indicating number of bytes pushed + those bytes).
return opcodenum === buf.length;
} else if (buf.length <= 255) {
// Could have used OP_PUSHDATA.
return opcodenum === Opcode.OP_PUSHDATA1;
} else if (buf.length <= 65535) {
// Could have used OP_PUSHDATA2.
return opcodenum === Opcode.OP_PUSHDATA2;
}
return true;
};
module.exports = Script;