bsv2 2.0.24 → 2.1.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 (113) hide show
  1. package/{.babelrc.test → .babel.test} +1 -1
  2. package/.husky/pre-commit +1 -1
  3. package/.mocharc.yml +1 -1
  4. package/.travis.yml +1 -1
  5. package/babel.config.cjs +44 -0
  6. package/benchmark.js +10 -18
  7. package/bin/bsv.js +1 -1
  8. package/bin/network.js +2 -1
  9. package/dist/bsv2.mjs +36061 -4
  10. package/dist/bsv2.umd.js +28 -0
  11. package/doc/assets/anchor.js +350 -0
  12. package/doc/assets/bass-addons.css +12 -0
  13. package/doc/assets/bass.css +544 -0
  14. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  15. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  16. package/doc/assets/fonts/LICENSE.txt +93 -0
  17. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  18. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  19. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  20. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  21. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  22. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  23. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  24. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  25. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  26. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  27. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  28. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  29. package/doc/assets/fonts/source-code-pro.css +23 -0
  30. package/doc/assets/github.css +123 -0
  31. package/doc/assets/site.js +168 -0
  32. package/doc/assets/split.css +15 -0
  33. package/doc/assets/split.js +782 -0
  34. package/doc/assets/style.css +147 -0
  35. package/doc/index.html +9461 -0
  36. package/entry.js +2 -3
  37. package/eslint.config.mjs +8 -0
  38. package/index.js +1 -5
  39. package/{karma.conf.js → karma.conf.ts} +2 -3
  40. package/lib/ach.js +5 -5
  41. package/lib/address.js +11 -9
  42. package/lib/aescbc.js +3 -3
  43. package/lib/bip-39.js +12 -11
  44. package/lib/block.js +2 -2
  45. package/lib/bn.js +12 -17
  46. package/lib/bsm.js +1 -1
  47. package/lib/config.js +1 -1
  48. package/lib/constants.js +1 -1
  49. package/lib/ecdsa.js +24 -23
  50. package/lib/interp.js +18 -14
  51. package/lib/merkle.js +1 -1
  52. package/lib/op-code.js +137 -135
  53. package/lib/priv-key.js +1 -1
  54. package/lib/script.js +44 -42
  55. package/lib/sig.js +12 -12
  56. package/lib/struct.js +9 -6
  57. package/lib/tx.js +6 -6
  58. package/load-babel.js +3 -1
  59. package/mtest/test.html +6 -4
  60. package/mtest/test.js +1 -1
  61. package/out/Sig.html +541 -0
  62. package/out/bip-32.js.html +523 -0
  63. package/out/bip-39.js.html +336 -0
  64. package/out/block.js.html +183 -0
  65. package/out/bn.js.html +512 -0
  66. package/out/ecdsa.js.html +546 -0
  67. package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
  68. package/out/fonts/OpenSans-Bold-webfont.svg +1830 -0
  69. package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
  70. package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  71. package/out/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
  72. package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  73. package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
  74. package/out/fonts/OpenSans-Italic-webfont.svg +1830 -0
  75. package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
  76. package/out/fonts/OpenSans-Light-webfont.eot +0 -0
  77. package/out/fonts/OpenSans-Light-webfont.svg +1831 -0
  78. package/out/fonts/OpenSans-Light-webfont.woff +0 -0
  79. package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  80. package/out/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
  81. package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  82. package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
  83. package/out/fonts/OpenSans-Regular-webfont.svg +1831 -0
  84. package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
  85. package/out/global.html +220 -0
  86. package/out/index.html +128 -0
  87. package/out/interp.js.html +2064 -0
  88. package/out/merkle.js.html +143 -0
  89. package/out/msg.js.html +235 -0
  90. package/out/priv-key.js.html +208 -0
  91. package/out/pub-key.js.html +281 -0
  92. package/out/script.js.html +876 -0
  93. package/out/scripts/linenumber.js +25 -0
  94. package/out/scripts/prettify/Apache-License-2.0.txt +202 -0
  95. package/out/scripts/prettify/lang-css.js +2 -0
  96. package/out/scripts/prettify/prettify.js +28 -0
  97. package/out/sig-operations.js.html +171 -0
  98. package/out/sig.js.html +450 -0
  99. package/out/struct.js.html +440 -0
  100. package/out/styles/jsdoc-default.css +358 -0
  101. package/out/styles/prettify-jsdoc.css +111 -0
  102. package/out/styles/prettify-tomorrow.css +132 -0
  103. package/out/tx-builder.js.html +644 -0
  104. package/out/tx-in.js.html +223 -0
  105. package/out/tx-verifier.js.html +260 -0
  106. package/out/tx.js.html +481 -0
  107. package/package.json +34 -39
  108. package/vite.config.js +36 -0
  109. package/dist/bsv2.bundle.js +0 -8
  110. package/dist/bsv2.cjs.js +0 -2
  111. package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
  112. package/dist/bsv2.module.js +0 -2
  113. /package/{webpack.config.js → mtest/webpack.config.js} +0 -0
