smartledger-bsv 3.0.0

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