bsv2 2.1.0 → 2.1.2

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