@@ -0,0 +1,876 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>JSDoc: Source: script.js</title>
6
+
7
+ <script src="scripts/prettify/prettify.js"> </script>
8
+ <script src="scripts/prettify/lang-css.js"> </script>
9
+ <!--[if lt IE 9]>
10
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11
+ <![endif]-->
12
+ <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14
+ </head>
15
+
16
+ <body>
17
+
18
+ <div id="main">
19
+
20
+ <h1 class="page-title">Source: script.js</h1>
21
+
22
+
23
+
24
+
25
+
26
+
27
+ <section>
28
+ <article>
29
+ <pre class="prettyprint source linenums"><code>/*
30
+ * Script
31
+ * ======
32
+ *
33
+ * Script is the scripting language built into bitcoin. The Script class lets
34
+ * you create an instance of a script, e.g. for a scriptSig or a scriptPubKey.
35
+ * It understands both the binary format, as well as two different string
36
+ * formats. The default string format, to/fromString, is a custom format only
37
+ * used by Yours Bitcoin because it is isomorphic to the binary format (or as
38
+ * isomorphic as it can be ... since OP_0 and OP_FALSE have the same byte
39
+ * value, and so do OP_1 and OP_TRUE). The bitcoind string format is also
40
+ * support, but that format is not isomorphic (i.e., if you pull in a string
41
+ * and then write it again, you are likely to get back a different string, even
42
+ * if you don't use OP_0, OP_FALSE, OP_1, or OP_TRUE).
43
+ */
44
+ 'use strict'
45
+
46
+ import { Bn } from './bn'
47
+ import { Br } from './br'
48
+ import { Bw } from './bw'
49
+ import { cmp } from './cmp'
50
+ import { OpCode } from './op-code'
51
+ import { PubKey } from './pub-key'
52
+ import { Sig } from './sig'
53
+ import { Struct } from './struct'
54
+
55
+ class Script extends Struct {
56
+ constructor (chunks = []) {
57
+ super({ chunks })
58
+ }
59
+
60
+ fromJSON (json) {
61
+ return this.fromString(json)
62
+ }
63
+
64
+ toJSON () {
65
+ return this.toString()
66
+ }
67
+
68
+ fromBuffer (buf) {
69
+ this.chunks = []
70
+
71
+ const br = new Br(buf)
72
+ while (!br.eof()) {
73
+ const opCodeNum = br.readUInt8()
74
+
75
+ let len = 0
76
+ let buf = Buffer.from([])
77
+ if (opCodeNum > 0 &amp;&amp; opCodeNum &lt; OpCode.OP_PUSHDATA1) {
78
+ len = opCodeNum
79
+ this.chunks.push({
80
+ buf: br.read(len),
81
+ len,
82
+ opCodeNum
83
+ })
84
+ } else if (opCodeNum === OpCode.OP_PUSHDATA1) {
85
+ try {
86
+ len = br.readUInt8()
87
+ buf = br.read(len)
88
+ } catch (err) {
89
+ br.read()
90
+ }
91
+ this.chunks.push({
92
+ buf,
93
+ len,
94
+ opCodeNum
95
+ })
96
+ } else if (opCodeNum === OpCode.OP_PUSHDATA2) {
97
+ try {
98
+ len = br.readUInt16LE()
99
+ buf = br.read(len)
100
+ } catch (err) {
101
+ br.read()
102
+ }
103
+ this.chunks.push({
104
+ buf,
105
+ len,
106
+ opCodeNum
107
+ })
108
+ } else if (opCodeNum === OpCode.OP_PUSHDATA4) {
109
+ try {
110
+ len = br.readUInt32LE()
111
+ buf = br.read(len)
112
+ } catch (err) {
113
+ br.read()
114
+ }
115
+ this.chunks.push({
116
+ buf,
117
+ len,
118
+ opCodeNum
119
+ })
120
+ } else {
121
+ this.chunks.push({
122
+ opCodeNum
123
+ })
124
+ }
125
+ }
126
+
127
+ return this
128
+ }
129
+
130
+ toBuffer () {
131
+ const bw = new Bw()
132
+
133
+ for (let i = 0; i &lt; this.chunks.length; i++) {
134
+ const chunk = this.chunks[i]
135
+ const opCodeNum = chunk.opCodeNum
136
+ bw.writeUInt8(opCodeNum)
137
+ if (chunk.buf) {
138
+ if (opCodeNum &lt; OpCode.OP_PUSHDATA1) {
139
+ bw.write(chunk.buf)
140
+ } else if (opCodeNum === OpCode.OP_PUSHDATA1) {
141
+ bw.writeUInt8(chunk.len)
142
+ bw.write(chunk.buf)
143
+ } else if (opCodeNum === OpCode.OP_PUSHDATA2) {
144
+ bw.writeUInt16LE(chunk.len)
145
+ bw.write(chunk.buf)
146
+ } else if (opCodeNum === OpCode.OP_PUSHDATA4) {
147
+ bw.writeUInt32LE(chunk.len)
148
+ bw.write(chunk.buf)
149
+ }
150
+ }
151
+ }
152
+
153
+ return bw.toBuffer()
154
+ }
155
+
156
+ fromString (str) {
157
+ this.chunks = []
158
+ if (str === '' || str === undefined) {
159
+ return this
160
+ }
161
+
162
+ const tokens = str.split(' ')
163
+ let i = 0
164
+ while (i &lt; tokens.length) {
165
+ const token = tokens[i]
166
+ let opCodeNum
167
+ try {
168
+ const opCode = new OpCode().fromString(token)
169
+ opCodeNum = opCode.toNumber()
170
+ } catch (err) {}
171
+
172
+ if (opCodeNum === undefined) {
173
+ opCodeNum = parseInt(token, 10)
174
+ if (opCodeNum > 0 &amp;&amp; opCodeNum &lt; OpCode.OP_PUSHDATA1) {
175
+ this.chunks.push({
176
+ buf: Buffer.from(tokens[i + 1].slice(2), 'hex'),
177
+ len: opCodeNum,
178
+ opCodeNum
179
+ })
180
+ i = i + 2
181
+ } else if (opCodeNum === 0) {
182
+ this.chunks.push({
183
+ opCodeNum: 0
184
+ })
185
+ i = i + 1
186
+ } else {
187
+ throw new Error('Invalid script')
188
+ }
189
+ } else if (
190
+ opCodeNum === OpCode.OP_PUSHDATA1 ||
191
+ opCodeNum === OpCode.OP_PUSHDATA2 ||
192
+ opCodeNum === OpCode.OP_PUSHDATA4
193
+ ) {
194
+ if (tokens[i + 2].slice(0, 2) !== '0x') {
195
+ throw new Error('Pushdata data must start with 0x')
196
+ }
197
+ this.chunks.push({
198
+ buf: Buffer.from(tokens[i + 2].slice(2), 'hex'),
199
+ len: parseInt(tokens[i + 1], 10),
200
+ opCodeNum
201
+ })
202
+ i = i + 3
203
+ } else {
204
+ this.chunks.push({
205
+ opCodeNum
206
+ })
207
+ i = i + 1
208
+ }
209
+ }
210
+ return this
211
+ }
212
+
213
+ toString (opts = { noOp: false }) {
214
+ let str = ''
215
+
216
+ for (let i = 0; i &lt; this.chunks.length; i++) {
217
+ const chunk = this.chunks[i]
218
+ const opCodeNum = chunk.opCodeNum?.num ? chunk.opCodeNum.num : chunk.opCodeNum
219
+ if (!chunk.buf) {
220
+ if (OpCode.str[opCodeNum] !== undefined || OpCode.noopstr[opCodeNum] !== undefined) {
221
+ str = opts.noOp ? str + ' ' + new OpCode(opCodeNum).toNoOpString() : str + ' ' + new OpCode(opCodeNum).toString()
222
+ } else {
223
+ str = str + ' ' + '0x' + opCodeNum.toString(16)
224
+ }
225
+ } else {
226
+ if (
227
+ opCodeNum === OpCode.OP_PUSHDATA1 ||
228
+ opCodeNum === OpCode.OP_PUSHDATA2 ||
229
+ opCodeNum === OpCode.OP_PUSHDATA4
230
+ ) {
231
+ str = opts.noOp ? str + ' ' + new OpCode(opCodeNum).toNoOpString() : str + ' ' + new OpCode(opCodeNum).toString()
232
+ }
233
+ str = str + ' ' + chunk.len
234
+ str = str + ' ' + '0x' + chunk.buf.toString('hex')
235
+ }
236
+ }
237
+
238
+ return str.substr(1)
239
+ }
240
+
241
+ /**
242
+ * Input the script from the script string format used in bitcoind data tests
243
+ */
244
+ fromBitcoindString (str) {
245
+ const bw = new Bw()
246
+ const tokens = str.split(' ')
247
+ let i
248
+ for (i = 0; i &lt; tokens.length; i++) {
249
+ const token = tokens[i]
250
+ if (token === '') {
251
+ continue
252
+ }
253
+ if (token[0] === '0' &amp;&amp; token[1] === 'x') {
254
+ const hex = token.slice(2)
255
+ bw.write(Buffer.from(hex, 'hex'))
256
+ } else if (token[0] === "'") {
257
+ const tstr = token.slice(1, token.length - 1)
258
+ const cbuf = Buffer.from(tstr)
259
+ const tbuf = new Script().writeBuffer(cbuf).toBuffer()
260
+ bw.write(tbuf)
261
+ } else if (OpCode['OP_' + token] !== undefined) {
262
+ const opstr = 'OP_' + token
263
+ const opCodeNum = OpCode[opstr]
264
+ bw.writeUInt8(opCodeNum)
265
+ } else if (typeof OpCode[token] === 'number') {
266
+ const opstr = token
267
+ const opCodeNum = OpCode[opstr]
268
+ bw.writeUInt8(opCodeNum)
269
+ } else if (!isNaN(parseInt(token, 10))) {
270
+ const bn = new Bn(token)
271
+ const script = new Script().writeBn(bn)
272
+ const tbuf = script.toBuffer()
273
+ bw.write(tbuf)
274
+ } else {
275
+ throw new Error('Could not determine type of script value')
276
+ }
277
+ }
278
+ const buf = bw.toBuffer()
279
+ return this.fromBuffer(buf)
280
+ }
281
+
282
+ static fromBitcoindString (str) {
283
+ return new this().fromBitcoindString(str)
284
+ }
285
+
286
+ /**
287
+ * Output the script to the script string format used in bitcoind data tests.
288
+ */
289
+ toBitcoindString () {
290
+ let str = ''
291
+ for (let i = 0; i &lt; this.chunks.length; i++) {
292
+ const chunk = this.chunks[i]
293
+ if (chunk.buf) {
294
+ const buf = new Script([chunk]).toBuffer()
295
+ const hex = buf.toString('hex')
296
+ str = str + ' ' + '0x' + hex
297
+ } else if (OpCode.str[chunk.opCodeNum] !== undefined) {
298
+ const ostr = new OpCode(chunk.opCodeNum).toString()
299
+ str = str + ' ' + ostr.slice(3) // remove OP_
300
+ } else {
301
+ str = str + ' ' + '0x' + chunk.opCodeNum.toString(16)
302
+ }
303
+ }
304
+ return str.substr(1)
305
+ }
306
+
307
+ /**
308
+ * Input the script from the script string format used in bitcoind data tests
309
+ */
310
+ fromAsmString (str) {
311
+ this.chunks = []
312
+
313
+ const tokens = str.split(' ')
314
+ let i = 0
315
+ while (i &lt; tokens.length) {
316
+ const token = tokens[i]
317
+ let opCode, opCodeNum
318
+ try {
319
+ opCode = OpCode.fromString(token)
320
+ opCodeNum = opCode.toNumber()
321
+ } catch (err) {
322
+ opCode = undefined
323
+ opCodeNum = undefined
324
+ }
325
+
326
+ // we start with two special cases, 0 and -1, which are handled specially in
327
+ // toASM. see _chunkToString.
328
+ if (token === '0') {
329
+ opCodeNum = 0
330
+ this.chunks.push({
331
+ opCodeNum
332
+ })
333
+ i = i + 1
334
+ } else if (token === '-1') {
335
+ opCodeNum = OpCode.OP_1NEGATE
336
+ this.chunks.push({
337
+ opCodeNum
338
+ })
339
+ i = i + 1
340
+ } else if (opCode === undefined) {
341
+ const hex = tokens[i]
342
+ const buf = Buffer.from(hex, 'hex')
343
+ if (buf.toString('hex') !== hex) {
344
+ throw new Error('invalid hex string in script')
345
+ }
346
+ const len = buf.length
347
+ if (len >= 0 &amp;&amp; len &lt; OpCode.OP_PUSHDATA1) {
348
+ opCodeNum = len
349
+ } else if (len &lt; Math.pow(2, 8)) {
350
+ opCodeNum = OpCode.OP_PUSHDATA1
351
+ } else if (len &lt; Math.pow(2, 16)) {
352
+ opCodeNum = OpCode.OP_PUSHDATA2
353
+ } else if (len &lt; Math.pow(2, 32)) {
354
+ opCodeNum = OpCode.OP_PUSHDATA4
355
+ }
356
+ this.chunks.push({
357
+ buf,
358
+ len: buf.length,
359
+ opCodeNum
360
+ })
361
+ i = i + 1
362
+ } else {
363
+ this.chunks.push({
364
+ opCodeNum
365
+ })
366
+ i = i + 1
367
+ }
368
+ }
369
+ return this
370
+ }
371
+
372
+ static fromAsmString (str) {
373
+ return new this().fromAsmString(str)
374
+ }
375
+
376
+ /**
377
+ * Output the script to the script string format used in bitcoind data tests.
378
+ */
379
+ toAsmString (opts = { noOp: false }) {
380
+ let str = ''
381
+ for (let i = 0; i &lt; this.chunks.length; i++) {
382
+ const chunk = this.chunks[i]
383
+ str += this._chunkToString(chunk, opts)
384
+ }
385
+
386
+ return str.substr(1)
387
+ }
388
+
389
+ _chunkToString (chunk, opts = { noOp: false }) {
390
+ const opCodeNum = chunk.opCodeNum
391
+ let str = ''
392
+ if (!chunk.buf) {
393
+ // no data chunk
394
+ if (typeof OpCode.str[opCodeNum] !== 'undefined') {
395
+ // A few cases where the opcode name differs from reverseMap
396
+ // aside from 1 to 16 data pushes.
397
+ if (opCodeNum === 0) {
398
+ // OP_0 -> 0
399
+ str = str + ' 0'
400
+ } else if (opCodeNum === 79) {
401
+ // OP_1NEGATE -> 1
402
+ str = str + ' -1'
403
+ } else {
404
+ str = opts.noOp ? str + ' ' + new OpCode(opCodeNum).toNoOpString() : str + ' ' + new OpCode(opCodeNum).toString()
405
+ }
406
+ } else {
407
+ let numstr = opCodeNum.toString(16)
408
+ if (numstr.length % 2 !== 0) {
409
+ numstr = '0' + numstr
410
+ }
411
+ str = str + ' ' + numstr
412
+ }
413
+ } else {
414
+ // data chunk
415
+ if (chunk.len > 0) {
416
+ str = str + ' ' + chunk.buf.toString('hex')
417
+ }
418
+ }
419
+ return str
420
+ }
421
+
422
+ fromOpReturnData (dataBuf) {
423
+ this.writeOpCode(OpCode.OP_RETURN)
424
+ this.writeBuffer(dataBuf)
425
+ return this
426
+ }
427
+
428
+ static fromOpReturnData (dataBuf) {
429
+ return new this().fromOpReturnData(dataBuf)
430
+ }
431
+
432
+ fromSafeData (dataBuf) {
433
+ this.writeOpCode(OpCode.OP_FALSE)
434
+ this.writeOpCode(OpCode.OP_RETURN)
435
+ this.writeBuffer(dataBuf)
436
+ return this
437
+ }
438
+
439
+ static fromSafeData (dataBuf) {
440
+ return new this().fromSafeData(dataBuf)
441
+ }
442
+
443
+ fromSafeDataArray (dataBufs) {
444
+ this.writeOpCode(OpCode.OP_FALSE)
445
+ this.writeOpCode(OpCode.OP_RETURN)
446
+ for (const i in dataBufs) {
447
+ const dataBuf = dataBufs[i]
448
+ this.writeBuffer(dataBuf)
449
+ }
450
+ return this
451
+ }
452
+
453
+ static fromSafeDataArray (dataBufs) {
454
+ return new this().fromSafeDataArray(dataBufs)
455
+ }
456
+
457
+ getData () {
458
+ if (this.isSafeDataOut()) {
459
+ const chunks = this.chunks.slice(2)
460
+ const buffers = chunks.map(chunk => chunk.buf)
461
+ return buffers
462
+ }
463
+ if (this.isOpReturn()) {
464
+ const chunks = this.chunks.slice(1)
465
+ const buffers = chunks.map(chunk => chunk.buf)
466
+ return buffers
467
+ }
468
+ throw new Error('Unrecognized script type to get data from')
469
+ }
470
+
471
+ /**
472
+ * Turn script into a standard pubKeyHash output script
473
+ */
474
+ fromPubKeyHash (hashBuf) {
475
+ if (hashBuf.length !== 20) {
476
+ throw new Error('hashBuf must be a 20 byte buffer')
477
+ }
478
+ this.writeOpCode(OpCode.OP_DUP)
479
+ this.writeOpCode(OpCode.OP_HASH160)
480
+ this.writeBuffer(hashBuf)
481
+ this.writeOpCode(OpCode.OP_EQUALVERIFY)
482
+ this.writeOpCode(OpCode.OP_CHECKSIG)
483
+ return this
484
+ }
485
+
486
+ static fromPubKeyHash (hashBuf) {
487
+ return new this().fromPubKeyHash(hashBuf)
488
+ }
489
+
490
+ static sortPubKeys (pubKeys) {
491
+ return pubKeys.slice().sort((pubKey1, pubKey2) => {
492
+ const buf1 = pubKey1.toBuffer()
493
+ const buf2 = pubKey2.toBuffer()
494
+ const len = Math.max(buf1.length, buf2.length)
495
+ for (let i = 0; i &lt;= len; i++) {
496
+ if (buf1[i] === undefined) {
497
+ return -1 // shorter strings come first
498
+ }
499
+ if (buf2[i] === undefined) {
500
+ return 1
501
+ }
502
+ if (buf1[i] &lt; buf2[i]) {
503
+ return -1
504
+ }
505
+ if (buf1[i] > buf2[i]) {
506
+ return 1
507
+ } else {
508
+ continue
509
+ }
510
+ }
511
+ })
512
+ }
513
+
514
+ /**
515
+ * Generate a multisig output script from a list of public keys. sort
516
+ * defaults to true. If sort is true, the pubKeys are sorted
517
+ * lexicographically.
518
+ */
519
+ fromPubKeys (m, pubKeys, sort = true) {
520
+ if (typeof m !== 'number') {
521
+ throw new Error('m must be a number')
522
+ }
523
+ if (sort === true) {
524
+ pubKeys = Script.sortPubKeys(pubKeys)
525
+ }
526
+ this.writeOpCode(m + OpCode.OP_1 - 1)
527
+ for (const i in pubKeys) {
528
+ this.writeBuffer(pubKeys[i].toBuffer())
529
+ }
530
+ this.writeOpCode(pubKeys.length + OpCode.OP_1 - 1)
531
+ this.writeOpCode(OpCode.OP_CHECKMULTISIG)
532
+ return this
533
+ }
534
+
535
+ static fromPubKeys (m, pubKeys, sort) {
536
+ return new this().fromPubKeys(m, pubKeys, sort)
537
+ }
538
+
539
+ removeCodeseparators () {
540
+ const chunks = []
541
+ for (let i = 0; i &lt; this.chunks.length; i++) {
542
+ if (this.chunks[i].opCodeNum !== OpCode.OP_CODESEPARATOR) {
543
+ chunks.push(this.chunks[i])
544
+ }
545
+ }
546
+ this.chunks = chunks
547
+ return this
548
+ }
549
+
550
+ isPushOnly () {
551
+ for (let i = 0; i &lt; this.chunks.length; i++) {
552
+ const chunk = this.chunks[i]
553
+ const opCodeNum = chunk.opCodeNum
554
+ if (opCodeNum > OpCode.OP_16) {
555
+ return false
556
+ }
557
+ }
558
+ return true
559
+ }
560
+
561
+ isNonSpendable () {
562
+ const startsWithOpFalse = this.chunks[0].opCodeNum === OpCode.OP_FALSE
563
+ const andThenReturns = this.chunks[1] &amp;&amp; this.chunks[1].opCodeNum === OpCode.OP_RETURN
564
+ return !!startsWithOpFalse &amp;&amp; !!andThenReturns
565
+ }
566
+
567
+ isOpReturn () {
568
+ if (
569
+ this.chunks[0].opCodeNum === OpCode.OP_RETURN &amp;&amp;
570
+ this.chunks.filter(chunk => Buffer.isBuffer(chunk.buf)).length === this.chunks.slice(1).length
571
+ ) {
572
+ return true
573
+ } else {
574
+ return false
575
+ }
576
+ }
577
+
578
+ isSafeDataOut () {
579
+ if (this.chunks.length &lt; 2) {
580
+ return false
581
+ }
582
+ if (this.chunks[0].opCodeNum !== OpCode.OP_FALSE) {
583
+ return false
584
+ }
585
+ const chunks = this.chunks.slice(1)
586
+ const script2 = new Script(chunks)
587
+ return script2.isOpReturn()
588
+ }
589
+
590
+ isPubKeyHashOut () {
591
+ if (
592
+ this.chunks[0] &amp;&amp;
593
+ this.chunks[0].opCodeNum === OpCode.OP_DUP &amp;&amp;
594
+ this.chunks[1] &amp;&amp;
595
+ this.chunks[1].opCodeNum === OpCode.OP_HASH160 &amp;&amp;
596
+ this.chunks[2].buf &amp;&amp;
597
+ this.chunks[3] &amp;&amp;
598
+ this.chunks[3].opCodeNum === OpCode.OP_EQUALVERIFY &amp;&amp;
599
+ this.chunks[4] &amp;&amp;
600
+ this.chunks[4].opCodeNum === OpCode.OP_CHECKSIG
601
+ ) {
602
+ return true
603
+ } else {
604
+ return false
605
+ }
606
+ }
607
+
608
+ /**
609
+ * A pubKeyHash input should consist of two push operations. The first push
610
+ * operation may be OP_0, which means the signature is missing, which is true
611
+ * for some partially signed (and invalid) transactions.
612
+ */
613
+ isPubKeyHashIn () {
614
+ if (
615
+ this.chunks.length === 2 &amp;&amp;
616
+ (this.chunks[0].buf || this.chunks[0].opCodeNum === OpCode.OP_0) &amp;&amp;
617
+ (this.chunks[1].buf || this.chunks[0].opCodeNum === OpCode.OP_0)
618
+ ) {
619
+ return true
620
+ } else {
621
+ return false
622
+ }
623
+ }
624
+
625
+ isScriptHashOut () {
626
+ const buf = this.toBuffer()
627
+ return (
628
+ buf.length === 23 &amp;&amp;
629
+ buf[0] === OpCode.OP_HASH160 &amp;&amp;
630
+ buf[1] === 0x14 &amp;&amp;
631
+ buf[22] === OpCode.OP_EQUAL
632
+ )
633
+ }
634
+
635
+ /**
636
+ * Note that these are frequently indistinguishable from pubKeyHashin
637
+ */
638
+ isScriptHashIn () {
639
+ if (!this.isPushOnly()) {
640
+ return false
641
+ }
642
+ try {
643
+ new Script().fromBuffer(this.chunks[this.chunks.length - 1].buf)
644
+ } catch (err) {
645
+ return false
646
+ }
647
+ return true
648
+ }
649
+
650
+ isMultiSigOut () {
651
+ const m = this.chunks[0].opCodeNum - OpCode.OP_1 + 1
652
+ if (!(m >= 1 &amp;&amp; m &lt;= 16)) {
653
+ return false
654
+ }
655
+ const pubKeychunks = this.chunks.slice(1, this.chunks.length - 2)
656
+ if (
657
+ !pubKeychunks.every(chunk => {
658
+ try {
659
+ const buf = chunk.buf
660
+ const pubKey = new PubKey().fromDer(buf)
661
+ pubKey.validate()
662
+ return true
663
+ } catch (err) {
664
+ return false
665
+ }
666
+ })
667
+ ) {
668
+ return false
669
+ }
670
+ const n = this.chunks[this.chunks.length - 2].opCodeNum - OpCode.OP_1 + 1
671
+ if (!(n >= m &amp;&amp; n &lt;= 16)) {
672
+ return false
673
+ }
674
+ if (this.chunks[1 + n + 1].opCodeNum !== OpCode.OP_CHECKMULTISIG) {
675
+ return false
676
+ }
677
+ return true
678
+ }
679
+
680
+ isMultiSigIn () {
681
+ if (this.chunks[0].opCodeNum !== OpCode.OP_0) {
682
+ return false
683
+ }
684
+ const remaining = this.chunks.slice(1)
685
+ if (remaining.length &lt; 1) {
686
+ return false
687
+ }
688
+ return remaining.every(
689
+ chunk => Buffer.isBuffer(chunk.buf) &amp;&amp; Sig.IsTxDer(chunk.buf)
690
+ )
691
+ }
692
+
693
+ /**
694
+ * Analagous to bitcoind's FindAndDelete Find and deconste equivalent chunks,
695
+ * typically used with push data chunks. Note that this will find and deconste
696
+ * not just the same data, but the same data with the same push data op as
697
+ * produced by default. i.e., if a pushdata in a tx does not use the minimal
698
+ * pushdata op, then when you try to remove the data it is pushing, it will not
699
+ * be removed, because they do not use the same pushdata op.
700
+ */
701
+ findAndDelete (script) {
702
+ const buf = script.toBuffer()
703
+ for (let i = 0; i &lt; this.chunks.length; i++) {
704
+ const script2 = new Script([this.chunks[i]])
705
+ const buf2 = script2.toBuffer()
706
+ if (cmp(buf, buf2)) {
707
+ this.chunks.splice(i, 1)
708
+ }
709
+ }
710
+ return this
711
+ }
712
+
713
+ writeScript (script) {
714
+ this.chunks = this.chunks.concat(script.chunks)
715
+ return this
716
+ }
717
+
718
+ static writeScript (script) {
719
+ return new this().writeScript(script)
720
+ }
721
+
722
+ writeString (str) {
723
+ const script = new Script().fromString(str)
724
+ this.chunks = this.chunks.concat(script.chunks)
725
+ return this
726
+ }
727
+
728
+ static writeString (str) {
729
+ return new this().writeString(str)
730
+ }
731
+
732
+ writeOpCode (opCodeNum) {
733
+ this.chunks.push({ opCodeNum })
734
+ return this
735
+ }
736
+
737
+ static writeOpCode (opCodeNum) {
738
+ return new this().writeOpCode(opCodeNum)
739
+ }
740
+
741
+ setChunkOpCode (i, opCodeNum) {
742
+ this.chunks[i] = { opCodeNum }
743
+ return this
744
+ }
745
+
746
+ // write a big number in the minimal way
747
+ writeBn (bn) {
748
+ if (bn.cmp(0) === OpCode.OP_0) {
749
+ this.chunks.push({
750
+ opCodeNum: OpCode.OP_0
751
+ })
752
+ } else if (bn.cmp(-1) === 0) {
753
+ this.chunks.push({
754
+ opCodeNum: OpCode.OP_1NEGATE
755
+ })
756
+ } else if (bn.cmp(1) >= 0 &amp;&amp; bn.cmp(16) &lt;= 0) {
757
+ // see OP_1 - OP_16
758
+ this.chunks.push({
759
+ opCodeNum: bn.toNumber() + OpCode.OP_1 - 1
760
+ })
761
+ } else {
762
+ const buf = bn.toSm({ endian: 'little' })
763
+ this.writeBuffer(buf)
764
+ }
765
+ return this
766
+ }
767
+
768
+ static writeBn (bn) {
769
+ return new this().writeBn(bn)
770
+ }
771
+
772
+ writeNumber (number) {
773
+ this.writeBn(new Bn().fromNumber(number))
774
+ return this
775
+ }
776
+
777
+ static writeNumber (number) {
778
+ return new this().writeNumber(number)
779
+ }
780
+
781
+ setChunkBn (i, bn) {
782
+ this.chunks[i] = new Script().writeBn(bn).chunks[0]
783
+ return this
784
+ }
785
+
786
+ // note: this does not necessarily write buffers in the minimal way
787
+ // to write numbers in the minimal way, see writeBn
788
+ writeBuffer (buf) {
789
+ let opCodeNum
790
+ const len = buf.length
791
+ if (buf.length > 0 &amp;&amp; buf.length &lt; OpCode.OP_PUSHDATA1) {
792
+ opCodeNum = buf.length
793
+ } else if (buf.length === 0) {
794
+ opCodeNum = OpCode.OP_0
795
+ } else if (buf.length &lt; Math.pow(2, 8)) {
796
+ opCodeNum = OpCode.OP_PUSHDATA1
797
+ } else if (buf.length &lt; Math.pow(2, 16)) {
798
+ opCodeNum = OpCode.OP_PUSHDATA2
799
+ } else if (buf.length &lt; Math.pow(2, 32)) {
800
+ opCodeNum = OpCode.OP_PUSHDATA4
801
+ } else {
802
+ throw new Error('You can\'t push that much data')
803
+ }
804
+ this.chunks.push({
805
+ buf,
806
+ len,
807
+ opCodeNum
808
+ })
809
+ return this
810
+ }
811
+
812
+ static writeBuffer (buf) {
813
+ return new this().writeBuffer(buf)
814
+ }
815
+
816
+ setChunkBuffer (i, buf) {
817
+ this.chunks[i] = new Script().writeBuffer(buf).chunks[0]
818
+ return this
819
+ }
820
+
821
+ // make sure a push is the smallest way to push that particular data
822
+ // comes from bitcoind's script interpreter CheckMinimalPush function
823
+ checkMinimalPush (i) {
824
+ const chunk = this.chunks[i]
825
+ const buf = chunk.buf
826
+ const opCodeNum = chunk.opCodeNum
827
+ if (!buf) {
828
+ return true
829
+ }
830
+ if (buf.length === 0) {
831
+ // Could have used OP_0.
832
+ return opCodeNum === OpCode.OP_0
833
+ } else if (buf.length === 1 &amp;&amp; buf[0] >= 1 &amp;&amp; buf[0] &lt;= 16) {
834
+ // Could have used OP_1 .. OP_16.
835
+ return opCodeNum === OpCode.OP_1 + (buf[0] - 1)
836
+ } else if (buf.length === 1 &amp;&amp; buf[0] === 0x81) {
837
+ // Could have used OP_1NEGATE.
838
+ return opCodeNum === OpCode.OP_1NEGATE
839
+ } else if (buf.length &lt;= 75) {
840
+ // Could have used a direct push (opCode indicating number of bytes pushed + those bytes).
841
+ return opCodeNum === buf.length
842
+ } else if (buf.length &lt;= 255) {
843
+ // Could have used OP_PUSHDATA.
844
+ return opCodeNum === OpCode.OP_PUSHDATA1
845
+ } else if (buf.length &lt;= 65535) {
846
+ // Could have used OP_PUSHDATA2.
847
+ return opCodeNum === OpCode.OP_PUSHDATA2
848
+ }
849
+ return true
850
+ }
851
+ }
852
+
853
+ export { Script }
854
+ </code></pre>
855
+ </article>
856
+ </section>
857
+
858
+
859
+
860
+
861
+ </div>
862
+
863
+ <nav>
864
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Sig.html">Sig</a></li></ul><h3>Global</h3><ul><li><a href="global.html#decorate">decorate</a></li></ul>
865
+ </nav>
866
+
867
+ <br class="clear">
868
+
869
+ <footer>
870
+ Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Fri Feb 28 2025 03:51:24 GMT+0100 (hora estándar de Europa central)
871
+ </footer>
872
+
873
+ <script> prettyPrint(); </script>
874
+ <script src="scripts/linenumber.js"> </script>
875
+ </body>
876
+ </html>