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