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/test/tx.js DELETED
@@ -1,539 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { Bn } from '../lib/bn'
4
- import { Br } from '../lib/br'
5
- import { KeyPair } from '../lib/key-pair'
6
- import { Script } from '../lib/script'
7
- import { Sig } from '../lib/sig'
8
- import { Tx } from '../lib/tx'
9
- import { TxIn } from '../lib/tx-in'
10
- import { TxOut } from '../lib/tx-out'
11
- import { VarInt } from '../lib/var-int'
12
- import { Interp } from '../lib/interp'
13
- import should from 'should'
14
- import sinon from 'sinon'
15
-
16
- import vectorsBitcoindSighash from './vectors/bitcoind/sighash.json'
17
- import vectorsBitcoinABCSighash from './vectors/bitcoin-abc/sighash.json'
18
- import vectorsBitcoindTxValid from './vectors/bitcoind/tx_valid.json'
19
- import vectorsBitcoindTxInvalid from './vectors/bitcoind/tx_invalid.json'
20
- import largesttxvector from './vectors/largesttx.json'
21
- import fixture from './vectors/bip69.json'
22
-
23
- describe('Tx', function () {
24
- const txIn = new TxIn().fromBuffer(
25
- Buffer.from(
26
- '00000000000000000000000000000000000000000000000000000000000000000000000001ae00000000',
27
- 'hex'
28
- )
29
- )
30
- const txOut = new TxOut().fromBuffer(Buffer.from('050000000000000001ae', 'hex'))
31
- const tx = new Tx().fromObject({
32
- versionBytesNum: 0,
33
- txInsVi: VarInt.fromNumber(1),
34
- txIns: [txIn],
35
- txOutsVi: VarInt.fromNumber(1),
36
- txOuts: [txOut],
37
- nLockTime: 0
38
- })
39
- const txhex =
40
- '000000000100000000000000000000000000000000000000000000000000000000000000000000000001ae0000000001050000000000000001ae00000000'
41
- const txbuf = Buffer.from(txhex, 'hex')
42
-
43
- const tx2idhex =
44
- '8c9aa966d35bfeaf031409e0001b90ccdafd8d859799eb945a3c515b8260bcf2'
45
- const tx2hex =
46
- '01000000029e8d016a7b0dc49a325922d05da1f916d1e4d4f0cb840c9727f3d22ce8d1363f000000008c493046022100e9318720bee5425378b4763b0427158b1051eec8b08442ce3fbfbf7b30202a44022100d4172239ebd701dae2fbaaccd9f038e7ca166707333427e3fb2a2865b19a7f27014104510c67f46d2cbb29476d1f0b794be4cb549ea59ab9cc1e731969a7bf5be95f7ad5e7f904e5ccf50a9dc1714df00fbeb794aa27aaff33260c1032d931a75c56f2ffffffffa3195e7a1ab665473ff717814f6881485dc8759bebe97e31c301ffe7933a656f020000008b48304502201c282f35f3e02a1f32d2089265ad4b561f07ea3c288169dedcf2f785e6065efa022100e8db18aadacb382eed13ee04708f00ba0a9c40e3b21cf91da8859d0f7d99e0c50141042b409e1ebbb43875be5edde9c452c82c01e3903d38fa4fd89f3887a52cb8aea9dc8aec7e2c9d5b3609c03eb16259a2537135a1bf0f9c5fbbcbdbaf83ba402442ffffffff02206b1000000000001976a91420bb5c3bfaef0231dc05190e7f1c8e22e098991e88acf0ca0100000000001976a9149e3e2d23973a04ec1b02be97c30ab9f2f27c3b2c88ac00000000'
47
- const tx2buf = Buffer.from(tx2hex, 'hex')
48
-
49
- it('should make a new transaction', function () {
50
- let tx = new Tx()
51
- should.exist(tx)
52
- tx = new Tx()
53
- should.exist(tx)
54
-
55
- Tx.fromBuffer(txbuf)
56
- .toBuffer()
57
- .toString('hex')
58
- .should.equal(txhex)
59
-
60
- // should set known defaults
61
- tx.versionBytesNum.should.equal(1)
62
- tx.txInsVi.toNumber().should.equal(0)
63
- tx.txIns.length.should.equal(0)
64
- tx.txOutsVi.toNumber().should.equal(0)
65
- tx.txOuts.length.should.equal(0)
66
- tx.nLockTime.should.equal(0)
67
- })
68
-
69
- describe('#constructor', function () {
70
- it('should set these known defaults', function () {
71
- const tx = new Tx()
72
- tx.versionBytesNum.should.equal(1)
73
- tx.txInsVi.toNumber().should.equal(0)
74
- tx.txIns.length.should.equal(0)
75
- tx.txOutsVi.toNumber().should.equal(0)
76
- tx.txOuts.length.should.equal(0)
77
- tx.nLockTime.should.equal(0)
78
- })
79
- })
80
-
81
- describe('#clone', function () {
82
- it('should clone a tx', function () {
83
- const tx1 = Tx.fromHex(tx2hex)
84
- const tx2 = tx1.clone()
85
- tx2.should.not.equal(tx1)
86
- tx2.toHex().should.equal(tx1.toHex())
87
- })
88
- })
89
-
90
- describe('#cloneByBuffer', function () {
91
- it('should clone a tx by buffer', function () {
92
- const tx1 = Tx.fromHex(tx2hex)
93
- tx1.toJSON = sinon.spy()
94
- const tx2 = tx1.cloneByBuffer()
95
- tx1.toJSON.calledOnce.should.equal(false)
96
- tx2.should.not.equal(tx1)
97
- tx2.toHex().should.equal(tx1.toHex())
98
- })
99
- })
100
-
101
- describe('#fromObject', function () {
102
- it('should set all the basic parameters', function () {
103
- const tx = new Tx().fromObject({
104
- versionBytesNum: 0,
105
- txInsVi: VarInt.fromNumber(1),
106
- txIns: [txIn],
107
- txOutsVi: VarInt.fromNumber(1),
108
- txOuts: [txOut],
109
- nLockTime: 0
110
- })
111
- should.exist(tx.versionBytesNum)
112
- should.exist(tx.txInsVi)
113
- should.exist(tx.txIns)
114
- should.exist(tx.txOutsVi)
115
- should.exist(tx.txOuts)
116
- should.exist(tx.nLockTime)
117
- })
118
- })
119
-
120
- describe('#fromJSON', function () {
121
- it('should set all the basic parameters', function () {
122
- const tx = new Tx().fromJSON({
123
- versionBytesNum: 0,
124
- txInsVi: VarInt.fromNumber(1).toJSON(),
125
- txIns: [txIn.toJSON()],
126
- txOutsVi: VarInt.fromNumber(1).toJSON(),
127
- txOuts: [txOut.toJSON()],
128
- nLockTime: 0
129
- })
130
- should.exist(tx.versionBytesNum)
131
- should.exist(tx.txInsVi)
132
- should.exist(tx.txIns)
133
- should.exist(tx.txOutsVi)
134
- should.exist(tx.txOuts)
135
- should.exist(tx.nLockTime)
136
- })
137
- })
138
-
139
- describe('#toJSON', function () {
140
- it('should recover all the basic parameters', function () {
141
- const json = tx.toJSON()
142
- should.exist(json.versionBytesNum)
143
- should.exist(json.txInsVi)
144
- should.exist(json.txIns)
145
- should.exist(json.txOutsVi)
146
- should.exist(json.txOuts)
147
- should.exist(json.nLockTime)
148
- })
149
- })
150
-
151
- describe('#fromHex', function () {
152
- it('should recover from this known tx', function () {
153
- new Tx()
154
- .fromHex(txhex)
155
- .toHex()
156
- .should.equal(txhex)
157
- })
158
-
159
- it('should recover from this known tx from the blockchain', function () {
160
- new Tx()
161
- .fromHex(tx2hex)
162
- .toHex()
163
- .should.equal(tx2hex)
164
- })
165
- })
166
-
167
- describe('#fromBuffer', function () {
168
- it('should recover from this known tx', function () {
169
- new Tx()
170
- .fromBuffer(txbuf)
171
- .toBuffer()
172
- .toString('hex')
173
- .should.equal(txhex)
174
- })
175
-
176
- it('should recover from this known tx from the blockchain', function () {
177
- new Tx()
178
- .fromBuffer(tx2buf)
179
- .toBuffer()
180
- .toString('hex')
181
- .should.equal(tx2hex)
182
- })
183
- })
184
-
185
- describe('#fromBr', function () {
186
- it('should recover from this known tx', function () {
187
- new Tx()
188
- .fromBr(new Br(txbuf))
189
- .toBuffer()
190
- .toString('hex')
191
- .should.equal(txhex)
192
- })
193
- })
194
-
195
- describe('#toHex', function () {
196
- it('should produce this known tx', function () {
197
- new Tx()
198
- .fromHex(txhex)
199
- .toHex()
200
- .should.equal(txhex)
201
- })
202
- })
203
-
204
- describe('#toBuffer', function () {
205
- it('should produce this known tx', function () {
206
- new Tx()
207
- .fromBuffer(txbuf)
208
- .toBuffer()
209
- .toString('hex')
210
- .should.equal(txhex)
211
- })
212
- })
213
-
214
- describe('#toBw', function () {
215
- it('should produce this known tx', function () {
216
- new Tx()
217
- .fromBuffer(txbuf)
218
- .toBw()
219
- .toBuffer()
220
- .toString('hex')
221
- .should.equal(txhex)
222
- })
223
- })
224
-
225
- describe('#sighash', function () {
226
- it('should hash this transaction', function () {
227
- tx.sighash(0, 0, new Script()).length.should.equal(32)
228
- })
229
-
230
- it('should return 1 for the SIGHASH_SINGLE bug', function () {
231
- const tx = Tx.fromBuffer(tx2buf)
232
- tx.txOuts.length = 1
233
- tx.txOutsVi = VarInt.fromNumber(1)
234
- tx
235
- .sighash(Sig.SIGHASH_SINGLE, 1, new Script())
236
- .toString('hex')
237
- .should.equal(
238
- '0000000000000000000000000000000000000000000000000000000000000001'
239
- )
240
- })
241
- })
242
-
243
- describe('#asyncSighash', function () {
244
- it('should hash this transaction', async function () {
245
- const hashBuf = await tx.asyncSighash(0, 0, new Script())
246
- hashBuf.length.should.equal(32)
247
- })
248
-
249
- it('should return 1 for the SIGHASH_SINGLE bug', async function () {
250
- const tx = Tx.fromBuffer(tx2buf)
251
- tx.txOuts.length = 1
252
- tx.txOutsVi = VarInt.fromNumber(1)
253
- const hashBuf = await tx.asyncSighash(Sig.SIGHASH_SINGLE, 1, new Script())
254
- hashBuf
255
- .toString('hex')
256
- .should.equal(
257
- '0000000000000000000000000000000000000000000000000000000000000001'
258
- )
259
- })
260
- })
261
-
262
- describe('#sign', function () {
263
- it('should return a signature', function () {
264
- const keyPair = new KeyPair().fromRandom()
265
- const sig1 = tx.sign(keyPair, Sig.SIGHASH_ALL, 0, new Script())
266
- should.exist(sig1)
267
- const sig2 = tx.sign(keyPair, Sig.SIGHASH_SINGLE, 0, new Script())
268
- const sig3 = tx.sign(
269
- keyPair,
270
- Sig.SIGHASH_ALL,
271
- 0,
272
- new Script().fromString('OP_RETURN')
273
- )
274
- sig1.toString().should.not.equal(sig2.toString())
275
- sig1.toString().should.not.equal(sig3.toString())
276
- })
277
- })
278
-
279
- describe('#asyncSign', function () {
280
- it('should return a signature', async function () {
281
- const keyPair = new KeyPair().fromRandom()
282
- const sig1 = tx.sign(keyPair, Sig.SIGHASH_ALL, 0, new Script())
283
- const sig1b = await tx.asyncSign(keyPair, Sig.SIGHASH_ALL, 0, new Script())
284
- const sig2 = tx.sign(keyPair, Sig.SIGHASH_SINGLE, 0, new Script())
285
- const sig2b = await tx.asyncSign(
286
- keyPair,
287
- Sig.SIGHASH_SINGLE,
288
- 0,
289
- new Script()
290
- )
291
- const sig3 = tx.sign(
292
- keyPair,
293
- Sig.SIGHASH_ALL,
294
- 0,
295
- new Script().fromString('OP_RETURN')
296
- )
297
- const sig3b = await tx.asyncSign(
298
- keyPair,
299
- Sig.SIGHASH_ALL,
300
- 0,
301
- new Script().fromString('OP_RETURN')
302
- )
303
- sig1.toString().should.equal(sig1b.toString())
304
- sig2.toString().should.equal(sig2b.toString())
305
- sig3.toString().should.equal(sig3b.toString())
306
- })
307
- })
308
-
309
- describe('#verify', function () {
310
- it('should return a signature', function () {
311
- const keyPair = new KeyPair().fromRandom()
312
- const sig1 = tx.sign(keyPair, Sig.SIGHASH_ALL, 0, new Script())
313
- tx.verify(sig1, keyPair.pubKey, 0, new Script()).should.equal(true)
314
- })
315
- })
316
-
317
- describe('#asyncVerify', function () {
318
- it('should return a signature', async function () {
319
- const keyPair = new KeyPair().fromRandom()
320
- const sig1 = tx.sign(keyPair, Sig.SIGHASH_ALL, 0, new Script())
321
- const verified = await tx.asyncVerify(sig1, keyPair.pubKey, 0, new Script())
322
- verified.should.equal(true)
323
- })
324
- })
325
-
326
- describe('#hash', function () {
327
- it('should correctly calculate the hash of this known transaction', function () {
328
- const tx = Tx.fromBuffer(tx2buf)
329
- const txHashBuf = Buffer.from(
330
- Array.apply([], Buffer.from(tx2idhex, 'hex')).reverse()
331
- )
332
- tx
333
- .hash()
334
- .toString('hex')
335
- .should.equal(txHashBuf.toString('hex'))
336
- })
337
- })
338
-
339
- describe('#asyncHash', function () {
340
- it('should correctly calculate the hash of this known transaction', async function () {
341
- const tx = Tx.fromBuffer(tx2buf)
342
- const txHashBuf = Buffer.from(
343
- Array.apply([], Buffer.from(tx2idhex, 'hex')).reverse()
344
- )
345
- const hashBuf = await tx.asyncHash()
346
- hashBuf.toString('hex').should.equal(txHashBuf.toString('hex'))
347
- })
348
- })
349
-
350
- describe('#id', function () {
351
- it('should correctly calculate the id of this known transaction', function () {
352
- const tx = Tx.fromBuffer(tx2buf)
353
- tx
354
- .id()
355
- .should.equal(tx2idhex)
356
- })
357
- })
358
-
359
- describe('#asyncId', function () {
360
- it('should correctly calculate the id of this known transaction', async function () {
361
- const tx = Tx.fromBuffer(tx2buf)
362
- const idbuf = await tx.asyncId()
363
- idbuf.should.equal(tx2idhex)
364
- })
365
- })
366
-
367
- describe('#addTxIn', function () {
368
- it('should add an input', function () {
369
- const txIn = new TxIn()
370
- const tx = new Tx()
371
- tx.txInsVi.toNumber().should.equal(0)
372
- tx.addTxIn(txIn)
373
- tx.txInsVi.toNumber().should.equal(1)
374
- tx.txIns.length.should.equal(1)
375
- })
376
- })
377
-
378
- describe('#addTxOut', function () {
379
- it('should add an output', function () {
380
- const txOut = new TxOut()
381
- const tx = new Tx()
382
- tx.txOutsVi.toNumber().should.equal(0)
383
- tx.addTxOut(txOut)
384
- tx.txOutsVi.toNumber().should.equal(1)
385
- tx.txOuts.length.should.equal(1)
386
- })
387
- })
388
-
389
- describe('bectors: bip69 (from bitcoinjs)', function () {
390
- // returns index-based order of sorted against original
391
- function getIndexOrder (original, sorted) {
392
- return sorted.map((value) => {
393
- return original.indexOf(value)
394
- })
395
- }
396
-
397
- fixture.inputs.forEach((inputSet) => {
398
- it(inputSet.description, () => {
399
- const tx = new Tx()
400
- const txIns = inputSet.inputs.map((input) => {
401
- const txHashBuf = Buffer.from(input.txId, 'hex').reverse()
402
- const txOutNum = input.vout
403
- const script = new Script()
404
- const txIn = TxIn.fromProperties(txHashBuf, txOutNum, script)
405
- return txIn
406
- })
407
- tx.txIns = [...txIns]
408
- tx.sort()
409
- getIndexOrder(txIns, tx.txIns).toString().should.equal(inputSet.expected.toString())
410
- })
411
- })
412
-
413
- fixture.outputs.forEach((outputSet) => {
414
- it(outputSet.description, () => {
415
- const tx = new Tx()
416
- const txOuts = outputSet.outputs.map(function (output) {
417
- const txOut = TxOut.fromProperties(new Bn(output.value), Script.fromAsmString(output.script))
418
- return txOut
419
- })
420
- tx.txOuts = [...txOuts]
421
- tx.sort()
422
- getIndexOrder(txOuts, tx.txOuts).toString().should.equal(outputSet.expected.toString())
423
- })
424
- })
425
- })
426
-
427
- describe('vectors: a 1mb transaction', function () {
428
- it('should find the correct id of this (valid, on the blockchain) 1 mb transaction', function () {
429
- const txidhex = largesttxvector.txidhex
430
- const txhex = largesttxvector.txhex
431
- const tx = Tx.fromHex(txhex)
432
- const txid = tx.id()
433
- txid.should.equal(txidhex)
434
- })
435
- })
436
-
437
- describe('vectors: sighash and serialization', function () {
438
- vectorsBitcoindSighash.forEach(function (vector, i) {
439
- if (i === 0) {
440
- return
441
- }
442
- it('should pass bitcoind sighash test vector ' + i, function () {
443
- const txbuf = Buffer.from(vector[0], 'hex')
444
- const scriptbuf = Buffer.from(vector[1], 'hex')
445
- const subScript = new Script().fromBuffer(scriptbuf)
446
- const nIn = vector[2]
447
- const nHashType = vector[3]
448
- const sighashBuf = Buffer.from(vector[4], 'hex')
449
- const tx = Tx.fromBuffer(txbuf)
450
-
451
- // make sure transacion to/from buffer is isomorphic
452
- tx
453
- .toHex()
454
- .should.equal(txbuf.toString('hex'))
455
-
456
- // sighash ought to be correct
457
- tx
458
- .sighash(nHashType, nIn, subScript)
459
- .toString('hex')
460
- .should.equal(sighashBuf.toString('hex'))
461
- })
462
- })
463
-
464
- vectorsBitcoinABCSighash.forEach(function (vector, i) {
465
- if (i === 0) {
466
- return
467
- }
468
- it('should pass bitcoin-abc sighash test vector ' + i, function () {
469
- if (vector[0] === 'Test vectors for SIGHASH_FORKID') {
470
- return
471
- }
472
- const txbuf = Buffer.from(vector[0], 'hex')
473
- const scriptbuf = Buffer.from(vector[1], 'hex')
474
- const subScript = new Script().fromBuffer(scriptbuf)
475
- const nIn = vector[2]
476
- const nHashType = vector[3]
477
- const sighashBuf = Buffer.from(vector[4], 'hex')
478
- const tx = Tx.fromBuffer(txbuf)
479
-
480
- // make sure transacion to/from buffer is isomorphic
481
- tx
482
- .toBuffer()
483
- .toString('hex')
484
- .should.equal(txbuf.toString('hex'))
485
-
486
- // sighash ought to be correct
487
- const valueBn = new Bn(0)
488
- let flags = 0
489
- if (nHashType & Sig.SIGHASH_FORKID) {
490
- flags = Interp.SCRIPT_ENABLE_SIGHASH_FORKID
491
- }
492
- tx
493
- .sighash(nHashType, nIn, subScript, valueBn, flags)
494
- .toString('hex')
495
- .should.equal(sighashBuf.toString('hex'))
496
- })
497
- })
498
-
499
- let j = 0
500
- vectorsBitcoindTxValid.forEach(function (vector, i) {
501
- if (vector.length === 1) {
502
- return
503
- }
504
- it(
505
- 'should correctly serialized/deserialize tx_valid test vector ' + j,
506
- function () {
507
- const txhex = vector[1]
508
- const txbuf = Buffer.from(vector[1], 'hex')
509
- const tx = Tx.fromBuffer(txbuf)
510
- tx
511
- .toBuffer()
512
- .toString('hex')
513
- .should.equal(txhex)
514
- }
515
- )
516
- j++
517
- })
518
-
519
- j = 0
520
- vectorsBitcoindTxInvalid.forEach(function (vector, i) {
521
- if (vector.length === 1) {
522
- return
523
- }
524
- it(
525
- 'should correctly serialized/deserialize tx_invalid test vector ' + j,
526
- function () {
527
- const txhex = vector[1]
528
- const txbuf = Buffer.from(vector[1], 'hex')
529
- const tx = Tx.fromBuffer(txbuf)
530
- tx
531
- .toBuffer()
532
- .toString('hex')
533
- .should.equal(txhex)
534
- }
535
- )
536
- j++
537
- })
538
- })
539
- })
package/test/var-int.js DELETED
@@ -1,132 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { Bn } from '../lib/bn'
4
- import should from 'should'
5
- import { Br } from '../lib/br'
6
- import { Bw } from '../lib/bw'
7
- import { VarInt } from '../lib/var-int'
8
-
9
- describe('VarInt', function () {
10
- it('should make a new varInt', function () {
11
- const buf = Buffer.from('00', 'hex')
12
- let varInt = new VarInt(buf)
13
- should.exist(varInt)
14
- varInt.buf.toString('hex').should.equal('00')
15
- varInt = new VarInt(buf)
16
- should.exist(varInt)
17
- varInt.buf.toString('hex').should.equal('00')
18
-
19
- // varInts can have multiple buffer representations
20
- VarInt.fromNumber(0)
21
- .toNumber()
22
- .should.equal(new VarInt(Buffer.from([0xfd, 0, 0])).toNumber())
23
- VarInt.fromNumber(0)
24
- .toBuffer()
25
- .toString('hex')
26
- .should.not.equal(
27
- new VarInt()
28
- .fromBuffer(Buffer.from([0xfd, 0, 0]))
29
- .toBuffer()
30
- .toString('hex')
31
- )
32
- })
33
-
34
- describe('#fromObject', function () {
35
- it('should set a buffer', function () {
36
- const buf = Buffer.from('00', 'hex')
37
- const varInt = new VarInt().fromObject({ buf: buf })
38
- varInt.buf.toString('hex').should.equal('00')
39
- varInt.fromObject({})
40
- varInt.buf.toString('hex').should.equal('00')
41
- })
42
- })
43
-
44
- describe('#fromJSON', function () {
45
- it('should set a buffer', function () {
46
- const buf = new Bw().writeVarIntNum(5).toBuffer()
47
- const varInt = new VarInt().fromJSON(buf.toString('hex'))
48
- varInt.toNumber().should.equal(5)
49
- })
50
- })
51
-
52
- describe('#toJSON', function () {
53
- it('should return a buffer', function () {
54
- const buf = new Bw().writeVarIntNum(5).toBuffer()
55
- const varInt = new VarInt().fromJSON(buf.toString('hex'))
56
- varInt.toJSON().should.equal('05')
57
- })
58
- })
59
-
60
- describe('#fromBuffer', function () {
61
- it('should set a buffer', function () {
62
- const buf = new Bw().writeVarIntNum(5).toBuffer()
63
- const varInt = new VarInt().fromBuffer(buf)
64
- varInt.toNumber().should.equal(5)
65
- })
66
- })
67
-
68
- describe('#fromBr', function () {
69
- it('should set a buffer reader', function () {
70
- const buf = new Bw().writeVarIntNum(5).toBuffer()
71
- const br = new Br(buf)
72
- const varInt = new VarInt().fromBr(br)
73
- varInt.toNumber().should.equal(5)
74
- })
75
- })
76
-
77
- describe('#fromBn', function () {
78
- it('should set a number', function () {
79
- const varInt = new VarInt().fromBn(new Bn(5))
80
- varInt.toNumber().should.equal(5)
81
- })
82
- })
83
-
84
- describe('@fromBn', function () {
85
- it('should set a number', function () {
86
- const varInt = VarInt.fromBn(new Bn(5))
87
- varInt.toNumber().should.equal(5)
88
- })
89
- })
90
-
91
- describe('#fromNumber', function () {
92
- it('should set a number', function () {
93
- const varInt = new VarInt().fromNumber(5)
94
- varInt.toNumber().should.equal(5)
95
- })
96
- })
97
-
98
- describe('@fromNumber', function () {
99
- it('should set a number', function () {
100
- const varInt = VarInt.fromNumber(5)
101
- varInt.toNumber().should.equal(5)
102
- })
103
- })
104
-
105
- describe('#toBuffer', function () {
106
- it('should return a buffer', function () {
107
- const buf = new Bw().writeVarIntNum(5).toBuffer()
108
- const varInt = new VarInt(buf)
109
- varInt
110
- .toBuffer()
111
- .toString('hex')
112
- .should.equal(buf.toString('hex'))
113
- })
114
- })
115
-
116
- describe('#toBn', function () {
117
- it('should return a buffer', function () {
118
- const varInt = VarInt.fromNumber(5)
119
- varInt
120
- .toBn()
121
- .toString()
122
- .should.equal(new Bn(5).toString())
123
- })
124
- })
125
-
126
- describe('#toNumber', function () {
127
- it('should return a buffer', function () {
128
- const varInt = VarInt.fromNumber(5)
129
- varInt.toNumber().should.equal(5)
130
- })
131
- })
132
- })