tidy up identity class, add tests.

This commit is contained in:
Eric Martindale 2014-10-02 22:27:18 -04:00
parent 5fc9721647
commit 75cf4c4f9a
3 changed files with 19 additions and 14 deletions

View File

@ -1,7 +1,13 @@
var Identity = require('../lib/identity'); 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 ); 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() ); console.log( identity.toString() );

View File

@ -58,12 +58,9 @@ Identity.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) {
}; };
Identity.prototype.fromPubkey = function(pubkey, networkstr) { Identity.prototype.fromPubkey = function(pubkey, networkstr) {
var p = new Buffer( 0x01 ); this.hashbuf = Hash.sha256ripemd160( pubkey.toBuffer() );
var b = pubkey.toBuffer();
this.hashbuf = Hash.sha256ripemd160( Buffer.concat([ p , b ]) );
this.networkstr = networkstr || 'mainnet'; this.networkstr = networkstr || 'mainnet';
this.typestr = 'identephem'; this.typestr = 'identephem';
return this; return this;
}; };
@ -91,6 +88,8 @@ Identity.prototype.isValid = function() {
}; };
Identity.prototype.toBuffer = function() { Identity.prototype.toBuffer = function() {
console.log( this.networkstr );
version = new Buffer([constants[this.networkstr][this.typestr]]); version = new Buffer([constants[this.networkstr][this.typestr]]);
var buf = Buffer.concat([version, this.hashbuf]); var buf = Buffer.concat([version, this.hashbuf]);
return buf; return buf;
@ -105,8 +104,8 @@ Identity.prototype.validate = function() {
throw new Error('hash must be a buffer of 20 bytes'); throw new Error('hash must be a buffer of 20 bytes');
if (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet') if (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet')
throw new Error('networkstr must be "mainnet" or "testnet"'); throw new Error('networkstr must be "mainnet" or "testnet"');
if (this.typestr !== 'identephem' && this.typestr !== 'scripthash') if (this.typestr !== 'identephem' && this.typestr !== 'identpersist')
throw new Error('typestr must be "identephem" or "scripthash"'); throw new Error('typestr must be "identephem" or "identpersist"');
return this; return this;
}; };

View File

@ -8,7 +8,7 @@ describe('Identity', function() {
var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex'); var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex');
var buf = Buffer.concat([new Buffer([0]), pubkeyhash]); var buf = Buffer.concat([new Buffer([0]), pubkeyhash]);
var str = '16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r'; var str = 'sMKQzi3RTDK8zRRimoPZQGw4sfsj9Ttx1';
it('should create a new identity object', function() { it('should create a new identity object', function() {
var identity = new Identity(); var identity = new Identity();
@ -45,7 +45,7 @@ describe('Identity', function() {
Identity().fromHashbuf(pubkeyhash).toString().should.equal(str); Identity().fromHashbuf(pubkeyhash).toString().should.equal(str);
var a = Identity().fromHashbuf(pubkeyhash, 'testnet', 'scripthash'); var a = Identity().fromHashbuf(pubkeyhash, 'testnet', 'scripthash');
a.networkstr.should.equal('testnet'); 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() { it('should throw an error for invalid length hashbuf', function() {
@ -113,7 +113,7 @@ describe('Identity', function() {
var identity = new Identity(); var identity = new Identity();
identity.fromString(str); identity.fromString(str);
identity.networkstr = 'mainnet'; identity.networkstr = 'mainnet';
identity.typestr = 'scripthash'; identity.typestr = 'identephem';
identity.fromString(identity.toString()); identity.fromString(identity.toString());
identity.toString().should.equal('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo'); identity.toString().should.equal('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
}); });
@ -122,7 +122,7 @@ describe('Identity', function() {
var identity = new Identity(); var identity = new Identity();
identity.fromString(str); identity.fromString(str);
identity.networkstr = 'testnet'; identity.networkstr = 'testnet';
identity.typestr = 'scripthash'; identity.typestr = 'identephem';
identity.fromString(identity.toString()); identity.fromString(identity.toString());
identity.toString().should.equal('2MxjnmaMtsJfyFcyG3WZCzS2RihdNuWqeX4'); identity.toString().should.equal('2MxjnmaMtsJfyFcyG3WZCzS2RihdNuWqeX4');
}); });
@ -196,7 +196,7 @@ describe('Identity', function() {
identity.typestr = 'unknown'; identity.typestr = 'unknown';
(function() { (function() {
identity.validate(); identity.validate();
}).should.throw('typestr must be "pubkeyhash" or "scripthash"'); }).should.throw('typestr must be "identephem" or "identpersist"');
}); });
}); });