rm ECKey/ECPubKey

This commit is contained in:
Daniel Cousens 2015-04-09 15:29:05 +10:00
parent 12dbe571a4
commit 5fee511ff5
2 changed files with 0 additions and 140 deletions

View file

@ -1,84 +0,0 @@
var assert = require('assert')
var base58check = require('bs58check')
var ecdsa = require('./ecdsa')
var networks = require('./networks')
var randomBytes = require('randombytes')
var typeForce = require('typeforce')
var BigInteger = require('bigi')
var ECPubKey = require('./ecpubkey')
var ecurve = require('ecurve')
var secp256k1 = ecurve.getCurveByName('secp256k1')
function ECKey (d, compressed) {
assert(d.signum() > 0, 'Private key must be greater than 0')
assert(d.compareTo(ECKey.curve.n) < 0, 'Private key must be less than the curve order')
var Q = ECKey.curve.G.multiply(d)
this.d = d
this.pub = new ECPubKey(Q, compressed)
}
// Constants
ECKey.curve = secp256k1
// Static constructors
ECKey.fromWIF = function (string) {
var payload = base58check.decode(string)
var compressed = false
// Ignore the version byte
payload = payload.slice(1)
if (payload.length === 33) {
assert.strictEqual(payload[32], 0x01, 'Invalid compression flag')
// Truncate the compression flag
payload = payload.slice(0, -1)
compressed = true
}
assert.equal(payload.length, 32, 'Invalid WIF payload length')
var d = BigInteger.fromBuffer(payload)
return new ECKey(d, compressed)
}
ECKey.makeRandom = function (compressed, rng) {
rng = rng || randomBytes
var buffer = rng(32)
typeForce('Buffer', buffer)
assert.equal(buffer.length, 32, 'Expected 256-bit Buffer from RNG')
var d = BigInteger.fromBuffer(buffer)
d = d.mod(ECKey.curve.n)
return new ECKey(d, compressed)
}
// Export functions
ECKey.prototype.toWIF = function (network) {
network = network || networks.bitcoin
var bufferLen = this.pub.compressed ? 34 : 33
var buffer = new Buffer(bufferLen)
buffer.writeUInt8(network.wif, 0)
this.d.toBuffer(32).copy(buffer, 1)
if (this.pub.compressed) {
buffer.writeUInt8(0x01, 33)
}
return base58check.encode(buffer)
}
// Operations
ECKey.prototype.sign = function (hash) {
return ecdsa.sign(ECKey.curve, hash, this.d)
}
module.exports = ECKey

View file

@ -1,56 +0,0 @@
var crypto = require('./crypto')
var ecdsa = require('./ecdsa')
var typeForce = require('typeforce')
var networks = require('./networks')
var Address = require('./address')
var ecurve = require('ecurve')
var secp256k1 = ecurve.getCurveByName('secp256k1')
function ECPubKey (Q, compressed) {
if (compressed === undefined) {
compressed = true
}
typeForce('Point', Q)
typeForce('Boolean', compressed)
this.compressed = compressed
this.Q = Q
}
// Constants
ECPubKey.curve = secp256k1
// Static constructors
ECPubKey.fromBuffer = function (buffer) {
var Q = ecurve.Point.decodeFrom(ECPubKey.curve, buffer)
return new ECPubKey(Q, Q.compressed)
}
ECPubKey.fromHex = function (hex) {
return ECPubKey.fromBuffer(new Buffer(hex, 'hex'))
}
// Operations
ECPubKey.prototype.getAddress = function (network) {
network = network || networks.bitcoin
return new Address(crypto.hash160(this.toBuffer()), network.pubKeyHash)
}
ECPubKey.prototype.verify = function (hash, signature) {
return ecdsa.verify(ECPubKey.curve, hash, signature, this.Q)
}
// Export functions
ECPubKey.prototype.toBuffer = function () {
return this.Q.getEncoded(this.compressed)
}
ECPubKey.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}
module.exports = ECPubKey