diff --git a/examples/identity.js b/examples/identity.js index 1bbc260..7a0476a 100644 --- a/examples/identity.js +++ b/examples/identity.js @@ -1,5 +1,7 @@ var Identity = require('../lib/identity'); +var Keypair = require('../lib/keypair'); -var identity = new Identity( 0x02 ); +var keypair = new Keypair(); +var identity = new Identity().fromPubkey( keypair.pubkey ); console.log( identity.toString() ); diff --git a/lib/identity.js b/lib/identity.js index 789cb54..22841f4 100644 --- a/lib/identity.js +++ b/lib/identity.js @@ -58,7 +58,10 @@ Identity.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) { }; Identity.prototype.fromPubkey = function(pubkey, networkstr) { - this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer()); + var p = new Buffer( 0x01 ); + var b = pubkey.toBuffer(); + + this.hashbuf = Hash.sha256ripemd160( Buffer.concat([ p , b ]) ); this.networkstr = networkstr || 'mainnet'; this.typestr = 'identephem'; return this; @@ -88,9 +91,6 @@ Identity.prototype.isValid = function() { }; Identity.prototype.toBuffer = function() { - - console.log(this.networkstr); process.exit(); - version = new Buffer([constants[this.networkstr][this.typestr]]); var buf = Buffer.concat([version, this.hashbuf]); return buf; diff --git a/lib/keypair.js b/lib/keypair.js index bc3adf8..1ba511d 100644 --- a/lib/keypair.js +++ b/lib/keypair.js @@ -3,16 +3,22 @@ var Pubkey = require('./pubkey'); var BN = require('./bn'); var point = require('./point'); -var Keypair = function Keypair(obj) { +var Keypair = function Keypair(obj) { if (!(this instanceof Keypair)) return new Keypair(obj); + + if (!obj) { + var privkey = Privkey().fromRandom(); + var obj = this.fromPrivkey( privkey ); + } + if (obj) this.set(obj); }; Keypair.prototype.set = function(obj) { this.privkey = obj.privkey || this.privkey || undefined; - this.pubkey = obj.pubkey || this.pubkey || undefined; + this.pubkey = obj.pubkey || this.pubkey || undefined; return this; };