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/point.js DELETED
@@ -1,375 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import should from 'should'
4
- import { Point } from '../lib/point'
5
- import { Bn } from '../lib/bn'
6
-
7
- describe('Point', function () {
8
- it('should create a point', function () {
9
- let p = new Point()
10
- should.exist(p)
11
- p = new Point()
12
- should.exist(p)
13
- ;(p instanceof Point).should.equal(true)
14
- })
15
-
16
- it('should have the standard properties for a point', function () {
17
- const p = new Point()
18
- const props = ['curve', 'type', 'precomputed', 'x', 'y', 'inf']
19
-
20
- // all enumerable, own properties should be in prop
21
- for (const k in p) {
22
- if (Object.prototype.hasOwnProperty.call(p, k)) {
23
- props.indexOf(k).should.not.equal(-1)
24
- }
25
- }
26
-
27
- // all props should be properties of a point
28
- props.forEach(function (k) {
29
- ;(p[k] === undefined).should.equal(false)
30
- })
31
- })
32
-
33
- describe('#copyFrom', function () {
34
- it('should copy G', function () {
35
- const point = Point.getG()
36
- let point2
37
- ;(function () {
38
- point.copyFrom(point2)
39
- }.should.throw()) // point2 is not a Point yet
40
- const point3 = new Point()
41
- point.copyFrom(point3)
42
- point.toString().should.equal(point3.toString())
43
- })
44
- })
45
-
46
- describe('#toJSON', function () {
47
- it('should print G to JSON', function () {
48
- const G = Point.getG()
49
- const json = G.toJSON()
50
- json.x.should.equal(G.x.toString())
51
- json.y.should.equal(G.y.toString())
52
- })
53
- })
54
-
55
- describe('#fromJSON', function () {
56
- it('should recover G', function () {
57
- new Point()
58
- .fromJSON(Point.getG().toJSON())
59
- .eq(Point.getG())
60
- .should.equal(true)
61
- })
62
- })
63
-
64
- describe('#toString', function () {
65
- it('should convert G to a string', function () {
66
- const G = Point.getG()
67
- G.toString().should.equal(JSON.stringify(G.toJSON()))
68
- })
69
- })
70
-
71
- describe('#fromString', function () {
72
- it('should convert a json string to G', function () {
73
- const G = Point.getG()
74
- new Point()
75
- .fromString(G.toString())
76
- .eq(G)
77
- .should.equal(true)
78
- })
79
- })
80
-
81
- describe('#getX', function () {
82
- it('should return a Bn', function () {
83
- const p = new Point()
84
- ;(p.getX() instanceof Bn).should.equal(true)
85
- })
86
-
87
- it('should return 0', function () {
88
- const p = new Point()
89
- p
90
- .getX()
91
- .toString()
92
- .should.equal('0')
93
- })
94
-
95
- it('should be convertable to a buffer', function () {
96
- const p = new Point()
97
- p
98
- .getX()
99
- .toBuffer({ size: 32 })
100
- .length.should.equal(32)
101
- })
102
- })
103
-
104
- describe('#getY', function () {
105
- it('should return a Bn', function () {
106
- const p = new Point()
107
- ;(p.getY() instanceof Bn).should.equal(true)
108
- })
109
-
110
- it('should return 0', function () {
111
- const p = new Point()
112
- p
113
- .getY()
114
- .toString()
115
- .should.equal('0')
116
- })
117
-
118
- it('should be convertable to a buffer', function () {
119
- const p = new Point()
120
- p
121
- .getY()
122
- .toBuffer({ size: 32 })
123
- .length.should.equal(32)
124
- })
125
- })
126
-
127
- describe('#add', function () {
128
- it('should get back a point', function () {
129
- const p1 = Point.getG()
130
- const p2 = Point.getG()
131
- const p3 = p1.add(p2)
132
- ;(p3 instanceof Point).should.equal(true)
133
- })
134
-
135
- it('should accurately add g to itself', function () {
136
- const p1 = Point.getG()
137
- const p2 = Point.getG()
138
- const p3 = p1.add(p2)
139
- p3
140
- .getX()
141
- .toString()
142
- .should.equal(
143
- '89565891926547004231252920425935692360644145829622209833684329913297188986597'
144
- )
145
- p3
146
- .getY()
147
- .toString()
148
- .should.equal(
149
- '12158399299693830322967808612713398636155367887041628176798871954788371653930'
150
- )
151
- })
152
- })
153
-
154
- describe('#mul', function () {
155
- it('should get back a point', function () {
156
- const g = Point.getG()
157
- const b = g.mul(new Bn(2))
158
- ;(b instanceof Point).should.equal(true)
159
- })
160
-
161
- it('should accurately multiply g by 2', function () {
162
- const g = Point.getG()
163
- const b = g.mul(new Bn(2))
164
- b
165
- .getX()
166
- .toString()
167
- .should.equal(
168
- '89565891926547004231252920425935692360644145829622209833684329913297188986597'
169
- )
170
- b
171
- .getY()
172
- .toString()
173
- .should.equal(
174
- '12158399299693830322967808612713398636155367887041628176798871954788371653930'
175
- )
176
- })
177
-
178
- it('should accurately multiply g by n-1', function () {
179
- const g = Point.getG()
180
- const n = Point.getN()
181
- const b = g.mul(n.sub(1))
182
- b
183
- .getX()
184
- .toString()
185
- .should.equal(
186
- '55066263022277343669578718895168534326250603453777594175500187360389116729240'
187
- )
188
- b
189
- .getY()
190
- .toString()
191
- .should.equal(
192
- '83121579216557378445487899878180864668798711284981320763518679672151497189239'
193
- )
194
- })
195
-
196
- it('should accurate multiply these problematic values related to a bug in bn.js', function () {
197
- // see these discussions:
198
- // https://github.com/bitpay/bitcore/pull/894
199
- // https://github.com/indutny/elliptic/issues/17
200
- // https://github.com/indutny/elliptic/pull/18
201
- // https://github.com/indutny/elliptic/pull/19
202
- // https://github.com/indutny/bn.js/commit/3557d780b07ed0ed301e128f326f83c2226fb679
203
- ;(function () {
204
- const nhex =
205
- '6d1229a6b24c2e775c062870ad26bc261051e0198c67203167273c7c62538846'
206
- const n = new Bn(nhex, 16)
207
- const g1 = Point.getG() // precomputed g
208
- const g2 = new Point().fromX(
209
- false,
210
- new Bn(
211
- '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
212
- 16
213
- )
214
- ) // non-precomputed g
215
- const p1 = g1.mul(n)
216
- const p2 = g2.mul(n)
217
- const pxhex =
218
- 'd6106302d2698d6a41e9c9a114269e7be7c6a0081317de444bb2980bf9265a01'
219
- const pyhex =
220
- 'e05fb262e64b108991a29979809fcef9d3e70cafceb3248c922c17d83d66bc9d'
221
- p1
222
- .getX()
223
- .toBuffer()
224
- .toString('hex')
225
- .should.equal(pxhex)
226
- p1
227
- .getY()
228
- .toBuffer()
229
- .toString('hex')
230
- .should.equal(pyhex)
231
- p2
232
- .getX()
233
- .toBuffer()
234
- .toString('hex')
235
- .should.equal(pxhex)
236
- p2
237
- .getY()
238
- .toBuffer()
239
- .toString('hex')
240
- .should.equal(pyhex)
241
- })()
242
- ;(function () {
243
- const nhex =
244
- 'f2cc9d2b008927db94b89e04e2f6e70c180e547b3e5e564b06b8215d1c264b53'
245
- const n = new Bn(nhex, 16)
246
- const g1 = Point.getG() // precomputed g
247
- const g2 = new Point().fromX(
248
- false,
249
- new Bn(
250
- '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
251
- 16
252
- )
253
- ) // non-precomputed g
254
- const p1 = g1.mul(n)
255
- const p2 = g2.mul(n)
256
- const pxhex =
257
- 'e275faa35bd1e88f5df6e8f9f6edb93bdf1d65f4915efc79fd7a726ec0c21700'
258
- const pyhex =
259
- '367216cb35b086e6686d69dddd822a8f4d52eb82ac5d9de18fdcd9bf44fa7df7'
260
- p1
261
- .getX()
262
- .toBuffer()
263
- .toString('hex')
264
- .should.equal(pxhex)
265
- p1
266
- .getY()
267
- .toBuffer()
268
- .toString('hex')
269
- .should.equal(pyhex)
270
- p2
271
- .getX()
272
- .toBuffer()
273
- .toString('hex')
274
- .should.equal(pxhex)
275
- p2
276
- .getY()
277
- .toBuffer()
278
- .toString('hex')
279
- .should.equal(pyhex)
280
- })()
281
- })
282
- })
283
-
284
- describe('#mulAdd', function () {
285
- it('should get back a point', function () {
286
- const p1 = Point.getG()
287
- const bn1 = new Bn(5)
288
- const p2 = Point.getG().add(p1)
289
- const bn2 = new Bn(6)
290
- p1
291
- .mulAdd(bn1, p2, bn2)
292
- .getX()
293
- .toString()
294
- .should.equal(
295
- p1
296
- .mul(bn1)
297
- .add(p2.mul(bn2))
298
- .getX()
299
- .toString()
300
- )
301
- p1
302
- .mulAdd(bn1, p2, bn2)
303
- .getY()
304
- .toString()
305
- .should.equal(
306
- p1
307
- .mul(bn1)
308
- .add(p2.mul(bn2))
309
- .getY()
310
- .toString()
311
- )
312
- })
313
- })
314
-
315
- describe('@getN', function () {
316
- it('should return n', function () {
317
- const bn = Point.getN()
318
- ;(bn instanceof Bn).should.equal(true)
319
- })
320
- })
321
-
322
- describe('@fromX', function () {
323
- it('should return g', function () {
324
- const g = Point.getG()
325
- const p = Point.fromX(false, g.getX())
326
- g.eq(p).should.equal(true)
327
- })
328
- })
329
-
330
- describe('#fromX', function () {
331
- it('should return g', function () {
332
- const g = Point.getG()
333
- const p = new Point().fromX(false, g.getX())
334
- g.eq(p).should.equal(true)
335
- })
336
- })
337
-
338
- describe('#validate', function () {
339
- it('should validate this valid point', function () {
340
- const x = new Bn().fromBuffer(
341
- Buffer.from(
342
- 'ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2',
343
- 'hex'
344
- )
345
- )
346
- const y = new Bn().fromBuffer(
347
- Buffer.from(
348
- '4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380',
349
- 'hex'
350
- )
351
- )
352
- const p = new Point(x, y)
353
- should.exist(p.validate())
354
- })
355
-
356
- it('should invalidate this invalid point', function () {
357
- const x = new Bn().fromBuffer(
358
- Buffer.from(
359
- 'ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2',
360
- 'hex'
361
- )
362
- )
363
- const y = new Bn().fromBuffer(
364
- Buffer.from(
365
- '0000000000000000000000000000000000000000000000000000000000000000',
366
- 'hex'
367
- )
368
- )
369
- const p = new Point(x, y)
370
- ;(function () {
371
- p.validate()
372
- }.should.throw('Invalid y value of public key'))
373
- })
374
- })
375
- })
package/test/priv-key.js DELETED
@@ -1,264 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { PrivKey } from '../lib/priv-key'
4
- import { Bn } from '../lib/bn'
5
- import { Point } from '../lib/point'
6
- import should from 'should'
7
-
8
- describe('PrivKey', function () {
9
- const hex = '96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a'
10
- const buf = Buffer.from(hex, 'hex')
11
- const enctestnet = 'cSdkPxkAjA4HDr5VHgsebAPDEh9Gyub4HK8UJr2DFGGqKKy4K5sG'
12
- const enctu = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu'
13
- const encmainnet = 'L2Gkw3kKJ6N24QcDuH4XDqt9cTqsKTVNDGz1CRZhk9cq4auDUbJy'
14
- const encmu = '5JxgQaFM1FMd38cd14e3mbdxsdSa9iM2BV6DHBYsvGzxkTNQ7Un'
15
-
16
- it('should satisfy these basic API features', function () {
17
- let privKey = new PrivKey()
18
- should.exist(privKey)
19
- privKey = new PrivKey()
20
- should.exist(privKey)
21
-
22
- new PrivKey().constructor.should.equal(new PrivKey().constructor)
23
- new PrivKey.Testnet().constructor.should.equal(
24
- new PrivKey.Testnet().constructor
25
- )
26
-
27
- PrivKey.Testnet.fromRandom()
28
- .toString()[0]
29
- .should.equal('c')
30
- })
31
-
32
- it('should create a 0 private key with this convenience method', function () {
33
- const bn = new Bn(0)
34
- const privKey = new PrivKey(bn)
35
- privKey.bn.toString().should.equal(bn.toString())
36
- })
37
-
38
- it('should create a mainnet private key', function () {
39
- const privKey = new PrivKey(Bn.fromBuffer(buf), true)
40
- privKey.toString().should.equal(encmainnet)
41
- })
42
-
43
- it('should create an uncompressed testnet private key', function () {
44
- const privKey = new PrivKey.Testnet(Bn.fromBuffer(buf), false)
45
- privKey.toString().should.equal(enctu)
46
- })
47
-
48
- it('should create an uncompressed mainnet private key', function () {
49
- const privKey = new PrivKey(Bn.fromBuffer(buf), false)
50
- privKey.toString().should.equal(encmu)
51
- })
52
-
53
- describe('#fromObject', function () {
54
- it('should set bn', function () {
55
- should.exist(new PrivKey().fromObject({ bn: Bn.fromBuffer(buf) }).bn)
56
- })
57
- })
58
-
59
- describe('#fromJSON', function () {
60
- it('should input this address correctly', function () {
61
- const privKey = new PrivKey()
62
- privKey.fromString(encmu)
63
- const privKey2 = new PrivKey()
64
- privKey2.fromJSON(privKey.toHex())
65
- privKey2.toWif().should.equal(encmu)
66
- })
67
- })
68
-
69
- describe('#toString', function () {
70
- it('should output this address correctly', function () {
71
- const privKey = new PrivKey()
72
- privKey.fromString(encmu)
73
- privKey.toString().should.equal(encmu)
74
- })
75
- })
76
-
77
- describe('#fromRandom', function () {
78
- it('should set bn gt 0 and lt n, and should be compressed', function () {
79
- const privKey = new PrivKey().fromRandom()
80
- privKey.bn.gt(new Bn(0)).should.equal(true)
81
- privKey.bn.lt(Point.getN()).should.equal(true)
82
- privKey.compressed.should.equal(true)
83
- })
84
- })
85
-
86
- describe('@fromRandom', function () {
87
- it('should set bn gt 0 and lt n, and should be compressed', function () {
88
- const privKey = PrivKey.fromRandom()
89
- privKey.bn.gt(new Bn(0)).should.equal(true)
90
- privKey.bn.lt(Point.getN()).should.equal(true)
91
- privKey.compressed.should.equal(true)
92
- })
93
- })
94
-
95
- describe('#toHex', function () {
96
- it('should return a hex string', function () {
97
- const privKey = new PrivKey().fromBn(new Bn(5))
98
- privKey
99
- .toHex()
100
- .should.equal(
101
- '80000000000000000000000000000000000000000000000000000000000000000501'
102
- )
103
- })
104
- })
105
-
106
- describe('#toBuffer', function () {
107
- it('should return a buffer', function () {
108
- const privKey = new PrivKey().fromBn(new Bn(5))
109
- privKey
110
- .toBuffer()
111
- .toString('hex')
112
- .should.equal(
113
- '80000000000000000000000000000000000000000000000000000000000000000501'
114
- )
115
- })
116
- })
117
-
118
- describe('#fromHex', function () {
119
- it('should return a hex string', function () {
120
- const privKey = new PrivKey().fromHex(
121
- '80000000000000000000000000000000000000000000000000000000000000000501'
122
- )
123
- privKey
124
- .toHex()
125
- .should.equal(
126
- '80000000000000000000000000000000000000000000000000000000000000000501'
127
- )
128
- })
129
- })
130
-
131
- describe('#fromBuffer', function () {
132
- it('should return a buffer', function () {
133
- const privKey = new PrivKey().fromBuffer(
134
- Buffer.from(
135
- '80000000000000000000000000000000000000000000000000000000000000000501',
136
- 'hex'
137
- )
138
- )
139
- privKey
140
- .toBuffer()
141
- .toString('hex')
142
- .should.equal(
143
- '80000000000000000000000000000000000000000000000000000000000000000501'
144
- )
145
- })
146
-
147
- it('should throw an error if buffer is wrong length', function () {
148
- ;(function () {
149
- new PrivKey().fromBuffer(
150
- Buffer.from(
151
- '8000000000000000000000000000000000000000000000000000000000000000050100',
152
- 'hex'
153
- )
154
- )
155
- }.should.throw(
156
- 'Length of privKey buffer must be 33 (uncompressed pubKey) or 34 (compressed pubKey)'
157
- ))
158
- ;(function () {
159
- new PrivKey().fromBuffer(
160
- Buffer.from(
161
- '8000000000000000000000000000000000000000000000000000000000000005',
162
- 'hex'
163
- )
164
- )
165
- }.should.throw(
166
- 'Length of privKey buffer must be 33 (uncompressed pubKey) or 34 (compressed pubKey)'
167
- ))
168
- })
169
-
170
- it('should throw an error if buffer has wrong versionByteNum byte', function () {
171
- ;(function () {
172
- new PrivKey().fromBuffer(
173
- Buffer.from(
174
- '90000000000000000000000000000000000000000000000000000000000000000501',
175
- 'hex'
176
- )
177
- )
178
- }.should.throw('Invalid versionByteNum byte'))
179
- })
180
- })
181
-
182
- describe('#toBn', function () {
183
- it('should return a bn', function () {
184
- const privKey = new PrivKey().fromBn(new Bn(5))
185
- privKey
186
- .toBn()
187
- .eq(new Bn(5))
188
- .should.equal(true)
189
- })
190
- })
191
-
192
- describe('#fromBn', function () {
193
- it('should create a privKey from a bignum', function () {
194
- const privKey = new PrivKey().fromBn(new Bn(5))
195
- privKey.bn.toString().should.equal('5')
196
- })
197
- })
198
-
199
- describe('@fromBn', function () {
200
- it('should create a privKey from a bignum', function () {
201
- const privKey = PrivKey.fromBn(new Bn(5))
202
- privKey.bn.toString().should.equal('5')
203
- })
204
- })
205
-
206
- describe('#validate', function () {
207
- it('should unvalidate these privKeys', function () {
208
- const privKey = new PrivKey()
209
- privKey.compressed = true
210
- privKey.bn = Point.getN()
211
- ;(function () {
212
- privKey.validate()
213
- }.should.throw('Number must be less than N'))
214
- privKey.bn = Point.getN().sub(1)
215
- privKey.compressed = undefined
216
- ;(function () {
217
- privKey.validate()
218
- }.should.throw(
219
- 'Must specify whether the corresponding public key is compressed or not (true or false)'
220
- ))
221
- privKey.compressed = true
222
- privKey.validate().should.equal(privKey)
223
- })
224
- })
225
-
226
- describe('#fromWif', function () {
227
- it('should parse this compressed testnet address correctly', function () {
228
- const privKey = new PrivKey()
229
- privKey.fromWif(encmainnet)
230
- privKey.toWif().should.equal(encmainnet)
231
- })
232
- })
233
-
234
- describe('@fromWif', function () {
235
- it('should parse this compressed testnet address correctly', function () {
236
- const privKey = PrivKey.fromWif(encmainnet)
237
- privKey.toWif().should.equal(encmainnet)
238
- })
239
- })
240
-
241
- describe('#toWif', function () {
242
- it('should parse this compressed testnet address correctly', function () {
243
- const privKey = new PrivKey.Testnet()
244
- privKey.fromWif(enctestnet)
245
- privKey.toWif().should.equal(enctestnet)
246
- })
247
- })
248
-
249
- describe('#fromString', function () {
250
- it('should parse this uncompressed testnet address correctly', function () {
251
- const privKey = new PrivKey.Testnet()
252
- privKey.fromString(enctu)
253
- privKey.toWif().should.equal(enctu)
254
- })
255
- })
256
-
257
- describe('#toString', function () {
258
- it('should parse this uncompressed mainnet address correctly', function () {
259
- const privKey = new PrivKey()
260
- privKey.fromString(encmu)
261
- privKey.toString().should.equal(encmu)
262
- })
263
- })
264
- })