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/test/tx.js ADDED
@@ -0,0 +1,539 @@
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
+ })
@@ -0,0 +1,132 @@
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
+ })