132 lines
3.1 KiB
JavaScript
132 lines
3.1 KiB
JavaScript
var bcrypto = require('./crypto')
|
|
var bs58check = require('bs58check')
|
|
var ecdsa = require('./ecdsa')
|
|
var randomBytes = require('randombytes')
|
|
var typeforce = require('typeforce')
|
|
var types = require('./types')
|
|
var wif = require('wif')
|
|
|
|
var NETWORKS = require('./networks')
|
|
var BigInteger = require('bigi')
|
|
|
|
var ecurve = require('ecurve')
|
|
var secp256k1 = ecdsa.__curve
|
|
|
|
function ECPair (d, Q, options) {
|
|
if (options) {
|
|
typeforce({
|
|
compressed: types.maybe(types.Boolean),
|
|
network: types.maybe(types.Network)
|
|
}, options)
|
|
}
|
|
|
|
options = options || {}
|
|
|
|
if (d) {
|
|
if (d.signum() <= 0) throw new Error('Private key must be greater than 0')
|
|
if (d.compareTo(secp256k1.n) >= 0) throw new Error('Private key must be less than the curve order')
|
|
if (Q) throw new TypeError('Unexpected publicKey parameter')
|
|
|
|
this.d = d
|
|
} else {
|
|
typeforce(types.ECPoint, Q)
|
|
|
|
this.__Q = Q
|
|
}
|
|
|
|
this.compressed = options.compressed === undefined ? true : options.compressed
|
|
this.network = options.network || NETWORKS.bitcoin
|
|
}
|
|
|
|
Object.defineProperty(ECPair.prototype, 'Q', {
|
|
get: function () {
|
|
if (!this.__Q && this.d) {
|
|
this.__Q = secp256k1.G.multiply(this.d)
|
|
}
|
|
|
|
return this.__Q
|
|
}
|
|
})
|
|
|
|
ECPair.fromPublicKeyBuffer = function (buffer, network) {
|
|
var Q = ecurve.Point.decodeFrom(secp256k1, buffer)
|
|
|
|
return new ECPair(null, Q, {
|
|
compressed: Q.compressed,
|
|
network: network
|
|
})
|
|
}
|
|
|
|
ECPair.fromWIF = function (string, network) {
|
|
network = network || NETWORKS.bitcoin
|
|
var buffer = bs58check.decode(string)
|
|
|
|
if (types.Array(network)) {
|
|
var version = buffer[0]
|
|
|
|
network = network.filter(function (network) {
|
|
return version === network.wif
|
|
}).pop() || {}
|
|
}
|
|
|
|
var decoded = wif.decodeRaw(network.wif, buffer)
|
|
var d = BigInteger.fromBuffer(decoded.d)
|
|
|
|
return new ECPair(d, null, {
|
|
compressed: decoded.compressed,
|
|
network: network
|
|
})
|
|
}
|
|
|
|
ECPair.makeRandom = function (options) {
|
|
options = options || {}
|
|
|
|
var rng = options.rng || randomBytes
|
|
|
|
var d
|
|
do {
|
|
var buffer = rng(32)
|
|
typeforce(types.Buffer256bit, buffer)
|
|
|
|
d = BigInteger.fromBuffer(buffer)
|
|
} while (d.signum() <= 0 || d.compareTo(secp256k1.n) >= 0)
|
|
|
|
return new ECPair(d, null, options)
|
|
}
|
|
|
|
ECPair.prototype.getAddress = function () {
|
|
var pubKey = this.getPublicKeyBuffer()
|
|
var pubKeyHash = bcrypto.hash160(pubKey)
|
|
|
|
var payload = new Buffer(21)
|
|
payload.writeUInt8(this.network.pubKeyHash, 0)
|
|
pubKeyHash.copy(payload, 1)
|
|
|
|
return bs58check.encode(payload)
|
|
}
|
|
|
|
ECPair.prototype.getNetwork = function () {
|
|
return this.network
|
|
}
|
|
|
|
ECPair.prototype.getPublicKeyBuffer = function () {
|
|
return this.Q.getEncoded(this.compressed)
|
|
}
|
|
|
|
ECPair.prototype.sign = function (hash) {
|
|
if (!this.d) throw new Error('Missing private key')
|
|
|
|
return ecdsa.sign(hash, this.d)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
ECPair.prototype.verify = function (hash, signature) {
|
|
return ecdsa.verify(hash, signature, this.Q)
|
|
}
|
|
|
|
module.exports = ECPair
|