bitcoinjs-lib/src/ecpubkey.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-05-13 16:44:29 +10:00
var assert = require('assert')
var crypto = require('./crypto')
var ecdsa = require('./ecdsa')
var networks = require('./networks')
var Address = require('./address')
var ECPointFp = require('./ec').ECPointFp
var sec = require('./sec')
var ecparams = sec('secp256k1')
function ECPubKey(Q, compressed) {
2014-05-29 16:09:47 +10:00
assert(Q instanceof ECPointFp, 'Expected ECPointFP, got ' + Q)
2014-05-13 16:44:29 +10:00
if (compressed == undefined) compressed = true
2014-05-29 16:09:47 +10:00
assert.strictEqual(typeof compressed, 'boolean', 'Expected boolean, got ' + compressed)
2014-05-13 16:44:29 +10:00
this.compressed = compressed
this.Q = Q
}
// Static constructors
ECPubKey.fromBuffer = function(buffer) {
var decode = ECPointFp.decodeFrom(ecparams.getCurve(), buffer)
return new ECPubKey(decode.Q, decode.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
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) {
2014-05-24 16:25:38 +10:00
return ecdsa.verify(ecparams, 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