add "compressed" feature to pubkeys
...not just privkeys. since, of course, they can be compressed or uncompressed.
This commit is contained in:
parent
ee0dff9e2e
commit
7c945cdc01
|
@ -11,10 +11,8 @@ function Address(hashbuf, network, type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
};
|
};
|
||||||
|
|
||||||
Address.prototype.fromPubkey = function(pubkey, network, compressed) {
|
Address.prototype.fromPubkey = function(pubkey, network) {
|
||||||
if (typeof compressed === 'undefined')
|
this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer());
|
||||||
compressed = true;
|
|
||||||
this.hashbuf = Hash.sha256ripemd160(pubkey.toDER(compressed));
|
|
||||||
this.network = network || 'mainnet';
|
this.network = network || 'mainnet';
|
||||||
this.type = 'pubkeyhash';
|
this.type = 'pubkeyhash';
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -34,12 +34,12 @@ Key.prototype.fromString = function(str) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Key.prototype.getAddress = function(network, compressed) {
|
Key.prototype.getAddress = function(network) {
|
||||||
return (new Address()).fromPubkey(this.pubkey, network, compressed);
|
return Address().fromPubkey(this.pubkey, network);
|
||||||
};
|
};
|
||||||
|
|
||||||
Key.prototype.privkey2pubkey = function() {
|
Key.prototype.privkey2pubkey = function() {
|
||||||
this.pubkey = new Pubkey(point.getG().mul(this.privkey.bn));
|
this.pubkey = new Pubkey(point.getG().mul(this.privkey.bn), this.privkey.compressed);
|
||||||
};
|
};
|
||||||
|
|
||||||
Key.prototype.toString = function() {
|
Key.prototype.toString = function() {
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
var Point = require('./point');
|
var Point = require('./point');
|
||||||
var bn = require('./bn');
|
var bn = require('./bn');
|
||||||
|
|
||||||
var Pubkey = function Pubkey(point) {
|
var Pubkey = function Pubkey(point, compressed) {
|
||||||
if (!(this instanceof Pubkey))
|
if (!(this instanceof Pubkey))
|
||||||
return new Pubkey(point);
|
return new Pubkey(point);
|
||||||
if (point && !point.getX() && !point.getY())
|
if (point && !point.getX() && !point.getY())
|
||||||
throw new Error('Invalid point');
|
throw new Error('Invalid point');
|
||||||
this.point = point;
|
this.point = point;
|
||||||
|
this.compressed = compressed;
|
||||||
};
|
};
|
||||||
|
|
||||||
Pubkey.prototype.fromDER = function(buf) {
|
Pubkey.prototype.fromDER = function(buf) {
|
||||||
|
@ -18,14 +19,17 @@ Pubkey.prototype.fromDER = function(buf) {
|
||||||
var x = bn(xbuf);
|
var x = bn(xbuf);
|
||||||
var y = bn(ybuf);
|
var y = bn(ybuf);
|
||||||
this.point = Point(x, y);
|
this.point = Point(x, y);
|
||||||
|
this.compressed = false;
|
||||||
} else if (buf[0] == 0x03) {
|
} else if (buf[0] == 0x03) {
|
||||||
var xbuf = buf.slice(1);
|
var xbuf = buf.slice(1);
|
||||||
var x = bn(xbuf);
|
var x = bn(xbuf);
|
||||||
this.fromX(true, x);
|
this.fromX(true, x);
|
||||||
|
this.compressed = true;
|
||||||
} else if (buf[0] == 0x02) {
|
} else if (buf[0] == 0x02) {
|
||||||
var xbuf = buf.slice(1);
|
var xbuf = buf.slice(1);
|
||||||
var x = bn(xbuf);
|
var x = bn(xbuf);
|
||||||
this.fromX(false, x);
|
this.fromX(false, x);
|
||||||
|
this.compressed = true;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid DER format pubkey');
|
throw new Error('Invalid DER format pubkey');
|
||||||
}
|
}
|
||||||
|
@ -43,10 +47,12 @@ Pubkey.prototype.fromX = function(odd, x) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Pubkey.prototype.toBuffer = function() {
|
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) {
|
Pubkey.prototype.toDER = function(compressed) {
|
||||||
|
compressed = typeof this.compressed === 'undefined' ? compressed : this.compressed;
|
||||||
if (typeof compressed !== 'boolean')
|
if (typeof compressed !== 'boolean')
|
||||||
throw new Error('Must specify whether the public key is compressed or not (true or false)');
|
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() {
|
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
|
//https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf
|
||||||
|
|
|
@ -26,7 +26,8 @@ describe('Address', function() {
|
||||||
var pubkey = new Pubkey();
|
var pubkey = new Pubkey();
|
||||||
pubkey.fromDER(new Buffer('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004', 'hex'));
|
pubkey.fromDER(new Buffer('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004', 'hex'));
|
||||||
var address = new Address();
|
var address = new Address();
|
||||||
address.fromPubkey(pubkey, 'mainnet', false);
|
pubkey.compressed = false;
|
||||||
|
address.fromPubkey(pubkey, 'mainnet');
|
||||||
address.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
|
address.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue