From 75cf4c4f9af3512f41ca10e5dee5ebc2069fa316 Mon Sep 17 00:00:00 2001 From: Eric Martindale Date: Thu, 2 Oct 2014 22:27:18 -0400 Subject: [PATCH] tidy up identity class, add tests. --- examples/identity.js | 10 ++++++++-- lib/identity.js | 13 ++++++------- test/identity.js | 10 +++++----- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/examples/identity.js b/examples/identity.js index 7a0476a..8ef2ba4 100644 --- a/examples/identity.js +++ b/examples/identity.js @@ -1,7 +1,13 @@ var Identity = require('../lib/identity'); -var Keypair = require('../lib/keypair'); +var Keypair = require('../lib/keypair'); -var keypair = new Keypair(); +var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex'); +var buf = Buffer.concat([new Buffer([0]), pubkeyhash]); + +var keypair = new Keypair().fromString( buf.toString('hex') ); var identity = new Identity().fromPubkey( keypair.pubkey ); +console.log( 'pubkey', keypair.pubkey.toString() ); +console.log( 'privkey', keypair.privkey.toString() ); +console.log( identity ); console.log( identity.toString() ); diff --git a/lib/identity.js b/lib/identity.js index 22841f4..7a66324 100644 --- a/lib/identity.js +++ b/lib/identity.js @@ -58,12 +58,9 @@ Identity.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) { }; Identity.prototype.fromPubkey = function(pubkey, networkstr) { - var p = new Buffer( 0x01 ); - var b = pubkey.toBuffer(); - - this.hashbuf = Hash.sha256ripemd160( Buffer.concat([ p , b ]) ); + this.hashbuf = Hash.sha256ripemd160( pubkey.toBuffer() ); this.networkstr = networkstr || 'mainnet'; - this.typestr = 'identephem'; + this.typestr = 'identephem'; return this; }; @@ -91,6 +88,8 @@ Identity.prototype.isValid = function() { }; Identity.prototype.toBuffer = function() { + console.log( this.networkstr ); + version = new Buffer([constants[this.networkstr][this.typestr]]); var buf = Buffer.concat([version, this.hashbuf]); return buf; @@ -105,8 +104,8 @@ Identity.prototype.validate = function() { throw new Error('hash must be a buffer of 20 bytes'); if (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet') throw new Error('networkstr must be "mainnet" or "testnet"'); - if (this.typestr !== 'identephem' && this.typestr !== 'scripthash') - throw new Error('typestr must be "identephem" or "scripthash"'); + if (this.typestr !== 'identephem' && this.typestr !== 'identpersist') + throw new Error('typestr must be "identephem" or "identpersist"'); return this; }; diff --git a/test/identity.js b/test/identity.js index a37be64..e3a12f5 100644 --- a/test/identity.js +++ b/test/identity.js @@ -8,7 +8,7 @@ describe('Identity', function() { var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex'); var buf = Buffer.concat([new Buffer([0]), pubkeyhash]); - var str = '16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r'; + var str = 'sMKQzi3RTDK8zRRimoPZQGw4sfsj9Ttx1'; it('should create a new identity object', function() { var identity = new Identity(); @@ -45,7 +45,7 @@ describe('Identity', function() { Identity().fromHashbuf(pubkeyhash).toString().should.equal(str); var a = Identity().fromHashbuf(pubkeyhash, 'testnet', 'scripthash'); a.networkstr.should.equal('testnet'); - a.typestr.should.equal('scripthash'); + a.typestr.should.equal('identephem'); }); it('should throw an error for invalid length hashbuf', function() { @@ -113,7 +113,7 @@ describe('Identity', function() { var identity = new Identity(); identity.fromString(str); identity.networkstr = 'mainnet'; - identity.typestr = 'scripthash'; + identity.typestr = 'identephem'; identity.fromString(identity.toString()); identity.toString().should.equal('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo'); }); @@ -122,7 +122,7 @@ describe('Identity', function() { var identity = new Identity(); identity.fromString(str); identity.networkstr = 'testnet'; - identity.typestr = 'scripthash'; + identity.typestr = 'identephem'; identity.fromString(identity.toString()); identity.toString().should.equal('2MxjnmaMtsJfyFcyG3WZCzS2RihdNuWqeX4'); }); @@ -196,7 +196,7 @@ describe('Identity', function() { identity.typestr = 'unknown'; (function() { identity.validate(); - }).should.throw('typestr must be "pubkeyhash" or "scripthash"'); + }).should.throw('typestr must be "identephem" or "identpersist"'); }); });