forked from bitpay/bitcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.js
710 lines (628 loc) · 17.2 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
var config = require('../config');
var log = require('../util/log');
var Opcode = require('./Opcode');
var buffertools = require('buffertools');
var util = require('../util/util');
var Parser = require('../util/BinaryParser');
var Put = require('bufferput');
var TX_UNKNOWN = 0;
var TX_PUBKEY = 1;
var TX_PUBKEYHASH = 2;
var TX_MULTISIG = 3;
var TX_SCRIPTHASH = 4;
var TX_RETURN = 5;
var TX_TYPES = [
'unknown',
'pubkey',
'pubkeyhash',
'multisig',
'scripthash',
'return'
];
function Script(buffer) {
if (buffer) {
this.buffer = buffer;
} else {
this.buffer = util.EMPTY_BUFFER;
}
this.chunks = [];
this.parse();
}
Script.TX_UNKNOWN = TX_UNKNOWN;
Script.TX_PUBKEY = TX_PUBKEY;
Script.TX_PUBKEYHASH = TX_PUBKEYHASH;
Script.TX_MULTISIG = TX_MULTISIG;
Script.TX_SCRIPTHASH = TX_SCRIPTHASH;
Script.TX_RETURN = TX_RETURN;
Script.prototype.parse = function() {
this.chunks = [];
var parser = new Parser(this.buffer);
while (!parser.eof()) {
var opcode = parser.word8();
var len, chunk;
if (opcode > 0 && opcode < Opcode.map.OP_PUSHDATA1) {
// Read some bytes of data, opcode value is the length of data
this.chunks.push(parser.buffer(opcode));
} else if (opcode === Opcode.map.OP_PUSHDATA1) {
len = parser.word8();
chunk = parser.buffer(len);
this.chunks.push(chunk);
} else if (opcode === Opcode.map.OP_PUSHDATA2) {
len = parser.word16le();
chunk = parser.buffer(len);
this.chunks.push(chunk);
} else if (opcode === Opcode.map.OP_PUSHDATA4) {
len = parser.word32le();
chunk = parser.buffer(len);
this.chunks.push(chunk);
} else {
this.chunks.push(opcode);
}
}
};
Script.prototype.isPushOnly = function() {
for (var i = 0; i < this.chunks.length; i++) {
var op = this.chunks[i];
if (!Buffer.isBuffer(op) && op > Opcode.map.OP_16) {
return false;
}
}
return true;
};
Script.prototype.isP2SH = function() {
return (this.chunks.length == 3 &&
this.chunks[0] == Opcode.map.OP_HASH160 &&
Buffer.isBuffer(this.chunks[1]) &&
this.chunks[1].length == 20 &&
this.chunks[2] == Opcode.map.OP_EQUAL);
};
Script.prototype.isPubkey = function() {
return (this.chunks.length == 2 &&
Buffer.isBuffer(this.chunks[0]) &&
this.chunks[1] == Opcode.map.OP_CHECKSIG);
};
Script.prototype.isReturn = function() {
return (this.chunks.length == 2 &&
Buffer.isBuffer(this.chunks[1]) &&
this.chunks[0] == Opcode.map.OP_RETURN);
};
Script.prototype.isPubkeyHash = function() {
return (this.chunks.length == 5 &&
this.chunks[0] == Opcode.map.OP_DUP &&
this.chunks[1] == Opcode.map.OP_HASH160 &&
Buffer.isBuffer(this.chunks[2]) &&
this.chunks[2].length == 20 &&
this.chunks[3] == Opcode.map.OP_EQUALVERIFY &&
this.chunks[4] == Opcode.map.OP_CHECKSIG);
};
function isSmallIntOp(opcode) {
return ((opcode == Opcode.map.OP_0) ||
((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16)));
};
Script.prototype.isMultiSig = function() {
return (this.chunks.length > 3 &&
isSmallIntOp(this.chunks[0]) &&
this.chunks.slice(1, this.chunks.length - 2).every(function(i) {
return Buffer.isBuffer(i);
}) &&
isSmallIntOp(this.chunks[this.chunks.length - 2]) &&
this.chunks[this.chunks.length - 1] == Opcode.map.OP_CHECKMULTISIG);
};
Script.prototype.isPubkeyHashScriptSig = function() {
return (this.chunks.length == 2 &&
Buffer.isBuffer(this.chunks[0]) &&
Buffer.isBuffer(this.chunks[1]));
};
Script.prototype.isP2shScriptSig = function() {
if (!isSmallIntOp(this.chunks[0]) || this.chunks[0] !== 0)
return false;
var redeemScript = new Script(this.chunks[this.chunks.length - 1]);
var type = redeemScript.classify();
return type !== TX_UNKNOWN;
};
Script.prototype.isMultiSigScriptSig = function() {
if (!isSmallIntOp(this.chunks[0]) || this.chunks[0] !== 0)
return false;
return !this.isP2shScriptSig();
};
Script.prototype.isPubkeyScriptSig = function() {
return (this.chunks.length == 1 &&
Buffer.isBuffer(this.chunks[0]));
};
Script.prototype.countSignatures = function() {
var ret = 0;
var l = this.chunks.length;
// Multisig?
if (this.isMultiSigScriptSig()) {
ret = l - 1;
}
// p2sh
else if (this.isP2shScriptSig()) {
ret = l - 2;
}
// p2pubkeyhash
else if (this.isPubkeyHashScriptSig()) {
ret = 1;
}
// p2pubkey
else {
ret = 0;
}
return ret;
};
Script.prototype.getSignatures = function() {
ret = [];
var l = this.chunks.length;
// Multisig?
if (this.isMultiSigScriptSig()) {
for(var i = 1; i<l; i++) {
ret.push(this.chunks[i]);
}
}
// p2sh
else if (this.isP2shScriptSig()) {
for (var i=1; i<l-1; i++) {
ret.push(this.chunks[i]);
}
}
// p2pubkeyhash
else if (this.isPubkeyHashScriptSig()) {
ret.push(this.chunks[0]);
}
// p2pubkey
else {
// no signatures
}
return ret;
};
Script.prototype.getHashType = function() {
var sigs = this.getSignatures();
var hashType = null;
for (var i=0; i<sigs.length; i++) {
var sig = sigs[i];
var hashTypeI = sig[sig.length - 1];
if (hashType !== null && hashType !== hashTypeI) return null;
hashType = hashTypeI;
}
return hashType;
};
Script.prototype.countMissingSignatures = function() {
if (this.isMultiSig()) {
log.debug("Can not count missing signatures on normal Multisig script");
return null;
}
var ret = 0;
var l = this.chunks.length;
// P2SH?
if (isSmallIntOp(this.chunks[0]) && this.chunks[0] === 0) {
var redeemScript = new Script(this.chunks[l - 1]);
if (!isSmallIntOp(redeemScript.chunks[0])) {
log.debug("Unrecognized script type");
} else {
var nreq = redeemScript.chunks[0] - 80; //see OP_2-OP_16
ret = nreq - (l - 2); // 2-> marked 0 + redeemScript
}
}
// p2pubkey or p2pubkeyhash
else {
if (buffertools.compare(this.getBuffer(), util.EMPTY_BUFFER) === 0) {
ret = 1;
}
}
return ret;
};
Script.prototype.finishedMultiSig = function() {
var missing = this.countMissingSignatures();
if (missing === null) return null;
return missing === 0;
};
Script.prototype.getMultiSigInfo = function() {
if (!this.isMultiSig()) {
throw new Error("Script.getMultiSigInfo(): Not a multiSig script.");
}
var nsigs = this.chunks[0] - 80; //see OP_2-OP_16;
var npubkeys = this.chunks[this.chunks.length - 2] - 80; //see OP_2-OP_16;
var pubkeys = [];
for (var i = 1; i < this.chunks.length - 2; i++) {
pubkeys.push(this.chunks[i]);
}
if (pubkeys.length != npubkeys) {
throw new Error("Script.getMultiSigInfo(): Amount of PKs does not match what the script specifies.");
}
return {
nsigs: nsigs,
npubkeys: npubkeys,
pubkeys: pubkeys
}
};
Script.prototype.prependOp0 = function() {
var chunks = [0];
for (i in this.chunks) {
if (this.chunks.hasOwnProperty(i)) {
chunks.push(this.chunks[i]);
}
}
this.chunks = chunks;
this.updateBuffer();
return this;
};
// is this a script form we know?
Script.prototype.classify = function() {
if (this.isPubkeyHash())
return TX_PUBKEYHASH;
if (this.isP2SH())
return TX_SCRIPTHASH;
if (this.isMultiSig())
return TX_MULTISIG;
if (this.isPubkey())
return TX_PUBKEY;
if (this.isReturn())
return TX_RETURN;
return TX_UNKNOWN;
};
// extract useful data items from known scripts
Script.prototype.capture = function() {
var txType = this.classify();
var res = [];
switch (txType) {
case TX_PUBKEY:
res.push(this.chunks[0]);
break;
case TX_PUBKEYHASH:
res.push(this.chunks[2]);
break;
case TX_MULTISIG:
for (var i = 1; i < (this.chunks.length - 2); i++)
res.push(this.chunks[i]);
break;
case TX_SCRIPTHASH:
res.push(this.chunks[1]);
break;
case TX_UNKNOWN:
default:
// do nothing
break;
}
return res;
};
// return first extracted data item from script
Script.prototype.captureOne = function() {
var arr = this.capture();
return arr[0];
};
Script.prototype.getOutType = function() {
var txType = this.classify();
switch (txType) {
case TX_PUBKEY:
return 'Pubkey';
case TX_PUBKEYHASH:
return 'Address';
default:
return 'Strange';
}
};
Script.prototype.getRawOutType = function() {
return TX_TYPES[this.classify()];
};
Script.prototype.simpleOutHash = function() {
switch (this.getOutType()) {
case 'Address':
return this.chunks[2];
case 'Pubkey':
return util.sha256ripe160(this.chunks[0]);
default:
log.debug("Encountered non-standard scriptPubKey");
log.debug("Strange script was: " + this.toString());
return null;
}
};
Script.prototype.getInType = function() {
if (this.chunks.length == 1) {
// Direct IP to IP transactions only have the public key in their scriptSig.
return 'Pubkey';
} else if (this.chunks.length == 2 &&
Buffer.isBuffer(this.chunks[0]) &&
Buffer.isBuffer(this.chunks[1])) {
return 'Address';
} else {
return 'Strange';
}
};
Script.prototype.simpleInPubKey = function() {
switch (this.getInType()) {
case 'Address':
return this.chunks[1];
case 'Pubkey':
return null;
default:
log.debug("Encountered non-standard scriptSig");
log.debug("Strange script was: " + this.toString());
return null;
}
};
Script.prototype.getBuffer = function() {
return this.buffer;
};
Script.prototype.serialize = Script.prototype.getBuffer;
Script.prototype.getStringContent = function(truncate, maxEl) {
if (truncate === null) {
truncate = true;
}
if ('undefined' === typeof maxEl) {
maxEl = 15;
}
var s = '';
for (var i = 0, l = this.chunks.length; i < l; i++) {
var chunk = this.chunks[i];
if (i > 0) {
s += ' ';
}
if (Buffer.isBuffer(chunk)) {
s += '0x' + util.formatBuffer(chunk, truncate ? null : 0);
} else {
s += Opcode.reverseMap[chunk];
}
if (maxEl && i > maxEl) {
s += ' ...';
break;
}
}
return s;
};
Script.prototype.toString = function(truncate, maxEl) {
var script = "<Script ";
script += this.getStringContent(truncate, maxEl);
script += ">";
return script;
};
Script.prototype.writeOp = function(opcode) {
var buf = Buffer(this.buffer.length + 1);
this.buffer.copy(buf);
buf.writeUInt8(opcode, this.buffer.length);
this.buffer = buf;
this.chunks.push(opcode);
return this;
};
Script.prototype.writeN = function(n) {
if (n < 0 || n > 16)
throw new Error("writeN: out of range value " + n);
if (n == 0)
this.writeOp(Opcode.map.OP_0);
else
this.writeOp(Opcode.map.OP_1 + n - 1);
};
function prefixSize(data_length) {
if (data_length < Opcode.map.OP_PUSHDATA1) {
return 1;
} else if (data_length <= 0xff) {
return 1 + 1;
} else if (data_length <= 0xffff) {
return 1 + 2;
} else {
return 1 + 4;
}
};
function encodeLen(data_length) {
var buf = undefined;
if (data_length < Opcode.map.OP_PUSHDATA1) {
buf = new Buffer(1);
buf.writeUInt8(data_length, 0);
} else if (data_length <= 0xff) {
buf = new Buffer(1 + 1);
buf.writeUInt8(Opcode.map.OP_PUSHDATA1, 0);
buf.writeUInt8(data_length, 1);
} else if (data_length <= 0xffff) {
buf = new Buffer(1 + 2);
buf.writeUInt8(Opcode.map.OP_PUSHDATA2, 0);
buf.writeUInt16LE(data_length, 1);
} else {
buf = new Buffer(1 + 4);
buf.writeUInt8(Opcode.map.OP_PUSHDATA4, 0);
buf.writeUInt32LE(data_length, 1);
}
return buf;
};
Script.prototype.writeBytes = function(data) {
var newSize = this.buffer.length + prefixSize(data.length) + data.length;
this.buffer = Buffer.concat([this.buffer, encodeLen(data.length), data]);
this.chunks.push(data);
};
Script.prototype.updateBuffer = function() {
this.buffer = Script.chunksToBuffer(this.chunks);
};
Script.prototype.findAndDelete = function(chunk) {
var dirty = false;
if (Buffer.isBuffer(chunk)) {
for (var i = 0, l = this.chunks.length; i < l; i++) {
if (Buffer.isBuffer(this.chunks[i]) &&
buffertools.compare(this.chunks[i], chunk) === 0) {
this.chunks.splice(i, 1);
i--;
dirty = true;
}
}
} else if ("number" === typeof chunk) {
for (var i = 0, l = this.chunks.length; i < l; i++) {
if (this.chunks[i] === chunk) {
this.chunks.splice(i, 1);
i--;
dirty = true;
}
}
} else {
throw new Error("Invalid chunk datatype.");
}
if (dirty) {
this.updateBuffer();
}
};
/**
* Creates a simple OP_CHECKSIG with pubkey output script.
*
* These are used for coinbase transactions and at some point were used for
* IP-based transactions as well.
*/
Script.createPubKeyOut = function(pubkey) {
var script = new Script();
script.writeBytes(pubkey);
script.writeOp(Opcode.map.OP_CHECKSIG);
return script;
};
/**
* Creates a standard txout script.
*/
Script.createPubKeyHashOut = function(pubKeyHash) {
var script = new Script();
script.writeOp(Opcode.map.OP_DUP);
script.writeOp(Opcode.map.OP_HASH160);
script.writeBytes(pubKeyHash);
script.writeOp(Opcode.map.OP_EQUALVERIFY);
script.writeOp(Opcode.map.OP_CHECKSIG);
return script;
};
Script._sortKeys = function(keys) {
return keys.sort(function(buf1, buf2) {
var len = buf1.length > buf1.length ? buf1.length : buf2.length;
for (var i = 0; i <= len; i++) {
if (buf1[i] === undefined)
return -1; //shorter strings come first
if (buf2[i] === undefined)
return 1;
if (buf1[i] < buf2[i])
return -1;
if (buf1[i] > buf2[i])
return 1;
else
continue;
}
return 0;
});
};
Script.createMultisig = function(n_required, inKeys, opts) {
opts = opts || {};
var keys = opts.noSorting ? inKeys : this._sortKeys(inKeys);
var script = new Script();
script.writeN(n_required);
keys.forEach(function(key) {
script.writeBytes(key);
});
script.writeN(keys.length);
script.writeOp(Opcode.map.OP_CHECKMULTISIG);
return script;
};
Script.createP2SH = function(scriptHash) {
var script = new Script();
script.writeOp(Opcode.map.OP_HASH160);
script.writeBytes(scriptHash);
script.writeOp(Opcode.map.OP_EQUAL);
return script;
};
Script.fromTestData = function(testData) {
testData = testData.map(function(chunk) {
if ("string" === typeof chunk) {
return new Buffer(chunk, 'hex');
} else {
return chunk;
}
});
var script = new Script();
script.chunks = testData;
script.updateBuffer();
return script;
};
Script.fromChunks = function(chunks) {
var script = new Script();
script.chunks = chunks;
script.updateBuffer();
return script;
};
Script.fromHumanReadable = function(s) {
return new Script(Script.stringToBuffer(s));
};
Script.prototype.toHumanReadable = function() {
var s = '';
for (var i = 0, l = this.chunks.length; i < l; i++) {
var chunk = this.chunks[i];
if (i > 0) {
s += ' ';
}
if (Buffer.isBuffer(chunk)) {
if (chunk.length === 0) {
s += '0';
} else {
s += '0x' + util.formatBuffer(encodeLen(chunk.length), 0) + ' ';
s += '0x' + util.formatBuffer(chunk, 0);
}
} else {
var opcode = Opcode.reverseMap[chunk];
if (typeof opcode === 'undefined') {
opcode = '0x' + chunk.toString(16);
}
s += opcode;
}
}
return s;
};
Script.stringToBuffer = function(s) {
var buf = new Put();
var split = s.split(' ');
for (var i = 0; i < split.length; i++) {
var word = split[i];
if (word === '') continue;
if (word.length > 2 && word.substring(0, 2) === '0x') {
// raw hex value
//console.log('hex value');
buf.put(new Buffer(word.substring(2, word.length), 'hex'));
} else {
var opcode = Opcode.map['OP_' + word] || Opcode.map[word];
if (typeof opcode !== 'undefined') {
// op code in string form
//console.log('opcode');
buf.word8(opcode);
} else {
var integer = parseInt(word);
if (!isNaN(integer)) {
// integer
//console.log('integer');
var data = util.intToBufferSM(integer);
buf.put(Script.chunksToBuffer([data]));
} else if (word[0] === '\'' && word[word.length - 1] === '\'') {
// string
//console.log('string');
word = word.substring(1, word.length - 1);
buf.put(Script.chunksToBuffer([new Buffer(word)]));
} else {
throw new Error('Could not parse word "' + word + '" from script "' + s + '"');
}
}
}
}
return buf.buffer();
};
Script.chunksToBuffer = function(chunks) {
var buf = new Put();
for (var i = 0, l = chunks.length; i < l; i++) {
var data = chunks[i];
if (Buffer.isBuffer(data)) {
if (data.length < Opcode.map.OP_PUSHDATA1) {
buf.word8(data.length);
} else if (data.length <= 0xff) {
buf.word8(Opcode.map.OP_PUSHDATA1);
buf.word8(data.length);
} else if (data.length <= 0xffff) {
buf.word8(Opcode.map.OP_PUSHDATA2);
buf.word16le(data.length);
} else {
buf.word8(Opcode.map.OP_PUSHDATA4);
buf.word32le(data.length);
}
buf.put(data);
} else if ("number" === typeof data) {
buf.word8(data);
} else {
throw new Error("Script.chunksToBuffer(): Invalid chunk datatype");
}
}
return buf.buffer();
};
module.exports = Script;