Pubkey().fromPrivkey()

This commit is contained in:
Ryan X. Charles 2014-08-28 20:19:30 -07:00
parent c39acbcca3
commit 5313085773
4 changed files with 22 additions and 4 deletions

View File

@ -40,7 +40,7 @@ Key.prototype.getAddress = function(networkstr) {
};
Key.prototype.privkey2pubkey = function() {
this.pubkey = new Pubkey({point: point.getG().mul(this.privkey.bn), compressed: this.privkey.compressed});
this.pubkey = Pubkey().fromPrivkey(this.privkey);
};
Key.prototype.toString = function() {

View File

@ -1,5 +1,5 @@
var BN = require('./bn');
var point = require('./point');
var Point = require('./point');
var constants = require('./constants');
var base58check = require('./base58check');
var Random = require('./random');
@ -22,14 +22,14 @@ Privkey.prototype.fromRandom = function() {
do {
var privbuf = Random.getRandomBuffer(32);
var bn = BN().fromBuffer(privbuf);
var condition = bn.lt(point.getN());
var condition = bn.lt(Point.getN());
} while (!condition);
this.bn = bn;
return this;
};
Privkey.prototype.validate = function() {
if (!this.bn.lt(point.getN()))
if (!this.bn.lt(Point.getN()))
throw new Error('Number must be less than N');
if (typeof constants[this.networkstr] === undefined)
throw new Error('Must specify the networkstr ("mainnet" or "testnet")');

View File

@ -1,5 +1,6 @@
var Point = require('./point');
var bn = require('./bn');
var privkey = require('./privkey');
var Pubkey = function Pubkey(obj) {
if (!(this instanceof Pubkey))
@ -16,6 +17,14 @@ Pubkey.prototype.set = function(obj) {
return this;
};
Pubkey.prototype.fromPrivkey = function(privkey) {
this.set({
point: Point.getG().mul(privkey.bn),
compressed: privkey.compressed}
);
return this;
};
Pubkey.prototype.fromDER = function(buf) {
if (buf[0] == 0x04) {
var xbuf = buf.slice(1, 33);

View File

@ -2,6 +2,7 @@ var should = require('chai').should();
var Pubkey = require('../lib/pubkey');
var Point = require('../lib/point');
var Bn = require('../lib/bn');
var Privkey = require('../lib/privkey');
describe('Pubkey', function() {
@ -24,6 +25,14 @@ describe('Pubkey', function() {
});
describe('#fromPrivkey', function() {
it('should make a public key from a privkey', function() {
should.exist(Pubkey().fromPrivkey(Privkey().fromRandom()));
});
});
describe('#fromDER', function() {
it('should parse this uncompressed public key', function() {