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/bn.js DELETED
@@ -1,643 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import should from 'should'
4
- import assert from 'assert'
5
- import { Bn } from '../lib/bn'
6
-
7
- describe('Bn', function () {
8
- it('should create a bn', function () {
9
- let bn = new Bn(50)
10
- should.exist(bn)
11
- bn.toString().should.equal('50')
12
- bn = new Bn(50)
13
- bn.toString().should.equal('50')
14
- ;(bn instanceof Bn).should.equal(true)
15
- bn = new Bn('ff00', 16)
16
- bn.toString(16).should.equal('ff00')
17
- bn = new Bn('ff00', 16)
18
- bn.toString(16).should.equal('ff00')
19
- })
20
-
21
- it('should parse this number', function () {
22
- const bn = new Bn(999970000)
23
- bn.toString().should.equal('999970000')
24
- })
25
-
26
- it('should parse numbers below and at bn.js internal word size', function () {
27
- let bn = new Bn(Math.pow(2, 26) - 1)
28
- bn.toString().should.equal((Math.pow(2, 26) - 1).toString())
29
- bn = new Bn(Math.pow(2, 26))
30
- bn.toString().should.equal(Math.pow(2, 26).toString())
31
- })
32
-
33
- it('should correctly square the number related to a bug in bn.js', function () {
34
- /**
35
- * These test vectors are related to this bn.js commit:
36
- * https://github.com/indutny/bn.js/commit/3557d780b07ed0ed301e128f326f83c2226fb679
37
- *
38
- * More information:
39
- * https://github.com/bitpay/bitcore/pull/894
40
- * https://github.com/indutny/elliptic/issues/17
41
- * https://github.com/indutny/elliptic/pull/18
42
- * https://github.com/indutny/elliptic/pull/19
43
- */
44
- const p = Bn._prime('k256').p
45
- const red = Bn.red('k256')
46
-
47
- const n = new Bn(
48
- '9cd8cb48c3281596139f147c1364a3ede88d3f310fdb0eb98c924e599ca1b3c9',
49
- 16
50
- )
51
- const expected = n.sqr().mod(p)
52
- const actual = n
53
- .toRed(red)
54
- .redSqr()
55
- .fromRed()
56
-
57
- assert.strict.equal(actual.toString(16), expected.toString(16))
58
- })
59
-
60
- it('should correctly square these numbers related to a bug in OpenSSL - CVE-2014-3570', function () {
61
- /**
62
- * Bitcoin developer Peter Wuile discovered a bug in OpenSSL in the course
63
- * of developing libsecp256k. The OpenSSL security advisory is here:
64
- *
65
- * https://www.openssl.org/news/secadv_20150108.txt
66
- *
67
- * Greg Maxwell has a description of the bug and how it was found here:
68
- *
69
- * https://www.reddit.com/r/Bitcoin/comments/2rrxq7/on_why_010s_release_notes_say_we_have_reason_to/
70
- * https://www.reddit.com/r/programming/comments/2rrc64/openssl_security_advisory_new_openssl_releases/
71
- *
72
- * The OpenSSL fix is here:
73
- *
74
- * https://github.com/openssl/openssl/commit/a7a44ba55cb4f884c6bc9ceac90072dea38e66d0
75
- *
76
- * This test uses the same big numbers that were problematic in OpenSSL.
77
- * The check is to ensure that squaring a number and multiplying it by
78
- * itself result in the same number. As an additional precaution, we check
79
- * this multiplication normally as well as mod(p).
80
- */
81
- const p = Bn._prime('k256').p
82
-
83
- let n = new Bn(
84
- '80000000000000008000000000000001FFFFFFFFFFFFFFFE0000000000000000',
85
- 16
86
- )
87
- let sqr = n.sqr()
88
- let nn = n.mul(n)
89
- nn.toString().should.equal(sqr.toString())
90
- sqr = n.sqr().mod(p)
91
- nn = n.mul(n).mod(p)
92
- nn.toString().should.equal(sqr.toString())
93
-
94
- n = new Bn(
95
- '80000000000000000000000080000001FFFFFFFE000000000000000000000000',
96
- 16
97
- )
98
- sqr = n.sqr()
99
- nn = n.mul(n)
100
- nn.toString().should.equal(sqr.toString())
101
- sqr = n.sqr().mod(p)
102
- nn = n.mul(n).mod(p)
103
- nn.toString().should.equal(sqr.toString())
104
- })
105
-
106
- describe('#copy', function () {
107
- it('should copy 5', function () {
108
- const bn = new Bn('5')
109
- let bn2
110
- ;(function () {
111
- bn.copy(bn2)
112
- }.should.throw()) // bn2 is not a Bn yet
113
- const bn3 = new Bn()
114
- bn.copy(bn3)
115
- bn3.toString().should.equal('5')
116
- })
117
- })
118
-
119
- describe('#neg', function () {
120
- it('should produce a negative', function () {
121
- const bn = new Bn(1).neg()
122
- ;(bn instanceof Bn).should.equal(true)
123
- bn.toString().should.equal('-1')
124
- })
125
- })
126
-
127
- describe('#add', function () {
128
- it('should add two small numbers together', function () {
129
- const bn1 = new Bn(50)
130
- const bn2 = new Bn(75)
131
- const bn3 = bn1.add(bn2)
132
- bn3.toString().should.equal('125')
133
- })
134
- })
135
-
136
- describe('#sub', function () {
137
- it('should subtract a small number', function () {
138
- const bn1 = new Bn(50)
139
- const bn2 = new Bn(25)
140
- const bn3 = bn1.sub(bn2)
141
- bn3.toString().should.equal('25')
142
- })
143
- })
144
-
145
- describe('#mul', function () {
146
- it('should mul a small number', function () {
147
- const bn1 = new Bn(50)
148
- const bn2 = new Bn(25)
149
- const bn3 = bn1.mul(bn2)
150
- bn3.toString().should.equal('1250')
151
- })
152
- })
153
-
154
- describe('#mod', function () {
155
- it('should mod a small number', function () {
156
- const bn1 = new Bn(50)
157
- const bn2 = new Bn(25)
158
- const bn3 = bn1.mod(bn2)
159
- bn3.toString().should.equal('0')
160
- })
161
-
162
- it('should mod a small number', function () {
163
- const bn1 = new Bn(-50)
164
- const bn2 = new Bn(25)
165
- const bn3 = bn1.mod(bn2)
166
- bn3.toString().should.equal('0')
167
- })
168
-
169
- it('should mod a small number', function () {
170
- const bn1 = new Bn(50)
171
- const bn2 = new Bn(47)
172
- const bn3 = bn1.mod(bn2)
173
- bn3.toString().should.equal('3')
174
- })
175
-
176
- it('should mod a small number', function () {
177
- const bn1 = new Bn(-50)
178
- const bn2 = new Bn(47)
179
- const bn3 = bn1.mod(bn2)
180
- bn3.toString().should.equal('-3')
181
- })
182
- })
183
-
184
- describe('#umod', function () {
185
- it('should mod a small number', function () {
186
- const bn1 = new Bn(50)
187
- const bn2 = new Bn(25)
188
- const bn3 = bn1.umod(bn2)
189
- bn3.toString().should.equal('0')
190
- })
191
-
192
- it('should mod a small number', function () {
193
- const bn1 = new Bn(50)
194
- const bn2 = new Bn(47)
195
- const bn3 = bn1.umod(bn2)
196
- bn3.toString().should.equal('3')
197
- })
198
- })
199
-
200
- describe('#invm', function () {
201
- it('should invm a small number', function () {
202
- const bn1 = new Bn(50)
203
- const bn2 = new Bn(25)
204
- const bn3 = bn1.invm(bn2)
205
- bn3.toString().should.equal('0')
206
- })
207
-
208
- it('should invm a small number', function () {
209
- const bn1 = new Bn(50)
210
- const bn2 = new Bn(47)
211
- const bn3 = bn1.invm(bn2)
212
- bn3.toString().should.equal('16')
213
- })
214
- })
215
-
216
- describe('#div', function () {
217
- it('should div a small number', function () {
218
- const bn1 = new Bn(50)
219
- const bn2 = new Bn(25)
220
- const bn3 = bn1.div(bn2)
221
- bn3.toString().should.equal('2')
222
- })
223
-
224
- it('should div a small number', function () {
225
- const bn1 = new Bn(50)
226
- const bn2 = new Bn(47)
227
- const bn3 = bn1.div(bn2)
228
- bn3.toString().should.equal('1')
229
- })
230
- })
231
-
232
- describe('#cmp', function () {
233
- it('should know A=B', function () {
234
- new Bn(5).cmp(5).should.equal(0)
235
- new Bn(5).cmp(4).should.equal(1)
236
- new Bn(5).cmp(6).should.equal(-1)
237
- })
238
- })
239
-
240
- describe('#eq', function () {
241
- it('should know A=B', function () {
242
- new Bn(5).eq(5).should.equal(true)
243
- new Bn(5).eq(4).should.equal(false)
244
- })
245
- })
246
-
247
- describe('#neq', function () {
248
- it('should know A!=B', function () {
249
- new Bn(5).neq(5).should.equal(false)
250
- new Bn(5).neq(4).should.equal(true)
251
- })
252
- })
253
-
254
- describe('#gt', function () {
255
- it('should say 1 is greater than 0', function () {
256
- const bn1 = new Bn(1)
257
- const bn0 = new Bn(0)
258
- bn1.gt(bn0).should.equal(true)
259
- })
260
-
261
- it('should say a big number is greater than a small big number', function () {
262
- const bn1 = new Bn('24023452345398529485723980457')
263
- const bn0 = new Bn('34098234283412341234049357')
264
- bn1.gt(bn0).should.equal(true)
265
- })
266
-
267
- it('should say a big number is great than a standard number', function () {
268
- const bn1 = new Bn('24023452345398529485723980457')
269
- const bn0 = new Bn(5)
270
- bn1.gt(bn0).should.equal(true)
271
- })
272
- })
273
-
274
- describe('#geq', function () {
275
- it('should know that A >= B', function () {
276
- new Bn(6).geq(5).should.equal(true)
277
- new Bn(5).geq(5).should.equal(true)
278
- new Bn(4).geq(5).should.equal(false)
279
- })
280
- })
281
-
282
- describe('#lt', function () {
283
- it('should know A < B', function () {
284
- new Bn(5).lt(6).should.equal(true)
285
- new Bn(5).lt(4).should.equal(false)
286
- })
287
- })
288
-
289
- describe('#leq', function () {
290
- it('should know A <= B', function () {
291
- new Bn(5).leq(6).should.equal(true)
292
- new Bn(5).leq(5).should.equal(true)
293
- new Bn(5).leq(4).should.equal(false)
294
- })
295
- })
296
-
297
- describe('#fromJSON', function () {
298
- it('should make Bn from a string', function () {
299
- new Bn()
300
- .fromJSON('5')
301
- .toString()
302
- .should.equal('5')
303
- })
304
- })
305
-
306
- describe('#toJSON', function () {
307
- it('should make string from a Bn', function () {
308
- new Bn(5).toJSON().should.equal('5')
309
- new Bn()
310
- .fromJSON('5')
311
- .toJSON()
312
- .should.equal('5')
313
- })
314
- })
315
-
316
- describe('#fromString', function () {
317
- it('should make Bn from a string', function () {
318
- new Bn()
319
- .fromString('5')
320
- .toString()
321
- .should.equal('5')
322
- })
323
- })
324
-
325
- describe('#toString', function () {
326
- it('should make a string', function () {
327
- new Bn(5).toString().should.equal('5')
328
- })
329
- })
330
-
331
- describe('@fromBuffer', function () {
332
- it('should work with big endian', function () {
333
- const bn = Bn.fromBuffer(Buffer.from('0001', 'hex'), { endian: 'big' })
334
- bn.toString().should.equal('1')
335
- })
336
-
337
- it('should work with big endian 256', function () {
338
- const bn = Bn.fromBuffer(Buffer.from('0100', 'hex'), { endian: 'big' })
339
- bn.toString().should.equal('256')
340
- })
341
-
342
- it('should work with little endian if we specify the size', function () {
343
- const bn = Bn.fromBuffer(Buffer.from('0100', 'hex'), {
344
- size: 2,
345
- endian: 'little'
346
- })
347
- bn.toString().should.equal('1')
348
- })
349
- })
350
-
351
- describe('#fromHex', function () {
352
- it('should create bn from known hex', function () {
353
- const bn = new Bn().fromHex('0100', { size: 2, endian: 'little' })
354
- bn.toString().should.equal('1')
355
- })
356
- })
357
-
358
- describe('#fromBuffer', function () {
359
- it('should work as a prototype method', function () {
360
- const bn = new Bn().fromBuffer(Buffer.from('0100', 'hex'), {
361
- size: 2,
362
- endian: 'little'
363
- })
364
- bn.toString().should.equal('1')
365
- })
366
- })
367
-
368
- describe('#toHex', function () {
369
- it('should create a hex string of 4 byte buffer', function () {
370
- const bn = new Bn(1)
371
- bn.toHex({ size: 4 }).should.equal('00000001')
372
- })
373
- })
374
-
375
- describe('#toBuffer', function () {
376
- it('should convert zero to empty buffer', function () {
377
- new Bn(0).toBuffer().length.should.equal(0)
378
- })
379
-
380
- it('should create a 4 byte buffer', function () {
381
- const bn = new Bn(1)
382
- bn
383
- .toBuffer({ size: 4 })
384
- .toString('hex')
385
- .should.equal('00000001')
386
- })
387
-
388
- it('should create a 4 byte buffer in little endian', function () {
389
- const bn = new Bn(1)
390
- bn
391
- .toBuffer({ size: 4, endian: 'little' })
392
- .toString('hex')
393
- .should.equal('01000000')
394
- })
395
-
396
- it('should create a 2 byte buffer even if you ask for a 1 byte', function () {
397
- const bn = new Bn('ff00', 16)
398
- bn
399
- .toBuffer({ size: 1 })
400
- .toString('hex')
401
- .should.equal('ff00')
402
- })
403
-
404
- it('should create a 4 byte buffer even if you ask for a 1 byte', function () {
405
- const bn = new Bn('ffffff00', 16)
406
- bn
407
- .toBuffer({ size: 4 })
408
- .toString('hex')
409
- .should.equal('ffffff00')
410
- })
411
- })
412
-
413
- describe('#toBits', function () {
414
- it('should convert these known Bns to bits', function () {
415
- new Bn()
416
- .fromHex('00')
417
- .toBits()
418
- .should.equal(0x00000000)
419
- new Bn()
420
- .fromHex('01')
421
- .toBits()
422
- .should.equal(0x01000001)
423
- new Bn()
424
- .fromHex('0101')
425
- .toBits()
426
- .should.equal(0x02000101)
427
- new Bn()
428
- .fromHex('010101')
429
- .toBits()
430
- .should.equal(0x03010101)
431
- new Bn()
432
- .fromHex('01010101')
433
- .toBits()
434
- .should.equal(0x04010101)
435
- new Bn()
436
- .fromHex('0101010101')
437
- .toBits()
438
- .should.equal(0x05010101)
439
- new Bn()
440
- .fromHex('010101010101')
441
- .toBits()
442
- .should.equal(0x06010101)
443
- new Bn()
444
- .fromNumber(-1)
445
- .toBits()
446
- .should.equal(0x01800001)
447
- })
448
- })
449
-
450
- describe('#fromBits', function () {
451
- it('should convert these known bits to Bns', function () {
452
- new Bn()
453
- .fromBits(0x01003456)
454
- .toHex()
455
- .should.equal('')
456
- new Bn()
457
- .fromBits(0x02003456)
458
- .toHex()
459
- .should.equal('34')
460
- new Bn()
461
- .fromBits(0x03003456)
462
- .toHex()
463
- .should.equal('3456')
464
- new Bn()
465
- .fromBits(0x04003456)
466
- .toHex()
467
- .should.equal('345600')
468
- new Bn()
469
- .fromBits(0x05003456)
470
- .toHex()
471
- .should.equal('34560000')
472
- new Bn()
473
- .fromBits(0x05f03456)
474
- .lt(0)
475
- .should.equal(true) // sign bit set
476
- ;(function () {
477
- new Bn().fromBits(0x05f03456, { strict: true }) // sign bit set
478
- }.should.throw('negative bit set'))
479
- new Bn()
480
- .fromBits(0x04923456)
481
- .lt(0)
482
- .should.equal(true)
483
- })
484
- })
485
-
486
- describe('#toSm', function () {
487
- it('should convert to Sm', function () {
488
- let buf
489
- buf = new Bn().toSm()
490
- buf.toString('hex').should.equal('')
491
- buf = new Bn(5).toSm()
492
- buf.toString('hex').should.equal('05')
493
- buf = new Bn(-5).toSm()
494
- buf.toString('hex').should.equal('85')
495
- buf = new Bn(128).toSm()
496
- buf.toString('hex').should.equal('0080')
497
- buf = new Bn(-128).toSm()
498
- buf.toString('hex').should.equal('8080')
499
- buf = new Bn(127).toSm()
500
- buf.toString('hex').should.equal('7f')
501
- buf = new Bn(-127).toSm()
502
- buf.toString('hex').should.equal('ff')
503
- buf = new Bn(128).toSm({ endian: 'little' })
504
- buf.toString('hex').should.equal('8000')
505
- buf = new Bn(-128).toSm({ endian: 'little' })
506
- buf.toString('hex').should.equal('8080')
507
- })
508
- })
509
-
510
- describe('#fromSm', function () {
511
- it('should convert from Sm', function () {
512
- let buf
513
- buf = Buffer.from([0])
514
- new Bn()
515
- .fromSm(buf)
516
- .cmp(0)
517
- .should.equal(0)
518
- buf = Buffer.from('05', 'hex')
519
- new Bn()
520
- .fromSm(buf)
521
- .cmp(5)
522
- .should.equal(0)
523
- buf = Buffer.from('85', 'hex')
524
- new Bn()
525
- .fromSm(buf)
526
- .cmp(-5)
527
- .should.equal(0)
528
- buf = Buffer.from('0080', 'hex')
529
- new Bn()
530
- .fromSm(buf)
531
- .cmp(128)
532
- .should.equal(0)
533
- buf = Buffer.from('8080', 'hex')
534
- new Bn()
535
- .fromSm(buf)
536
- .cmp(-128)
537
- .should.equal(0)
538
- buf = Buffer.from('8000', 'hex')
539
- new Bn()
540
- .fromSm(buf, { endian: 'little' })
541
- .cmp(128)
542
- .should.equal(0)
543
- buf = Buffer.from('8080', 'hex')
544
- new Bn()
545
- .fromSm(buf, { endian: 'little' })
546
- .cmp(-128)
547
- .should.equal(0)
548
- buf = Buffer.from('0080', 'hex') // negative zero
549
- new Bn()
550
- .fromSm(buf, { endian: 'little' })
551
- .cmp(0)
552
- .should.equal(0)
553
- })
554
- })
555
-
556
- describe('#toScriptNumBuffer', function () {
557
- it('should output a little endian Sm number', function () {
558
- const bn = new Bn(-23434234)
559
- bn
560
- .toScriptNumBuffer()
561
- .toString('hex')
562
- .should.equal(bn.toSm({ endian: 'little' }).toString('hex'))
563
- })
564
- })
565
-
566
- describe('#fromScriptNumBuffer', function () {
567
- it('should parse this normal number', function () {
568
- new Bn()
569
- .fromScriptNumBuffer(Buffer.from('01', 'hex'))
570
- .toNumber()
571
- .should.equal(1)
572
- new Bn()
573
- .fromScriptNumBuffer(Buffer.from('0080', 'hex'))
574
- .toNumber()
575
- .should.equal(0)
576
- new Bn()
577
- .fromScriptNumBuffer(Buffer.from('0180', 'hex'))
578
- .toNumber()
579
- .should.equal(-1)
580
- })
581
-
582
- it('should throw an error for a number over 4 bytes', function () {
583
- ;(function () {
584
- new Bn()
585
- .fromScriptNumBuffer(Buffer.from('8100000000', 'hex'))
586
- .toNumber()
587
- .should.equal(-1)
588
- }.should.throw('script number overflow'))
589
- })
590
-
591
- it('should throw an error for number that is not a minimal size representation', function () {
592
- // invalid
593
- ;(function () {
594
- new Bn().fromScriptNumBuffer(Buffer.from('80000000', 'hex'), true)
595
- }.should.throw('non-minimally encoded script number'))
596
- ;(function () {
597
- new Bn().fromScriptNumBuffer(Buffer.from('800000', 'hex'), true)
598
- }.should.throw('non-minimally encoded script number'))
599
- ;(function () {
600
- new Bn().fromScriptNumBuffer(Buffer.from('00', 'hex'), true)
601
- }.should.throw('non-minimally encoded script number'))
602
-
603
- // valid
604
- new Bn()
605
- .fromScriptNumBuffer(Buffer.from('8000', 'hex'), true)
606
- .toString()
607
- .should.equal('128')
608
- new Bn()
609
- .fromScriptNumBuffer(Buffer.from('0081', 'hex'), true)
610
- .toString()
611
- .should.equal('-256')
612
- new Bn()
613
- .fromScriptNumBuffer(Buffer.from('', 'hex'), true)
614
- .toString()
615
- .should.equal('0')
616
- new Bn()
617
- .fromScriptNumBuffer(Buffer.from('01', 'hex'), true)
618
- .toString()
619
- .should.equal('1')
620
-
621
- // invalid, but flag not set
622
- new Bn()
623
- .fromScriptNumBuffer(Buffer.from('00000000', 'hex'))
624
- .toString()
625
- .should.equal('0')
626
- })
627
- })
628
-
629
- describe('#fromNumber', function () {
630
- it('should convert from a number', function () {
631
- new Bn()
632
- .fromNumber(5)
633
- .toNumber()
634
- .should.equal(5)
635
- })
636
- })
637
-
638
- describe('#toNumber', function () {
639
- it('it should convert to a number', function () {
640
- new Bn(5).toNumber().should.equal(5)
641
- })
642
- })
643
- })