bitcoinjs-lib/src/ecpubkey.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-05-13 16:44:29 +10:00
var crypto = require('./crypto')
var ecdsa = require('./ecdsa')
2014-12-23 15:08:20 +11:00
var typeForce = require('typeforce')
2014-05-13 16:44:29 +10:00
var networks = require('./networks')
var Address = require('./address')
2014-06-07 16:24:27 +10:00
var ecurve = require('ecurve')
var secp256k1 = ecurve.getCurveByName('secp256k1')
2014-05-13 16:44:29 +10:00
function ECPubKey(Q, compressed) {
if (compressed === undefined) compressed = true
2014-05-13 16:44:29 +10:00
2014-12-23 15:08:20 +11:00
typeForce('Point', Q)
typeForce('Boolean', compressed)
2014-05-13 16:44:29 +10:00
this.compressed = compressed
this.Q = Q
}
// Constants
ECPubKey.curve = secp256k1
2014-05-13 16:44:29 +10:00
// Static constructors
ECPubKey.fromBuffer = function(buffer) {
var Q = ecurve.Point.decodeFrom(ECPubKey.curve, buffer)
2014-06-07 16:24:27 +10:00
return new ECPubKey(Q, Q.compressed)
2014-05-13 16:44:29 +10:00
}
ECPubKey.fromHex = function(hex) {
return ECPubKey.fromBuffer(new Buffer(hex, 'hex'))
}
// Operations
ECPubKey.prototype.getAddress = function(network) {
network = network || networks.bitcoin
2014-05-13 16:44:29 +10:00
2014-06-17 21:05:18 +10:00
return new Address(crypto.hash160(this.toBuffer()), network.pubKeyHash)
2014-05-13 16:44:29 +10:00
}
2014-05-10 22:38:05 +10:00
ECPubKey.prototype.verify = function(hash, signature) {
return ecdsa.verify(ECPubKey.curve, hash, signature, this.Q)
2014-05-13 16:44:29 +10:00
}
// Export functions
ECPubKey.prototype.toBuffer = function() {
2014-05-17 00:33:33 +10:00
return this.Q.getEncoded(this.compressed)
2014-05-13 16:44:29 +10:00
}
ECPubKey.prototype.toHex = function() {
return this.toBuffer().toString('hex')
}
module.exports = ECPubKey