refactor address - use "set" function

...intend for this to become standard throughout the lib
This commit is contained in:
Ryan X. Charles 2014-08-28 15:18:48 -07:00
parent cc316e9455
commit f52e679f93
2 changed files with 40 additions and 34 deletions

View File

@ -3,18 +3,24 @@ var constants = require('./constants');
var Hash = require('./hash');
var Pubkey = require('./pubkey');
function Address(hashbuf, network, type) {
function Address(obj) {
if (!(this instanceof Address))
return new Address(hashbuf, network, type);
this.hashbuf = hashbuf;
this.network = network;
this.type = type;
return new Address(obj);
if (obj)
this.set(obj);
};
Address.prototype.fromPubkey = function(pubkey, network) {
Address.prototype.set = function(obj) {
this.hashbuf = obj.hashbuf || this.hashbuf || null;
this.networkstr = obj.networkstr || this.networkstr || 'mainnet';
this.typestr = obj.typestr || this.typestr || 'pubkeyhash';
return this;
};
Address.prototype.fromPubkey = function(pubkey, networkstr) {
this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer());
this.network = network || 'mainnet';
this.type = 'pubkeyhash';
this.networkstr = networkstr || 'mainnet';
this.typestr = 'pubkeyhash';
return this;
};
@ -24,20 +30,20 @@ Address.prototype.fromString = function(str) {
throw new Error('Address buffers must be exactly 21 bytes');
var version = buf[0];
if (version === constants['mainnet']['pubkeyhash']) {
this.network = 'mainnet';
this.type = 'pubkeyhash';
this.networkstr = 'mainnet';
this.typestr = 'pubkeyhash';
} else if (version === constants['mainnet']['p2sh']) {
this.network = 'mainnet';
this.type = 'p2sh';
this.networkstr = 'mainnet';
this.typestr = 'p2sh';
} else if (version === constants['testnet']['pubkeyhash']) {
this.network = 'testnet';
this.type = 'pubkeyhash';
this.networkstr = 'testnet';
this.typestr = 'pubkeyhash';
} else if (version === constants['testnet']['p2sh']) {
this.network = 'testnet';
this.type = 'p2sh';
this.networkstr = 'testnet';
this.typestr = 'p2sh';
} else {
this.network = 'unknown';
this.type = 'unknown';
this.networkstr = 'unknown';
this.typestr = 'unknown';
}
this.hashbuf = buf.slice(1);
@ -55,7 +61,7 @@ Address.prototype.isValid = function() {
};
Address.prototype.toBuffer = function() {
version = new Buffer([constants[this.network][this.type]]);
version = new Buffer([constants[this.networkstr][this.typestr]]);
var buf = Buffer.concat([version, this.hashbuf]);
return buf;
};
@ -67,10 +73,10 @@ Address.prototype.toString = function() {
Address.prototype.validate = function() {
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 20)
throw new Error('hash must be a buffer of 20 bytes');
if (this.network !== 'mainnet' && this.network !== 'testnet')
throw new Error('network must be "mainnet" or "testnet"');
if (this.type !== 'pubkeyhash' && this.type !== 'p2sh')
throw new Error('type must be "pubkeyhash" or "p2sh"');
if (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet')
throw new Error('networkstr must be "mainnet" or "testnet"');
if (this.typestr !== 'pubkeyhash' && this.typestr !== 'p2sh')
throw new Error('typestr must be "pubkeyhash" or "p2sh"');
return this;
};

View File

@ -44,7 +44,7 @@ describe('Address', function() {
it('should derive from this known address string testnet', function() {
var address = new Address();
address.fromString(str);
address.network = 'testnet';
address.networkstr = 'testnet';
address.fromString(address.toString());
address.toString().should.equal('mm1X5M2QWyHVjn7txrF7mmtZDpjCXzoa98');
});
@ -52,8 +52,8 @@ describe('Address', function() {
it('should derive from this known address string mainnet p2sh', function() {
var address = new Address();
address.fromString(str);
address.network = 'mainnet';
address.type = 'p2sh';
address.networkstr = 'mainnet';
address.typestr = 'p2sh';
address.fromString(address.toString());
address.toString().should.equal('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
});
@ -61,8 +61,8 @@ describe('Address', function() {
it('should derive from this known address string testnet p2sh', function() {
var address = new Address();
address.fromString(str);
address.network = 'testnet';
address.type = 'p2sh';
address.networkstr = 'testnet';
address.typestr = 'p2sh';
address.fromString(address.toString());
address.toString().should.equal('2MxjnmaMtsJfyFcyG3WZCzS2RihdNuWqeX4');
});
@ -80,14 +80,14 @@ describe('Address', function() {
it('should describe this address with unknown network as invalid', function() {
var address = new Address();
address.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
address.network = 'unknown';
address.networkstr = 'unknown';
address.isValid().should.equal(false);
});
it('should describe this address with unknown type as invalid', function() {
var address = new Address();
address.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
address.type = 'unknown';
address.typestr = 'unknown';
address.isValid().should.equal(false);
});
@ -124,19 +124,19 @@ describe('Address', function() {
it('should throw an error on this invalid network', function() {
var address = new Address();
address.fromString(str);
address.network = 'unknown';
address.networkstr = 'unknown';
(function() {
address.validate();
}).should.throw('network must be "mainnet" or "testnet"');
}).should.throw('networkstr must be "mainnet" or "testnet"');
});
it('should throw an error on this invalid type', function() {
var address = new Address();
address.fromString(str);
address.type = 'unknown';
address.typestr = 'unknown';
(function() {
address.validate();
}).should.throw('type must be "pubkeyhash" or "p2sh"');
}).should.throw('typestr must be "pubkeyhash" or "p2sh"');
});
});