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/lib/ecdsa.js DELETED
@@ -1,495 +0,0 @@
1
- /**
2
- * Ecdsa
3
- * =====
4
- *
5
- * Ecdsa is the signature algorithm used by bitcoin. The way you probably want
6
- * to use this is with the static Ecdsa.sign( ... ) and Ecdsa.verify( ... )
7
- * functions. Note that in bitcoin, the hashBuf is little endian, so if you are
8
- * signing or verifying something that has to do with a transaction, you should
9
- * explicitly plug in that it is little endian as an option to the sign and
10
- * verify functions.
11
- *
12
- * This implementation of Ecdsa uses deterministic signatures as defined in RFC
13
- * 6979 as the default, which has become a defacto standard in bitcoin wallets
14
- * due to recurring security issues around using a value of k pulled from a
15
- * possibly faulty entropy pool. If you use the same value of k twice, someone
16
- * can derive your private key. Deterministic k prevents this without needing
17
- * an entropy pool.
18
- */
19
- 'use strict'
20
-
21
- import { Bn } from './bn'
22
- import { Br } from './br'
23
- import { Hash } from './hash'
24
- import { KeyPair } from './key-pair'
25
- import { Point } from './point'
26
- import { PubKey } from './pub-key'
27
- import { Random } from './random'
28
- import { Sig } from './sig'
29
- import { Struct } from './struct'
30
- import { Workers } from './workers'
31
-
32
- class Ecdsa extends Struct {
33
- constructor (sig, keyPair, hashBuf, k, endian, verified) {
34
- super({ sig, keyPair, hashBuf, k, endian, verified })
35
- }
36
-
37
- toJSON () {
38
- return {
39
- sig: this.sig ? this.sig.toString() : undefined,
40
- keyPair: this.keyPair
41
- ? this.keyPair.toBuffer().toString('hex')
42
- : undefined,
43
- hashBuf: this.hashBuf ? this.hashBuf.toString('hex') : undefined,
44
- k: this.k ? this.k.toString() : undefined,
45
- endian: this.endian,
46
- verified: this.verified
47
- }
48
- }
49
-
50
- fromJSON (json) {
51
- this.sig = json.sig ? new Sig().fromString(json.sig) : undefined
52
- this.keyPair = json.keyPair
53
- ? new KeyPair().fromBuffer(Buffer.from(json.keyPair, 'hex'))
54
- : undefined
55
- this.hashBuf = json.hashBuf ? Buffer.from(json.hashBuf, 'hex') : undefined
56
- this.k = json.k ? new Bn().fromString(json.k) : undefined
57
- this.endian = json.endian
58
- this.verified = json.verified
59
- return this
60
- }
61
-
62
- toBuffer () {
63
- const str = JSON.stringify(this.toJSON())
64
- return Buffer.from(str)
65
- }
66
-
67
- fromBuffer (buf) {
68
- const json = JSON.parse(buf.toString())
69
- return this.fromJSON(json)
70
- }
71
-
72
- calcrecovery () {
73
- for (let recovery = 0; recovery < 4; recovery++) {
74
- let Qprime
75
- this.sig.recovery = recovery
76
- try {
77
- Qprime = this.sig2PubKey()
78
- } catch (e) {
79
- continue
80
- }
81
-
82
- if (Qprime.point.eq(this.keyPair.pubKey.point)) {
83
- const compressed = this.keyPair.pubKey.compressed
84
- this.sig.compressed =
85
- this.keyPair.pubKey.compressed === undefined ? true : compressed
86
- return this
87
- }
88
- }
89
-
90
- this.sig.recovery = undefined
91
- throw new Error('Unable to find valid recovery factor')
92
- }
93
-
94
- async asyncCalcrecovery () {
95
- const workersResult = await Workers.asyncObjectMethod(
96
- this,
97
- 'calcrecovery',
98
- []
99
- )
100
- return this.fromFastBuffer(workersResult.resbuf)
101
- }
102
-
103
- /**
104
- * Calculates the recovery factor, and mutates sig so that it now contains
105
- * the recovery factor and the "compressed" variable. Throws an exception on
106
- * failure.
107
- */
108
- static calcrecovery (sig, pubKey, hashBuf) {
109
- const ecdsa = new Ecdsa().fromObject({
110
- sig: sig,
111
- keyPair: new KeyPair().fromObject({ pubKey: pubKey }),
112
- hashBuf: hashBuf
113
- })
114
- return ecdsa.calcrecovery().sig
115
- }
116
-
117
- static async asyncCalcrecovery (sig, pubKey, hashBuf) {
118
- const workersResult = await Workers.asyncClassMethod(
119
- Ecdsa,
120
- 'calcrecovery',
121
- [sig, pubKey, hashBuf]
122
- )
123
- return new Sig().fromFastBuffer(workersResult.resbuf)
124
- }
125
-
126
- fromString (str) {
127
- const obj = JSON.parse(str)
128
- if (obj.hashBuf) {
129
- this.hashBuf = Buffer.from(obj.hashBuf, 'hex')
130
- }
131
- if (obj.keyPair) {
132
- this.keyPair = new KeyPair().fromString(obj.keyPair)
133
- }
134
- if (obj.sig) {
135
- this.sig = new Sig().fromString(obj.sig)
136
- }
137
- if (obj.k) {
138
- this.k = new Bn(obj.k, 10)
139
- }
140
- return this
141
- }
142
-
143
- randomK () {
144
- const N = Point.getN()
145
- let k
146
- do {
147
- k = new Bn().fromBuffer(Random.getRandomBuffer(32))
148
- } while (!(k.lt(N) && k.gt(0)))
149
- this.k = k
150
- return this
151
- }
152
-
153
- /**
154
- * The traditional Ecdsa algorithm uses a purely random value of k. This has
155
- * the negative that when signing, your entropy must be good, or the private
156
- * key can be recovered if two signatures use the same value of k. It turns out
157
- * that k does not have to be purely random. It can be deterministic, so long
158
- * as an attacker can't guess it. RFC 6979 specifies how to do this using a
159
- * combination of the private key and the hash of the thing to be signed. It is
160
- * best practice to use this value, which can be tested for byte-for-byte
161
- * accuracy, and is resistant to a broken RNG. Note that it is actually the
162
- * case that bitcoin private keys have been compromised through that attack.
163
- * Deterministic k is a best practice.
164
- *
165
- * https://tools.ietf.org/html/rfc6979#section-3.2
166
- */
167
- deterministicK (badrs) {
168
- let v = Buffer.alloc(32)
169
- v.fill(0x01)
170
- let k = Buffer.alloc(32)
171
- k.fill(0x00)
172
- const x = this.keyPair.privKey.bn.toBuffer({ size: 32 })
173
- k = Hash.sha256Hmac(
174
- Buffer.concat([v, Buffer.from([0x00]), x, this.hashBuf]),
175
- k
176
- )
177
- v = Hash.sha256Hmac(v, k)
178
- k = Hash.sha256Hmac(
179
- Buffer.concat([v, Buffer.from([0x01]), x, this.hashBuf]),
180
- k
181
- )
182
- v = Hash.sha256Hmac(v, k)
183
- v = Hash.sha256Hmac(v, k)
184
- let T = new Bn().fromBuffer(v)
185
- const N = Point.getN()
186
-
187
- // if r or s were invalid when this function was used in signing,
188
- // we do not want to actually compute r, s here for efficiency, so,
189
- // we can increment badrs. explained at end of RFC 6979 section 3.2
190
- if (badrs === undefined) {
191
- badrs = 0
192
- }
193
- // also explained in 3.2, we must ensure T is in the proper range (0, N)
194
- for (let i = 0; i < badrs || !(T.lt(N) && T.gt(0)); i++) {
195
- k = Hash.sha256Hmac(Buffer.concat([v, Buffer.from([0x00])]), k)
196
- v = Hash.sha256Hmac(v, k)
197
- v = Hash.sha256Hmac(v, k)
198
- T = new Bn().fromBuffer(v)
199
- }
200
-
201
- this.k = T
202
- return this
203
- }
204
-
205
- /**
206
- * Information about public key recovery:
207
- * https://bitcointalk.org/index.php?topic=6430.0
208
- * http://stackoverflow.com/questions/19665491/how-do-i-get-an-ecdsa-public-key-from-just-a-bitcoin-signature-sec1-4-1-6-k
209
- * This code was originally taken from BitcoinJS
210
- */
211
- sig2PubKey () {
212
- const recovery = this.sig.recovery
213
- if (
214
- !(recovery === 0 || recovery === 1 || recovery === 2 || recovery === 3)
215
- ) {
216
- throw new Error('i must be equal to 0, 1, 2, or 3')
217
- }
218
-
219
- const e = new Bn().fromBuffer(this.hashBuf)
220
- const r = this.sig.r
221
- const s = this.sig.s
222
-
223
- // A set LSB signifies that the y-coordinate is odd
224
- const isYOdd = recovery & 1
225
-
226
- // The more significant bit specifies whether we should use the
227
- // first or second candidate key.
228
- const isSecondKey = recovery >> 1
229
-
230
- const n = Point.getN()
231
- const G = Point.getG()
232
-
233
- // 1.1 LEt x = r + jn
234
- const x = isSecondKey ? r.add(n) : r
235
- const R = Point.fromX(isYOdd, x)
236
-
237
- // 1.4 Check that nR is at infinity
238
- let errm = ''
239
- try {
240
- R.mul(n)
241
- } catch (err) {
242
- errm = err.message
243
- }
244
- if (errm !== 'point mul out of range') {
245
- throw new Error('nR is not a valid curve point')
246
- }
247
-
248
- // Compute -e from e
249
- const eNeg = e.neg().umod(n)
250
-
251
- // 1.6.1 Compute Q = r^-1 (sR - eG)
252
- // Q = r^-1 (sR + -eG)
253
- const rInv = r.invm(n)
254
-
255
- // const Q = R.multiplyTwo(s, G, eNeg).mul(rInv)
256
- const Q = R.mul(s)
257
- .add(G.mul(eNeg))
258
- .mul(rInv)
259
-
260
- const pubKey = new PubKey(Q)
261
- pubKey.compressed = this.sig.compressed
262
- pubKey.validate()
263
-
264
- return pubKey
265
- }
266
-
267
- async asyncSig2PubKey () {
268
- const workersResult = await Workers.asyncObjectMethod(
269
- this,
270
- 'sig2PubKey',
271
- []
272
- )
273
- return PubKey.fromFastBuffer(workersResult.resbuf)
274
- }
275
-
276
- static sig2PubKey (sig, hashBuf) {
277
- const ecdsa = new Ecdsa().fromObject({
278
- sig: sig,
279
- hashBuf: hashBuf
280
- })
281
- return ecdsa.sig2PubKey()
282
- }
283
-
284
- static async asyncSig2PubKey (sig, hashBuf) {
285
- const ecdsa = new Ecdsa().fromObject({
286
- sig: sig,
287
- hashBuf: hashBuf
288
- })
289
- const pubKey = await ecdsa.asyncSig2PubKey()
290
- return pubKey
291
- }
292
-
293
- verifyStr (enforceLowS = true) {
294
- if (!Buffer.isBuffer(this.hashBuf) || this.hashBuf.length !== 32) {
295
- return 'hashBuf must be a 32 byte buffer'
296
- }
297
-
298
- try {
299
- this.keyPair.pubKey.validate()
300
- } catch (e) {
301
- return 'Invalid pubKey: ' + e
302
- }
303
-
304
- const r = this.sig.r
305
- const s = this.sig.s
306
- if (
307
- !(r.gt(0) && r.lt(Point.getN())) ||
308
- !(s.gt(0) && s.lt(Point.getN()))
309
- ) {
310
- return 'r and s not in range'
311
- }
312
-
313
- if (enforceLowS) {
314
- if (!this.sig.hasLowS()) {
315
- return 's is too high and does not satisfy low s contraint - see bip 62'
316
- }
317
- }
318
-
319
- const e = new Bn().fromBuffer(
320
- this.hashBuf,
321
- this.endian ? { endian: this.endian } : undefined
322
- )
323
- const n = Point.getN()
324
- const sinv = s.invm(n)
325
- const u1 = sinv.mul(e).mod(n)
326
- const u2 = sinv.mul(r).mod(n)
327
-
328
- const p = Point.getG().mulAdd(u1, this.keyPair.pubKey.point, u2)
329
- // const p = Point.getG().mulAdd(u1, this.keyPair.pubKey.point, u2)
330
- if (p.isInfinity()) {
331
- return 'p is infinity'
332
- }
333
-
334
- if (
335
- !(
336
- p
337
- .getX()
338
- .mod(n)
339
- .cmp(r) === 0
340
- )
341
- ) {
342
- return 'Invalid signature'
343
- } else {
344
- return false
345
- }
346
- }
347
-
348
- sign () {
349
- const hashBuf =
350
- this.endian === 'little'
351
- ? new Br(this.hashBuf).readReverse()
352
- : this.hashBuf
353
-
354
- const privKey = this.keyPair.privKey
355
-
356
- const d = privKey.bn
357
-
358
- if (!hashBuf || !privKey || !d) {
359
- throw new Error('invalid parameters')
360
- }
361
-
362
- if (!Buffer.isBuffer(hashBuf) || hashBuf.length !== 32) {
363
- throw new Error('hashBuf must be a 32 byte buffer')
364
- }
365
-
366
- const N = Point.getN()
367
- const G = Point.getG()
368
- const e = new Bn().fromBuffer(hashBuf)
369
-
370
- // try different values of k until r, s are valid
371
- let badrs = 0
372
- let k, Q, r, s
373
- do {
374
- if (!this.k || badrs > 0) {
375
- this.deterministicK(badrs)
376
- }
377
- badrs++
378
- k = this.k
379
- Q = G.mul(k)
380
- r = Q.getX().mod(N)
381
- s = k
382
- .invm(N)
383
- .mul(e.add(d.mul(r)))
384
- .mod(N)
385
- } while (r.cmp(0) <= 0 || s.cmp(0) <= 0)
386
-
387
- // enforce low s
388
- // see Bip 62, "low S values in signatures"
389
- if (
390
- s.gt(
391
- new Bn().fromBuffer(
392
- Buffer.from(
393
- '7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0',
394
- 'hex'
395
- )
396
- )
397
- )
398
- ) {
399
- s = Point.getN().sub(s)
400
- }
401
- this.sig = Sig.fromObject({
402
- r: r,
403
- s: s,
404
- compressed: this.keyPair.pubKey.compressed
405
- })
406
- return this
407
- }
408
-
409
- async asyncSign () {
410
- const workersResult = await Workers.asyncObjectMethod(this, 'sign', [])
411
- return this.fromFastBuffer(workersResult.resbuf)
412
- }
413
-
414
- signRandomK () {
415
- this.randomK()
416
- return this.sign()
417
- }
418
-
419
- toString () {
420
- const obj = {}
421
- if (this.hashBuf) {
422
- obj.hashBuf = this.hashBuf.toString('hex')
423
- }
424
- if (this.keyPair) {
425
- obj.keyPair = this.keyPair.toString()
426
- }
427
- if (this.sig) {
428
- obj.sig = this.sig.toString()
429
- }
430
- if (this.k) {
431
- obj.k = this.k.toString()
432
- }
433
- return JSON.stringify(obj)
434
- }
435
-
436
- verify (enforceLowS = true) {
437
- if (!this.verifyStr(enforceLowS)) {
438
- this.verified = true
439
- } else {
440
- this.verified = false
441
- }
442
- return this
443
- }
444
-
445
- async asyncVerify (enforceLowS = true) {
446
- const workersResult = await Workers.asyncObjectMethod(this, 'verify', [
447
- enforceLowS
448
- ])
449
- return this.fromFastBuffer(workersResult.resbuf)
450
- }
451
-
452
- static sign (hashBuf, keyPair, endian) {
453
- return new Ecdsa()
454
- .fromObject({
455
- hashBuf: hashBuf,
456
- endian: endian,
457
- keyPair: keyPair
458
- })
459
- .sign().sig
460
- }
461
-
462
- static async asyncSign (hashBuf, keyPair, endian) {
463
- const ecdsa = new Ecdsa().fromObject({
464
- hashBuf: hashBuf,
465
- endian: endian,
466
- keyPair: keyPair
467
- })
468
- await ecdsa.asyncSign()
469
- return ecdsa.sig
470
- }
471
-
472
- static verify (hashBuf, sig, pubKey, endian, enforceLowS = true) {
473
- return new Ecdsa()
474
- .fromObject({
475
- hashBuf: hashBuf,
476
- endian: endian,
477
- sig: sig,
478
- keyPair: new KeyPair().fromObject({ pubKey: pubKey })
479
- })
480
- .verify(enforceLowS).verified
481
- }
482
-
483
- static async asyncVerify (hashBuf, sig, pubKey, endian, enforceLowS = true) {
484
- const ecdsa = new Ecdsa().fromObject({
485
- hashBuf: hashBuf,
486
- endian: endian,
487
- sig: sig,
488
- keyPair: new KeyPair().fromObject({ pubKey: pubKey })
489
- })
490
- await ecdsa.asyncVerify(enforceLowS)
491
- return ecdsa.verified
492
- }
493
- }
494
-
495
- export { Ecdsa }
package/lib/ecies.js DELETED
@@ -1,151 +0,0 @@
1
- /**
2
- * Ecies
3
- * =====
4
- */
5
- 'use strict'
6
-
7
- import { Aescbc } from './aescbc'
8
- import { cmp } from './cmp'
9
- import { Hash } from './hash'
10
- import { KeyPair } from './key-pair'
11
- import { Point } from './point'
12
- import { PubKey } from './pub-key'
13
- import { Random } from './random'
14
- import { Workers } from './workers'
15
-
16
- class Ecies {}
17
-
18
- Ecies.ivkEkM = function (privKey, pubKey) {
19
- const r = privKey.bn
20
- const KB = pubKey.point
21
- const P = KB.mul(r)
22
- const S = new PubKey(P)
23
- const Sbuf = S.toBuffer()
24
- const hash = Hash.sha512(Sbuf)
25
- return {
26
- iv: hash.slice(0, 16),
27
- kE: hash.slice(16, 32),
28
- kM: hash.slice(32, 64)
29
- }
30
- }
31
-
32
- Ecies.electrumEncrypt = function (messageBuf, toPubKey, fromKeyPair, noKey = false) {
33
- if (!Buffer.isBuffer(messageBuf)) {
34
- throw new Error('messageBuf must be a buffer')
35
- }
36
- let Rbuf
37
- if (fromKeyPair === null) {
38
- fromKeyPair = KeyPair.fromRandom()
39
- }
40
- if (!noKey) {
41
- Rbuf = fromKeyPair.pubKey.toDer(true)
42
- }
43
- const { iv, kE, kM } = Ecies.ivkEkM(fromKeyPair.privKey, toPubKey)
44
- const ciphertext = Aescbc.encrypt(messageBuf, kE, iv, false)
45
- const BIE1 = Buffer.from('BIE1')
46
- let encBuf
47
- if (Rbuf) {
48
- encBuf = Buffer.concat([BIE1, Rbuf, ciphertext])
49
- } else {
50
- encBuf = Buffer.concat([BIE1, ciphertext])
51
- }
52
- const hmac = Hash.sha256Hmac(encBuf, kM)
53
- return Buffer.concat([encBuf, hmac])
54
- }
55
-
56
- Ecies.electrumDecrypt = function (encBuf, toPrivKey, fromPubKey = null) {
57
- if (!Buffer.isBuffer(encBuf)) {
58
- throw new Error('encBuf must be a buffer')
59
- }
60
- const tagLength = 32
61
-
62
- const magic = encBuf.slice(0, 4)
63
- if (!magic.equals(Buffer.from('BIE1'))) {
64
- throw new Error('Invalid Magic')
65
- }
66
- let offset = 4
67
- if (fromPubKey === null) {
68
- // BIE1 use compressed public key, length is always 33.
69
- const pub = encBuf.slice(4, 37)
70
- fromPubKey = PubKey.fromDer(pub)
71
- offset = 37
72
- }
73
- const { iv, kE, kM } = Ecies.ivkEkM(toPrivKey, fromPubKey)
74
- const ciphertext = encBuf.slice(offset, encBuf.length - tagLength)
75
- const hmac = encBuf.slice(encBuf.length - tagLength, encBuf.length)
76
-
77
- const hmac2 = Hash.sha256Hmac(encBuf.slice(0, encBuf.length - tagLength), kM)
78
-
79
- if (!hmac.equals(hmac2)) {
80
- throw new Error('Invalid checksum')
81
- }
82
- return Aescbc.decrypt(ciphertext, kE, iv)
83
- }
84
-
85
- Ecies.bitcoreEncrypt = function (messageBuf, toPubKey, fromKeyPair, ivBuf) {
86
- if (!fromKeyPair) {
87
- fromKeyPair = KeyPair.fromRandom()
88
- }
89
- const r = fromKeyPair.privKey.bn
90
- const RPubKey = fromKeyPair.pubKey
91
- const RBuf = RPubKey.toDer(true)
92
- const KB = toPubKey.point
93
- const P = KB.mul(r)
94
- const S = P.getX()
95
- const Sbuf = S.toBuffer({ size: 32 })
96
- const kEkM = Hash.sha512(Sbuf)
97
- const kE = kEkM.slice(0, 32)
98
- const kM = kEkM.slice(32, 64)
99
- const c = Aescbc.encrypt(messageBuf, kE, ivBuf)
100
- const d = Hash.sha256Hmac(c, kM)
101
- const encBuf = Buffer.concat([RBuf, c, d])
102
- return encBuf
103
- }
104
-
105
- Ecies.asyncBitcoreEncrypt = async function (
106
- messageBuf,
107
- toPubKey,
108
- fromKeyPair,
109
- ivBuf
110
- ) {
111
- if (!fromKeyPair) {
112
- fromKeyPair = await KeyPair.asyncFromRandom()
113
- }
114
- if (!ivBuf) {
115
- ivBuf = Random.getRandomBuffer(128 / 8)
116
- }
117
- const args = [messageBuf, toPubKey, fromKeyPair, ivBuf]
118
- const workersResult = await Workers.asyncClassMethod(Ecies, 'bitcoreEncrypt', args)
119
- return workersResult.resbuf
120
- }
121
-
122
- Ecies.bitcoreDecrypt = function (encBuf, toPrivKey) {
123
- const kB = toPrivKey.bn
124
- const fromPubKey = PubKey.fromDer(encBuf.slice(0, 33))
125
- const R = fromPubKey.point
126
- const P = R.mul(kB)
127
- if (P.eq(new Point())) {
128
- throw new Error('P equals 0')
129
- }
130
- const S = P.getX()
131
- const Sbuf = S.toBuffer({ size: 32 })
132
- const kEkM = Hash.sha512(Sbuf)
133
- const kE = kEkM.slice(0, 32)
134
- const kM = kEkM.slice(32, 64)
135
- const c = encBuf.slice(33, encBuf.length - 32)
136
- const d = encBuf.slice(encBuf.length - 32, encBuf.length)
137
- const d2 = Hash.sha256Hmac(c, kM)
138
- if (!cmp(d, d2)) {
139
- throw new Error('Invalid checksum')
140
- }
141
- const messageBuf = Aescbc.decrypt(c, kE)
142
- return messageBuf
143
- }
144
-
145
- Ecies.asyncBitcoreDecrypt = async function (encBuf, toPrivKey) {
146
- const args = [encBuf, toPrivKey]
147
- const workersResult = await Workers.asyncClassMethod(Ecies, 'bitcoreDecrypt', args)
148
- return workersResult.resbuf
149
- }
150
-
151
- export { Ecies }
package/lib/hash-cache.js DELETED
@@ -1,41 +0,0 @@
1
- /**
2
- * Hash Cache
3
- * ==========
4
- *
5
- * For use in sighash.
6
- */
7
- 'use strict'
8
-
9
- import { Struct } from './struct'
10
-
11
- class HashCache extends Struct {
12
- constructor (prevoutsHashBuf, sequenceHashBuf, outputsHashBuf) {
13
- super()
14
- this.fromObject({ prevoutsHashBuf, sequenceHashBuf, outputsHashBuf })
15
- }
16
-
17
- fromBuffer (buf) {
18
- return this.fromJSON(JSON.parse(buf.toString()))
19
- }
20
-
21
- toBuffer () {
22
- return Buffer.from(JSON.stringify(this.toJSON()))
23
- }
24
-
25
- fromJSON (json) {
26
- this.prevoutsHashBuf = json.prevoutsHashBuf ? Buffer.from(json.prevoutsHashBuf, 'hex') : undefined
27
- this.sequenceHashBuf = json.sequenceHashBuf ? Buffer.from(json.sequenceHashBuf, 'hex') : undefined
28
- this.outputsHashBuf = json.outputsHashBuf ? Buffer.from(json.outputsHashBuf, 'hex') : undefined
29
- return this
30
- }
31
-
32
- toJSON () {
33
- return {
34
- prevoutsHashBuf: this.prevoutsHashBuf ? this.prevoutsHashBuf.toString('hex') : undefined,
35
- sequenceHashBuf: this.sequenceHashBuf ? this.sequenceHashBuf.toString('hex') : undefined,
36
- outputsHashBuf: this.outputsHashBuf ? this.outputsHashBuf.toString('hex') : undefined
37
- }
38
- }
39
- }
40
-
41
- export { HashCache }