@onekeyfe/kaspa-core-lib 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +32 -0
  2. package/README.md +59 -0
  3. package/index.d.ts +410 -0
  4. package/index.js +93 -0
  5. package/lib/address.js +792 -0
  6. package/lib/block/block.js +281 -0
  7. package/lib/block/blockheader.js +296 -0
  8. package/lib/block/index.js +4 -0
  9. package/lib/block/merkleblock.js +313 -0
  10. package/lib/crypto/bn.js +218 -0
  11. package/lib/crypto/ecdsa.js +296 -0
  12. package/lib/crypto/hash.js +85 -0
  13. package/lib/crypto/point.js +166 -0
  14. package/lib/crypto/random.js +57 -0
  15. package/lib/crypto/schnorr.js +287 -0
  16. package/lib/crypto/signature.js +441 -0
  17. package/lib/encoding/base58.js +70 -0
  18. package/lib/encoding/base58check.js +95 -0
  19. package/lib/encoding/bufferreader.js +202 -0
  20. package/lib/encoding/bufferwriter.js +155 -0
  21. package/lib/encoding/varint.js +72 -0
  22. package/lib/errors/index.js +61 -0
  23. package/lib/errors/spec.js +191 -0
  24. package/lib/hdprivatekey.js +646 -0
  25. package/lib/hdpublickey.js +496 -0
  26. package/lib/networks.js +277 -0
  27. package/lib/opcode.js +261 -0
  28. package/lib/privatekey.js +431 -0
  29. package/lib/publickey.js +375 -0
  30. package/lib/script/index.js +3 -0
  31. package/lib/script/interpreter.js +2107 -0
  32. package/lib/script/script.js +1163 -0
  33. package/lib/transaction/index.js +7 -0
  34. package/lib/transaction/input/index.js +6 -0
  35. package/lib/transaction/input/input.js +375 -0
  36. package/lib/transaction/input/multisig.js +221 -0
  37. package/lib/transaction/input/multisigscripthash.js +178 -0
  38. package/lib/transaction/input/publickey.js +91 -0
  39. package/lib/transaction/input/publickeyhash.js +100 -0
  40. package/lib/transaction/output.js +188 -0
  41. package/lib/transaction/sighash.js +534 -0
  42. package/lib/transaction/signature.js +89 -0
  43. package/lib/transaction/transaction.js +1384 -0
  44. package/lib/transaction/unspentoutput.js +109 -0
  45. package/lib/unit.js +238 -0
  46. package/lib/uri.js +223 -0
  47. package/lib/util/base32.js +63 -0
  48. package/lib/util/buffer.js +162 -0
  49. package/lib/util/convertBits.js +62 -0
  50. package/lib/util/js.js +110 -0
  51. package/lib/util/log.js +10 -0
  52. package/lib/util/preconditions.js +34 -0
  53. package/package.json +62 -0
