bsv2 2.1.0 → 2.1.2

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