bitcoinjs-lib/src/ecpair.js

158 lines
3.7 KiB
JavaScript
Raw Normal View History

var bs58check = require('bs58check')
2014-10-17 04:31:01 +02:00
var bcrypto = require('./crypto')
var ecdsa = require('./ecdsa')
var ecurve = require('ecurve')
2015-07-28 08:42:57 +02:00
var NETWORKS = require('./networks')
2014-10-17 04:31:01 +02:00
var randomBytes = require('randombytes')
2015-08-11 09:01:47 +02:00
var typeforce = require('typeforce')
var types = require('./types')
2014-10-17 04:31:01 +02:00
var BigInteger = require('bigi')
function ECPair (d, Q, options) {
options = options || {}
2015-08-11 09:01:47 +02:00
typeforce({
compressed: types.maybe(types.Boolean),
network: types.maybe(types.Network)
}, 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')
if (d.compareTo(ECPair.curve.n) >= 0) throw new Error('Private key must be less than the curve order')
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) {
this.__Q = ECPair.curve.G.multiply(this.d)
}
return this.__Q
}
})
2014-10-17 04:31:01 +02:00
// Public access to secp256k1 curve
ECPair.curve = ecurve.getCurveByName('secp256k1')
ECPair.fromPublicKeyBuffer = function (buffer, network) {
var Q = ecurve.Point.decodeFrom(ECPair.curve, buffer)
return new ECPair(null, Q, {
compressed: Q.compressed,
network: network
})
}
2015-07-28 08:42:57 +02:00
ECPair.fromWIF = function (string, networks) {
var payload = bs58check.decode(string)
2014-10-17 04:31:01 +02:00
var version = payload.readUInt8(0)
var compressed
if (payload.length === 34) {
2015-08-11 09:03:10 +02:00
if (payload[33] !== 0x01) throw new Error('Invalid compression flag')
2014-10-17 04:31:01 +02:00
// truncate the version/compression bytes
payload = payload.slice(1, -1)
compressed = true
// no compression flag
} else {
2015-08-11 09:03:10 +02:00
if (payload.length !== 33) throw new Error('Invalid WIF payload length')
2014-10-17 04:31:01 +02:00
// Truncate the version byte
payload = payload.slice(1)
compressed = false
}
2015-07-28 08:42:57 +02:00
var network
// list of networks?
if (Array.isArray(networks)) {
network = networks.filter(function (network) {
return version === network.wif
2015-07-28 08:42:57 +02:00
}).pop() || {}
// otherwise, assume a network object (or default to bitcoin)
} else {
network = networks || NETWORKS.bitcoin
}
if (version !== network.wif) throw new Error('Invalid network')
2015-07-28 08:42:57 +02:00
2014-10-17 04:31:01 +02:00
var d = BigInteger.fromBuffer(payload)
return new ECPair(d, null, {
compressed: compressed,
network: network
})
}
ECPair.makeRandom = function (options) {
options = options || {}
var rng = options.rng || randomBytes
var buffer = rng(32)
2015-08-11 09:01:47 +02:00
typeforce(types.Buffer256bit, buffer)
2014-10-17 04:31:01 +02:00
var d = BigInteger.fromBuffer(buffer)
d = d.mod(ECPair.curve.n)
return new ECPair(d, null, options)
}
ECPair.prototype.toWIF = function () {
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
var bufferLen = this.compressed ? 34 : 33
var buffer = new Buffer(bufferLen)
buffer.writeUInt8(this.network.wif, 0)
this.d.toBuffer(32).copy(buffer, 1)
if (this.compressed) {
buffer.writeUInt8(0x01, 33)
}
return bs58check.encode(buffer)
2014-10-17 04:31:01 +02:00
}
ECPair.prototype.getAddress = function () {
var pubKey = this.getPublicKeyBuffer()
var pubKeyHash = bcrypto.hash160(pubKey)
2014-10-17 04:31:01 +02:00
var payload = new Buffer(21)
payload.writeUInt8(this.network.pubKeyHash, 0)
pubKeyHash.copy(payload, 1)
return bs58check.encode(payload)
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
return ecdsa.sign(ECPair.curve, hash, this.d)
}
ECPair.prototype.verify = function (hash, signature) {
return ecdsa.verify(ECPair.curve, hash, signature, this.Q)
}
module.exports = ECPair