Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit 1bfd335

Browse files
Add src/bitgo/keyutil
* Move simplified `getPrivateKeyBuffer` and `fromPublicKeyBuffer` to `src/bitgo/keyutil`. * Use new helper funcs in `getPublicKeyBuffer` and `fromPublicKeyBuffer` * Add deprecation notices
1 parent fdf2d22 commit 1bfd335

File tree

6 files changed

+147
-26
lines changed

6 files changed

+147
-26
lines changed

src/bitgo/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
keyutil: require('./keyutil')
3+
}

src/bitgo/keyutil.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const ecurve = require('ecurve')
2+
const BigInteger = require('bigi')
3+
const ECPair = require('../ecpair')
4+
5+
/**
6+
* Create an ECPair from the raw private key bytes
7+
* @param {Buffer} buffer - Private key for the ECPair. Must be exactly 32 bytes.
8+
* @param {Object} [network] - Network for the ECPair. Defaults to bitcoin.
9+
* @return {ECPair}
10+
*/
11+
function privateKeyBufferToECPair (buffer, network) {
12+
if (!Buffer.isBuffer(buffer) || buffer.length !== 32) {
13+
throw new Error('invalid private key buffer')
14+
}
15+
16+
const d = BigInteger.fromBuffer(buffer)
17+
return new ECPair(d, null, { network })
18+
}
19+
20+
/**
21+
* Get the private key as a 32 bytes buffer. If it is smaller than 32 bytes, pad it with zeros
22+
* @param {ECPair} ecPair
23+
* @return {Buffer} 32 bytes
24+
*/
25+
function privateKeyBufferFromECPair (ecPair) {
26+
if (!(ecPair instanceof ECPair)) {
27+
throw new TypeError(`invalid argument ecpair`)
28+
}
29+
30+
if (!ecPair.d) throw new Error('Missing private key')
31+
32+
return ecPair.d.toBuffer(32)
33+
}
34+
35+
module.exports = {
36+
privateKeyBufferToECPair,
37+
privateKeyBufferFromECPair
38+
}

