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