bitcoinjs-lib/src/ecpubkey.js
2014-10-05 15:36:25 +11:00

51 lines
1.2 KiB
JavaScript

var crypto = require('./crypto')
var ecdsa = require('./ecdsa')
var enforceType = require('./types')
var networks = require('./networks')
var Address = require('./address')
var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1')
function ECPubKey(Q, compressed) {
if (compressed === undefined) compressed = true
enforceType(ecurve.Point, Q)
enforceType('Boolean', compressed)
this.compressed = compressed
this.Q = Q
}
// Static constructors
ECPubKey.fromBuffer = function(buffer) {
var Q = ecurve.Point.decodeFrom(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(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