|
|
@ -1,12 +1,13 @@ |
|
|
|
var Point = require('./point'); |
|
|
|
var bn = require('./bn'); |
|
|
|
|
|
|
|
var Pubkey = function Pubkey(point) { |
|
|
|
var Pubkey = function Pubkey(point, compressed) { |
|
|
|
if (!(this instanceof Pubkey)) |
|
|
|
return new Pubkey(point); |
|
|
|
if (point && !point.getX() && !point.getY()) |
|
|
|
throw new Error('Invalid point'); |
|
|
|
this.point = point; |
|
|
|
this.compressed = compressed; |
|
|
|
}; |
|
|
|
|
|
|
|
Pubkey.prototype.fromDER = function(buf) { |
|
|
@ -18,14 +19,17 @@ Pubkey.prototype.fromDER = function(buf) { |
|
|
|
var x = bn(xbuf); |
|
|
|
var y = bn(ybuf); |
|
|
|
this.point = Point(x, y); |
|
|
|
this.compressed = false; |
|
|
|
} else if (buf[0] == 0x03) { |
|
|
|
var xbuf = buf.slice(1); |
|
|
|
var x = bn(xbuf); |
|
|
|
this.fromX(true, x); |
|
|
|
this.compressed = true; |
|
|
|
} else if (buf[0] == 0x02) { |
|
|
|
var xbuf = buf.slice(1); |
|
|
|
var x = bn(xbuf); |
|
|
|
this.fromX(false, x); |
|
|
|
this.compressed = true; |
|
|
|
} else { |
|
|
|
throw new Error('Invalid DER format pubkey'); |
|
|
|
} |
|
|
@ -43,10 +47,12 @@ Pubkey.prototype.fromX = function(odd, x) { |
|
|
|
}; |
|
|
|
|
|
|
|
Pubkey.prototype.toBuffer = function() { |
|
|
|
return this.toDER(true); |
|
|
|
var compressed = typeof this.compressed === 'undefined' ? true : this.compressed; |
|
|
|
return this.toDER(compressed); |
|
|
|
}; |
|
|
|
|
|
|
|
Pubkey.prototype.toDER = function(compressed) { |
|
|
|
compressed = typeof this.compressed === 'undefined' ? compressed : this.compressed; |
|
|
|
if (typeof compressed !== 'boolean') |
|
|
|
throw new Error('Must specify whether the public key is compressed or not (true or false)'); |
|
|
|
|
|
|
@ -70,7 +76,8 @@ Pubkey.prototype.toDER = function(compressed) { |
|
|
|
}; |
|
|
|
|
|
|
|
Pubkey.prototype.toString = function() { |
|
|
|
return this.toDER(true).toString('hex'); |
|
|
|
var compressed = typeof this.compressed === 'undefined' ? true : this.compressed; |
|
|
|
return this.toDER(compressed).toString('hex'); |
|
|
|
}; |
|
|
|
|
|
|
|
//https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf
|
|
|
|