-
Notifications
You must be signed in to change notification settings - Fork 285
/
ecpair.spec.js
232 lines (185 loc) · 6.33 KB
/
ecpair.spec.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
import ecurve from 'ecurve'
import sinon from 'sinon'
import sinonTestFactory from 'sinon-test'
import BigInteger from 'bigi'
import ECPair from '../../lib/crypto/ecpair'
import ecdsa from '../../lib/crypto/ecdsa'
import configManager from '../../lib/managers/config'
import fixtures from './fixtures/ecpair.json'
import { NETWORKS, NETWORKS_LIST } from '../utils/network-list'
const curve = ecdsa.__curve
const sinonTest = sinonTestFactory(sinon)
beforeEach(() => configManager.setConfig(NETWORKS.mainnet))
describe('ECPair', () => {
describe('constructor', () => {
it('defaults to compressed', () => {
const keyPair = new ECPair(BigInteger.ONE)
expect(keyPair.compressed).toBeTruthy()
})
it('supports the uncompressed option', () => {
const keyPair = new ECPair(BigInteger.ONE, null, {
compressed: false
})
expect(keyPair.compressed).toBeFalsy()
})
it('supports the network option', () => {
const keyPair = new ECPair(BigInteger.ONE, null, {
compressed: false,
network: NETWORKS.devnet
})
expect(keyPair.network).toEqual(NETWORKS.devnet)
})
fixtures.valid.forEach((f) => {
it(`calculates the public point for ${f.WIF}`, () => {
const d = new BigInteger(f.d)
const keyPair = new ECPair(d, null, {
compressed: f.compressed,
network: NETWORKS[f.network]
})
expect(keyPair.getPublicKeyBuffer().toString('hex')).toBe(f.Q)
})
})
fixtures.invalid.constructor.forEach((f) => {
it(`throws ${f.exception}`, () => {
const d = f.d && new BigInteger(f.d) // eslint-disable-line no-new
const Q = f.Q && ecurve.Point.decodeFrom(curve, Buffer.from(f.Q, 'hex'))
expect(() => {
new ECPair(d, Q, f.options) // eslint-disable-line no-new
}).toThrowError(new RegExp(f.exception))
})
})
})
describe('getPublicKeyBuffer', () => {
let keyPair
beforeEach(() => {
keyPair = new ECPair(BigInteger.ONE)
})
it('wraps Q.getEncoded', sinonTest(function () {
this
.mock(keyPair.Q)
.expects('getEncoded')
.once()
.withArgs(keyPair.compressed)
keyPair.getPublicKeyBuffer()
}))
})
describe('fromWIF', () => {
fixtures.valid.forEach((f) => {
it(`imports ${f.WIF} (${f.network})`, () => {
const network = NETWORKS[f.network]
const keyPair = ECPair.fromWIF(f.WIF, network)
expect(keyPair.d.toString()).toBe(f.d)
expect(keyPair.getPublicKeyBuffer().toString('hex')).toBe(f.Q)
expect(keyPair.compressed).toBe(f.compressed)
expect(keyPair.network).toEqual(network)
})
})
fixtures.valid.forEach((f) => {
it(`imports ${f.WIF} (via list of networks)`, () => {
const network = NETWORKS[f.network]
const keyPair = ECPair.fromWIF(f.WIF, network)
expect(keyPair.d.toString()).toBe(f.d)
expect(keyPair.getPublicKeyBuffer().toString('hex')).toBe(f.Q)
expect(keyPair.compressed).toBe(f.compressed)
expect(keyPair.network).toEqual(network)
})
})
fixtures.invalid.fromWIF.forEach((f) => {
it(`throws on ${f.WIF}`, () => {
expect(() => {
const networks = f.network ? NETWORKS[f.network] : NETWORKS_LIST
ECPair.fromWIF(f.WIF, networks)
}).toThrowError(new RegExp(f.exception))
})
})
})
describe('toWIF', () => {
fixtures.valid.forEach((f) => {
it(`exports ${f.WIF}`, () => {
const keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
expect(keyPair.toWIF()).toBe(f.WIF)
})
})
})
describe('makeRandom', () => {
let d = Buffer.from('0404040404040404040404040404040404040404040404040404040404040404', 'hex')
let exWIF = 'S9hzwiZ5ziKjUiFpuZX4Lri3rUocDxZSTy7YzKKHvx8TSjUrYQ27'
it('uses randombytes RNG to generate a ECPair', () => {
const stub = {
rng: () => {
return d
}
}
const keyPair = ECPair.makeRandom(stub)
expect(keyPair.toWIF()).toBe(exWIF)
})
it('allows a custom RNG to be used', () => {
const keyPair = ECPair.makeRandom({
rng: (size) => {
return d.slice(0, size)
}
})
expect(keyPair.toWIF()).toBe(exWIF)
})
it('retains the same defaults as ECPair constructor', () => {
const keyPair = ECPair.makeRandom()
expect(keyPair.compressed).toBeTruthy()
expect(keyPair.network).toEqual(NETWORKS.mainnet)
})
it('supports the options parameter', () => {
const keyPair = ECPair.makeRandom({
compressed: false,
network: NETWORKS.devnet
})
expect(keyPair.compressed).toBeFalsy()
expect(keyPair.network).toEqual(NETWORKS.devnet)
})
it('loops until d is within interval [1, n - 1] : 1', sinonTest(function () {
const 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})
}))
it('loops until d is within interval [1, n - 1] : n - 1', sinonTest(function () {
const 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})
}))
})
describe('getAddress', () => {
fixtures.valid.forEach((f) => {
it(`returns ${f.address} for ${f.WIF}`, () => {
const keyPair = ECPair.fromWIF(f.WIF, NETWORKS[f.network])
expect(keyPair.getAddress()).toBe(f.address)
})
})
})
describe('getNetwork', () => {
fixtures.valid.forEach((f) => {
it(`returns ${f.network} for ${f.WIF}`, () => {
const keyPair = ECPair.fromWIF(f.WIF, NETWORKS[f.network])
expect(keyPair.network).toEqual(NETWORKS[f.network])
})
})
})
describe('ecdsa wrappers', () => {
let keyPair
let hash
beforeEach(() => {
keyPair = ECPair.makeRandom()
hash = Buffer.alloc(32)
})
describe('signing', () => {
it('throws if no private key is found', () => {
keyPair.d = null
expect(() => {
keyPair.sign(hash)
}).toThrowError(/Missing private key/)
})
})
})
})