src/ecpair.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,6 @@ ECPair.fromPublicKeyBuffer = function (buffer, network) {
6060
network: network
6161
})
6262
}
63-
64-
/**
65-
* Create an ECPair from the raw private key bytes
66-
* @param buffer {Buffer} Private key for the ECPair. Must be exactly 32 bytes.
67-
* @param network {Object} Network for the ECPair. Defaults to bitcoin.
68-
* @return {ECPair}
69-
*/
70-
ECPair.fromPrivateKeyBuffer = function (buffer, network) {
71-
if (!Buffer.isBuffer(buffer) || buffer.length !== 32) {
72-
throw new Error('invalid private key buffer')
73-
}
74-
75-
var d = BigInteger.fromBuffer(buffer)
76-
return new ECPair(d, null, { network: network })
77-
}
78-
7963
ECPair.fromWIF = function (string, network) {
8064
var decoded = wif.decode(string)
8165
var version = decoded.version
@@ -131,16 +115,6 @@ ECPair.prototype.getPublicKeyBuffer = function () {
131115
return this.Q.getEncoded(this.compressed)
132116
}
133117

134-
/**
135-
* Get the private key as a 32 bytes buffer. If it is smaller than 32 bytes, pad it with zeros
136-
* @return Buffer
137-
*/
138-
ECPair.prototype.getPrivateKeyBuffer = function () {
139-
if (!this.d) throw new Error('Missing private key')
140-
141-
return this.d.toBuffer(32)
142-
}
143-
144118
ECPair.prototype.sign = function (hash) {
145119
if (!this.d) throw new Error('Missing private key')
146120

@@ -161,4 +135,26 @@ ECPair.prototype.verify = function (hash, signature) {
161135
return ecdsa.verify(hash, signature, this.Q)
162136
}
163137

138+
/**
139+
* @deprecated
140+
* Use {@see keyutil.privateKeyBufferToECPair} instead
141+
* Will be removed in next major version (BLOCK-267)
142+
*/
143+
ECPair.fromPrivateKeyBuffer = function (buffer, network) {
144+
// toplevel import unavailable due to circular dependency
145+
var keyutil = require('./bitgo/keyutil')
146+
return keyutil.privateKeyBufferToECPair(buffer, network)
147+
}
148+
149+
/**
150+
* @deprecated
151+
* Use {@see keyutil.privateKeyBufferFromECPair} instead
152+
* Will be removed in next major version (BLOCK-267)
153+
*/
154+
ECPair.prototype.getPrivateKeyBuffer = function () {
155+
// toplevel import unavailable due to circular dependency
156+
var keyutil = require('./bitgo/keyutil')
157+
return keyutil.privateKeyBufferFromECPair(this)
158+
}
159+
164160
module.exports = ECPair

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ for (var key in templates) {
66
}
77

88
module.exports = {
9+
bitgo: require('./bitgo'),
910
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
1011

1112
Block: require('./block'),

test/bitgo/keyutil.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* global describe, it */
2+
3+
const assert = require('assert')
4+
var randombytes = require('randombytes')
5+
6+
const BigInteger = require('bigi')
7+
8+
const {
9+
bitgo: {
10+
keyutil: {
11+
privateKeyBufferToECPair,
12+
privateKeyBufferFromECPair
13+
}
14+
},
15+
ECPair
16+
} = require('../../src')
17+
18+
describe('privateKeyBufferFromECPair', function () {
19+
it('pads short private keys', function () {
20+
var keyPair = new ECPair(BigInteger.ONE)
21+
assert.strictEqual(privateKeyBufferFromECPair(keyPair).byteLength, 32)
22+
assert.strictEqual(privateKeyBufferFromECPair(keyPair).toString('hex'),
23+
'0000000000000000000000000000000000000000000000000000000000000001')
24+
})
25+
26+
it('does not pad 32 bytes private keys', function () {
27+
var hexString = 'a000000000000000000000000000000000000000000000000000000000000000'
28+
var keyPair = new ECPair(new BigInteger(hexString, 16))
29+
assert.strictEqual(privateKeyBufferFromECPair(keyPair).byteLength, 32)
30+
assert.strictEqual(privateKeyBufferFromECPair(keyPair).toString('hex'), hexString)
31+
})
32+
33+
it('throws if the key is too long', function () {
34+
var hexString = '10000000000000000000000000000000000000000000000000000000000000000'
35+
36+
assert.throws(function () {
37+
var keyPair = new ECPair(new BigInteger(hexString, 16))
38+
privateKeyBufferFromECPair(keyPair)
39+
}, new RegExp('Private key must be less than the curve order'))
40+
})
41+
})
42+
43+
describe('privateKeyBufferToECPair', function () {
44+
it('constructs an ECPair from a random private key buffer', function () {
45+
var prvKeyBuffer = randombytes(32)
46+
var ecPair = privateKeyBufferToECPair(prvKeyBuffer)
47+
var ecPairPrvBuffer = privateKeyBufferFromECPair(ecPair)
48+
assert.strictEqual(Buffer.compare(ecPairPrvBuffer, prvKeyBuffer), 0)
49+
})
50+
51+
it('throws if the private key is out of range', function () {
52+
var prvKeyBuffer = Buffer.alloc(32, 0xff)
53+
assert.throws(function () {
54+
privateKeyBufferToECPair(prvKeyBuffer)
55+
}, new RegExp('private key out of range'))
56+
})
57+
58+
it('throws if the private key buffer is not a buffer', function () {
59+
assert.throws(function () {
60+
privateKeyBufferToECPair('not a buffer')
61+
}, new RegExp('invalid private key buffer'))
62+
})
63+
64+
it('throws if the private key buffer is not 32 bytes', function () {
65+
assert.throws(function () {
66+
privateKeyBufferToECPair(Buffer.alloc(31, 0x00))
67+
}, new RegExp('invalid private key buffer'))
68+
69+
assert.throws(function () {
70+
privateKeyBufferToECPair(Buffer.alloc(33, 0x00))
71+
}, new RegExp('invalid private key buffer'))
72+
})
73+
})

test/ecpair.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ describe('ECPair', function () {
8484
}))
8585
})
8686

87+
/**
88+
* @deprecated
89+
* Use {@see keyutil.privateKeyBufferToECPair} instead
90+
* Will be removed in next major version (BLOCK-267)
91+
*/
8792
describe('getPrivateKeyBuffer', function () {
8893
it('pads short private keys', sinon.test(function () {
8994
var keyPair = new ECPair(BigInteger.ONE)
@@ -301,6 +306,11 @@ describe('ECPair', function () {
301306
})
302307
})
303308

309+
/**
310+
* @deprecated
311+
* Use {@see keyutil.privateKeyBufferToECPair} instead
312+
* Will be removed in next major version (BLOCK-267)
313+
*/
304314
describe('fromPrivateKeyBuffer', function () {
305315
it('constructs an ECPair from a random private key buffer', function () {
306316
var prvKeyBuffer = randombytes(32)

0 commit comments

Comments
 (0)