Improve tests.
This commit is contained in:
parent
dac2acc743
commit
4b6e9aaf03
|
@ -98,7 +98,7 @@ Address.prototype.isValid = 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]);
|
||||
return buf;
|
||||
};
|
||||
|
|
|
@ -30,13 +30,10 @@ Identity.prototype.set = function(obj) {
|
|||
Identity.prototype.fromBuffer = function(buf) {
|
||||
// Identities are prefix + type + key
|
||||
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 version = buf[1];
|
||||
|
||||
if (prefix !== constants['ephemeral'][ prefix ])
|
||||
throw new Error('Identity buffers must contain an identity prefix (0x0f)');
|
||||
|
||||
if (version === constants['ephemeral']['identity']) {
|
||||
this.networkstr = 'ephemeral';
|
||||
|
@ -51,6 +48,9 @@ Identity.prototype.fromBuffer = function(buf) {
|
|||
this.networkstr = '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 );
|
||||
|
||||
|
@ -111,9 +111,9 @@ Identity.prototype.validate = function() {
|
|||
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 20)
|
||||
throw new Error('hash must be a buffer of 20 bytes');
|
||||
if (['ephemeral', 'mainnet', 'testnet'].indexOf( this.networkstr ))
|
||||
throw new Error('networkstr must be "mainnet" or "testnet"');
|
||||
if (this.typestr !== 'identephem' && this.typestr !== 'identpersist')
|
||||
throw new Error('typestr must be "identephem" or "identpersist"');
|
||||
throw new Error('networkstr must be "ephemeral", "mainnet", or "testnet"');
|
||||
if (this.typestr !== 'identity')
|
||||
throw new Error('typestr must be "identity"');
|
||||
return this;
|
||||
};
|
||||
|
||||
|
|
|
@ -68,8 +68,9 @@ Pubkey.prototype.fromDER = function(buf) {
|
|||
return this;
|
||||
};
|
||||
|
||||
Pubkey.prototype.fromString = function(str) {
|
||||
this.fromDER(new Buffer(str, 'hex'));
|
||||
Pubkey.prototype.fromString = function( str , encoding ) {
|
||||
var encoding = encoding || 'hex';
|
||||
this.fromDER( new Buffer(str, encoding ) );
|
||||
};
|
||||
|
||||
Pubkey.prototype.fromX = function(odd, x) {
|
||||
|
|
|
@ -1,14 +1,23 @@
|
|||
var should = require('chai').should();
|
||||
var constants = require('../lib/constants');
|
||||
var Pubkey = require('../lib/pubkey');
|
||||
var PubKey = require('../lib/pubkey');
|
||||
var Identity = require('../lib/identity');
|
||||
var Script = require('../lib/script');
|
||||
|
||||
describe('Identity', function() {
|
||||
|
||||
var pubkeyhash = new Buffer('3c3fa3d4adcaf8f52d5b1843975e122548269937', 'hex');
|
||||
var buf = Buffer.concat([new Buffer([0]), pubkeyhash]);
|
||||
var str = 'sMKQzi3RTDK8zRRimoPZQGw4sfsj9Ttx1';
|
||||
var knownPrivKey = 'L1KL3xHiuBF9YuBKTZMorW6TVDG2QW9UHWdSFEzcVFpLuGxTe9bQ';
|
||||
var knownPubKey = '026006aa5fd6e800e6b529258dbb73b531da735a863c87e6673bf96def1372a59e';
|
||||
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() {
|
||||
var identity = new Identity();
|
||||
|
@ -22,7 +31,7 @@ describe('Identity', function() {
|
|||
describe('@isValid', 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() {
|
||||
|
@ -45,7 +54,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('identephem');
|
||||
a.typestr.should.equal('scripthash');
|
||||
});
|
||||
|
||||
it('should throw an error for invalid length hashbuf', function() {
|
||||
|
@ -59,94 +68,52 @@ describe('Identity', function() {
|
|||
describe('#fromPubkey', function() {
|
||||
|
||||
it('should make this identity from a compressed pubkey', function() {
|
||||
var pubkey = new Pubkey();
|
||||
pubkey.fromDER(new Buffer('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004', 'hex'));
|
||||
var pubkey = new PubKey();
|
||||
pubkey.fromDER(new Buffer( knownPubKey , 'hex'));
|
||||
var identity = new Identity();
|
||||
identity.fromPubkey(pubkey);
|
||||
identity.toString().should.equal('19gH5uhqY6DKrtkU66PsZPUZdzTd11Y7ke');
|
||||
identity.toString().should.equal( knownIdent );
|
||||
});
|
||||
|
||||
it('should make this identity from an uncompressed pubkey', function() {
|
||||
var pubkey = new Pubkey();
|
||||
pubkey.fromDER(new Buffer('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004', 'hex'));
|
||||
var pubkey = new PubKey();
|
||||
pubkey.fromDER(new Buffer( knownPubKey , 'hex'));
|
||||
var identity = new Identity();
|
||||
pubkey.compressed = false;
|
||||
identity.fromPubkey(pubkey, 'mainnet');
|
||||
identity.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
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');
|
||||
identity.fromPubkey(pubkey, 'ephemeral');
|
||||
identity.toString().should.equal( knownIdent );
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
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();
|
||||
identity.fromString(str);
|
||||
identity.fromString( str );
|
||||
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() {
|
||||
|
||||
it('should describe this valid identity as valid', function() {
|
||||
var identity = new Identity();
|
||||
identity.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
||||
identity.fromString( knownIdent );
|
||||
identity.isValid().should.equal(true);
|
||||
});
|
||||
|
||||
it('should describe this identity with unknown network as invalid', function() {
|
||||
var identity = new Identity();
|
||||
identity.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
||||
identity.fromString( knownIdent );
|
||||
identity.networkstr = 'unknown';
|
||||
identity.isValid().should.equal(false);
|
||||
});
|
||||
|
||||
it('should describe this identity with unknown type as invalid', function() {
|
||||
var identity = new Identity();
|
||||
identity.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
||||
identity.fromString( knownIdent );
|
||||
identity.typestr = 'unknown';
|
||||
identity.isValid().should.equal(false);
|
||||
});
|
||||
|
@ -187,7 +154,7 @@ describe('Identity', function() {
|
|||
identity.networkstr = 'unknown';
|
||||
(function() {
|
||||
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() {
|
||||
|
@ -196,7 +163,7 @@ describe('Identity', function() {
|
|||
identity.typestr = 'unknown';
|
||||
(function() {
|
||||
identity.validate();
|
||||
}).should.throw('typestr must be "identephem" or "identpersist"');
|
||||
}).should.throw('typestr must be "identity"');
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue