bsv2 2.0.13
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.
- package/.babelrc.test +10 -0
- package/.editorconfig +4 -0
- package/.env.defaults +1 -0
- package/.env.schema +1 -0
- package/.mocharc.yml +4 -0
- package/.nvmrc +1 -0
- package/.travis.yml +7 -0
- package/LICENSE +39 -0
- package/README.md +53 -0
- package/benchmark.js +50 -0
- package/bin/bsv.js +11 -0
- package/bin/network.js +92 -0
- package/build/bsv.js +3 -0
- package/build/index.html +20 -0
- package/dist/bsv.bundle.js +3 -0
- package/dist/bsv.bundle.js.LICENSE.txt +10 -0
- package/dist/bsv.bundle.js.map +1 -0
- package/dist/bsv.cjs.js +3 -0
- package/dist/bsv.cjs.js.LICENSE.txt +1 -0
- package/dist/bsv.cjs.js.map +1 -0
- package/dist/bsv.min.js +2 -0
- package/dist/bsv.module.js +3 -0
- package/dist/bsv.module.js.LICENSE.txt +1 -0
- package/dist/bsv.module.js.map +1 -0
- package/doc/assets/anchor.js +350 -0
- package/doc/assets/bass-addons.css +12 -0
- package/doc/assets/bass.css +544 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/doc/assets/fonts/LICENSE.txt +93 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/doc/assets/fonts/source-code-pro.css +23 -0
- package/doc/assets/github.css +123 -0
- package/doc/assets/site.js +168 -0
- package/doc/assets/split.css +15 -0
- package/doc/assets/split.js +784 -0
- package/doc/assets/style.css +147 -0
- package/doc/index.html +9387 -0
- package/docs/block.html +36 -0
- package/docs/ecies.html +81 -0
- package/docs/index.html +84 -0
- package/docs/networks.html +43 -0
- package/docs/script.html +101 -0
- package/docs/transaction.html +150 -0
- package/docs/unspentoutput.html +32 -0
- package/entry.js +82 -0
- package/index.js +102 -0
- package/jsdoc.json +5 -0
- package/karma.conf.js +78 -0
- package/lib/ach.js +59 -0
- package/lib/address.js +264 -0
- package/lib/aes.js +57 -0
- package/lib/aescbc.js +38 -0
- package/lib/base-58-check.js +83 -0
- package/lib/base-58.js +59 -0
- package/lib/bip-32.js +472 -0
- package/lib/bip-39-en-wordlist.js +2054 -0
- package/lib/bip-39-en.js +10 -0
- package/lib/bip-39-es-wordlist.js +2055 -0
- package/lib/bip-39-es.js +10 -0
- package/lib/bip-39-jp-wordlist.js +2053 -0
- package/lib/bip-39-jp.js +10 -0
- package/lib/bip-39-words.js +5 -0
- package/lib/bip-39.js +275 -0
- package/lib/block-header.js +79 -0
- package/lib/block.js +132 -0
- package/lib/bn.js +467 -0
- package/lib/br.js +172 -0
- package/lib/bsm.js +127 -0
- package/lib/bw.js +188 -0
- package/lib/cbc.js +140 -0
- package/lib/cmp.js +37 -0
- package/lib/config.js +50 -0
- package/lib/constants.js +150 -0
- package/lib/ecdsa.js +495 -0
- package/lib/ecies.js +151 -0
- package/lib/hash-cache.js +41 -0
- package/lib/hash.js +191 -0
- package/lib/interp.js +1853 -0
- package/lib/inv.js +49 -0
- package/lib/key-pair.js +133 -0
- package/lib/mainnet-address.js +5 -0
- package/lib/mainnet-bip-32.js +5 -0
- package/lib/mainnet-key-pair.js +5 -0
- package/lib/mainnet-priv-key.js +5 -0
- package/lib/merkle.js +92 -0
- package/lib/msg.js +184 -0
- package/lib/op-code.js +398 -0
- package/lib/point.js +134 -0
- package/lib/priv-key.js +157 -0
- package/lib/pub-key.js +230 -0
- package/lib/rabin.js +300 -0
- package/lib/random.js +28 -0
- package/lib/script.js +826 -0
- package/lib/sig-operations.js +120 -0
- package/lib/sig.js +399 -0
- package/lib/struct.js +389 -0
- package/lib/testnet-address.js +5 -0
- package/lib/testnet-bip-32.js +5 -0
- package/lib/testnet-key-pair.js +5 -0
- package/lib/testnet-priv-key.js +5 -0
- package/lib/tx-builder.js +593 -0
- package/lib/tx-in.js +172 -0
- package/lib/tx-out-map.js +57 -0
- package/lib/tx-out.js +73 -0
- package/lib/tx-verifier.js +209 -0
- package/lib/tx.js +429 -0
- package/lib/var-int.js +73 -0
- package/lib/version.js +92 -0
- package/lib/workers-result.js +65 -0
- package/lib/workers.js +77 -0
- package/load-babel.js +3 -0
- package/mkdocs.yml +37 -0
- package/package.json +138 -0
- package/test/ach.js +172 -0
- package/test/address.js +464 -0
- package/test/aes.js +96 -0
- package/test/aescbc.js +65 -0
- package/test/base-58-check.js +128 -0
- package/test/base-58.js +113 -0
- package/test/bip-32.js +721 -0
- package/test/bip-39.js +282 -0
- package/test/block-header.js +152 -0
- package/test/block.js +243 -0
- package/test/bn.js +643 -0
- package/test/br.js +353 -0
- package/test/bsm.js +141 -0
- package/test/bw.js +292 -0
- package/test/cbc.js +335 -0
- package/test/cmp.js +56 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +152 -0
- package/test/data/bitcoind/base58_keys_valid.json +452 -0
- package/test/data/bitcoind/blocks.json +27 -0
- package/test/data/bitcoind/script_tests.json +2244 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +22 -0
- package/test/data/bitcoind/tx_invalid.json +177 -0
- package/test/data/bitcoind/tx_valid.json +224 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +12 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +1004 -0
- package/test/data/tx_creation.json +85 -0
- package/test/ecdsa.js +748 -0
- package/test/ecies.js +238 -0
- package/test/global.js +13 -0
- package/test/hash-cache.js +52 -0
- package/test/hash.js +354 -0
- package/test/index.js +10 -0
- package/test/interp.js +512 -0
- package/test/inv.js +65 -0
- package/test/key-pair.js +243 -0
- package/test/merkle.js +179 -0
- package/test/msg.js +295 -0
- package/test/op-code.js +79 -0
- package/test/point.js +375 -0
- package/test/priv-key.js +264 -0
- package/test/pub-key.js +549 -0
- package/test/random.js +31 -0
- package/test/script.js +1340 -0
- package/test/sig-operations.js +149 -0
- package/test/sig.js +662 -0
- package/test/struct.js +552 -0
- package/test/tx-builder.js +2104 -0
- package/test/tx-in.js +198 -0
- package/test/tx-out-map.js +104 -0
- package/test/tx-out.js +162 -0
- package/test/tx-verifier.js +246 -0
- package/test/tx.js +539 -0
- package/test/var-int.js +132 -0
- package/test/vectors/aes.json +10392 -0
- package/test/vectors/aescbc.json +620 -0
- package/test/vectors/bip39.json +502 -0
- package/test/vectors/bip69.json +215 -0
- package/test/vectors/bitcoin-abc/script_tests.json +48 -0
- package/test/vectors/bitcoin-abc/sighash.json +7004 -0
- package/test/vectors/bitcoin-sv/script_tests.json +248 -0
- package/test/vectors/bitcoind/script_invalid.json +1151 -0
- package/test/vectors/bitcoind/script_valid.json +1462 -0
- package/test/vectors/bitcoind/sighash.json +3503 -0
- package/test/vectors/bitcoind/tx_invalid.json +883 -0
- package/test/vectors/bitcoind/tx_valid.json +1150 -0
- package/test/vectors/coolest-tx-ever-sent.json +8 -0
- package/test/vectors/ecdsa.json +300 -0
- package/test/vectors/hash.json +6133 -0
- package/test/vectors/kdf.json +47 -0
- package/test/vectors/largesttx.json +5 -0
- package/test/vectors/largesttxblock.json +6 -0
- package/test/vectors/sig.json +79 -0
- package/test/vectors/sighash-single-bug.json +8 -0
- package/test/version.js +81 -0
- package/test/workers-result.js +89 -0
- package/test/workers.js +91 -0
- package/test-index.js +3 -0
- package/test1.html +88 -0
- package/webpack.config.js +60 -0
- package/webpack.config.js_entry +57 -0
- package/yarn-error.log +6199 -0
package/test/ecdsa.js
ADDED
|
@@ -0,0 +1,748 @@
|
|
|
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
|
+
})
|