bsv2 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (241) hide show
  1. package/dist/bsv2.mjs +1 -1
  2. package/dist/bsv2.umd.js +1 -1
  3. package/package.json +7 -3
  4. package/.babel.test +0 -10
  5. package/.editorconfig +0 -4
  6. package/.env.defaults +0 -1
  7. package/.env.schema +0 -1
  8. package/.husky/pre-commit +0 -1
  9. package/.mocharc.yml +0 -4
  10. package/.nvmrc +0 -1
  11. package/.travis.yml +0 -7
  12. package/babel.config.cjs +0 -44
  13. package/benchmark.js +0 -42
  14. package/bin/bsv.js +0 -11
  15. package/bin/network.js +0 -93
  16. package/build/index.html +0 -20
  17. package/doc/assets/anchor.js +0 -350
  18. package/doc/assets/bass-addons.css +0 -12
  19. package/doc/assets/bass.css +0 -544
  20. package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
  21. package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
  22. package/doc/assets/fonts/LICENSE.txt +0 -93
  23. package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
  24. package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
  25. package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
  26. package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
  27. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
  28. package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
  29. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
  30. package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
  31. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
  32. package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
  33. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
  34. package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
  35. package/doc/assets/fonts/source-code-pro.css +0 -23
  36. package/doc/assets/github.css +0 -123
  37. package/doc/assets/site.js +0 -168
  38. package/doc/assets/split.css +0 -15
  39. package/doc/assets/split.js +0 -782
  40. package/doc/assets/style.css +0 -147
  41. package/doc/index.html +0 -9461
  42. package/entry.js +0 -81
  43. package/eslint.config.mjs +0 -8
  44. package/index.js +0 -98
  45. package/karma.conf.ts +0 -77
  46. package/lib/ach.js +0 -59
  47. package/lib/address.js +0 -266
  48. package/lib/aes.js +0 -57
  49. package/lib/aescbc.js +0 -38
  50. package/lib/base-58-check.js +0 -83
  51. package/lib/base-58.js +0 -59
  52. package/lib/bip-32.js +0 -472
  53. package/lib/bip-39-en-wordlist.js +0 -2054
  54. package/lib/bip-39-en.js +0 -10
  55. package/lib/bip-39-es-wordlist.js +0 -2055
  56. package/lib/bip-39-es.js +0 -10
  57. package/lib/bip-39-jp-wordlist.js +0 -2053
  58. package/lib/bip-39-jp.js +0 -10
  59. package/lib/bip-39-words.js +0 -5
  60. package/lib/bip-39.js +0 -286
  61. package/lib/block-header.js +0 -79
  62. package/lib/block.js +0 -132
  63. package/lib/bn.js +0 -462
  64. package/lib/br.js +0 -172
  65. package/lib/bsm.js +0 -127
  66. package/lib/bw.js +0 -188
  67. package/lib/cbc.js +0 -140
  68. package/lib/cmp.js +0 -37
  69. package/lib/config.js +0 -50
  70. package/lib/constants.js +0 -150
  71. package/lib/ecdsa.js +0 -496
  72. package/lib/ecies.js +0 -151
  73. package/lib/hash-cache.js +0 -41
  74. package/lib/hash.js +0 -191
  75. package/lib/interp.js +0 -2017
  76. package/lib/inv.js +0 -49
  77. package/lib/key-pair.js +0 -133
  78. package/lib/mainnet-address.js +0 -5
  79. package/lib/mainnet-bip-32.js +0 -5
  80. package/lib/mainnet-key-pair.js +0 -5
  81. package/lib/mainnet-priv-key.js +0 -5
  82. package/lib/merkle.js +0 -92
  83. package/lib/msg.js +0 -184
  84. package/lib/op-code.js +0 -400
  85. package/lib/point.js +0 -134
  86. package/lib/priv-key.js +0 -157
  87. package/lib/pub-key.js +0 -230
  88. package/lib/random.js +0 -28
  89. package/lib/script.js +0 -828
  90. package/lib/sig-operations.js +0 -120
  91. package/lib/sig.js +0 -399
  92. package/lib/struct.js +0 -392
  93. package/lib/testnet-address.js +0 -5
  94. package/lib/testnet-bip-32.js +0 -5
  95. package/lib/testnet-key-pair.js +0 -5
  96. package/lib/testnet-priv-key.js +0 -5
  97. package/lib/tx-builder.js +0 -593
  98. package/lib/tx-in.js +0 -172
  99. package/lib/tx-out-map.js +0 -57
  100. package/lib/tx-out.js +0 -73
  101. package/lib/tx-verifier.js +0 -209
  102. package/lib/tx.js +0 -430
  103. package/lib/var-int.js +0 -73
  104. package/lib/version.js +0 -92
  105. package/lib/workers-result.js +0 -65
  106. package/lib/workers.js +0 -77
  107. package/load-babel.js +0 -5
  108. package/mtest/test.html +0 -17
  109. package/mtest/test.js +0 -2
  110. package/mtest/test.mjs +0 -2
  111. package/mtest/webpack.config.js +0 -115
  112. package/out/Sig.html +0 -541
  113. package/out/bip-32.js.html +0 -523
  114. package/out/bip-39.js.html +0 -336
  115. package/out/block.js.html +0 -183
  116. package/out/bn.js.html +0 -512
  117. package/out/ecdsa.js.html +0 -546
  118. package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
  119. package/out/fonts/OpenSans-Bold-webfont.svg +0 -1830
  120. package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
  121. package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  122. package/out/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
  123. package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  124. package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
  125. package/out/fonts/OpenSans-Italic-webfont.svg +0 -1830
  126. package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
  127. package/out/fonts/OpenSans-Light-webfont.eot +0 -0
  128. package/out/fonts/OpenSans-Light-webfont.svg +0 -1831
  129. package/out/fonts/OpenSans-Light-webfont.woff +0 -0
  130. package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  131. package/out/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
  132. package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  133. package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
  134. package/out/fonts/OpenSans-Regular-webfont.svg +0 -1831
  135. package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
  136. package/out/global.html +0 -220
  137. package/out/index.html +0 -128
  138. package/out/interp.js.html +0 -2064
  139. package/out/merkle.js.html +0 -143
  140. package/out/msg.js.html +0 -235
  141. package/out/priv-key.js.html +0 -208
  142. package/out/pub-key.js.html +0 -281
  143. package/out/script.js.html +0 -876
  144. package/out/scripts/linenumber.js +0 -25
  145. package/out/scripts/prettify/Apache-License-2.0.txt +0 -202
  146. package/out/scripts/prettify/lang-css.js +0 -2
  147. package/out/scripts/prettify/prettify.js +0 -28
  148. package/out/sig-operations.js.html +0 -171
  149. package/out/sig.js.html +0 -450
  150. package/out/struct.js.html +0 -440
  151. package/out/styles/jsdoc-default.css +0 -358
  152. package/out/styles/prettify-jsdoc.css +0 -111
  153. package/out/styles/prettify-tomorrow.css +0 -132
  154. package/out/tx-builder.js.html +0 -644
  155. package/out/tx-in.js.html +0 -223
  156. package/out/tx-verifier.js.html +0 -260
  157. package/out/tx.js.html +0 -481
  158. package/test/ach.js +0 -172
  159. package/test/address.js +0 -464
  160. package/test/aes.js +0 -96
  161. package/test/aescbc.js +0 -65
  162. package/test/base-58-check.js +0 -128
  163. package/test/base-58.js +0 -113
  164. package/test/bip-32.js +0 -721
  165. package/test/bip-39.js +0 -282
  166. package/test/block-header.js +0 -152
  167. package/test/block.js +0 -243
  168. package/test/bn.js +0 -643
  169. package/test/br.js +0 -353
  170. package/test/bsm.js +0 -141
  171. package/test/bw.js +0 -292
  172. package/test/cbc.js +0 -335
  173. package/test/cmp.js +0 -56
  174. package/test/data/bip69.json +0 -215
  175. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  176. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  177. package/test/data/bitcoind/blocks.json +0 -27
  178. package/test/data/bitcoind/script_tests.json +0 -2244
  179. package/test/data/bitcoind/sig_canonical.json +0 -7
  180. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  181. package/test/data/bitcoind/tx_invalid.json +0 -177
  182. package/test/data/bitcoind/tx_valid.json +0 -224
  183. package/test/data/blk86756-testnet.dat +0 -0
  184. package/test/data/blk86756-testnet.js +0 -12
  185. package/test/data/blk86756-testnet.json +0 -684
  186. package/test/data/ecdsa.json +0 -230
  187. package/test/data/messages.json +0 -22
  188. package/test/data/sighash.json +0 -1004
  189. package/test/data/tx_creation.json +0 -85
  190. package/test/ecdsa.js +0 -748
  191. package/test/ecies.js +0 -238
  192. package/test/global.js +0 -13
  193. package/test/hash-cache.js +0 -52
  194. package/test/hash.js +0 -354
  195. package/test/index.js +0 -10
  196. package/test/interp.js +0 -512
  197. package/test/inv.js +0 -65
  198. package/test/key-pair.js +0 -243
  199. package/test/merkle.js +0 -179
  200. package/test/msg.js +0 -295
  201. package/test/op-code.js +0 -79
  202. package/test/point.js +0 -375
  203. package/test/priv-key.js +0 -264
  204. package/test/pub-key.js +0 -549
  205. package/test/random.js +0 -31
  206. package/test/script.js +0 -1340
  207. package/test/sig-operations.js +0 -149
  208. package/test/sig.js +0 -662
  209. package/test/struct.js +0 -552
  210. package/test/tx-builder.js +0 -2104
  211. package/test/tx-in.js +0 -198
  212. package/test/tx-out-map.js +0 -104
  213. package/test/tx-out.js +0 -162
  214. package/test/tx-verifier.js +0 -246
  215. package/test/tx.js +0 -539
  216. package/test/var-int.js +0 -132
  217. package/test/vectors/aes.json +0 -10392
  218. package/test/vectors/aescbc.json +0 -620
  219. package/test/vectors/bip39.json +0 -502
  220. package/test/vectors/bip69.json +0 -215
  221. package/test/vectors/bitcoin-abc/script_tests.json +0 -48
  222. package/test/vectors/bitcoin-abc/sighash.json +0 -7004
  223. package/test/vectors/bitcoin-sv/script_tests.json +0 -248
  224. package/test/vectors/bitcoind/script_invalid.json +0 -1151
  225. package/test/vectors/bitcoind/script_valid.json +0 -1462
  226. package/test/vectors/bitcoind/sighash.json +0 -3503
  227. package/test/vectors/bitcoind/tx_invalid.json +0 -883
  228. package/test/vectors/bitcoind/tx_valid.json +0 -1150
  229. package/test/vectors/coolest-tx-ever-sent.json +0 -8
  230. package/test/vectors/ecdsa.json +0 -300
  231. package/test/vectors/hash.json +0 -6133
  232. package/test/vectors/kdf.json +0 -47
  233. package/test/vectors/largesttx.json +0 -5
  234. package/test/vectors/largesttxblock.json +0 -6
  235. package/test/vectors/sig.json +0 -79
  236. package/test/vectors/sighash-single-bug.json +0 -8
  237. package/test/version.js +0 -81
  238. package/test/workers-result.js +0 -89
  239. package/test/workers.js +0 -91
  240. package/test-index.js +0 -3
  241. package/vite.config.js +0 -36
package/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
- })