@@ -0,0 +1,1163 @@
1
+ var Address = require('../address');
2
+ var BufferReader = require('../encoding/bufferreader');
3
+ var BufferWriter = require('../encoding/bufferwriter');
4
+ var Hash = require('../crypto/hash');
5
+ var Opcode = require('../opcode');
6
+ var PublicKey = require('../publickey');
7
+ var Signature = require('../crypto/signature');
8
+ var Networks = require('../networks');
9
+ var $ = require('../util/preconditions');
10
+ var _ = require('lodash');
11
+ var errors = require('../errors');
12
+ var buffer = require('buffer');
13
+ var BufferUtil = require('../util/buffer');
14
+ var JSUtil = require('../util/js');
15
+
16
+ /**
17
+ * A bitcoin transaction script. Each transaction's inputs and outputs
18
+ * has a script that is evaluated to validate it's spending.
19
+ *
20
+ * See https://en.bitcoin.it/wiki/Script
21
+ *
22
+ * @constructor
23
+ * @param {Object|string|Buffer=} from optional data to populate script
24
+ */
25
+ var Script = function Script(from) {
26
+ if (!(this instanceof Script)) {
27
+ return new Script(from);
28
+ }
29
+ this.chunks = [];
30
+
31
+ if (BufferUtil.isBuffer(from)) {
32
+ return Script.fromBuffer(from);
33
+ } else if (from instanceof Address) {
34
+ return Script.fromAddress(from);
35
+ } else if (from instanceof Script) {
36
+ return Script.fromBuffer(from.toBuffer());
37
+ } else if (_.isString(from)) {
38
+ return Script.fromString(from);
39
+ } else if (_.isObject(from) && _.isArray(from.chunks)) {
40
+ this.set(from);
41
+ }
42
+ };
43
+
44
+ Script.prototype.set = function(obj) {
45
+ $.checkArgument(_.isObject(obj));
46
+ $.checkArgument(_.isArray(obj.chunks));
47
+ this.chunks = obj.chunks;
48
+ return this;
49
+ };
50
+
51
+ Script.fromBuffer = function(buffer) {
52
+ var script = new Script();
53
+ script.chunks = [];
54
+
55
+ var br = new BufferReader(buffer);
56
+ while (!br.finished()) {
57
+ try {
58
+ var opcodenum = br.readUInt8();
59
+
60
+ var len, buf;
61
+ if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
62
+ len = opcodenum;
63
+ script.chunks.push({
64
+ buf: br.read(len),
65
+ len: len,
66
+ opcodenum: opcodenum
67
+ });
68
+ } else if (opcodenum === Opcode.OP_PUSHDATA1) {
69
+ len = br.readUInt8();
70
+ buf = br.read(len);
71
+ script.chunks.push({
72
+ buf: buf,
73
+ len: len,
74
+ opcodenum: opcodenum
75
+ });
76
+ } else if (opcodenum === Opcode.OP_PUSHDATA2) {
77
+ len = br.readUInt16LE();
78
+ buf = br.read(len);
79
+ script.chunks.push({
80
+ buf: buf,
81
+ len: len,
82
+ opcodenum: opcodenum
83
+ });
84
+ } else if (opcodenum === Opcode.OP_PUSHDATA4) {
85
+ len = br.readUInt32LE();
86
+ buf = br.read(len);
87
+ script.chunks.push({
88
+ buf: buf,
89
+ len: len,
90
+ opcodenum: opcodenum
91
+ });
92
+ } else {
93
+ script.chunks.push({
94
+ opcodenum: opcodenum
95
+ });
96
+ }
97
+ } catch (e) {
98
+ if (e instanceof RangeError) {
99
+ throw new errors.Script.InvalidBuffer(buffer.toString('hex'));
100
+ }
101
+ throw e;
102
+ }
103
+ }
104
+
105
+ return script;
106
+ };
107
+
108
+ Script.prototype.toBuffer = function() {
109
+ var bw = new BufferWriter();
110
+
111
+ for (var i = 0; i < this.chunks.length; i++) {
112
+ var chunk = this.chunks[i];
113
+ var opcodenum = chunk.opcodenum;
114
+ bw.writeUInt8(chunk.opcodenum);
115
+ if (chunk.buf) {
116
+ if (opcodenum < Opcode.OP_PUSHDATA1) {
117
+ bw.write(chunk.buf);
118
+ } else if (opcodenum === Opcode.OP_PUSHDATA1) {
119
+ bw.writeUInt8(chunk.len);
120
+ bw.write(chunk.buf);
121
+ } else if (opcodenum === Opcode.OP_PUSHDATA2) {
122
+ bw.writeUInt16LE(chunk.len);
123
+ bw.write(chunk.buf);
124
+ } else if (opcodenum === Opcode.OP_PUSHDATA4) {
125
+ bw.writeUInt32LE(chunk.len);
126
+ bw.write(chunk.buf);
127
+ }
128
+ }
129
+ }
130
+
131
+ return bw.concat();
132
+ };
133
+
134
+ Script.fromASM = function(str) {
135
+ var script = new Script();
136
+ script.chunks = [];
137
+
138
+ var tokens = str.split(' ');
139
+ var i = 0;
140
+ while (i < tokens.length) {
141
+ var token = tokens[i];
142
+ var opcode = Opcode(token);
143
+ var opcodenum = opcode.toNumber();
144
+
145
+ if (_.isUndefined(opcodenum)) {
146
+ var buf = Buffer.from(tokens[i], 'hex');
147
+ var opcodenum;
148
+ var len = buf.length;
149
+ if (len >= 0 && len < Opcode.OP_PUSHDATA1) {
150
+ opcodenum = len;
151
+ } else if (len < Math.pow(2, 8)) {
152
+ opcodenum = Opcode.OP_PUSHDATA1;
153
+ } else if (len < Math.pow(2, 16)) {
154
+ opcodenum = Opcode.OP_PUSHDATA2;
155
+ } else if (len < Math.pow(2, 32)) {
156
+ opcodenum = Opcode.OP_PUSHDATA4;
157
+ }
158
+ script.chunks.push({
159
+ buf: buf,
160
+ len: buf.length,
161
+ opcodenum: opcodenum
162
+ });
163
+ i = i + 1;
164
+ } else {
165
+ script.chunks.push({
166
+ opcodenum: opcodenum
167
+ });
168
+ i = i + 1;
169
+ }
170
+ }
171
+ return script;
172
+ };
173
+
174
+ Script.fromHex = function(str) {
175
+ return new Script(Buffer.from(str, 'hex'));
176
+ };
177
+
178
+ Script.fromString = function(str) {
179
+ if (JSUtil.isHexa(str) || str.length === 0) {
180
+ return new Script(Buffer.from(str, 'hex'));
181
+ }
182
+ var script = new Script();
183
+ script.chunks = [];
184
+
185
+ var tokens = str.split(' ');
186
+ var i = 0;
187
+ while (i < tokens.length) {
188
+ var token = tokens[i];
189
+ var opcode = Opcode(token);
190
+ var opcodenum = opcode.toNumber();
191
+
192
+ if (_.isUndefined(opcodenum)) {
193
+ opcodenum = parseInt(token);
194
+ if (opcodenum > 0 && opcodenum < Opcode.OP_PUSHDATA1) {
195
+ script.chunks.push({
196
+ buf: Buffer.from(tokens[i + 1].slice(2), 'hex'),
197
+ len: opcodenum,
198
+ opcodenum: opcodenum
199
+ });
200
+ i = i + 2;
201
+ } else {
202
+ throw new Error('Invalid script: ' + JSON.stringify(str));
203
+ }
204
+ } else if (opcodenum === Opcode.OP_PUSHDATA1 ||
205
+ opcodenum === Opcode.OP_PUSHDATA2 ||
206
+ opcodenum === Opcode.OP_PUSHDATA4) {
207
+ if (tokens[i + 2].slice(0, 2) !== '0x') {
208
+ throw new Error('Pushdata data must start with 0x');
209
+ }
210
+ script.chunks.push({
211
+ buf: Buffer.from(tokens[i + 2].slice(2), 'hex'),
212
+ len: parseInt(tokens[i + 1]),
213
+ opcodenum: opcodenum
214
+ });
215
+ i = i + 3;
216
+ } else {
217
+ script.chunks.push({
218
+ opcodenum: opcodenum
219
+ });
220
+ i = i + 1;
221
+ }
222
+ }
223
+ return script;
224
+ };
225
+
226
+ Script.prototype._chunkToString = function(chunk, type) {
227
+ var opcodenum = chunk.opcodenum;
228
+ var asm = (type === 'asm');
229
+ var str = '';
230
+ if (!chunk.buf) {
231
+ // no data chunk
232
+ if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
233
+ if (asm) {
234
+ // A few cases where the opcode name differs from reverseMap
235
+ // aside from 1 to 16 data pushes.
236
+ if (opcodenum === 0) {
237
+ // OP_0 -> 0
238
+ str = str + ' 0';
239
+ } else if(opcodenum === 79) {
240
+ // OP_1NEGATE -> 1
241
+ str = str + ' -1';
242
+ } else {
243
+ str = str + ' ' + Opcode(opcodenum).toString();
244
+ }
245
+ } else {
246
+ str = str + ' ' + Opcode(opcodenum).toString();
247
+ }
248
+ } else {
249
+ var numstr = opcodenum.toString(16);
250
+ if (numstr.length % 2 !== 0) {
251
+ numstr = '0' + numstr;
252
+ }
253
+ if (asm) {
254
+ str = str + ' ' + numstr;
255
+ } else {
256
+ str = str + ' ' + '0x' + numstr;
257
+ }
258
+ }
259
+ } else {
260
+ // data chunk
261
+ if (!asm && (opcodenum === Opcode.OP_PUSHDATA1 ||
262
+ opcodenum === Opcode.OP_PUSHDATA2 ||
263
+ opcodenum === Opcode.OP_PUSHDATA4)) {
264
+ str = str + ' ' + Opcode(opcodenum).toString();
265
+ }
266
+ if (chunk.len > 0) {
267
+ if (asm) {
268
+ str = str + ' ' + chunk.buf.toString('hex');
269
+ } else {
270
+ str = str + ' ' + chunk.len + ' ' + '0x' + chunk.buf.toString('hex');
271
+ }
272
+ }
273
+ }
274
+ return str;
275
+ };
276
+
277
+ Script.prototype.toASM = function() {
278
+ var str = '';
279
+ for (var i = 0; i < this.chunks.length; i++) {
280
+ var chunk = this.chunks[i];
281
+ str += this._chunkToString(chunk, 'asm');
282
+ }
283
+
284
+ return str.substr(1);
285
+ };
286
+
287
+ Script.prototype.toString = function() {
288
+ var str = '';
289
+ for (var i = 0; i < this.chunks.length; i++) {
290
+ var chunk = this.chunks[i];
291
+ str += this._chunkToString(chunk);
292
+ }
293
+
294
+ return str.substr(1);
295
+ };
296
+
297
+ Script.prototype.toHex = function() {
298
+ return this.toBuffer().toString('hex');
299
+ };
300
+
301
+ Script.prototype.inspect = function() {
302
+ return '<Script: ' + this.toString() + '>';
303
+ };
304
+
305
+ // script classification methods
306
+
307
+ /**
308
+ * @returns {boolean} if this is a pay to pubkey hash output script
309
+ */
310
+ Script.prototype.isPublicKeyHashOut = function() {
311
+ return !!(this.chunks.length === 5 &&
312
+ this.chunks[0].opcodenum === Opcode.OP_DUP &&
313
+ this.chunks[1].opcodenum === Opcode.OP_HASH160 &&
314
+ this.chunks[2].buf &&
315
+ this.chunks[2].buf.length === 20 &&
316
+ this.chunks[3].opcodenum === Opcode.OP_EQUALVERIFY &&
317
+ this.chunks[4].opcodenum === Opcode.OP_CHECKSIG);
318
+ };
319
+
320
+ /**
321
+ * @returns {boolean} if this is a pay to public key hash input script
322
+ */
323
+ Script.prototype.isPublicKeyHashIn = function() {
324
+ if (this.chunks.length === 2) {
325
+ var signatureBuf = this.chunks[0].buf;
326
+ var pubkeyBuf = this.chunks[1].buf;
327
+ if (signatureBuf &&
328
+ signatureBuf.length &&
329
+ pubkeyBuf &&
330
+ pubkeyBuf.length
331
+ ) {
332
+ var version = pubkeyBuf[0];
333
+ if ((version === 0x04 ||
334
+ version === 0x06 ||
335
+ version === 0x07) && pubkeyBuf.length === 65) {
336
+ return true;
337
+ } else if ((version === 0x03 || version === 0x02) && pubkeyBuf.length === 33) {
338
+ return true;
339
+ }
340
+ }
341
+ }
342
+ return false;
343
+ };
344
+
345
+ Script.prototype.getPublicKey = function() {
346
+ $.checkState(this.isPublicKeyOut(), 'Can\'t retrieve PublicKey from a non-PK output');
347
+ return this.chunks[0].buf;
348
+ };
349
+
350
+ Script.prototype.getPublicKeyHash = function() {
351
+ $.checkState(this.isPublicKeyHashOut(), 'Can\'t retrieve PublicKeyHash from a non-PKH output');
352
+ return this.chunks[2].buf;
353
+ };
354
+
355
+ /**
356
+ * @returns {boolean} if this is a public key output script
357
+ */
358
+ Script.prototype.isPublicKeyOut = function() {
359
+ return !!(this.chunks.length === 2 &&
360
+ this.chunks[0].buf &&
361
+ this.chunks[0].buf.length === 32 &&
362
+ this.chunks[1].opcodenum === Opcode.OP_CHECKSIG);
363
+ };
364
+
365
+ /**
366
+ * @returns {boolean} if this is a pay to public key input script
367
+ */
368
+ Script.prototype.isPublicKeyIn = function() {
369
+ if (this.chunks.length === 1) {
370
+ var signatureBuf = this.chunks[0].buf;
371
+ if (signatureBuf &&
372
+ signatureBuf.length &&
373
+ signatureBuf[0] === 0x30) {
374
+ return true;
375
+ }
376
+ }
377
+ return false;
378
+ };
379
+
380
+ /**
381
+ * @returns {boolean} if this is a p2sh output script
382
+ */
383
+ Script.prototype.isScriptHashOut = function() {
384
+ var buf = this.toBuffer();
385
+ return (buf.length === 23 &&
386
+ buf[0] === Opcode.OP_HASH160 &&
387
+ buf[1] === 0x14 &&
388
+ buf[buf.length - 1] === Opcode.OP_EQUAL);
389
+ };
390
+
391
+ /**
392
+ * @returns {boolean} if this is a p2sh input script
393
+ * Note that these are frequently indistinguishable from pubkeyhashin
394
+ */
395
+ Script.prototype.isScriptHashIn = function() {
396
+ if (this.chunks.length <= 1) {
397
+ return false;
398
+ }
399
+ var redeemChunk = this.chunks[this.chunks.length - 1];
400
+ var redeemBuf = redeemChunk.buf;
401
+ if (!redeemBuf) {
402
+ return false;
403
+ }
404
+
405
+ var redeemScript;
406
+ try {
407
+ redeemScript = Script.fromBuffer(redeemBuf);
408
+ } catch (e) {
409
+ if (e instanceof errors.Script.InvalidBuffer) {
410
+ return false;
411
+ }
412
+ throw e;
413
+ }
414
+ var type = redeemScript.classify();
415
+ return type !== Script.types.UNKNOWN;
416
+ };
417
+
418
+ /**
419
+ * @returns {boolean} if this is a mutlsig output script
420
+ */
421
+ Script.prototype.isMultisigOut = function() {
422
+ return (this.chunks.length > 3 &&
423
+ Opcode.isSmallIntOp(this.chunks[0].opcodenum) &&
424
+ this.chunks.slice(1, this.chunks.length - 2).every(function(obj) {
425
+ return obj.buf && BufferUtil.isBuffer(obj.buf);
426
+ }) &&
427
+ Opcode.isSmallIntOp(this.chunks[this.chunks.length - 2].opcodenum) &&
428
+ this.chunks[this.chunks.length - 1].opcodenum === Opcode.OP_CHECKMULTISIG);
429
+ };
430
+
431
+
432
+ /**
433
+ * @returns {boolean} if this is a multisig input script
434
+ */
435
+ Script.prototype.isMultisigIn = function() {
436
+ return this.chunks.length >= 2 &&
437
+ this.chunks[0].opcodenum === 0 &&
438
+ this.chunks.slice(1, this.chunks.length).every(function(obj) {
439
+ return obj.buf &&
440
+ BufferUtil.isBuffer(obj.buf) &&
441
+ Signature.isTxDER(obj.buf);
442
+ });
443
+ };
444
+
445
+ /**
446
+ * @returns {boolean} true if this is a valid standard OP_RETURN output
447
+ */
448
+ Script.prototype.isDataOut = function() {
449
+ var step1 = this.chunks.length >= 1 &&
450
+ this.chunks[0].opcodenum === Opcode.OP_RETURN &&
451
+ this.toBuffer().length <= 223; // 223 instead of 220 because (+1 for OP_RETURN, +2 for the pushdata opcodes)
452
+ if (!step1) return false;
453
+ var chunks = this.chunks.slice(1);
454
+ var script2 = new Script({chunks: chunks});
455
+ return script2.isPushOnly();
456
+ };
457
+
458
+ /**
459
+ * Retrieve the associated data for this script.
460
+ * In the case of a pay to public key hash or P2SH, return the hash.
461
+ * In the case of a standard OP_RETURN, return the data
462
+ * @returns {Buffer}
463
+ */
464
+ Script.prototype.getData = function() {
465
+ if (this.isDataOut() || this.isScriptHashOut()) {
466
+ if (_.isUndefined(this.chunks[1])) {
467
+ return Buffer.alloc(0);
468
+ } else {
469
+ return Buffer.from(this.chunks[1].buf);
470
+ }
471
+ }
472
+ if (this.isPublicKeyHashOut()) {
473
+ return Buffer.from(this.chunks[2].buf);
474
+ }
475
+ throw new Error('Unrecognized script type to get data from');
476
+ };
477
+
478
+ /**
479
+ * @returns {boolean} if the script is only composed of data pushing
480
+ * opcodes or small int opcodes (OP_0, OP_1, ..., OP_16)
481
+ */
482
+ Script.prototype.isPushOnly = function() {
483
+ return _.every(this.chunks, function(chunk) {
484
+ return chunk.opcodenum <= Opcode.OP_16 ||
485
+ chunk.opcodenum === Opcode.OP_PUSHDATA1 ||
486
+ chunk.opcodenum === Opcode.OP_PUSHDATA2 ||
487
+ chunk.opcodenum === Opcode.OP_PUSHDATA4;
488
+ });
489
+ };
490
+
491
+
492
+ Script.types = {};
493
+ Script.types.UNKNOWN = 'Unknown';
494
+ Script.types.PUBKEY_OUT = 'Pay to public key';
495
+ Script.types.PUBKEY_IN = 'Spend from public key';
496
+ Script.types.PUBKEYHASH_OUT = 'Pay to public key hash';
497
+ Script.types.PUBKEYHASH_IN = 'Spend from public key hash';
498
+ Script.types.SCRIPTHASH_OUT = 'Pay to script hash';
499
+ Script.types.SCRIPTHASH_IN = 'Spend from script hash';
500
+ Script.types.MULTISIG_OUT = 'Pay to multisig';
501
+ Script.types.MULTISIG_IN = 'Spend from multisig';
502
+ Script.types.DATA_OUT = 'Data push';
503
+
504
+ Script.OP_RETURN_STANDARD_SIZE = 220;
505
+
506
+ /**
507
+ * @returns {object} The Script type if it is a known form,
508
+ * or Script.UNKNOWN if it isn't
509
+ */
510
+ Script.prototype.classify = function() {
511
+ if (this._isInput) {
512
+ return this.classifyInput();
513
+ } else if (this._isOutput) {
514
+ return this.classifyOutput();
515
+ } else {
516
+ var outputType = this.classifyOutput();
517
+ return outputType != Script.types.UNKNOWN ? outputType : this.classifyInput();
518
+ }
519
+ };
520
+
521
+ Script.outputIdentifiers = {};
522
+ Script.outputIdentifiers.PUBKEY_OUT = Script.prototype.isPublicKeyOut;
523
+ Script.outputIdentifiers.PUBKEYHASH_OUT = Script.prototype.isPublicKeyHashOut;
524
+ Script.outputIdentifiers.MULTISIG_OUT = Script.prototype.isMultisigOut;
525
+ Script.outputIdentifiers.SCRIPTHASH_OUT = Script.prototype.isScriptHashOut;
526
+ Script.outputIdentifiers.DATA_OUT = Script.prototype.isDataOut;
527
+
528
+ /**
529
+ * @returns {object} The Script type if it is a known form,
530
+ * or Script.UNKNOWN if it isn't
531
+ */
532
+ Script.prototype.classifyOutput = function() {
533
+ for (var type in Script.outputIdentifiers) {
534
+ if (Script.outputIdentifiers[type].bind(this)()) {
535
+ return Script.types[type];
536
+ }
537
+ }
538
+ return Script.types.UNKNOWN;
539
+ };
540
+
541
+ Script.inputIdentifiers = {};
542
+ Script.inputIdentifiers.PUBKEY_IN = Script.prototype.isPublicKeyIn;
543
+ Script.inputIdentifiers.PUBKEYHASH_IN = Script.prototype.isPublicKeyHashIn;
544
+ Script.inputIdentifiers.MULTISIG_IN = Script.prototype.isMultisigIn;
545
+ Script.inputIdentifiers.SCRIPTHASH_IN = Script.prototype.isScriptHashIn;
546
+
547
+ /**
548
+ * @returns {object} The Script type if it is a known form,
549
+ * or Script.UNKNOWN if it isn't
550
+ */
551
+ Script.prototype.classifyInput = function() {
552
+ for (var type in Script.inputIdentifiers) {
553
+ if (Script.inputIdentifiers[type].bind(this)()) {
554
+ return Script.types[type];
555
+ }
556
+ }
557
+ return Script.types.UNKNOWN;
558
+ };
559
+
560
+
561
+ /**
562
+ * @returns {boolean} if script is one of the known types
563
+ */
564
+ Script.prototype.isStandard = function() {
565
+ // TODO: Add BIP62 compliance
566
+ return this.classify() !== Script.types.UNKNOWN;
567
+ };
568
+
569
+
570
+ // Script construction methods
571
+
572
+ /**
573
+ * Adds a script element at the start of the script.
574
+ * @param {*} obj a string, number, Opcode, Buffer, or object to add
575
+ * @returns {Script} this script instance
576
+ */
577
+ Script.prototype.prepend = function(obj) {
578
+ this._addByType(obj, true);
579
+ return this;
580
+ };
581
+
582
+ /**
583
+ * Compares a script with another script
584
+ */
585
+ Script.prototype.equals = function(script) {
586
+ $.checkState(script instanceof Script, 'Must provide another script');
587
+ if (this.chunks.length !== script.chunks.length) {
588
+ return false;
589
+ }
590
+ var i;
591
+ for (i = 0; i < this.chunks.length; i++) {
592
+ if (BufferUtil.isBuffer(this.chunks[i].buf) && !BufferUtil.isBuffer(script.chunks[i].buf)) {
593
+ return false;
594
+ }
595
+ if (BufferUtil.isBuffer(this.chunks[i].buf) && !BufferUtil.equals(this.chunks[i].buf, script.chunks[i].buf)) {
596
+ return false;
597
+ } else if (this.chunks[i].opcodenum !== script.chunks[i].opcodenum) {
598
+ return false;
599
+ }
600
+ }
601
+ return true;
602
+ };
603
+
604
+ /**
605
+ * Adds a script element to the end of the script.
606
+ *
607
+ * @param {*} obj a string, number, Opcode, Buffer, or object to add
608
+ * @returns {Script} this script instance
609
+ *
610
+ */
611
+ Script.prototype.add = function(obj) {
612
+ this._addByType(obj, false);
613
+ return this;
614
+ };
615
+
616
+ Script.prototype._addByType = function(obj, prepend) {
617
+ if (typeof obj === 'string') {
618
+ this._addOpcode(obj, prepend);
619
+ } else if (typeof obj === 'number') {
620
+ this._addOpcode(obj, prepend);
621
+ } else if (obj instanceof Opcode) {
622
+ this._addOpcode(obj, prepend);
623
+ } else if (BufferUtil.isBuffer(obj)) {
624
+ this._addBuffer(obj, prepend);
625
+ } else if (obj instanceof Script) {
626
+ this.chunks = this.chunks.concat(obj.chunks);
627
+ } else if (typeof obj === 'object') {
628
+ this._insertAtPosition(obj, prepend);
629
+ } else {
630
+ throw new Error('Invalid script chunk');
631
+ }
632
+ };
633
+
634
+ Script.prototype._insertAtPosition = function(op, prepend) {
635
+ if (prepend) {
636
+ this.chunks.unshift(op);
637
+ } else {
638
+ this.chunks.push(op);
639
+ }
640
+ };
641
+
642
+ Script.prototype._addOpcode = function(opcode, prepend) {
643
+ var op;
644
+ if (typeof opcode === 'number') {
645
+ op = opcode;
646
+ } else if (opcode instanceof Opcode) {
647
+ op = opcode.toNumber();
648
+ } else {
649
+ op = Opcode(opcode).toNumber();
650
+ }
651
+ this._insertAtPosition({
652
+ opcodenum: op
653
+ }, prepend);
654
+ return this;
655
+ };
656
+
657
+ Script.prototype._addBuffer = function(buf, prepend) {
658
+ var opcodenum;
659
+ var len = buf.length;
660
+ if (len >= 0 && len < Opcode.OP_PUSHDATA1) {
661
+ opcodenum = len;
662
+ } else if (len < Math.pow(2, 8)) {
663
+ opcodenum = Opcode.OP_PUSHDATA1;
664
+ } else if (len < Math.pow(2, 16)) {
665
+ opcodenum = Opcode.OP_PUSHDATA2;
666
+ } else if (len < Math.pow(2, 32)) {
667
+ opcodenum = Opcode.OP_PUSHDATA4;
668
+ } else {
669
+ throw new Error('You can\'t push that much data');
670
+ }
671
+ this._insertAtPosition({
672
+ buf: buf,
673
+ len: len,
674
+ opcodenum: opcodenum
675
+ }, prepend);
676
+ return this;
677
+ };
678
+
679
+
680
+ Script.prototype.removeCodeseparators = function() {
681
+ var chunks = [];
682
+ for (var i = 0; i < this.chunks.length; i++) {
683
+ if (this.chunks[i].opcodenum !== Opcode.OP_CODESEPARATOR) {
684
+ chunks.push(this.chunks[i]);
685
+ }
686
+ }
687
+ this.chunks = chunks;
688
+ return this;
689
+ };
690
+
691
+ // high level script builder methods
692
+
693
+ /**
694
+ * @returns {Script} a new Multisig output script for given public keys,
695
+ * requiring m of those public keys to spend
696
+ * @param {PublicKey[]} publicKeys - list of all public keys controlling the output
697
+ * @param {number} threshold - amount of required signatures to spend the output
698
+ * @param {Object=} opts - Several options:
699
+ * - noSorting: defaults to false, if true, don't sort the given
700
+ * public keys before creating the script
701
+ */
702
+ Script.buildMultisigOut = function(publicKeys, threshold, opts) {
703
+ $.checkArgument(threshold <= publicKeys.length,
704
+ 'Number of required signatures must be less than or equal to the number of public keys');
705
+ opts = opts || {};
706
+ var script = new Script();
707
+ script.add(Opcode.smallInt(threshold));
708
+ publicKeys = _.map(publicKeys, PublicKey);
709
+ var sorted = publicKeys;
710
+ if (!opts.noSorting) {
711
+ sorted = _.sortBy(publicKeys, function(publicKey) {
712
+ return publicKey.toString('hex');
713
+ });
714
+ }
715
+ for (var i = 0; i < sorted.length; i++) {
716
+ var publicKey = sorted[i];
717
+ script.add(publicKey.toBuffer());
718
+ }
719
+ script.add(Opcode.smallInt(publicKeys.length));
720
+ script.add(Opcode.OP_CHECKMULTISIG);
721
+ return script;
722
+ };
723
+
724
+ /**
725
+ * A new Multisig input script for the given public keys, requiring m of those public keys to spend
726
+ *
727
+ * @param {PublicKey[]} pubkeys list of all public keys controlling the output
728
+ * @param {number} threshold amount of required signatures to spend the output
729
+ * @param {Array} signatures and array of signature buffers to append to the script
730
+ * @param {Object=} opts
731
+ * @param {boolean=} opts.noSorting don't sort the given public keys before creating the script (false by default)
732
+ * @param {Script=} opts.cachedMultisig don't recalculate the redeemScript
733
+ *
734
+ * @returns {Script}
735
+ */
736
+ Script.buildMultisigIn = function(pubkeys, threshold, signatures, opts) {
737
+ $.checkArgument(_.isArray(pubkeys));
738
+ $.checkArgument(_.isNumber(threshold));
739
+ $.checkArgument(_.isArray(signatures));
740
+ opts = opts || {};
741
+ var s = new Script();
742
+
743
+ if (opts.signingMethod === "schnorr" && opts.checkBits) {
744
+
745
+ // Spec according to https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/2019-11-15-schnorrmultisig.md#scriptsig-size
746
+ let checkBitsString = Buffer.from(opts.checkBits).reverse().join('');
747
+ let checkBitsDecimal = parseInt(checkBitsString, 2);
748
+ let checkBitsHex = parseInt(checkBitsDecimal.toString(16), 16);
749
+ let N = pubkeys.length;
750
+ // N should only be 1-20
751
+ if (N >= 1 && N <= 4) {
752
+ s.add(Opcode(checkBitsHex));
753
+ }
754
+ else if (N >= 5 && N <= 8) {
755
+ if(checkBitsHex === 0x81) {
756
+ s.add(Opcode("OP_1NEGATE")) // OP_1NEGATE
757
+ } else if(checkBitsHex > 0x10) {
758
+ s.add(0x01);
759
+ s.add(checkBitsHex);
760
+ } else {
761
+ s.add(Opcode(checkBitsHex));
762
+ }
763
+
764
+ }
765
+ else if (N >= 9 && N <= 16) {
766
+ s.add(0x02);
767
+ s.add(checkBitsHex);
768
+ }
769
+ else if (N >= 17 && N <= 20) {
770
+ s.add(0x03);
771
+ s.add(checkBitsHex);
772
+ }
773
+ } else {
774
+ s.add(Opcode.OP_0); // ecdsa schnorr mode; multisig dummy param of 0
775
+ }
776
+
777
+
778
+ _.each(signatures, function(signature) {
779
+ $.checkArgument(BufferUtil.isBuffer(signature), 'Signatures must be an array of Buffers');
780
+ // TODO: allow signatures to be an array of Signature objects
781
+ s.add(signature);
782
+ });
783
+ return s;
784
+ };
785
+
786
+ /**
787
+ * A new P2SH Multisig input script for the given public keys, requiring m of those public keys to spend
788
+ *
789
+ * @param {PublicKey[]} pubkeys list of all public keys controlling the output
790
+ * @param {number} threshold amount of required signatures to spend the output
791
+ * @param {Array} signatures and array of signature buffers to append to the script
792
+ * @param {Object=} opts
793
+ * @param {boolean=} opts.noSorting don't sort the given public keys before creating the script (false by default)
794
+ * @param {Script=} opts.cachedMultisig don't recalculate the redeemScript
795
+ * @param {Uint8Array} opts.checkBits bitfield map 1 or 0 to check which signatures to map against public keys for verification in schnorr multisig mode
796
+ * @param {String} opts.signingMethod method with which input will be signed "ecdsa" or "schnorr"
797
+ *
798
+ * @returns {Script}
799
+ */
800
+ Script.buildP2SHMultisigIn = function(pubkeys, threshold, signatures, opts) {
801
+ $.checkArgument(_.isArray(pubkeys));
802
+ $.checkArgument(_.isNumber(threshold));
803
+ $.checkArgument(_.isArray(signatures));
804
+ opts = opts || {};
805
+ var s = new Script();
806
+
807
+ if (opts.signingMethod === "schnorr" && opts.checkBits) {
808
+
809
+ // Spec according to https://github.com/bitcoincashorg/bitcoincash.org/blob/master/spec/2019-11-15-schnorrmultisig.md#scriptsig-size
810
+ let checkBitsString = Buffer.from(opts.checkBits).reverse().join('');
811
+ let checkBitsDecimal = parseInt(checkBitsString, 2);
812
+ let checkBitsHex = parseInt(checkBitsDecimal.toString(16), 16);
813
+ let N = pubkeys.length;
814
+ // N should only be 1-20
815
+ if (N >= 1 && N <= 4) {
816
+ s.add(Opcode.smallInt(checkBitsDecimal));
817
+ }
818
+ else if (N >= 5 && N <= 8) {
819
+ if(checkBitsHex === 0x81) {
820
+ s.add(Opcode("OP_1NEGATE")) // OP_1NEGATE
821
+ } else if(checkBitsHex > 0x10) {
822
+ s.add(0x01);
823
+ s.add(checkBitsHex);
824
+ } else {
825
+ s.add(Opcode.smallInt(checkBitsDecimal));
826
+ }
827
+ }
828
+ else if (N >= 9 && N <= 16) {
829
+ s.add(0x02);
830
+ s.add(checkBitsHex);
831
+ }
832
+ else if (N >= 17 && N <= 20) {
833
+ s.add(0x03);
834
+ s.add(checkBitsHex);
835
+ }
836
+ } else {
837
+ s.add(Opcode.OP_0); // ecdsa schnorr mode; multisig dummy param of 0
838
+ }
839
+
840
+ _.each(signatures, function(signature) {
841
+ $.checkArgument(BufferUtil.isBuffer(signature), 'Signatures must be an array of Buffers');
842
+ // TODO: allow signatures to be an array of Signature objects
843
+ s.add(signature);
844
+ });
845
+ s.add((opts.cachedMultisig || Script.buildMultisigOut(pubkeys, threshold, opts)).toBuffer());
846
+ return s;
847
+ };
848
+
849
+ /**
850
+ * @returns {Script} a new pay to public key hash output for the given
851
+ * address or public key
852
+ * @param {(Address|PublicKey)} to - destination address or public key
853
+ */
854
+ Script.buildPublicKeyHashOut = function(to) {
855
+ $.checkArgument(!_.isUndefined(to));
856
+ $.checkArgument(to instanceof PublicKey || to instanceof Address || _.isString(to));
857
+ if (to instanceof PublicKey) {
858
+ to = to.toAddress();
859
+ } else if (_.isString(to)) {
860
+ to = new Address(to);
861
+ }
862
+ let opcode = Opcode.OP_CHECKSIG;
863
+ if (to.hashBuffer.length == 33){
864
+ opcode = Opcode.OP_CODESEPARATOR;
865
+ }
866
+ var s = new Script();
867
+ s
868
+ .add(to.hashBuffer)
869
+ .add(opcode);
870
+ s._network = to.network;
871
+ return s;
872
+ };
873
+
874
+ /**
875
+ * @returns {Script} a new pay to public key output for the given
876
+ * public key
877
+ */
878
+ Script.buildPublicKeyOut = function(pubkey) {
879
+ $.checkArgument(pubkey instanceof PublicKey);
880
+ var s = new Script();
881
+ s.add(pubkey.toBuffer())
882
+ .add(Opcode.OP_CHECKSIG);
883
+ return s;
884
+ };
885
+
886
+ /**
887
+ * @returns {Script} a new OP_RETURN script with data
888
+ * @param {(string|Buffer)} data - the data to embed in the output
889
+ * @param {(string)} encoding - the type of encoding of the string
890
+ */
891
+ Script.buildDataOut = function(data, encoding) {
892
+ $.checkArgument(_.isUndefined(data) || _.isString(data) || BufferUtil.isBuffer(data));
893
+ if (_.isString(data)) {
894
+ data = Buffer.from(data, encoding);
895
+ }
896
+ var s = new Script();
897
+ s.add(Opcode.OP_RETURN);
898
+ if (!_.isUndefined(data)) {
899
+ s.add(data);
900
+ }
901
+ return s;
902
+ };
903
+
904
+ /**
905
+ * @param {Script|Address} script - the redeemScript for the new p2sh output.
906
+ * It can also be a p2sh address
907
+ * @returns {Script} new pay to script hash script for given script
908
+ */
909
+ Script.buildScriptHashOut = function(script) {
910
+ $.checkArgument(script instanceof Script ||
911
+ (script instanceof Address && script.isPayToScriptHash()));
912
+ var s = new Script();
913
+ //s.add(Opcode.OP_HASH160)
914
+ s.add(Opcode.OP_BLAKE2B)
915
+ .add(script instanceof Address ? script.hashBuffer : Hash.sha256ripemd160(script.toBuffer()))
916
+ .add(Opcode.OP_EQUAL);
917
+
918
+ s._network = script._network || script.network;
919
+ return s;
920
+ };
921
+
922
+ /**
923
+ * Builds a scriptSig (a script for an input) that signs a public key output script.
924
+ *
925
+ * @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding
926
+ * @param {number=} sigtype - the type of the signature (defaults to SIGHASH_ALL)
927
+ */
928
+ Script.buildPublicKeyIn = function(signature, sigtype) {
929
+ $.checkArgument(signature instanceof Signature || BufferUtil.isBuffer(signature));
930
+ $.checkArgument(_.isUndefined(sigtype) || _.isNumber(sigtype));
931
+ if (signature instanceof Signature) {
932
+ signature = signature.toBuffer();
933
+ }
934
+ var script = new Script()
935
+ .add(BufferUtil.concat([
936
+ signature,
937
+ BufferUtil.integerAsSingleByteBuffer(sigtype || Signature.SIGHASH_ALL)
938
+ ]));
939
+ return script;
940
+ };
941
+
942
+ /**
943
+ * Builds a scriptSig (a script for an input) that signs a public key hash
944
+ * output script.
945
+ *
946
+ * @param {Buffer|string|PublicKey} publicKey
947
+ * @param {Signature|Buffer} signature - a Signature object, or the signature in DER canonical encoding
948
+ * @param {number=} sigtype - the type of the signature (defaults to SIGHASH_ALL)
949
+ */
950
+ Script.buildPublicKeyHashIn = function(publicKey, signature, sigtype) {
951
+ $.checkArgument(signature instanceof Signature || BufferUtil.isBuffer(signature));
952
+ $.checkArgument(_.isUndefined(sigtype) || _.isNumber(sigtype));
953
+ if (signature instanceof Signature) {
954
+ signature = signature.toBuffer();
955
+ }
956
+ var script = new Script()
957
+ .add(BufferUtil.concat([
958
+ signature,
959
+ BufferUtil.integerAsSingleByteBuffer(sigtype || Signature.SIGHASH_ALL)
960
+ ]))
961
+ .add(new PublicKey(publicKey).toBuffer().slice(1));
962
+ return script;
963
+ };
964
+
965
+ /**
966
+ * @returns {Script} an empty script
967
+ */
968
+ Script.empty = function() {
969
+ return new Script();
970
+ };
971
+
972
+ /**
973
+ * @returns {Script} a new pay to script hash script that pays to this script
974
+ */
975
+ Script.prototype.toScriptHashOut = function() {
976
+ return Script.buildScriptHashOut(this);
977
+ };
978
+
979
+ /**
980
+ * @return {Script} an output script built from the address
981
+ */
982
+ Script.fromAddress = function(address) {
983
+ address = Address(address);
984
+ if (address.isPayToScriptHash()) {
985
+ return Script.buildScriptHashOut(address);
986
+ } else if (address.isPayToPublicKey()) {
987
+ return Script.buildPublicKeyHashOut(address);
988
+ }
989
+ throw new errors.Script.UnrecognizedAddress(address);
990
+ };
991
+
992
+ /**
993
+ * Will return the associated address information object
994
+ * @return {Address|boolean}
995
+ */
996
+ Script.prototype.getAddressInfo = function(opts) {
997
+ if (this._isInput) {
998
+ return this._getInputAddressInfo();
999
+ } else if (this._isOutput) {
1000
+ return this._getOutputAddressInfo();
1001
+ } else {
1002
+ var info = this._getOutputAddressInfo();
1003
+ if (!info) {
1004
+ return this._getInputAddressInfo();
1005
+ }
1006
+ return info;
1007
+ }
1008
+ };
1009
+
1010
+ /**
1011
+ * Will return the associated output scriptPubKey address information object
1012
+ * @return {Address|boolean}
1013
+ * @private
1014
+ */
1015
+ Script.prototype._getOutputAddressInfo = function() {
1016
+ var info = {};
1017
+ if (this.isScriptHashOut()) {
1018
+ info.hashBuffer = this.getData();
1019
+ info.type = Address.PayToScriptHash;
1020
+ } else if (this.isPublicKeyHashOut()) {
1021
+ info.hashBuffer = this.getData();
1022
+ info.type = Address.PayToPublicKey;
1023
+ } else {
1024
+ return false;
1025
+ }
1026
+ return info;
1027
+ };
1028
+
1029
+ /**
1030
+ * Will return the associated input scriptSig address information object
1031
+ * @return {Address|boolean}
1032
+ * @private
1033
+ */
1034
+ Script.prototype._getInputAddressInfo = function() {
1035
+ var info = {};
1036
+ if (this.isPublicKeyHashIn()) {
1037
+ // hash the publickey found in the scriptSig
1038
+ debugger
1039
+ info.hashBuffer = Hash.sha256ripemd160(this.chunks[1].buf);
1040
+ info.type = Address.PayToPublicKey;
1041
+ } else if (this.isScriptHashIn()) {
1042
+ // hash the redeemscript found at the end of the scriptSig
1043
+ info.hashBuffer = Hash.sha256ripemd160(this.chunks[this.chunks.length - 1].buf);
1044
+ info.type = Address.PayToScriptHash;
1045
+ } else {
1046
+ return false;
1047
+ }
1048
+ return info;
1049
+ };
1050
+
1051
+ /**
1052
+ * @param {Network=} network
1053
+ * @return {Address|boolean} the associated address for this script if possible, or false
1054
+ */
1055
+ Script.prototype.toAddress = function(network) {
1056
+ var info = this.getAddressInfo();
1057
+ if (!info) {
1058
+ return false;
1059
+ }
1060
+ info.network = Networks.get(network) || this._network || Networks.defaultNetwork;
1061
+ return new Address(info);
1062
+ };
1063
+
1064
+ /**
1065
+ * Analogous to bitcoind's FindAndDelete. Find and delete equivalent chunks,
1066
+ * typically used with push data chunks. Note that this will find and delete
1067
+ * not just the same data, but the same data with the same push data op as
1068
+ * produced by default. i.e., if a pushdata in a tx does not use the minimal
1069
+ * pushdata op, then when you try to remove the data it is pushing, it will not
1070
+ * be removed, because they do not use the same pushdata op.
1071
+ */
1072
+ Script.prototype.findAndDelete = function(script) {
1073
+ var buf = script.toBuffer();
1074
+ var hex = buf.toString('hex');
1075
+ for (var i = 0; i < this.chunks.length; i++) {
1076
+ var script2 = Script({
1077
+ chunks: [this.chunks[i]]
1078
+ });
1079
+ var buf2 = script2.toBuffer();
1080
+ var hex2 = buf2.toString('hex');
1081
+ if (hex === hex2) {
1082
+ this.chunks.splice(i, 1);
1083
+ }
1084
+ }
1085
+ return this;
1086
+ };
1087
+
1088
+ /**
1089
+ * Comes from bitcoind's script interpreter CheckMinimalPush function
1090
+ * @returns {boolean} if the chunk {i} is the smallest way to push that particular data.
1091
+ */
1092
+ Script.prototype.checkMinimalPush = function(i) {
1093
+ var chunk = this. chunks[i];
1094
+ var buf = chunk.buf;
1095
+ var opcodenum = chunk.opcodenum;
1096
+ if (!buf) {
1097
+ return true;
1098
+ }
1099
+ if (buf.length === 0) {
1100
+ // Could have used OP_0.
1101
+ return opcodenum === Opcode.OP_0;
1102
+ } else if (buf.length === 1 && buf[0] >= 1 && buf[0] <= 16) {
1103
+ // Could have used OP_1 .. OP_16.
1104
+ // return opcodenum === Opcode.OP_1 + (buf[0] - 1);
1105
+ return false;
1106
+ } else if (buf.length === 1 && buf[0] === 0x81) {
1107
+ // Could have used OP_1NEGATE
1108
+ return false;
1109
+ } else if (buf.length <= 75) {
1110
+ // Could have used a direct push (opcode indicating number of bytes pushed + those bytes).
1111
+ return opcodenum === buf.length;
1112
+ } else if (buf.length <= 255) {
1113
+ // Could have used OP_PUSHDATA.
1114
+ return opcodenum === Opcode.OP_PUSHDATA1;
1115
+ } else if (buf.length <= 65535) {
1116
+ // Could have used OP_PUSHDATA2.
1117
+ return opcodenum === Opcode.OP_PUSHDATA2;
1118
+ }
1119
+ return true;
1120
+ };
1121
+
1122
+ /**
1123
+ * Comes from bitcoind's script DecodeOP_N function
1124
+ * @param {number} opcode
1125
+ * @returns {number} numeric value in range of 0 to 16
1126
+ */
1127
+ Script.prototype._decodeOP_N = function(opcode) {
1128
+ if (opcode === Opcode.OP_0) {
1129
+ return 0;
1130
+ } else if (opcode >= Opcode.OP_1 && opcode <= Opcode.OP_16) {
1131
+ return opcode - (Opcode.OP_1 - 1);
1132
+ } else {
1133
+ throw new Error('Invalid opcode: ' + JSON.stringify(opcode));
1134
+ }
1135
+ };
1136
+
1137
+ /**
1138
+ * Comes from bitcoind's script GetSigOpCount(boolean) function
1139
+ * @param {boolean} use current (true) or pre-version-0.6 (false) logic
1140
+ * @returns {number} number of signature operations required by this script
1141
+ */
1142
+ Script.prototype.getSignatureOperationsCount = function(accurate) {
1143
+ accurate = (_.isUndefined(accurate) ? true : accurate);
1144
+ var self = this;
1145
+ var n = 0;
1146
+ var lastOpcode = Opcode.OP_INVALIDOPCODE;
1147
+ _.each(self.chunks, function getChunk(chunk) {
1148
+ var opcode = chunk.opcodenum;
1149
+ if (opcode == Opcode.OP_CHECKSIG || opcode == Opcode.OP_CHECKSIGVERIFY) {
1150
+ n++;
1151
+ } else if (opcode == Opcode.OP_CHECKMULTISIG || opcode == Opcode.OP_CHECKMULTISIGVERIFY) {
1152
+ if (accurate && lastOpcode >= Opcode.OP_1 && lastOpcode <= Opcode.OP_16) {
1153
+ n += self._decodeOP_N(lastOpcode);
1154
+ } else {
1155
+ n += 20;
1156
+ }
1157
+ }
1158
+ lastOpcode = opcode;
1159
+ });
1160
+ return n;
1161
+ };
1162
+
1163
+ module.exports = Script;