This repository was archived by the owner on Jan 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathecpair.js
335 lines (266 loc) · 10.4 KB
/
ecpair.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* global describe, it, beforeEach */
/* eslint-disable no-new */
var assert = require('assert')
var ecdsa = require('../src/ecdsa')
var ecurve = require('ecurve')
var proxyquire = require('proxyquire')
var sinon = require('sinon')
var randombytes = require('randombytes')
var BigInteger = require('bigi')
var ECPair = require('../src/ecpair')
var fastcurve = require('../src/fastcurve')
var fixtures = require('./fixtures/ecpair.json')
var curve = ecdsa.__curve
var NETWORKS = require('../src/networks')
var NETWORKS_LIST = [] // Object.values(NETWORKS)
for (var networkName in NETWORKS) {
NETWORKS_LIST.push(NETWORKS[networkName])
}
describe('ECPair', function () {
describe('constructor', function () {
it('defaults to compressed', function () {
var keyPair = new ECPair(BigInteger.ONE)
assert.strictEqual(keyPair.compressed, true)
})
it('supports the uncompressed option', function () {
var keyPair = new ECPair(BigInteger.ONE, null, {
compressed: false
})
assert.strictEqual(keyPair.compressed, false)
})
it('supports the network option', function () {
var keyPair = new ECPair(BigInteger.ONE, null, {
compressed: false,
network: NETWORKS.testnet
})
assert.strictEqual(keyPair.network, NETWORKS.testnet)
})
fixtures.valid.forEach(function (f) {
it('calculates the public point for ' + f.WIF, function () {
var d = new BigInteger(f.d)
var keyPair = new ECPair(d, null, {
compressed: f.compressed
})
assert.strictEqual(keyPair.getPublicKeyBuffer().toString('hex'), f.Q)
})
})
fixtures.invalid.constructor.forEach(function (f) {
it('throws ' + f.exception, function () {
var d = f.d && new BigInteger(f.d)
var Q = f.Q && ecurve.Point.decodeFrom(curve, Buffer.from(f.Q, 'hex'))
assert.throws(function () {
new ECPair(d, Q, f.options)
}, new RegExp(f.exception))
})
})
})
describe('getPublicKeyBuffer', function () {
var keyPair
beforeEach(function () {
keyPair = new ECPair(BigInteger.ONE)
})
it('wraps Q.getEncoded', sinon.test(function () {
this.mock(keyPair.Q).expects('getEncoded')
.once().withArgs(keyPair.compressed)
keyPair.getPublicKeyBuffer()
}))
})
describe('getPrivateKeyBuffer', function () {
it('pads short private keys', sinon.test(function () {
var keyPair = new ECPair(BigInteger.ONE)
assert.strictEqual(keyPair.getPrivateKeyBuffer().byteLength, 32)
assert.strictEqual(keyPair.getPrivateKeyBuffer().toString('hex'),
'0000000000000000000000000000000000000000000000000000000000000001')
}))
it('does not pad 32 bytes private keys', sinon.test(function () {
var hexString = 'a000000000000000000000000000000000000000000000000000000000000000'
var keyPair = new ECPair(new BigInteger(hexString, 16))
assert.strictEqual(keyPair.getPrivateKeyBuffer().byteLength, 32)
assert.strictEqual(keyPair.getPrivateKeyBuffer().toString('hex'), hexString)
}))
it('throws if the key is too long', sinon.test(function () {
var hexString = '10000000000000000000000000000000000000000000000000000000000000000'
assert.throws(function () {
var keyPair = new ECPair(new BigInteger(hexString, 16))
keyPair.getPrivateKeyBuffer()
}, new RegExp('Private key must be less than the curve order'))
}))
})
describe('fromWIF', function () {
fixtures.valid.forEach(function (f) {
it('imports ' + f.WIF + ' (' + f.network + ')', function () {
var network = NETWORKS[f.network]
var keyPair = ECPair.fromWIF(f.WIF, network)
assert.strictEqual(keyPair.d.toString(), f.d)
assert.strictEqual(keyPair.compressed, f.compressed)
assert.strictEqual(keyPair.network, network)
})
})
fixtures.valid.forEach(function (f) {
it('imports ' + f.WIF + ' (via list of networks)', function () {
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
assert.strictEqual(keyPair.d.toString(), f.d)
assert.strictEqual(keyPair.compressed, f.compressed)
assert.strictEqual(keyPair.network, NETWORKS[f.network])
})
})
fixtures.invalid.fromWIF.forEach(function (f) {
it('throws on ' + f.WIF, function () {
assert.throws(function () {
var networks = f.network ? NETWORKS[f.network] : NETWORKS_LIST
ECPair.fromWIF(f.WIF, networks)
}, new RegExp(f.exception))
})
})
})
describe('toWIF', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.WIF, function () {
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
var result = keyPair.toWIF()
assert.strictEqual(result, f.WIF)
})
})
})
describe('makeRandom', function () {
var d = Buffer.from('0404040404040404040404040404040404040404040404040404040404040404', 'hex')
var exWIF = 'KwMWvwRJeFqxYyhZgNwYuYjbQENDAPAudQx5VEmKJrUZcq6aL2pv'
describe('uses randombytes RNG', function () {
it('generates a ECPair', function () {
var stub = { randombytes: function () { return d } }
var ProxiedECPair = proxyquire('../src/ecpair', stub)
var keyPair = ProxiedECPair.makeRandom()
assert.strictEqual(keyPair.toWIF(), exWIF)
})
})
it('allows a custom RNG to be used', function () {
var keyPair = ECPair.makeRandom({
rng: function (size) { return d.slice(0, size) }
})
assert.strictEqual(keyPair.toWIF(), exWIF)
})
it('retains the same defaults as ECPair constructor', function () {
var keyPair = ECPair.makeRandom()
assert.strictEqual(keyPair.compressed, true)
assert.strictEqual(keyPair.network, NETWORKS.bitcoin)
})
it('supports the options parameter', function () {
var keyPair = ECPair.makeRandom({
compressed: false,
network: NETWORKS.testnet
})
assert.strictEqual(keyPair.compressed, false)
assert.strictEqual(keyPair.network, NETWORKS.testnet)
})
it('loops until d is within interval [1, n - 1] : 1', sinon.test(function () {
var rng = this.mock()
rng.exactly(2)
rng.onCall(0).returns(BigInteger.ZERO.toBuffer(32)) // invalid length
rng.onCall(1).returns(BigInteger.ONE.toBuffer(32)) // === 1
ECPair.makeRandom({ rng: rng })
}))
it('loops until d is within interval [1, n - 1] : n - 1', sinon.test(function () {
var rng = this.mock()
rng.exactly(3)
rng.onCall(0).returns(BigInteger.ZERO.toBuffer(32)) // < 1
rng.onCall(1).returns(curve.n.toBuffer(32)) // > n-1
rng.onCall(2).returns(curve.n.subtract(BigInteger.ONE).toBuffer(32)) // === n-1
ECPair.makeRandom({ rng: rng })
}))
})
describe('getAddress', function () {
fixtures.valid.forEach(function (f) {
it('returns ' + f.address + ' for ' + f.WIF, function () {
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
assert.strictEqual(keyPair.getAddress(), f.address)
})
})
})
describe('getNetwork', function () {
fixtures.valid.forEach(function (f) {
it('returns ' + f.network + ' for ' + f.WIF, function () {
var network = NETWORKS[f.network]
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
assert.strictEqual(keyPair.getNetwork(), network)
})
})
})
describe('ecdsa wrappers', function () {
var keyPair, hash
beforeEach(function () {
keyPair = ECPair.makeRandom()
hash = Buffer.alloc(32)
})
describe('signing', function () {
it('wraps ecdsa.sign', sinon.test(function () {
this.mock(fastcurve).expects('sign')
.once().withArgs(hash, keyPair.d).returns(undefined)
this.mock(ecdsa).expects('sign')
.once().withArgs(hash, keyPair.d)
keyPair.sign(hash)
}))
it('wraps fastcurve.sign', sinon.test(function () {
this.mock(fastcurve).expects('sign')
.once().withArgs(hash, keyPair.d)
keyPair.sign(hash)
}))
it('throws if no private key is found', function () {
keyPair.d = null
assert.throws(function () {
keyPair.sign(hash)
}, /Missing private key/)
})
})
describe('verify', function () {
var signature
beforeEach(function () {
signature = keyPair.sign(hash)
})
it('wraps ecdsa.verify', sinon.test(function () {
this.mock(fastcurve).expects('verify')
.once().withArgs(hash, signature, keyPair.getPublicKeyBuffer()).returns(undefined)
this.mock(ecdsa).expects('verify')
.once().withArgs(hash, signature, keyPair.Q)
keyPair.verify(hash, signature)
}))
it('wraps fastcurve.verify', sinon.test(function () {
this.mock(fastcurve).expects('verify')
.once().withArgs(hash, signature, keyPair.getPublicKeyBuffer())
keyPair.verify(hash, signature)
}))
it('handles falsey return values from fastcurve.verify', sinon.test(function () {
this.mock(fastcurve).expects('verify')
.once().withArgs(hash, signature, keyPair.getPublicKeyBuffer()).returns(false)
this.mock(ecdsa).expects('verify').never()
keyPair.verify(hash, signature)
}))
})
})
describe('fromPrivateKeyBuffer', function () {
it('constructs an ECPair from a random private key buffer', function () {
var prvKeyBuffer = randombytes(32)
var ecPair = ECPair.fromPrivateKeyBuffer(prvKeyBuffer)
var ecPairPrvBuffer = ecPair.getPrivateKeyBuffer()
assert.strictEqual(Buffer.compare(ecPairPrvBuffer, prvKeyBuffer), 0)
})
it('throws if the private key is out of range', function () {
var prvKeyBuffer = Buffer.alloc(32, 0xff)
assert.throws(function () {
ECPair.fromPrivateKeyBuffer(prvKeyBuffer)
}, new RegExp('Private key must be less than the curve order'))
})
it('throws if the private key buffer is not a buffer', function () {
assert.throws(function () {
ECPair.fromPrivateKeyBuffer('not a buffer')
}, new RegExp('invalid private key buffer'))
})
it('throws if the private key buffer is not 32 bytes', function () {
assert.throws(function () {
ECPair.fromPrivateKeyBuffer(Buffer.alloc(31, 0x00))
}, new RegExp('invalid private key buffer'))
assert.throws(function () {
ECPair.fromPrivateKeyBuffer(Buffer.alloc(33, 0x00))
}, new RegExp('invalid private key buffer'))
})
})
})