bitcoinjs-lib/src/ecpair.js

132 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-05-22 09:43:25 +02:00
let ecdsa = require('./ecdsa')
let randomBytes = require('randombytes')
let typeforce = require('typeforce')
let types = require('./types')
let wif = require('wif')
2014-10-17 04:31:01 +02:00
2018-05-22 09:43:25 +02:00
let NETWORKS = require('./networks')
let BigInteger = require('bigi')
2014-10-17 04:31:01 +02:00
2018-05-22 09:43:25 +02:00
let ecurve = require('ecurve')
let secp256k1 = ecdsa.__curve
2015-08-19 07:12:55 +02:00
2014-10-17 04:31:01 +02:00
function ECPair (d, Q, options) {
2015-08-19 07:12:55 +02:00
if (options) {
typeforce({
compressed: types.maybe(types.Boolean),
network: types.maybe(types.Network)
}, options)
}
2014-10-17 04:31:01 +02:00
2015-08-19 07:12:55 +02:00
options = options || {}
2014-10-17 04:31:01 +02:00
if (d) {
2015-08-11 09:01:47 +02:00
if (d.signum() <= 0) throw new Error('Private key must be greater than 0')
2015-08-19 07:12:55 +02:00
if (d.compareTo(secp256k1.n) >= 0) throw new Error('Private key must be less than the curve order')
2015-08-11 09:01:47 +02:00
if (Q) throw new TypeError('Unexpected publicKey parameter')
2014-10-17 04:31:01 +02:00
2015-03-19 03:25:41 +01:00
this.d = d
2014-10-17 04:31:01 +02:00
} else {
2015-08-11 09:01:47 +02:00
typeforce(types.ECPoint, Q)
2015-03-19 03:25:41 +01:00
this.__Q = Q
2014-10-17 04:31:01 +02:00
}
2015-08-11 09:01:47 +02:00
this.compressed = options.compressed === undefined ? true : options.compressed
this.network = options.network || NETWORKS.bitcoin
2014-10-17 04:31:01 +02:00
}
2015-03-19 03:25:41 +01:00
Object.defineProperty(ECPair.prototype, 'Q', {
2015-03-19 03:35:25 +01:00
get: function () {
2015-03-19 03:25:41 +01:00
if (!this.__Q && this.d) {
2015-08-19 07:12:55 +02:00
this.__Q = secp256k1.G.multiply(this.d)
2015-03-19 03:25:41 +01:00
}
return this.__Q
}
})
2014-10-17 04:31:01 +02:00
ECPair.fromPublicKeyBuffer = function (buffer, network) {
2015-08-19 07:12:55 +02:00
var Q = ecurve.Point.decodeFrom(secp256k1, buffer)
2014-10-17 04:31:01 +02:00
return new ECPair(null, Q, {
compressed: Q.compressed,
network: network
})
}
ECPair.fromWIF = function (string, network) {
var decoded = wif.decode(string)
var version = decoded.version
2014-10-17 04:31:01 +02:00
// list of networks?
if (types.Array(network)) {
network = network.filter(function (x) {
return version === x.wif
2016-02-24 11:08:40 +01:00
}).pop()
if (!network) throw new Error('Unknown network version')
// otherwise, assume a network object (or default to bitcoin)
} else {
network = network || NETWORKS.bitcoin
if (version !== network.wif) throw new Error('Invalid network version')
2015-07-28 08:42:57 +02:00
}
2016-02-24 11:08:40 +01:00
var d = BigInteger.fromBuffer(decoded.privateKey)
2014-10-17 04:31:01 +02:00
return new ECPair(d, null, {
compressed: decoded.compressed,
2014-10-17 04:31:01 +02:00
network: network
})
}
ECPair.makeRandom = function (options) {
options = options || {}
var rng = options.rng || randomBytes
2015-08-21 08:46:18 +02:00
var d
do {
var buffer = rng(32)
typeforce(types.Buffer256bit, buffer)
d = BigInteger.fromBuffer(buffer)
2015-09-08 16:35:54 +02:00
} while (d.signum() <= 0 || d.compareTo(secp256k1.n) >= 0)
2014-10-17 04:31:01 +02:00
return new ECPair(d, null, options)
}
2015-09-21 09:37:21 +02:00
ECPair.prototype.getNetwork = function () {
return this.network
}
2014-10-17 04:31:01 +02:00
ECPair.prototype.getPublicKeyBuffer = function () {
return this.Q.getEncoded(this.compressed)
}
ECPair.prototype.sign = function (hash) {
2015-08-11 09:03:10 +02:00
if (!this.d) throw new Error('Missing private key')
2014-10-17 04:31:01 +02:00
let signature = ecdsa.sign(hash, this.d)
return Buffer.concat([signature.r.toBuffer(32), signature.s.toBuffer(32)], 64)
2014-10-17 04:31:01 +02:00
}
ECPair.prototype.toWIF = function () {
if (!this.d) throw new Error('Missing private key')
return wif.encode(this.network.wif, this.d.toBuffer(32), this.compressed)
}
2014-10-17 04:31:01 +02:00
ECPair.prototype.verify = function (hash, signature) {
signature = {
r: BigInteger.fromBuffer(signature.slice(0, 32)),
s: BigInteger.fromBuffer(signature.slice(32, 64))
}
2015-08-25 12:43:32 +02:00
return ecdsa.verify(hash, signature, this.Q)
2014-10-17 04:31:01 +02:00
}
module.exports = ECPair