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/ecdsa.js DELETED
@@ -1,748 +0,0 @@
1
- /* global describe,it */
2
- 'use strict'
3
- import { Bn } from '../lib/bn'
4
- import { Ecdsa } from '../lib/ecdsa'
5
- import { Hash } from '../lib/hash'
6
- import { KeyPair } from '../lib/key-pair'
7
- import { PrivKey } from '../lib/priv-key'
8
- import { PubKey } from '../lib/pub-key'
9
- import { Sig } from '../lib/sig'
10
- import { Point } from '../lib/point'
11
- import should from 'should'
12
- import vectors from './vectors/ecdsa.json'
13
-
14
- describe('Ecdsa', function () {
15
- it('should create a blank ecdsa', function () {
16
- const ecdsa = new Ecdsa()
17
- should.exist(ecdsa)
18
- })
19
-
20
- const ecdsa = new Ecdsa()
21
- ecdsa.hashBuf = Hash.sha256(Buffer.from('test data'))
22
- ecdsa.keyPair = new KeyPair()
23
- ecdsa.keyPair.privKey = new PrivKey().fromBn(
24
- new Bn().fromBuffer(
25
- Buffer.from(
26
- 'fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e',
27
- 'hex'
28
- )
29
- )
30
- )
31
- ecdsa.keyPair.pubKey = new PubKey().fromObject({
32
- point: new Point(
33
- new Bn().fromBuffer(
34
- Buffer.from(
35
- 'ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2',
36
- 'hex'
37
- )
38
- ),
39
- new Bn().fromBuffer(
40
- Buffer.from(
41
- '4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380',
42
- 'hex'
43
- )
44
- )
45
- )
46
- })
47
-
48
- describe('#fromObject', function () {
49
- it('should set hashBuf', function () {
50
- should.exist(new Ecdsa().fromObject({ hashBuf: ecdsa.hashBuf }).hashBuf)
51
- })
52
- })
53
-
54
- describe('#toJSON', function () {
55
- it('should return json', function () {
56
- const json = ecdsa.toJSON()
57
- should.exist(json.keyPair)
58
- should.exist(json.hashBuf)
59
- })
60
- })
61
-
62
- describe('#fromJSON', function () {
63
- it('should convert from json', function () {
64
- const json = ecdsa.toJSON()
65
- const ecdsa2 = new Ecdsa().fromJSON(json)
66
- should.exist(ecdsa2.keyPair)
67
- should.exist(ecdsa2.hashBuf)
68
- })
69
- })
70
-
71
- describe('#toBuffer', function () {
72
- it('should return buffer', function () {
73
- const buf = ecdsa.toBuffer()
74
- Buffer.isBuffer(buf).should.equal(true)
75
- })
76
- })
77
-
78
- describe('#fromBuffer', function () {
79
- it('should return from buffer', function () {
80
- const buf = ecdsa.toBuffer()
81
- const ecdsa2 = new Ecdsa().fromBuffer(buf)
82
- should.exist(ecdsa2.keyPair)
83
- should.exist(ecdsa2.hashBuf)
84
- })
85
- })
86
-
87
- describe('#calcrecovery', function () {
88
- it('should calculate pubKey recovery number', function () {
89
- ecdsa.randomK()
90
- ecdsa.sign()
91
- ecdsa.calcrecovery()
92
- should.exist(ecdsa.sig.recovery)
93
- })
94
-
95
- it('should calculate this known pubKey recovery number', function () {
96
- const hashBuf = Hash.sha256(Buffer.from('some data'))
97
- const r = new Bn(
98
- '71706645040721865894779025947914615666559616020894583599959600180037551395766',
99
- 10
100
- )
101
- const s = new Bn(
102
- '109412465507152403114191008482955798903072313614214706891149785278625167723646',
103
- 10
104
- )
105
- const ecdsa = new Ecdsa()
106
- ecdsa.keyPair = new KeyPair().fromPrivKey(
107
- new PrivKey().fromBn(
108
- new Bn().fromBuffer(Hash.sha256(Buffer.from('test')))
109
- )
110
- )
111
- ecdsa.hashBuf = hashBuf
112
- ecdsa.sig = Sig.fromObject({ r: r, s: s })
113
-
114
- ecdsa.calcrecovery()
115
- ecdsa.sig.recovery.should.equal(1)
116
- })
117
-
118
- it('should do a round trip with signature parsing', function () {
119
- ecdsa.calcrecovery()
120
- const pubKey = ecdsa.keyPair.pubKey
121
- let sig = ecdsa.sig
122
- const hashBuf = ecdsa.hashBuf
123
- Ecdsa.sig2PubKey(sig, hashBuf)
124
- .toHex()
125
- .should.equal(pubKey.toHex())
126
-
127
- sig = sig.fromCompact(sig.toCompact())
128
- Ecdsa.sig2PubKey(sig, hashBuf)
129
- .toHex()
130
- .should.equal(pubKey.toHex())
131
- })
132
- })
133
-
134
- describe('#asyncCalcrecovery', function () {
135
- it('should calculate pubKey recovery number', async function () {
136
- ecdsa.randomK()
137
- ecdsa.sign()
138
- await ecdsa.asyncCalcrecovery()
139
- should.exist(ecdsa.sig.recovery)
140
- })
141
-
142
- it('should calculate this known pubKey recovery number', async function () {
143
- const hashBuf = Hash.sha256(Buffer.from('some data'))
144
- const r = new Bn(
145
- '71706645040721865894779025947914615666559616020894583599959600180037551395766',
146
- 10
147
- )
148
- const s = new Bn(
149
- '109412465507152403114191008482955798903072313614214706891149785278625167723646',
150
- 10
151
- )
152
- const ecdsa = new Ecdsa()
153
- ecdsa.keyPair = new KeyPair().fromPrivKey(
154
- new PrivKey().fromBn(
155
- new Bn().fromBuffer(Hash.sha256(Buffer.from('test')))
156
- )
157
- )
158
- ecdsa.hashBuf = hashBuf
159
- ecdsa.sig = Sig.fromObject({ r: r, s: s })
160
-
161
- await ecdsa.asyncCalcrecovery()
162
- ecdsa.sig.recovery.should.equal(1)
163
- })
164
-
165
- it('should do a round trip with signature parsing', async function () {
166
- await ecdsa.asyncCalcrecovery()
167
- const pubKey = ecdsa.keyPair.pubKey
168
- let sig = ecdsa.sig
169
- const hashBuf = ecdsa.hashBuf
170
- Ecdsa.sig2PubKey(sig, hashBuf)
171
- .toHex()
172
- .should.equal(pubKey.toHex())
173
-
174
- sig = sig.fromCompact(sig.toCompact())
175
- Ecdsa.sig2PubKey(sig, hashBuf)
176
- .toHex()
177
- .should.equal(pubKey.toHex())
178
- })
179
- })
180
-
181
- describe('@calcrecovery', function () {
182
- it('should calculate pubKey recovery number same as #calcrecovery', function () {
183
- ecdsa.randomK()
184
- ecdsa.sign()
185
- const sig1 = ecdsa.calcrecovery().sig
186
- const sig2 = Ecdsa.calcrecovery(
187
- ecdsa.sig,
188
- ecdsa.keyPair.pubKey,
189
- ecdsa.hashBuf
190
- )
191
- Buffer.compare(sig1.toCompact(), sig2.toCompact()).should.equal(0)
192
- })
193
-
194
- it('should calulate this known pubKey recovery number same as #calcrecovery', function () {
195
- const hashBuf = Hash.sha256(Buffer.from('some data'))
196
- const r = new Bn(
197
- '71706645040721865894779025947914615666559616020894583599959600180037551395766',
198
- 10
199
- )
200
- const s = new Bn(
201
- '109412465507152403114191008482955798903072313614214706891149785278625167723646',
202
- 10
203
- )
204
- const ecdsa = new Ecdsa()
205
- ecdsa.keyPair = new KeyPair().fromPrivKey(
206
- new PrivKey().fromBn(
207
- new Bn().fromBuffer(Hash.sha256(Buffer.from('test')))
208
- )
209
- )
210
- ecdsa.hashBuf = hashBuf
211
- ecdsa.sig = Sig.fromObject({ r: r, s: s })
212
-
213
- const sig1 = ecdsa.calcrecovery().sig
214
- const sig2 = Ecdsa.calcrecovery(
215
- ecdsa.sig,
216
- ecdsa.keyPair.pubKey,
217
- ecdsa.hashBuf
218
- )
219
- Buffer.compare(sig1.toCompact(), sig2.toCompact()).should.equal(0)
220
- })
221
- })
222
-
223
- describe('@asyncCalcrecovery', function () {
224
- it('should calculate pubKey recovery number same as #calcrecovery', async function () {
225
- ecdsa.randomK()
226
- ecdsa.sign()
227
- const sig1 = ecdsa.calcrecovery().sig
228
- const sig2 = await Ecdsa.asyncCalcrecovery(
229
- ecdsa.sig,
230
- ecdsa.keyPair.pubKey,
231
- ecdsa.hashBuf
232
- )
233
- Buffer.compare(sig1.toCompact(), sig2.toCompact()).should.equal(0)
234
- })
235
-
236
- it('should calulate this known pubKey recovery number same as #calcrecovery', async function () {
237
- const hashBuf = Hash.sha256(Buffer.from('some data'))
238
- const r = new Bn(
239
- '71706645040721865894779025947914615666559616020894583599959600180037551395766',
240
- 10
241
- )
242
- const s = new Bn(
243
- '109412465507152403114191008482955798903072313614214706891149785278625167723646',
244
- 10
245
- )
246
- const ecdsa = new Ecdsa()
247
- ecdsa.keyPair = new KeyPair().fromPrivKey(
248
- new PrivKey().fromBn(
249
- new Bn().fromBuffer(Hash.sha256(Buffer.from('test')))
250
- )
251
- )
252
- ecdsa.hashBuf = hashBuf
253
- ecdsa.sig = Sig.fromObject({ r: r, s: s })
254
-
255
- const sig1 = ecdsa.calcrecovery().sig
256
- const sig2 = await Ecdsa.asyncCalcrecovery(
257
- ecdsa.sig,
258
- ecdsa.keyPair.pubKey,
259
- ecdsa.hashBuf
260
- )
261
- Buffer.compare(sig1.toCompact(), sig2.toCompact()).should.equal(0)
262
- })
263
- })
264
-
265
- describe('#fromString', function () {
266
- it('should to a round trip with to string', function () {
267
- const str = ecdsa.toString()
268
- const ecdsa2 = new Ecdsa()
269
- ecdsa2.fromString(str)
270
- should.exist(ecdsa.hashBuf)
271
- should.exist(ecdsa.keyPair)
272
- })
273
- })
274
-
275
- describe('#randomK', function () {
276
- it('should generate a new random k when called twice in a row', function () {
277
- ecdsa.randomK()
278
- const k1 = ecdsa.k
279
- ecdsa.randomK()
280
- const k2 = ecdsa.k
281
- ;(k1.cmp(k2) === 0).should.equal(false)
282
- })
283
-
284
- it('should generate a random k that is (almost always) greater than this relatively small number', function () {
285
- ecdsa.randomK()
286
- const k1 = ecdsa.k
287
- const k2 = new Bn(Math.pow(2, 32))
288
- .mul(new Bn(Math.pow(2, 32)))
289
- .mul(new Bn(Math.pow(2, 32)))
290
- k2.gt(k1).should.equal(false)
291
- })
292
- })
293
-
294
- describe('#deterministicK', function () {
295
- it('should generate the same deterministic k', function () {
296
- ecdsa.deterministicK()
297
- ecdsa.k
298
- .toBuffer()
299
- .toString('hex')
300
- .should.equal(
301
- 'fcce1de7a9bcd6b2d3defade6afa1913fb9229e3b7ddf4749b55c4848b2a196e'
302
- )
303
- })
304
-
305
- it('should generate the same deterministic k if badrs is set', function () {
306
- ecdsa.deterministicK(0)
307
- ecdsa.k
308
- .toBuffer()
309
- .toString('hex')
310
- .should.equal(
311
- 'fcce1de7a9bcd6b2d3defade6afa1913fb9229e3b7ddf4749b55c4848b2a196e'
312
- )
313
- ecdsa.deterministicK(1)
314
- ecdsa.k
315
- .toBuffer()
316
- .toString('hex')
317
- .should.not.equal(
318
- 'fcce1de7a9bcd6b2d3defade6afa1913fb9229e3b7ddf4749b55c4848b2a196e'
319
- )
320
- ecdsa.k
321
- .toBuffer()
322
- .toString('hex')
323
- .should.equal(
324
- '727fbcb59eb48b1d7d46f95a04991fc512eb9dbf9105628e3aec87428df28fd8'
325
- )
326
- })
327
-
328
- it('should compute this test vector correctly', function () {
329
- // test fixture from bitcoinjs
330
- // https://github.com/bitcoinjs/bitcoinjs-lib/blob/10630873ebaa42381c5871e20336fbfb46564ac8/test/fixtures/ecdsa.json#L6
331
- const ecdsa = new Ecdsa()
332
- ecdsa.hashBuf = Hash.sha256(
333
- Buffer.from(
334
- 'Everything should be made as simple as possible, but not simpler.'
335
- )
336
- )
337
- ecdsa.keyPair = new KeyPair().fromPrivKey(
338
- new PrivKey().fromObject({ bn: new Bn(1) })
339
- )
340
- ecdsa.deterministicK()
341
- ecdsa.k
342
- .toBuffer()
343
- .toString('hex')
344
- .should.equal(
345
- 'ec633bd56a5774a0940cb97e27a9e4e51dc94af737596a0c5cbb3d30332d92a5'
346
- )
347
- ecdsa.sign()
348
- ecdsa.sig.r
349
- .toString()
350
- .should.equal(
351
- '23362334225185207751494092901091441011938859014081160902781146257181456271561'
352
- )
353
- ecdsa.sig.s
354
- .toString()
355
- .should.equal(
356
- '50433721247292933944369538617440297985091596895097604618403996029256432099938'
357
- )
358
- })
359
- })
360
-
361
- describe('#sig2PubKey', function () {
362
- it('should calculate the correct public key for this signature with low s', function () {
363
- ecdsa.sig = new Sig().fromString(
364
- '3045022100ec3cfe0e335791ad278b4ec8eac93d0347a97877bb1d54d35d189e225c15f6650220278cf15b05ce47fb37d2233802899d94c774d5480bba9f0f2d996baa13370c43'
365
- )
366
- ecdsa.sig.recovery = 0
367
- const pubKey = ecdsa.sig2PubKey()
368
- pubKey.point.eq(ecdsa.keyPair.pubKey.point).should.equal(true)
369
- })
370
-
371
- it('should calculate the correct public key for this signature with high s', function () {
372
- ecdsa.sign()
373
- ecdsa.sig = new Sig().fromString(
374
- '3046022100ec3cfe0e335791ad278b4ec8eac93d0347a97877bb1d54d35d189e225c15f665022100d8730ea4fa31b804c82ddcc7fd766269f33a079ea38e012c9238f2e2bcff34fe'
375
- )
376
- ecdsa.sig.recovery = 1
377
- const pubKey = ecdsa.sig2PubKey()
378
- pubKey.point.eq(ecdsa.keyPair.pubKey.point).should.equal(true)
379
- })
380
- })
381
-
382
- describe('#asyncSig2PubKey', function () {
383
- it('should calculate the correct public key for this signature with low s', async function () {
384
- ecdsa.sig = new Sig().fromString(
385
- '3045022100ec3cfe0e335791ad278b4ec8eac93d0347a97877bb1d54d35d189e225c15f6650220278cf15b05ce47fb37d2233802899d94c774d5480bba9f0f2d996baa13370c43'
386
- )
387
- ecdsa.sig.recovery = 0
388
- const pubKey = await ecdsa.asyncSig2PubKey()
389
- pubKey.point.eq(ecdsa.keyPair.pubKey.point).should.equal(true)
390
- })
391
-
392
- it('should calculate the correct public key for this signature with high s', async function () {
393
- ecdsa.sign()
394
- ecdsa.sig = new Sig().fromString(
395
- '3046022100ec3cfe0e335791ad278b4ec8eac93d0347a97877bb1d54d35d189e225c15f665022100d8730ea4fa31b804c82ddcc7fd766269f33a079ea38e012c9238f2e2bcff34fe'
396
- )
397
- ecdsa.sig.recovery = 1
398
- const pubKey = await ecdsa.asyncSig2PubKey()
399
- pubKey.point.eq(ecdsa.keyPair.pubKey.point).should.equal(true)
400
- })
401
- })
402
-
403
- describe('@sig2PubKey', function () {
404
- it('should calculate the correct public key', function () {
405
- ecdsa.k = new Bn(
406
- '114860389168127852803919605627759231199925249596762615988727970217268189974335',
407
- 10
408
- )
409
- ecdsa.sign()
410
- ecdsa.sig.recovery = 0
411
- const pubKey1 = ecdsa.sig2PubKey()
412
- const pubKey2 = Ecdsa.sig2PubKey(ecdsa.sig, ecdsa.hashBuf)
413
- pubKey1.toString().should.equal(pubKey2.toString())
414
- })
415
-
416
- it('should calculate the correct public key for this signature with low s', function () {
417
- ecdsa.k = new Bn(
418
- '114860389168127852803919605627759231199925249596762615988727970217268189974335',
419
- 10
420
- )
421
- ecdsa.sig = new Sig().fromString(
422
- '3045022100ec3cfe0e335791ad278b4ec8eac93d0347a97877bb1d54d35d189e225c15f6650220278cf15b05ce47fb37d2233802899d94c774d5480bba9f0f2d996baa13370c43'
423
- )
424
- ecdsa.sig.recovery = 0
425
- const pubKey1 = ecdsa.sig2PubKey()
426
- const pubKey2 = Ecdsa.sig2PubKey(ecdsa.sig, ecdsa.hashBuf)
427
- pubKey1.toString().should.equal(pubKey2.toString())
428
- })
429
-
430
- it('should calculate the correct public key for this signature with high s', function () {
431
- ecdsa.k = new Bn(
432
- '114860389168127852803919605627759231199925249596762615988727970217268189974335',
433
- 10
434
- )
435
- ecdsa.sign()
436
- ecdsa.sig = new Sig().fromString(
437
- '3046022100ec3cfe0e335791ad278b4ec8eac93d0347a97877bb1d54d35d189e225c15f665022100d8730ea4fa31b804c82ddcc7fd766269f33a079ea38e012c9238f2e2bcff34fe'
438
- )
439
- ecdsa.sig.recovery = 1
440
- const pubKey1 = ecdsa.sig2PubKey()
441
- const pubKey2 = Ecdsa.sig2PubKey(ecdsa.sig, ecdsa.hashBuf)
442
- pubKey1.toString().should.equal(pubKey2.toString())
443
- })
444
- })
445
-
446
- describe('@asyncSig2PubKey', function () {
447
- it('should calculate the correct public key', async function () {
448
- ecdsa.k = new Bn(
449
- '114860389168127852803919605627759231199925249596762615988727970217268189974335',
450
- 10
451
- )
452
- ecdsa.sign()
453
- ecdsa.sig.recovery = 0
454
- const pubKey1 = ecdsa.sig2PubKey()
455
- const pubKey2 = await Ecdsa.asyncSig2PubKey(ecdsa.sig, ecdsa.hashBuf)
456
- pubKey1.toString().should.equal(pubKey2.toString())
457
- })
458
-
459
- it('should calculate the correct public key for this signature with low s', async function () {
460
- ecdsa.k = new Bn(
461
- '114860389168127852803919605627759231199925249596762615988727970217268189974335',
462
- 10
463
- )
464
- ecdsa.sig = new Sig().fromString(
465
- '3045022100ec3cfe0e335791ad278b4ec8eac93d0347a97877bb1d54d35d189e225c15f6650220278cf15b05ce47fb37d2233802899d94c774d5480bba9f0f2d996baa13370c43'
466
- )
467
- ecdsa.sig.recovery = 0
468
- const pubKey1 = ecdsa.sig2PubKey()
469
- const pubKey2 = await Ecdsa.asyncSig2PubKey(ecdsa.sig, ecdsa.hashBuf)
470
- pubKey1.toString().should.equal(pubKey2.toString())
471
- })
472
-
473
- it('should calculate the correct public key for this signature with high s', async function () {
474
- ecdsa.k = new Bn(
475
- '114860389168127852803919605627759231199925249596762615988727970217268189974335',
476
- 10
477
- )
478
- ecdsa.sign()
479
- ecdsa.sig = new Sig().fromString(
480
- '3046022100ec3cfe0e335791ad278b4ec8eac93d0347a97877bb1d54d35d189e225c15f665022100d8730ea4fa31b804c82ddcc7fd766269f33a079ea38e012c9238f2e2bcff34fe'
481
- )
482
- ecdsa.sig.recovery = 1
483
- const pubKey1 = ecdsa.sig2PubKey()
484
- const pubKey2 = await Ecdsa.asyncSig2PubKey(ecdsa.sig, ecdsa.hashBuf)
485
- pubKey1.toString().should.equal(pubKey2.toString())
486
- })
487
- })
488
-
489
- describe('#verifyStr', function () {
490
- it('should return an error if the hash is invalid', function () {
491
- const ecdsa = new Ecdsa()
492
- ecdsa.verifyStr().should.equal('hashBuf must be a 32 byte buffer')
493
- })
494
-
495
- it('should return an error if the pubKey is invalid', function () {
496
- const ecdsa = new Ecdsa()
497
- ecdsa.hashBuf = Hash.sha256(Buffer.from('test'))
498
- ecdsa
499
- .verifyStr()
500
- .indexOf('Invalid pubKey')
501
- .should.equal(0)
502
- })
503
-
504
- it('should return an error if r, s are invalid', function () {
505
- const ecdsa = new Ecdsa()
506
- ecdsa.hashBuf = Hash.sha256(Buffer.from('test'))
507
- const pk = new PubKey()
508
- pk.fromDer(
509
- Buffer.from(
510
- '041ff0fe0f7b15ffaa85ff9f4744d539139c252a49710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341',
511
- 'hex'
512
- )
513
- )
514
- ecdsa.keyPair = new KeyPair()
515
- ecdsa.keyPair.pubKey = pk
516
- ecdsa.sig = new Sig()
517
- ecdsa.sig.r = new Bn(0)
518
- ecdsa.sig.s = new Bn(0)
519
- ecdsa.verifyStr().should.equal('r and s not in range')
520
- })
521
-
522
- it('should return an error if the signature is incorrect', function () {
523
- ecdsa.sig = new Sig()
524
- ecdsa.sig.fromString(
525
- '3046022100e9915e6236695f093a4128ac2a956c40ed971531de2f4f41ba05fac7e2bd019c02210094e6a4a769cc7f2a8ab3db696c7cd8d56bcdbfff860a8c81de4bc6a798b90827'
526
- )
527
- ecdsa.sig.r = ecdsa.sig.r.add(new Bn(1))
528
- ecdsa.verifyStr(false).should.equal('Invalid signature')
529
- })
530
- })
531
-
532
- describe('#sign', function () {
533
- it('should create a valid signature', function () {
534
- ecdsa.randomK()
535
- ecdsa.sign()
536
- ecdsa.verify().verified.should.equal(true)
537
- })
538
-
539
- it('should should throw an error if hashBuf is not 32 bytes', function () {
540
- const ecdsa2 = new Ecdsa().fromObject({
541
- hashBuf: ecdsa.hashBuf.slice(0, 31),
542
- keyPair: ecdsa.keyPair
543
- })
544
- ecdsa2.randomK()
545
- ;(function () {
546
- ecdsa2.sign()
547
- }.should.throw('hashBuf must be a 32 byte buffer'))
548
- })
549
- })
550
-
551
- describe('#asyncSign', function () {
552
- it('should create the same signature as sign', async function () {
553
- ecdsa.sign()
554
- const sig = ecdsa.sig
555
- const sig2 = ecdsa.sig
556
- await ecdsa.asyncSign()
557
- sig.toString().should.equal(sig2.toString())
558
- })
559
- })
560
-
561
- describe('#signRandomK', function () {
562
- it('should produce a signature, and be different when called twice', function () {
563
- ecdsa.signRandomK()
564
- should.exist(ecdsa.sig)
565
- const ecdsa2 = new Ecdsa().fromObject(ecdsa)
566
- ecdsa2.signRandomK()
567
- ecdsa.sig.toString().should.not.equal(ecdsa2.sig.toString())
568
- })
569
- })
570
-
571
- describe('#toString', function () {
572
- it('should convert this to a string', function () {
573
- const str = ecdsa.toString()
574
- ;(typeof str === 'string').should.equal(true)
575
- })
576
- })
577
-
578
- describe('#verify', function () {
579
- it('should verify a signature that was just signed', function () {
580
- ecdsa.sign()
581
- ecdsa.verify().verified.should.equal(true)
582
- })
583
-
584
- it('should verify this known good signature', function () {
585
- ecdsa.sig = new Sig()
586
- ecdsa.sig.fromString(
587
- '3046022100e9915e6236695f093a4128ac2a956c40ed971531de2f4f41ba05fac7e2bd019c02210094e6a4a769cc7f2a8ab3db696c7cd8d56bcdbfff860a8c81de4bc6a798b90827'
588
- )
589
- ecdsa.verify(false).verified.should.equal(true)
590
- })
591
- })
592
-
593
- describe('#asyncVerify', function () {
594
- it('should verify this known good signature', async function () {
595
- ecdsa.verified = undefined
596
- ecdsa.signRandomK()
597
- await ecdsa.asyncVerify()
598
- ecdsa.verified.should.equal(true)
599
- })
600
- })
601
-
602
- describe('@sign', function () {
603
- it('should produce a signature', function () {
604
- const sig = Ecdsa.sign(ecdsa.hashBuf, ecdsa.keyPair)
605
- ;(sig instanceof Sig).should.equal(true)
606
- })
607
- })
608
-
609
- describe('@asyncSign', function () {
610
- it('should produce the same signature as @sign', async function () {
611
- const sig = Ecdsa.sign(ecdsa.hashBuf, ecdsa.keyPair)
612
- const sigstr = sig.toString()
613
- const sig2 = await Ecdsa.asyncSign(ecdsa.hashBuf, ecdsa.keyPair)
614
- const sig2str = sig2.toString()
615
- sigstr.should.equal(sig2str)
616
- })
617
- })
618
-
619
- describe('@verify', function () {
620
- it('should verify a valid signature, and unverify an invalid signature', function () {
621
- const sig = Ecdsa.sign(ecdsa.hashBuf, ecdsa.keyPair)
622
- Ecdsa.verify(ecdsa.hashBuf, sig, ecdsa.keyPair.pubKey).should.equal(true)
623
- const fakesig = new Sig(sig.r.add(1), sig.s)
624
- Ecdsa.verify(ecdsa.hashBuf, fakesig, ecdsa.keyPair.pubKey).should.equal(
625
- false
626
- )
627
- })
628
-
629
- it('should work with big and little endian', function () {
630
- let sig = Ecdsa.sign(ecdsa.hashBuf, ecdsa.keyPair, 'big')
631
- Ecdsa.verify(
632
- ecdsa.hashBuf,
633
- sig,
634
- ecdsa.keyPair.pubKey,
635
- 'big'
636
- ).should.equal(true)
637
- Ecdsa.verify(
638
- ecdsa.hashBuf,
639
- sig,
640
- ecdsa.keyPair.pubKey,
641
- 'little'
642
- ).should.equal(false)
643
- sig = Ecdsa.sign(ecdsa.hashBuf, ecdsa.keyPair, 'little')
644
- Ecdsa.verify(
645
- ecdsa.hashBuf,
646
- sig,
647
- ecdsa.keyPair.pubKey,
648
- 'big'
649
- ).should.equal(false)
650
- Ecdsa.verify(
651
- ecdsa.hashBuf,
652
- sig,
653
- ecdsa.keyPair.pubKey,
654
- 'little'
655
- ).should.equal(true)
656
- })
657
- })
658
-
659
- describe('@asyncVerify', function () {
660
- it('should verify a valid signature, and unverify an invalid signature', async function () {
661
- const sig = Ecdsa.sign(ecdsa.hashBuf, ecdsa.keyPair)
662
- let verified = await Ecdsa.asyncVerify(
663
- ecdsa.hashBuf,
664
- sig,
665
- ecdsa.keyPair.pubKey
666
- )
667
- verified.should.equal(true)
668
- const fakesig = new Sig(sig.r.add(1), sig.s)
669
- verified = await Ecdsa.asyncVerify(
670
- ecdsa.hashBuf,
671
- fakesig,
672
- ecdsa.keyPair.pubKey
673
- )
674
- verified.should.equal(false)
675
- })
676
- })
677
-
678
- describe('vectors', function () {
679
- vectors.valid.forEach(function (obj, i) {
680
- it('should validate valid vector ' + i, function () {
681
- const ecdsa = new Ecdsa().fromObject({
682
- keyPair: new KeyPair().fromPrivKey(
683
- new PrivKey().fromBn(new Bn().fromBuffer(Buffer.from(obj.d, 'hex')))
684
- ),
685
- k: new Bn().fromBuffer(Buffer.from(obj.k, 'hex')),
686
- hashBuf: Hash.sha256(Buffer.from(obj.message)),
687
- sig: new Sig().fromObject({
688
- r: new Bn(obj.signature.r),
689
- s: new Bn(obj.signature.s),
690
- recovery: obj.i
691
- })
692
- })
693
- const ecdsa2 = new Ecdsa().fromObject(ecdsa)
694
- ecdsa2.k = undefined
695
- ecdsa2.sign()
696
- ecdsa2.calcrecovery()
697
- // ecdsa2.k.toString().should.equal(ecdsa.k.toString())
698
- ecdsa2.sig.toString().should.equal(ecdsa.sig.toString())
699
- ecdsa2.sig.recovery.should.equal(ecdsa.sig.recovery)
700
- ecdsa.verify().verified.should.equal(true)
701
- })
702
- })
703
-
704
- vectors.invalid.verifystr.forEach(function (obj, i) {
705
- it(
706
- 'should validate invalid.verifystr vector ' +
707
- i +
708
- ': ' +
709
- obj.description,
710
- function () {
711
- const ecdsa = new Ecdsa().fromObject({
712
- keyPair: new KeyPair().fromObject({
713
- pubKey: new PubKey().fromObject({ point: Point.fromX(true, 1) })
714
- }),
715
- sig: new Sig(new Bn(obj.signature.r), new Bn(obj.signature.s)),
716
- hashBuf: Hash.sha256(Buffer.from(obj.message))
717
- })
718
- ecdsa.verifyStr().should.equal(obj.exception)
719
- }
720
- )
721
- })
722
-
723
- vectors.deterministicK.forEach(function (obj, i) {
724
- it('should validate deterministicK vector ' + i, function () {
725
- const hashBuf = Hash.sha256(Buffer.from(obj.message))
726
- const privKey = new PrivKey().fromObject({
727
- bn: new Bn().fromBuffer(Buffer.from(obj.privkey, 'hex'))
728
- })
729
- const ecdsa = new Ecdsa().fromObject({
730
- keyPair: new KeyPair().fromObject({ privKey: privKey }),
731
- hashBuf: hashBuf
732
- })
733
- ecdsa
734
- .deterministicK(0)
735
- .k.toString('hex')
736
- .should.equal(obj.k_bad00)
737
- ecdsa
738
- .deterministicK(1)
739
- .k.toString('hex')
740
- .should.equal(obj.k_bad01)
741
- ecdsa
742
- .deterministicK(15)
743
- .k.toString('hex')
744
- .should.equal(obj.k_bad15)
745
- })
746
- })
747
- })
748
- })