bsv2 2.0.13

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 (212) hide show
  1. package/.babelrc.test +10 -0
  2. package/.editorconfig +4 -0
  3. package/.env.defaults +1 -0
  4. package/.env.schema +1 -0
  5. package/.mocharc.yml +4 -0
  6. package/.nvmrc +1 -0
  7. package/.travis.yml +7 -0
  8. package/LICENSE +39 -0
  9. package/README.md +53 -0
  10. package/benchmark.js +50 -0
  11. package/bin/bsv.js +11 -0
  12. package/bin/network.js +92 -0
  13. package/build/bsv.js +3 -0
  14. package/build/index.html +20 -0
  15. package/dist/bsv.bundle.js +3 -0
  16. package/dist/bsv.bundle.js.LICENSE.txt +10 -0
  17. package/dist/bsv.bundle.js.map +1 -0
  18. package/dist/bsv.cjs.js +3 -0
  19. package/dist/bsv.cjs.js.LICENSE.txt +1 -0
  20. package/dist/bsv.cjs.js.map +1 -0
  21. package/dist/bsv.min.js +2 -0
  22. package/dist/bsv.module.js +3 -0
  23. package/dist/bsv.module.js.LICENSE.txt +1 -0
  24. package/dist/bsv.module.js.map +1 -0
  25. package/doc/assets/anchor.js +350 -0
  26. package/doc/assets/bass-addons.css +12 -0
  27. package/doc/assets/bass.css +544 -0
  28. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  29. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  30. package/doc/assets/fonts/LICENSE.txt +93 -0
  31. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  32. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  33. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  34. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  35. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  36. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  37. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  38. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  39. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  40. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  41. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  42. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  43. package/doc/assets/fonts/source-code-pro.css +23 -0
  44. package/doc/assets/github.css +123 -0
  45. package/doc/assets/site.js +168 -0
  46. package/doc/assets/split.css +15 -0
  47. package/doc/assets/split.js +784 -0
  48. package/doc/assets/style.css +147 -0
  49. package/doc/index.html +9387 -0
  50. package/docs/block.html +36 -0
  51. package/docs/ecies.html +81 -0
  52. package/docs/index.html +84 -0
  53. package/docs/networks.html +43 -0
  54. package/docs/script.html +101 -0
  55. package/docs/transaction.html +150 -0
  56. package/docs/unspentoutput.html +32 -0
  57. package/entry.js +82 -0
  58. package/index.js +102 -0
  59. package/jsdoc.json +5 -0
  60. package/karma.conf.js +78 -0
  61. package/lib/ach.js +59 -0
  62. package/lib/address.js +264 -0
  63. package/lib/aes.js +57 -0
  64. package/lib/aescbc.js +38 -0
  65. package/lib/base-58-check.js +83 -0
  66. package/lib/base-58.js +59 -0
  67. package/lib/bip-32.js +472 -0
  68. package/lib/bip-39-en-wordlist.js +2054 -0
  69. package/lib/bip-39-en.js +10 -0
  70. package/lib/bip-39-es-wordlist.js +2055 -0
  71. package/lib/bip-39-es.js +10 -0
  72. package/lib/bip-39-jp-wordlist.js +2053 -0
  73. package/lib/bip-39-jp.js +10 -0
  74. package/lib/bip-39-words.js +5 -0
  75. package/lib/bip-39.js +275 -0
  76. package/lib/block-header.js +79 -0
  77. package/lib/block.js +132 -0
  78. package/lib/bn.js +467 -0
  79. package/lib/br.js +172 -0
  80. package/lib/bsm.js +127 -0
  81. package/lib/bw.js +188 -0
  82. package/lib/cbc.js +140 -0
  83. package/lib/cmp.js +37 -0
  84. package/lib/config.js +50 -0
  85. package/lib/constants.js +150 -0
  86. package/lib/ecdsa.js +495 -0
  87. package/lib/ecies.js +151 -0
  88. package/lib/hash-cache.js +41 -0
  89. package/lib/hash.js +191 -0
  90. package/lib/interp.js +1853 -0
  91. package/lib/inv.js +49 -0
  92. package/lib/key-pair.js +133 -0
  93. package/lib/mainnet-address.js +5 -0
  94. package/lib/mainnet-bip-32.js +5 -0
  95. package/lib/mainnet-key-pair.js +5 -0
  96. package/lib/mainnet-priv-key.js +5 -0
  97. package/lib/merkle.js +92 -0
  98. package/lib/msg.js +184 -0
  99. package/lib/op-code.js +398 -0
  100. package/lib/point.js +134 -0
  101. package/lib/priv-key.js +157 -0
  102. package/lib/pub-key.js +230 -0
  103. package/lib/rabin.js +300 -0
  104. package/lib/random.js +28 -0
  105. package/lib/script.js +826 -0
  106. package/lib/sig-operations.js +120 -0
  107. package/lib/sig.js +399 -0
  108. package/lib/struct.js +389 -0
  109. package/lib/testnet-address.js +5 -0
  110. package/lib/testnet-bip-32.js +5 -0
  111. package/lib/testnet-key-pair.js +5 -0
  112. package/lib/testnet-priv-key.js +5 -0
  113. package/lib/tx-builder.js +593 -0
  114. package/lib/tx-in.js +172 -0
  115. package/lib/tx-out-map.js +57 -0
  116. package/lib/tx-out.js +73 -0
  117. package/lib/tx-verifier.js +209 -0
  118. package/lib/tx.js +429 -0
  119. package/lib/var-int.js +73 -0
  120. package/lib/version.js +92 -0
  121. package/lib/workers-result.js +65 -0
  122. package/lib/workers.js +77 -0
  123. package/load-babel.js +3 -0
  124. package/mkdocs.yml +37 -0
  125. package/package.json +138 -0
  126. package/test/ach.js +172 -0
  127. package/test/address.js +464 -0
  128. package/test/aes.js +96 -0
  129. package/test/aescbc.js +65 -0
  130. package/test/base-58-check.js +128 -0
  131. package/test/base-58.js +113 -0
  132. package/test/bip-32.js +721 -0
  133. package/test/bip-39.js +282 -0
  134. package/test/block-header.js +152 -0
  135. package/test/block.js +243 -0
  136. package/test/bn.js +643 -0
  137. package/test/br.js +353 -0
  138. package/test/bsm.js +141 -0
  139. package/test/bw.js +292 -0
  140. package/test/cbc.js +335 -0
  141. package/test/cmp.js +56 -0
  142. package/test/data/bip69.json +215 -0
  143. package/test/data/bitcoind/base58_keys_invalid.json +152 -0
  144. package/test/data/bitcoind/base58_keys_valid.json +452 -0
  145. package/test/data/bitcoind/blocks.json +27 -0
  146. package/test/data/bitcoind/script_tests.json +2244 -0
  147. package/test/data/bitcoind/sig_canonical.json +7 -0
  148. package/test/data/bitcoind/sig_noncanonical.json +22 -0
  149. package/test/data/bitcoind/tx_invalid.json +177 -0
  150. package/test/data/bitcoind/tx_valid.json +224 -0
  151. package/test/data/blk86756-testnet.dat +0 -0
  152. package/test/data/blk86756-testnet.js +12 -0
  153. package/test/data/blk86756-testnet.json +684 -0
  154. package/test/data/ecdsa.json +230 -0
  155. package/test/data/messages.json +22 -0
  156. package/test/data/sighash.json +1004 -0
  157. package/test/data/tx_creation.json +85 -0
  158. package/test/ecdsa.js +748 -0
  159. package/test/ecies.js +238 -0
  160. package/test/global.js +13 -0
  161. package/test/hash-cache.js +52 -0
  162. package/test/hash.js +354 -0
  163. package/test/index.js +10 -0
  164. package/test/interp.js +512 -0
  165. package/test/inv.js +65 -0
  166. package/test/key-pair.js +243 -0
  167. package/test/merkle.js +179 -0
  168. package/test/msg.js +295 -0
  169. package/test/op-code.js +79 -0
  170. package/test/point.js +375 -0
  171. package/test/priv-key.js +264 -0
  172. package/test/pub-key.js +549 -0
  173. package/test/random.js +31 -0
  174. package/test/script.js +1340 -0
  175. package/test/sig-operations.js +149 -0
  176. package/test/sig.js +662 -0
  177. package/test/struct.js +552 -0
  178. package/test/tx-builder.js +2104 -0
  179. package/test/tx-in.js +198 -0
  180. package/test/tx-out-map.js +104 -0
  181. package/test/tx-out.js +162 -0
  182. package/test/tx-verifier.js +246 -0
  183. package/test/tx.js +539 -0
  184. package/test/var-int.js +132 -0
  185. package/test/vectors/aes.json +10392 -0
  186. package/test/vectors/aescbc.json +620 -0
  187. package/test/vectors/bip39.json +502 -0
  188. package/test/vectors/bip69.json +215 -0
  189. package/test/vectors/bitcoin-abc/script_tests.json +48 -0
  190. package/test/vectors/bitcoin-abc/sighash.json +7004 -0
  191. package/test/vectors/bitcoin-sv/script_tests.json +248 -0
  192. package/test/vectors/bitcoind/script_invalid.json +1151 -0
  193. package/test/vectors/bitcoind/script_valid.json +1462 -0
  194. package/test/vectors/bitcoind/sighash.json +3503 -0
  195. package/test/vectors/bitcoind/tx_invalid.json +883 -0
  196. package/test/vectors/bitcoind/tx_valid.json +1150 -0
  197. package/test/vectors/coolest-tx-ever-sent.json +8 -0
  198. package/test/vectors/ecdsa.json +300 -0
  199. package/test/vectors/hash.json +6133 -0
  200. package/test/vectors/kdf.json +47 -0
  201. package/test/vectors/largesttx.json +5 -0
  202. package/test/vectors/largesttxblock.json +6 -0
  203. package/test/vectors/sig.json +79 -0
  204. package/test/vectors/sighash-single-bug.json +8 -0
  205. package/test/version.js +81 -0
  206. package/test/workers-result.js +89 -0
  207. package/test/workers.js +91 -0
  208. package/test-index.js +3 -0
  209. package/test1.html +88 -0
  210. package/webpack.config.js +60 -0
  211. package/webpack.config.js_entry +57 -0
  212. package/yarn-error.log +6199 -0
package/lib/script.js ADDED
@@ -0,0 +1,826 @@
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 }