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/bip-39.js DELETED
@@ -1,282 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { Address } from '../lib/address'
4
- import { Bip32 } from '../lib/bip-32'
5
- import { Bip39 } from '../lib/bip-39'
6
- import { Bip39Jp } from '../lib/bip-39-jp'
7
- import { Bip39En } from '../lib/bip-39-en'
8
- import { Random } from '../lib/random'
9
- import should from 'should'
10
- import vectors from './vectors/bip39.json'
11
- import { wordList as enWordList } from '../lib/bip-39-en-wordlist'
12
- import { wordList as jpWordList } from '../lib/bip-39-jp-wordlist'
13
-
14
- describe('Bip39', function () {
15
- this.timeout(5000)
16
-
17
- it('should initialize the class', function () {
18
- should.exist(Bip39)
19
- should.exist(new Bip39())
20
- })
21
-
22
- it('should have a wordlist of length 2048', function () {
23
- enWordList.length.should.equal(2048)
24
- jpWordList.length.should.equal(2048)
25
- })
26
-
27
- it('should handle this community-derived test vector', function () {
28
- // There was a bug in Copay and bip32jp about deriving addresses with bip39
29
- // and bip44. This confirms we are handling the situation correctly and
30
- // derive the correct value.
31
- //
32
- // More information here:
33
- // https://github.com/iancoleman/bip39/issues/58
34
- const seed = Bip39.fromString(
35
- 'fruit wave dwarf banana earth journey tattoo true farm silk olive fence'
36
- ).toSeed('banana')
37
- let bip32 = Bip32.fromSeed(seed)
38
- bip32 = bip32.derive("m/44'/0'/0'/0/0")
39
- const address = Address.fromPubKey(bip32.pubKey)
40
- address.toString().should.equal('17rxURoF96VhmkcEGCj5LNQkmN9HVhWb7F')
41
- })
42
-
43
- it('should generate a mnemonic phrase that passes the check', function () {
44
- let mnemonic
45
-
46
- // should be able to make a mnemonic with or without the default wordlist
47
- let bip39 = new Bip39En().fromRandom(128)
48
- bip39.check().should.equal(true)
49
- bip39 = new Bip39().fromRandom(128)
50
- bip39.check().should.equal(true)
51
-
52
- const entropy = Buffer.alloc(32)
53
- entropy.fill(0)
54
- bip39 = new Bip39En().fromEntropy(entropy)
55
- bip39.check().should.equal(true)
56
-
57
- mnemonic = bip39.mnemonic
58
-
59
- // mnemonics with extra whitespace do not pass the check
60
- bip39 = new Bip39En().fromString(mnemonic + ' ')
61
- bip39.check().should.equal(false)
62
-
63
- // mnemonics with a word replaced do not pass the check
64
- const words = mnemonic.split(' ')
65
- words[words.length - 1].should.not.equal('zoo')
66
- words[words.length - 1] = 'zoo'
67
- mnemonic = words.join(' ')
68
- new Bip39En()
69
- .fromString(mnemonic)
70
- .check()
71
- .should.equal(false)
72
- })
73
-
74
- describe('#toFastBuffer', function () {
75
- it('should convert to a buffer', function () {
76
- const bip39 = new Bip39().fromRandom()
77
- should.exist(bip39.seed)
78
- should.exist(bip39.mnemonic)
79
- const buf = bip39.toFastBuffer()
80
- buf.length.should.greaterThan(512 / 8 + 1 + 1)
81
- })
82
- })
83
-
84
- describe('#fromFastBuffer', function () {
85
- it('should convert from a buffer', function () {
86
- const bip39a = new Bip39().fromRandom()
87
- const bip39b = new Bip39().fromFastBuffer(bip39a.toFastBuffer())
88
- bip39a.mnemonic.should.equal(bip39b.mnemonic)
89
- bip39b.seed.toString('hex').should.equal(bip39b.seed.toString('hex'))
90
- })
91
- })
92
-
93
- describe('#fromRandom', function () {
94
- it('should throw an error if bits is too low', function () {
95
- ;(function () {
96
- new Bip39().fromRandom(127)
97
- }.should.throw('bits must be multiple of 32'))
98
- })
99
-
100
- it('should throw an error if bits is not a multiple of 32', function () {
101
- ;(function () {
102
- new Bip39().fromRandom(256 - 1)
103
- }.should.throw('bits must be multiple of 32'))
104
- })
105
- })
106
-
107
- describe('@fromRandom', function () {
108
- it('should throw an error if bits is too low', function () {
109
- ;(function () {
110
- Bip39.fromRandom(127)
111
- }.should.throw('bits must be multiple of 32'))
112
- })
113
-
114
- it('should throw an error if bits is not a multiple of 32', function () {
115
- ;(function () {
116
- Bip39.fromRandom(256 - 1)
117
- }.should.throw('bits must be multiple of 32'))
118
- })
119
- })
120
-
121
- describe('#asyncFromRandom', function () {
122
- it('should have a seed and a mnemonic', async function () {
123
- const bip39 = await new Bip39().asyncFromRandom()
124
- should.exist(bip39.mnemonic)
125
- should.exist(bip39.seed)
126
- const seed = bip39.seed
127
- bip39.mnemonic2Seed()
128
- seed.toString('hex').should.equal(bip39.seed.toString('hex'))
129
- })
130
- })
131
-
132
- describe('@asyncFromRandom', function () {
133
- it('should have a seed and a mnemonic', async function () {
134
- const bip39 = await Bip39.asyncFromRandom()
135
- should.exist(bip39.mnemonic)
136
- should.exist(bip39.seed)
137
- const seed = bip39.seed
138
- bip39.mnemonic2Seed()
139
- seed.toString('hex').should.equal(bip39.seed.toString('hex'))
140
- })
141
- })
142
-
143
- describe('#fromEntropy', function () {
144
- it('should build from entropy', function () {
145
- const bip39 = new Bip39().fromEntropy(Random.getRandomBuffer(32))
146
- should.exist(bip39)
147
- })
148
- })
149
-
150
- describe('@fromEntropy', function () {
151
- it('should build from entropy', function () {
152
- const bip39 = Bip39.fromEntropy(Random.getRandomBuffer(32))
153
- should.exist(bip39)
154
- })
155
- })
156
-
157
- describe('#asyncFromEntropy', function () {
158
- it('should return same as fromEntropy', async function () {
159
- const entropy = Random.getRandomBuffer(32)
160
- const bip39a = await new Bip39().asyncFromEntropy(entropy)
161
- const bip39b = await new Bip39().fromEntropy(entropy)
162
- bip39a
163
- .toSeed()
164
- .toString('hex')
165
- .should.equal(bip39b.toSeed().toString('hex'))
166
- })
167
- })
168
-
169
- describe('@asyncFromEntropy', function () {
170
- it('should return same as fromEntropy', async function () {
171
- const entropy = Random.getRandomBuffer(32)
172
- const bip39a = await Bip39.asyncFromEntropy(entropy)
173
- const bip39b = await Bip39.fromEntropy(entropy)
174
- bip39a
175
- .toSeed()
176
- .toString('hex')
177
- .should.equal(bip39b.toSeed().toString('hex'))
178
- })
179
- })
180
-
181
- describe('#entropy2Mnemonic', function () {
182
- it('should throw an error if you do not use enough entropy', function () {
183
- const buf = Buffer.alloc(128 / 8 - 1)
184
- buf.fill(0)
185
- ;(function () {
186
- new Bip39().entropy2Mnemonic(buf)
187
- }.should.throw(
188
- 'Entropy is less than 128 bits. It must be 128 bits or more.'
189
- ))
190
- })
191
-
192
- it('should work with or without the wordlist', function () {
193
- const buf = Buffer.alloc(128 / 8)
194
- buf.fill(0)
195
- const mnemonic1 = new Bip39().entropy2Mnemonic(buf).mnemonic
196
- const mnemonic2 = new Bip39En().entropy2Mnemonic(buf).mnemonic
197
- mnemonic1.should.equal(mnemonic2)
198
- })
199
- })
200
-
201
- describe('#check', function () {
202
- it('should work with or without optional wordlist', function () {
203
- const buf = Buffer.alloc(128 / 8)
204
- buf.fill(0)
205
- const mnemonic = new Bip39().entropy2Mnemonic(buf).mnemonic
206
- new Bip39()
207
- .fromString(mnemonic)
208
- .check()
209
- .should.equal(true)
210
- new Bip39En()
211
- .fromString(mnemonic)
212
- .check()
213
- .should.equal(true)
214
- })
215
- })
216
-
217
- describe('#fromString', function () {
218
- it('should throw an error in invalid mnemonic', function () {
219
- ;(function () {
220
- new Bip39().fromString('invalid mnemonic').toSeed()
221
- }.should.throw(
222
- 'Mnemonic does not pass the check - was the mnemonic typed incorrectly? Are there extra spaces?'
223
- ))
224
- })
225
- })
226
-
227
- describe('#asyncToSeed', function () {
228
- it('should result the same as toSeed', async function () {
229
- const bip39 = new Bip39().fromRandom()
230
- const seed1a = bip39.toSeed()
231
- const seed2a = await bip39.asyncToSeed()
232
- seed1a.toString('hex').should.equal(seed2a.toString('hex'))
233
- const seed1b = bip39.toSeed('pass')
234
- const seed2b = await bip39.asyncToSeed('pass')
235
- seed1b.toString('hex').should.equal(seed2b.toString('hex'))
236
- })
237
- })
238
-
239
- describe('@isValid', function () {
240
- it('should know this is valid', function () {
241
- const isValid = Bip39.isValid('abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', 'TREZOR')
242
- isValid.should.equal(true)
243
- })
244
-
245
- it('should know this is invalid', function () {
246
- const isValid = Bip39.isValid('abandonnnnnnn abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', 'TREZOR')
247
- isValid.should.equal(false)
248
- })
249
- })
250
-
251
- describe('vectors', function () {
252
- vectors.english.forEach(function (vector, v) {
253
- it('should pass english test vector ' + v, function () {
254
- const entropy = Buffer.from(vector.entropy, 'hex')
255
- const bip39 = new Bip39En().fromEntropy(entropy)
256
- bip39.toString().should.equal(vector.mnemonic)
257
- bip39.check().should.equal(true)
258
- const seed = bip39.toSeed(vector.passphrase)
259
- seed.toString('hex').should.equal(vector.seed)
260
- new Bip32()
261
- .fromSeed(seed)
262
- .toString()
263
- .should.equal(vector.bip32_xprv)
264
- })
265
- })
266
-
267
- vectors.japanese.forEach(function (vector, v) {
268
- it('should pass japanese test vector ' + v, function () {
269
- const entropy = Buffer.from(vector.entropy, 'hex')
270
- const bip39 = new Bip39Jp().fromEntropy(entropy)
271
- bip39.toString().should.equal(vector.mnemonic)
272
- bip39.check().should.equal(true)
273
- const seed = bip39.toSeed(vector.passphrase)
274
- seed.toString('hex').should.equal(vector.seed)
275
- new Bip32()
276
- .fromSeed(seed)
277
- .toString()
278
- .should.equal(vector.bip32_xprv)
279
- })
280
- })
281
- })
282
- })
@@ -1,152 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { BlockHeader } from '../lib/block-header'
4
- import { Bw } from '../lib/bw'
5
- import { Br } from '../lib/br'
6
- import should from 'should'
7
-
8
- describe('BlockHeader', function () {
9
- const bh = new BlockHeader()
10
- const versionBytesNum = 1
11
- const prevBlockHashBuf = Buffer.alloc(32)
12
- prevBlockHashBuf.fill(5)
13
- const merkleRootBuf = Buffer.alloc(32)
14
- merkleRootBuf.fill(9)
15
- const time = 2
16
- const bits = 3
17
- const nonce = 4
18
- bh.fromObject({
19
- versionBytesNum: versionBytesNum,
20
- prevBlockHashBuf: prevBlockHashBuf,
21
- merkleRootBuf: merkleRootBuf,
22
- time: time,
23
- bits: bits,
24
- nonce: nonce
25
- })
26
- const bhhex =
27
- '0100000005050505050505050505050505050505050505050505050505050505050505050909090909090909090909090909090909090909090909090909090909090909020000000300000004000000'
28
- const bhbuf = Buffer.from(bhhex, 'hex')
29
-
30
- it('should make a new blockHeader', function () {
31
- let blockHeader = new BlockHeader()
32
- should.exist(blockHeader)
33
- blockHeader = new BlockHeader()
34
- should.exist(blockHeader)
35
- })
36
-
37
- describe('#fromObject', function () {
38
- it('should set all the variables', function () {
39
- bh.fromObject({
40
- versionBytesNum: versionBytesNum,
41
- prevBlockHashBuf: prevBlockHashBuf,
42
- merkleRootBuf: merkleRootBuf,
43
- time: time,
44
- bits: bits,
45
- nonce: nonce
46
- })
47
- should.exist(bh.versionBytesNum)
48
- should.exist(bh.prevBlockHashBuf)
49
- should.exist(bh.merkleRootBuf)
50
- should.exist(bh.time)
51
- should.exist(bh.bits)
52
- should.exist(bh.nonce)
53
- })
54
- })
55
-
56
- describe('#fromJSON', function () {
57
- it('should set all the variables', function () {
58
- const bh = new BlockHeader().fromJSON({
59
- versionBytesNum: versionBytesNum,
60
- prevBlockHashBuf: prevBlockHashBuf.toString('hex'),
61
- merkleRootBuf: merkleRootBuf.toString('hex'),
62
- time: time,
63
- bits: bits,
64
- nonce: nonce
65
- })
66
- should.exist(bh.versionBytesNum)
67
- should.exist(bh.prevBlockHashBuf)
68
- should.exist(bh.merkleRootBuf)
69
- should.exist(bh.time)
70
- should.exist(bh.bits)
71
- should.exist(bh.nonce)
72
- })
73
- })
74
-
75
- describe('#toJSON', function () {
76
- it('should set all the variables', function () {
77
- const json = bh.toJSON()
78
- should.exist(json.versionBytesNum)
79
- should.exist(json.prevBlockHashBuf)
80
- should.exist(json.merkleRootBuf)
81
- should.exist(json.time)
82
- should.exist(json.bits)
83
- should.exist(json.nonce)
84
- })
85
- })
86
-
87
- describe('#fromHex', function () {
88
- it('should parse this known hex string', function () {
89
- new BlockHeader()
90
- .fromHex(bhhex)
91
- .toBuffer()
92
- .toString('hex')
93
- .should.equal(bhhex)
94
- })
95
- })
96
-
97
- describe('#fromBuffer', function () {
98
- it('should parse this known buffer', function () {
99
- new BlockHeader()
100
- .fromBuffer(bhbuf)
101
- .toBuffer()
102
- .toString('hex')
103
- .should.equal(bhhex)
104
- })
105
- })
106
-
107
- describe('#fromBr', function () {
108
- it('should parse this known buffer', function () {
109
- new BlockHeader()
110
- .fromBr(new Br(bhbuf))
111
- .toBuffer()
112
- .toString('hex')
113
- .should.equal(bhhex)
114
- })
115
- })
116
-
117
- describe('#toHex', function () {
118
- it('should output this known hex string', function () {
119
- new BlockHeader()
120
- .fromBuffer(bhbuf)
121
- .toHex()
122
- .should.equal(bhhex)
123
- })
124
- })
125
-
126
- describe('#toBuffer', function () {
127
- it('should output this known buffer', function () {
128
- new BlockHeader()
129
- .fromBuffer(bhbuf)
130
- .toBuffer()
131
- .toString('hex')
132
- .should.equal(bhhex)
133
- })
134
- })
135
-
136
- describe('#toBw', function () {
137
- it('should output this known buffer', function () {
138
- new BlockHeader()
139
- .fromBuffer(bhbuf)
140
- .toBw()
141
- .toBuffer()
142
- .toString('hex')
143
- .should.equal(bhhex)
144
- const bw = new Bw()
145
- new BlockHeader().fromBuffer(bhbuf).toBw(bw)
146
- bw
147
- .toBuffer()
148
- .toString('hex')
149
- .should.equal(bhhex)
150
- })
151
- })
152
- })
package/test/block.js DELETED
@@ -1,243 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { Br } from '../lib/br'
4
- import { Bw } from '../lib/bw'
5
- import { Block } from '../lib/block'
6
- import { BlockHeader } from '../lib/block-header'
7
- import { Tx } from '../lib/tx'
8
- import { VarInt } from '../lib/var-int'
9
- import largesttxblockvector from './vectors/largesttxblock'
10
- import should from 'should'
11
-
12
- describe('Block', function () {
13
- // const txidhex = '8c9aa966d35bfeaf031409e0001b90ccdafd8d859799eb945a3c515b8260bcf2'
14
- const txhex =
15
- '01000000029e8d016a7b0dc49a325922d05da1f916d1e4d4f0cb840c9727f3d22ce8d1363f000000008c493046022100e9318720bee5425378b4763b0427158b1051eec8b08442ce3fbfbf7b30202a44022100d4172239ebd701dae2fbaaccd9f038e7ca166707333427e3fb2a2865b19a7f27014104510c67f46d2cbb29476d1f0b794be4cb549ea59ab9cc1e731969a7bf5be95f7ad5e7f904e5ccf50a9dc1714df00fbeb794aa27aaff33260c1032d931a75c56f2ffffffffa3195e7a1ab665473ff717814f6881485dc8759bebe97e31c301ffe7933a656f020000008b48304502201c282f35f3e02a1f32d2089265ad4b561f07ea3c288169dedcf2f785e6065efa022100e8db18aadacb382eed13ee04708f00ba0a9c40e3b21cf91da8859d0f7d99e0c50141042b409e1ebbb43875be5edde9c452c82c01e3903d38fa4fd89f3887a52cb8aea9dc8aec7e2c9d5b3609c03eb16259a2537135a1bf0f9c5fbbcbdbaf83ba402442ffffffff02206b1000000000001976a91420bb5c3bfaef0231dc05190e7f1c8e22e098991e88acf0ca0100000000001976a9149e3e2d23973a04ec1b02be97c30ab9f2f27c3b2c88ac00000000'
16
- const txbuf = Buffer.from(txhex, 'hex')
17
- const magicNum = 0xd9b4bef9
18
- const blockSize = 50
19
- const bhhex =
20
- '0100000005050505050505050505050505050505050505050505050505050505050505050909090909090909090909090909090909090909090909090909090909090909020000000300000004000000'
21
- const bhbuf = Buffer.from(bhhex, 'hex')
22
- const bh = new BlockHeader().fromBuffer(bhbuf)
23
- const txsVi = VarInt.fromNumber(1)
24
- const txs = [new Tx().fromBuffer(txbuf)]
25
- const block = new Block().fromObject({
26
- magicNum: magicNum,
27
- blockSize: blockSize,
28
- blockHeader: bh,
29
- txsVi: txsVi,
30
- txs: txs
31
- })
32
- const blockHex =
33
- '01000000050505050505050505050505050505050505050505050505050505050505050509090909090909090909090909090909090909090909090909090909090909090200000003000000040000000101000000029e8d016a7b0dc49a325922d05da1f916d1e4d4f0cb840c9727f3d22ce8d1363f000000008c493046022100e9318720bee5425378b4763b0427158b1051eec8b08442ce3fbfbf7b30202a44022100d4172239ebd701dae2fbaaccd9f038e7ca166707333427e3fb2a2865b19a7f27014104510c67f46d2cbb29476d1f0b794be4cb549ea59ab9cc1e731969a7bf5be95f7ad5e7f904e5ccf50a9dc1714df00fbeb794aa27aaff33260c1032d931a75c56f2ffffffffa3195e7a1ab665473ff717814f6881485dc8759bebe97e31c301ffe7933a656f020000008b48304502201c282f35f3e02a1f32d2089265ad4b561f07ea3c288169dedcf2f785e6065efa022100e8db18aadacb382eed13ee04708f00ba0a9c40e3b21cf91da8859d0f7d99e0c50141042b409e1ebbb43875be5edde9c452c82c01e3903d38fa4fd89f3887a52cb8aea9dc8aec7e2c9d5b3609c03eb16259a2537135a1bf0f9c5fbbcbdbaf83ba402442ffffffff02206b1000000000001976a91420bb5c3bfaef0231dc05190e7f1c8e22e098991e88acf0ca0100000000001976a9149e3e2d23973a04ec1b02be97c30ab9f2f27c3b2c88ac00000000'
34
- const blockBuf = Buffer.from(blockHex, 'hex')
35
-
36
- const genesishex =
37
- '0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000'
38
- const genesisbuf = Buffer.from(genesishex, 'hex')
39
- const genesisidhex =
40
- '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
41
-
42
- it('should make a new block', function () {
43
- let block = new Block()
44
- should.exist(block)
45
- block = new Block()
46
- should.exist(block)
47
- })
48
-
49
- describe('#fromObject', function () {
50
- it('should set these known values', function () {
51
- const block = new Block().fromObject({
52
- magicNum: magicNum,
53
- blockSize: blockSize,
54
- blockHeader: bh,
55
- txsVi: txsVi,
56
- txs: txs
57
- })
58
- should.exist(block.magicNum)
59
- should.exist(block.blockSize)
60
- should.exist(block.blockHeader)
61
- should.exist(block.txsVi)
62
- should.exist(block.txs)
63
- })
64
- })
65
-
66
- describe('#fromJSON', function () {
67
- it('should set these known values', function () {
68
- const block = new Block().fromJSON({
69
- magicNum: magicNum,
70
- blockSize: blockSize,
71
- blockHeader: bh.toJSON(),
72
- txsVi: txsVi.toJSON(),
73
- txs: [txs[0].toJSON()]
74
- })
75
- should.exist(block.blockHeader)
76
- should.exist(block.txsVi)
77
- should.exist(block.txs)
78
- })
79
- })
80
-
81
- describe('#toJSON', function () {
82
- it('should recover these known values', function () {
83
- const json = block.toJSON()
84
- should.exist(json.blockHeader)
85
- should.exist(json.txsVi)
86
- should.exist(json.txs)
87
- })
88
- })
89
-
90
- describe('#fromHex', function () {
91
- it('should make a block from this known hex', function () {
92
- const block = new Block().fromHex(blockHex)
93
- block
94
- .toBuffer()
95
- .toString('hex')
96
- .should.equal(blockHex)
97
- })
98
- })
99
-
100
- describe('#fromBuffer', function () {
101
- it('should make a block from this known buffer', function () {
102
- const block = new Block().fromBuffer(blockBuf)
103
- block.txs.length.should.equal(1)
104
- block
105
- .toBuffer()
106
- .toString('hex')
107
- .should.equal(blockHex)
108
- })
109
- })
110
-
111
- describe('#fromBr', function () {
112
- it('should make a block from this known buffer', function () {
113
- const block = new Block().fromBr(new Br(blockBuf))
114
- block
115
- .toBuffer()
116
- .toString('hex')
117
- .should.equal(blockHex)
118
- })
119
- })
120
-
121
- describe('#toHex', function () {
122
- it('should recover a block from this known hex', function () {
123
- const block = new Block().fromHex(blockHex)
124
- block
125
- .toBuffer()
126
- .toString('hex')
127
- .should.equal(blockHex)
128
- })
129
- })
130
-
131
- describe('#toBuffer', function () {
132
- it('should recover a block from this known buffer', function () {
133
- const block = new Block().fromBuffer(blockBuf)
134
- block
135
- .toBuffer()
136
- .toString('hex')
137
- .should.equal(blockHex)
138
- })
139
- })
140
-
141
- describe('#toBw', function () {
142
- it('should recover a block from this known buffer', function () {
143
- const block = new Block().fromBuffer(blockBuf)
144
- block
145
- .toBw()
146
- .toBuffer()
147
- .toString('hex')
148
- .should.equal(blockHex)
149
- const bw = new Bw()
150
- block.toBw(bw)
151
- bw
152
- .toBuffer()
153
- .toString('hex')
154
- .should.equal(blockHex)
155
- })
156
- })
157
-
158
- describe('#hash', function () {
159
- it('should return the correct hash of the genesis block', function () {
160
- const block = new Block().fromBuffer(genesisbuf)
161
- const blockhash = Buffer.from(
162
- Array.apply([], Buffer.from(genesisidhex, 'hex')).reverse()
163
- )
164
- block
165
- .hash()
166
- .toString('hex')
167
- .should.equal(blockhash.toString('hex'))
168
- })
169
- })
170
-
171
- describe('#asyncHash', function () {
172
- it('should return the correct hash of the genesis block', async function () {
173
- const block = new Block().fromBuffer(genesisbuf)
174
- const hash = await block.asyncHash()
175
- const genesishashhex = new Br(Buffer.from(genesisidhex, 'hex'))
176
- .readReverse()
177
- .toString('hex')
178
- hash.toString('hex').should.equal(genesishashhex)
179
- })
180
-
181
- it('should return the correct hash of block containing the largest tx', async function () {
182
- this.timeout(10000)
183
- let block = new Block().fromHex(largesttxblockvector.blockhex)
184
- const buf = block.toBuffer()
185
- block = block.fromBuffer(buf)
186
- const hash = await block.asyncHash()
187
- const blockidhex = largesttxblockvector.blockidhex
188
- const blockhashBuf = new Br(Buffer.from(blockidhex, 'hex')).readReverse()
189
- const blockhashhex = blockhashBuf.toString('hex')
190
- hash.toString('hex').should.equal(blockhashhex)
191
- })
192
- })
193
-
194
- describe('#id', function () {
195
- it('should return the correct id of the genesis block', function () {
196
- const block = new Block().fromBuffer(genesisbuf)
197
- block
198
- .id()
199
- .should.equal(genesisidhex)
200
- })
201
-
202
- it('should return the correct id of block containing the largest tx', function () {
203
- const block = new Block().fromHex(largesttxblockvector.blockhex)
204
- block
205
- .id()
206
- .should.equal(largesttxblockvector.blockidhex)
207
- })
208
- })
209
-
210
- describe('#asyncId', function () {
211
- it('should return the correct id of the genesis block', async function () {
212
- const block = new Block().fromBuffer(genesisbuf)
213
- const id = await block.asyncId()
214
- id.should.equal(genesisidhex)
215
- })
216
-
217
- it('should return the correct id of block containing the largest tx', async function () {
218
- const block = new Block().fromHex(largesttxblockvector.blockhex)
219
- const id = await block.asyncId()
220
- id.should.equal(largesttxblockvector.blockidhex)
221
- })
222
- })
223
-
224
- describe('#verifyMerkleRoot', function () {
225
- it('should verify the merkle root of this known block with one tx (in addition to the coinbase tx)', function () {
226
- const block = new Block().fromHex(largesttxblockvector.blockhex)
227
- block.verifyMerkleRoot().should.equal(0)
228
- })
229
- })
230
-
231
- describe('@iterateTxs', function () {
232
- it('should make a block from this known buffer', function () {
233
- const txs = Block.iterateTxs(blockBuf)
234
- txs.txsNum.should.equal(1)
235
- let count = 0
236
- for (const tx of txs) {
237
- ;(tx instanceof Tx).should.equal(true)
238
- count++
239
- }
240
- count.should.equal(1)
241
- })
242
- })
243
- })