Browse Source

Improve tests.

patch-2
Eric Martindale 10 years ago
parent
commit
4b6e9aaf03
  1. 2
      lib/address.js
  2. 14
      lib/identity.js
  3. 5
      lib/pubkey.js
  4. 93
      test/identity.js

2
lib/address.js

@ -98,7 +98,7 @@ Address.prototype.isValid = function() {
}; };
Address.prototype.toBuffer = function() { Address.prototype.toBuffer = function() {
version = new Buffer([constants[this.networkstr][this.typestr]]); var 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;
}; };

14
lib/identity.js

@ -30,13 +30,10 @@ Identity.prototype.set = function(obj) {
Identity.prototype.fromBuffer = function(buf) { Identity.prototype.fromBuffer = function(buf) {
// Identities are prefix + type + key // Identities are prefix + type + key
if (buf.length !== 1 + 1 + 20) if (buf.length !== 1 + 1 + 20)
throw new Error('Identity buffers must be exactly 22 bytes'); throw new Error('Identity buffers must be exactly 22 bytes (was '+buf.length+')');
var prefix = buf[0]; var prefix = buf[0];
var version = buf[1]; var version = buf[1];
if (prefix !== constants['ephemeral'][ prefix ])
throw new Error('Identity buffers must contain an identity prefix (0x0f)');
if (version === constants['ephemeral']['identity']) { if (version === constants['ephemeral']['identity']) {
this.networkstr = 'ephemeral'; this.networkstr = 'ephemeral';
@ -51,6 +48,9 @@ Identity.prototype.fromBuffer = function(buf) {
this.networkstr = 'unknown'; this.networkstr = 'unknown';
this.typestr = 'unknown'; this.typestr = 'unknown';
} }
if (prefix !== constants['ephemeral']['prefix'])
throw new Error('Identity buffers must contain an identity prefix ('+constants['ephemeral']['prefix']+', was '+ prefix.toString() + ')');
this.hashbuf = buf.slice( 2 ); this.hashbuf = buf.slice( 2 );
@ -111,9 +111,9 @@ Identity.prototype.validate = function() {
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 20) if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 20)
throw new Error('hash must be a buffer of 20 bytes'); throw new Error('hash must be a buffer of 20 bytes');
if (['ephemeral', 'mainnet', 'testnet'].indexOf( this.networkstr )) if (['ephemeral', 'mainnet', 'testnet'].indexOf( this.networkstr ))
throw new Error('networkstr must be "mainnet" or "testnet"'); throw new Error('networkstr must be "ephemeral", "mainnet", or "testnet"');
if (this.typestr !== 'identephem' && this.typestr !== 'identpersist') if (this.typestr !== 'identity')
throw new Error('typestr must be "identephem" or "identpersist"'); throw new Error('typestr must be "identity"');
return this; return this;
}; };

5
lib/pubkey.js

@ -68,8 +68,9 @@ Pubkey.prototype.fromDER = function(buf) {
return this; return this;
}; };
Pubkey.prototype.fromString = function(str) { Pubkey.prototype.fromString = function( str , encoding ) {
this.fromDER(new Buffer(str, 'hex')); var encoding = encoding || 'hex';
this.fromDER( new Buffer(str, encoding ) );
}; };
Pubkey.prototype.fromX = function(odd, x) { Pubkey.prototype.fromX = function(odd, x) {

93
test/identity.js

@ -1,14 +1,23 @@
var should = require('chai').should(); var should = require('chai').should();
var constants = require('../lib/constants'); var constants = require('../lib/constants');
var Pubkey = require('../lib/pubkey'); var PubKey = require('../lib/pubkey');
var Identity = require('../lib/identity'); var Identity = require('../lib/identity');
var Script = require('../lib/script');
describe('Identity', function() { describe('Identity', function() {
var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex'); var knownPrivKey = 'L1KL3xHiuBF9YuBKTZMorW6TVDG2QW9UHWdSFEzcVFpLuGxTe9bQ';
var buf = Buffer.concat([new Buffer([0]), pubkeyhash]); var knownPubKey = '026006aa5fd6e800e6b529258dbb73b531da735a863c87e6673bf96def1372a59e';
var str = 'sMKQzi3RTDK8zRRimoPZQGw4sfsj9Ttx1'; var knownIdent = 'Tf8vkF9HPCDbNcLoFHk8ENAwJLQMVmWRz5P';
//var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex');
var pubkey = '0348dc031a1499c455eeb593407e0505dfbd95a88411fa78e8cab9cbe89d064048';
var key = new PubKey();
key.fromString( pubkey );
//var buf = Buffer.concat([ new Buffer([0]), new Buffer([0]), pubkeyhash ]);
// note: key is wrong string until I figure out how to duplicate the generation of short keys
var buf = Buffer.concat([ new Buffer( 0x0f ) , new Buffer( 0x02 ) , new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex') ])
var str = 'Tf3sWHK314o6hpeUDHpqu8RAxPypoAinbDg';
it('should create a new identity object', function() { it('should create a new identity object', function() {
var identity = new Identity(); var identity = new Identity();
@ -22,7 +31,7 @@ describe('Identity', function() {
describe('@isValid', function() { describe('@isValid', function() {
it('should validate this valid identity string', function() { it('should validate this valid identity string', function() {
Identity.isValid(str).should.equal(true); Identity.isValid( str ).should.equal( true );
}); });
it('should invalidate this valid identity string', function() { it('should invalidate this valid identity string', function() {
@ -45,7 +54,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('identephem'); a.typestr.should.equal('scripthash');
}); });
it('should throw an error for invalid length hashbuf', function() { it('should throw an error for invalid length hashbuf', function() {
@ -59,94 +68,52 @@ describe('Identity', function() {
describe('#fromPubkey', function() { describe('#fromPubkey', function() {
it('should make this identity from a compressed pubkey', function() { it('should make this identity from a compressed pubkey', function() {
var pubkey = new Pubkey(); var pubkey = new PubKey();
pubkey.fromDER(new Buffer('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004', 'hex')); pubkey.fromDER(new Buffer( knownPubKey , 'hex'));
var identity = new Identity(); var identity = new Identity();
identity.fromPubkey(pubkey); identity.fromPubkey(pubkey);
identity.toString().should.equal('19gH5uhqY6DKrtkU66PsZPUZdzTd11Y7ke'); identity.toString().should.equal( knownIdent );
}); });
it('should make this identity from an uncompressed pubkey', function() { it('should make this identity from an uncompressed pubkey', function() {
var pubkey = new Pubkey(); var pubkey = new PubKey();
pubkey.fromDER(new Buffer('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004', 'hex')); pubkey.fromDER(new Buffer( knownPubKey , 'hex'));
var identity = new Identity(); var identity = new Identity();
pubkey.compressed = false; pubkey.compressed = false;
identity.fromPubkey(pubkey, 'mainnet'); identity.fromPubkey(pubkey, 'ephemeral');
identity.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX'); identity.toString().should.equal( knownIdent );
});
});
describe('#fromScript', function() {
it('should make this identity from a script', function() {
var script = Script().fromString("OP_CHECKMULTISIG");
var identity = Identity().fromScript(script);
identity.toString().should.equal('3BYmEwgV2vANrmfRymr1mFnHXgLjD6gAWm');
});
it('should make this identity from other script', function() {
var script = Script().fromString("OP_CHECKSIG OP_HASH160");
var identity = Identity().fromScript(script);
identity.toString().should.equal('347iRqVwks5r493N1rsLN4k9J7Ljg488W7');
}); });
}); });
describe('#fromString', function() { describe('#fromString', function() {
it('should derive from this known identity string mainnet', function() { it('should derive from this known ephemeral identity string', function() {
var identity = new Identity(); var identity = new Identity();
identity.fromString(str); identity.fromString( str );
identity.toBuffer().slice(1).toString('hex').should.equal(pubkeyhash.toString('hex')); identity.toBuffer().slice(1).toString('hex').should.equal(pubkeyhash.toString('hex'));
}); });
it('should derive from this known identity string testnet', function() {
var identity = new Identity();
identity.fromString(str);
identity.networkstr = 'testnet';
identity.fromString(identity.toString());
identity.toString().should.equal('mm1X5M2QWyHVjn7txrF7mmtZDpjCXzoa98');
});
it('should derive from this known identity string mainnet scripthash', function() {
var identity = new Identity();
identity.fromString(str);
identity.networkstr = 'mainnet';
identity.typestr = 'identephem';
identity.fromString(identity.toString());
identity.toString().should.equal('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
});
it('should derive from this known identity string testnet scripthash', function() {
var identity = new Identity();
identity.fromString(str);
identity.networkstr = 'testnet';
identity.typestr = 'identephem';
identity.fromString(identity.toString());
identity.toString().should.equal('2MxjnmaMtsJfyFcyG3WZCzS2RihdNuWqeX4');
});
}); });
describe('#isValid', function() { describe('#isValid', function() {
it('should describe this valid identity as valid', function() { it('should describe this valid identity as valid', function() {
var identity = new Identity(); var identity = new Identity();
identity.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo'); identity.fromString( knownIdent );
identity.isValid().should.equal(true); identity.isValid().should.equal(true);
}); });
it('should describe this identity with unknown network as invalid', function() { it('should describe this identity with unknown network as invalid', function() {
var identity = new Identity(); var identity = new Identity();
identity.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo'); identity.fromString( knownIdent );
identity.networkstr = 'unknown'; identity.networkstr = 'unknown';
identity.isValid().should.equal(false); identity.isValid().should.equal(false);
}); });
it('should describe this identity with unknown type as invalid', function() { it('should describe this identity with unknown type as invalid', function() {
var identity = new Identity(); var identity = new Identity();
identity.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo'); identity.fromString( knownIdent );
identity.typestr = 'unknown'; identity.typestr = 'unknown';
identity.isValid().should.equal(false); identity.isValid().should.equal(false);
}); });
@ -187,7 +154,7 @@ describe('Identity', function() {
identity.networkstr = 'unknown'; identity.networkstr = 'unknown';
(function() { (function() {
identity.validate(); identity.validate();
}).should.throw('networkstr must be "mainnet" or "testnet"'); }).should.throw('networkstr must be "ephemeral", "mainnet", or "testnet"');
}); });
it('should throw an error on this invalid type', function() { it('should throw an error on this invalid type', function() {
@ -196,7 +163,7 @@ describe('Identity', function() {
identity.typestr = 'unknown'; identity.typestr = 'unknown';
(function() { (function() {
identity.validate(); identity.validate();
}).should.throw('typestr must be "identephem" or "identpersist"'); }).should.throw('typestr must be "identity"');
}); });
}); });

Loading…
Cancel
Save