ECKey: remove *Buffer/*Hex functions
An ECKey is a composition of a private key (D), a public key (Q) and its compression flag. These functions gave the impression of serialization of this composition, when really they only serialized `D`. They have therefore been removed in favour of always using a sane serialization format (WIF) that matches the needed behaviour. If a user needs the previous functionality, simply use `privKey.D.*` instead of `privKey.*`, as BigInteger supports `*Buffer/*Hex` functions as expected.
This commit is contained in:
parent
bf109f32a9
commit
10fe4474d1
7 changed files with 196 additions and 110 deletions
src
43
src/eckey.js
43
src/eckey.js
|
@ -21,29 +21,22 @@ function ECKey(D, compressed) {
|
|||
}
|
||||
|
||||
// Static constructors
|
||||
ECKey.fromBuffer = function(buffer, compressed) {
|
||||
assert(Buffer.isBuffer(buffer), 'First argument must be a Buffer')
|
||||
assert.strictEqual(buffer.length, 32, 'Invalid buffer length')
|
||||
|
||||
var D = BigInteger.fromBuffer(buffer)
|
||||
return new ECKey(D, compressed)
|
||||
}
|
||||
|
||||
ECKey.fromHex = function(hex, compressed) {
|
||||
return ECKey.fromBuffer(new Buffer(hex, 'hex'), compressed)
|
||||
}
|
||||
|
||||
ECKey.fromWIF = function(string) {
|
||||
var decode = base58check.decode(string)
|
||||
|
||||
var payload = decode.payload
|
||||
var compressed = false
|
||||
|
||||
if (payload.length === 33) {
|
||||
assert.strictEqual(payload[32], 0x01, 'Invalid WIF string')
|
||||
|
||||
return ECKey.fromBuffer(payload.slice(0, 32), true)
|
||||
payload = payload.slice(0, -1)
|
||||
compressed = true
|
||||
}
|
||||
|
||||
return ECKey.fromBuffer(payload, false)
|
||||
assert.equal(payload.length, 32, 'Invalid WIF payload length')
|
||||
|
||||
var D = BigInteger.fromBuffer(payload.slice(0, 32))
|
||||
return new ECKey(D, compressed)
|
||||
}
|
||||
|
||||
ECKey.makeRandom = function(compressed, rng) {
|
||||
|
@ -56,24 +49,11 @@ ECKey.makeRandom = function(compressed, rng) {
|
|||
return new ECKey(D, compressed)
|
||||
}
|
||||
|
||||
// Operations
|
||||
ECKey.prototype.sign = function(hash) {
|
||||
return ecdsa.sign(hash, this.D)
|
||||
}
|
||||
|
||||
// Export functions
|
||||
ECKey.prototype.toBuffer = function() {
|
||||
return this.D.toBuffer(32)
|
||||
}
|
||||
|
||||
ECKey.prototype.toHex = function() {
|
||||
return this.toBuffer().toString('hex')
|
||||
}
|
||||
|
||||
ECKey.prototype.toWIF = function(version) {
|
||||
version = version || networks.bitcoin.wif
|
||||
|
||||
var buffer = this.toBuffer()
|
||||
var buffer = this.D.toBuffer(32)
|
||||
if (this.pub.compressed) {
|
||||
buffer = Buffer.concat([buffer, new Buffer([0x01])])
|
||||
}
|
||||
|
@ -81,4 +61,9 @@ ECKey.prototype.toWIF = function(version) {
|
|||
return base58check.encode(buffer, version)
|
||||
}
|
||||
|
||||
// Operations
|
||||
ECKey.prototype.sign = function(hash) {
|
||||
return ecdsa.sign(hash, this.D)
|
||||
}
|
||||
|
||||
module.exports = ECKey
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue