Pubkey().fromPrivkey()
This commit is contained in:
parent
c39acbcca3
commit
5313085773
@ -40,7 +40,7 @@ Key.prototype.getAddress = function(networkstr) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Key.prototype.privkey2pubkey = function() {
|
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() {
|
Key.prototype.toString = function() {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
var BN = require('./bn');
|
var BN = require('./bn');
|
||||||
var point = require('./point');
|
var Point = require('./point');
|
||||||
var constants = require('./constants');
|
var constants = require('./constants');
|
||||||
var base58check = require('./base58check');
|
var base58check = require('./base58check');
|
||||||
var Random = require('./random');
|
var Random = require('./random');
|
||||||
@ -22,14 +22,14 @@ Privkey.prototype.fromRandom = function() {
|
|||||||
do {
|
do {
|
||||||
var privbuf = Random.getRandomBuffer(32);
|
var privbuf = Random.getRandomBuffer(32);
|
||||||
var bn = BN().fromBuffer(privbuf);
|
var bn = BN().fromBuffer(privbuf);
|
||||||
var condition = bn.lt(point.getN());
|
var condition = bn.lt(Point.getN());
|
||||||
} while (!condition);
|
} while (!condition);
|
||||||
this.bn = bn;
|
this.bn = bn;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
Privkey.prototype.validate = function() {
|
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');
|
throw new Error('Number must be less than N');
|
||||||
if (typeof constants[this.networkstr] === undefined)
|
if (typeof constants[this.networkstr] === undefined)
|
||||||
throw new Error('Must specify the networkstr ("mainnet" or "testnet")');
|
throw new Error('Must specify the networkstr ("mainnet" or "testnet")');
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
var Point = require('./point');
|
var Point = require('./point');
|
||||||
var bn = require('./bn');
|
var bn = require('./bn');
|
||||||
|
var privkey = require('./privkey');
|
||||||
|
|
||||||
var Pubkey = function Pubkey(obj) {
|
var Pubkey = function Pubkey(obj) {
|
||||||
if (!(this instanceof Pubkey))
|
if (!(this instanceof Pubkey))
|
||||||
@ -16,6 +17,14 @@ Pubkey.prototype.set = function(obj) {
|
|||||||
return this;
|
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) {
|
Pubkey.prototype.fromDER = function(buf) {
|
||||||
if (buf[0] == 0x04) {
|
if (buf[0] == 0x04) {
|
||||||
var xbuf = buf.slice(1, 33);
|
var xbuf = buf.slice(1, 33);
|
||||||
|
|||||||
@ -2,6 +2,7 @@ var should = require('chai').should();
|
|||||||
var Pubkey = require('../lib/pubkey');
|
var Pubkey = require('../lib/pubkey');
|
||||||
var Point = require('../lib/point');
|
var Point = require('../lib/point');
|
||||||
var Bn = require('../lib/bn');
|
var Bn = require('../lib/bn');
|
||||||
|
var Privkey = require('../lib/privkey');
|
||||||
|
|
||||||
describe('Pubkey', function() {
|
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() {
|
describe('#fromDER', function() {
|
||||||
|
|
||||||
it('should parse this uncompressed public key', function() {
|
it('should parse this uncompressed public key', function() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user