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 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() );

View File

@ -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;
};

View File

@ -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"');
});
});