You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

46 lines
1.1 KiB

var bignum = require('bignum');
//x and y are both bignums
var Point = function(x, y) {
this.x = x;
this.y = y;
};
//convert the public key of a Key into a Point
Point.fromUncompressedPubKey = function(pubkey) {
var point = new Point();
point.x = bignum.fromBuffer(pubkey.slice(1, 33), {
size: 32
});
point.y = bignum.fromBuffer(pubkey.slice(33, 65), {
size: 32
});
return point;
};
//convert the Point into the Key containing a compressed public key
Point.prototype.toUncompressedPubKey = function() {
var xbuf = this.x.toBuffer({
size: 32
});
var ybuf = this.y.toBuffer({
size: 32
});
var prefix = new Buffer([0x04]);
var pubkey = Buffer.concat([prefix, xbuf, ybuf]);
return pubkey;
};
Point.prototype.toCompressedPubKey = function() {
var xbuf = this.x.toBuffer({size: 32});
var ybuf = this.y.toBuffer({size: 32});
if (ybuf[ybuf.length-1] % 2) { //odd
var pub = Buffer.concat([new Buffer([3]), xbuf]);
}
else { //even
var pub = Buffer.concat([new Buffer([2]), xbuf]);
}
return pub;
};
module.exports = Point;