refactor address - use "set" function
...intend for this to become standard throughout the lib
This commit is contained in:
parent
cc316e9455
commit
f52e679f93
|
@ -3,18 +3,24 @@ var constants = require('./constants');
|
||||||
var Hash = require('./hash');
|
var Hash = require('./hash');
|
||||||
var Pubkey = require('./pubkey');
|
var Pubkey = require('./pubkey');
|
||||||
|
|
||||||
function Address(hashbuf, network, type) {
|
function Address(obj) {
|
||||||
if (!(this instanceof Address))
|
if (!(this instanceof Address))
|
||||||
return new Address(hashbuf, network, type);
|
return new Address(obj);
|
||||||
this.hashbuf = hashbuf;
|
if (obj)
|
||||||
this.network = network;
|
this.set(obj);
|
||||||
this.type = type;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer());
|
||||||
this.network = network || 'mainnet';
|
this.networkstr = networkstr || 'mainnet';
|
||||||
this.type = 'pubkeyhash';
|
this.typestr = 'pubkeyhash';
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -24,20 +30,20 @@ Address.prototype.fromString = function(str) {
|
||||||
throw new Error('Address buffers must be exactly 21 bytes');
|
throw new Error('Address buffers must be exactly 21 bytes');
|
||||||
var version = buf[0];
|
var version = buf[0];
|
||||||
if (version === constants['mainnet']['pubkeyhash']) {
|
if (version === constants['mainnet']['pubkeyhash']) {
|
||||||
this.network = 'mainnet';
|
this.networkstr = 'mainnet';
|
||||||
this.type = 'pubkeyhash';
|
this.typestr = 'pubkeyhash';
|
||||||
} else if (version === constants['mainnet']['p2sh']) {
|
} else if (version === constants['mainnet']['p2sh']) {
|
||||||
this.network = 'mainnet';
|
this.networkstr = 'mainnet';
|
||||||
this.type = 'p2sh';
|
this.typestr = 'p2sh';
|
||||||
} else if (version === constants['testnet']['pubkeyhash']) {
|
} else if (version === constants['testnet']['pubkeyhash']) {
|
||||||
this.network = 'testnet';
|
this.networkstr = 'testnet';
|
||||||
this.type = 'pubkeyhash';
|
this.typestr = 'pubkeyhash';
|
||||||
} else if (version === constants['testnet']['p2sh']) {
|
} else if (version === constants['testnet']['p2sh']) {
|
||||||
this.network = 'testnet';
|
this.networkstr = 'testnet';
|
||||||
this.type = 'p2sh';
|
this.typestr = 'p2sh';
|
||||||
} else {
|
} else {
|
||||||
this.network = 'unknown';
|
this.networkstr = 'unknown';
|
||||||
this.type = 'unknown';
|
this.typestr = 'unknown';
|
||||||
}
|
}
|
||||||
|
|
||||||
this.hashbuf = buf.slice(1);
|
this.hashbuf = buf.slice(1);
|
||||||
|
@ -55,7 +61,7 @@ Address.prototype.isValid = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
Address.prototype.toBuffer = 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]);
|
var buf = Buffer.concat([version, this.hashbuf]);
|
||||||
return buf;
|
return buf;
|
||||||
};
|
};
|
||||||
|
@ -67,10 +73,10 @@ Address.prototype.toString = function() {
|
||||||
Address.prototype.validate = function() {
|
Address.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 (this.network !== 'mainnet' && this.network !== 'testnet')
|
if (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet')
|
||||||
throw new Error('network must be "mainnet" or "testnet"');
|
throw new Error('networkstr must be "mainnet" or "testnet"');
|
||||||
if (this.type !== 'pubkeyhash' && this.type !== 'p2sh')
|
if (this.typestr !== 'pubkeyhash' && this.typestr !== 'p2sh')
|
||||||
throw new Error('type must be "pubkeyhash" or "p2sh"');
|
throw new Error('typestr must be "pubkeyhash" or "p2sh"');
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ describe('Address', function() {
|
||||||
it('should derive from this known address string testnet', function() {
|
it('should derive from this known address string testnet', function() {
|
||||||
var address = new Address();
|
var address = new Address();
|
||||||
address.fromString(str);
|
address.fromString(str);
|
||||||
address.network = 'testnet';
|
address.networkstr = 'testnet';
|
||||||
address.fromString(address.toString());
|
address.fromString(address.toString());
|
||||||
address.toString().should.equal('mm1X5M2QWyHVjn7txrF7mmtZDpjCXzoa98');
|
address.toString().should.equal('mm1X5M2QWyHVjn7txrF7mmtZDpjCXzoa98');
|
||||||
});
|
});
|
||||||
|
@ -52,8 +52,8 @@ describe('Address', function() {
|
||||||
it('should derive from this known address string mainnet p2sh', function() {
|
it('should derive from this known address string mainnet p2sh', function() {
|
||||||
var address = new Address();
|
var address = new Address();
|
||||||
address.fromString(str);
|
address.fromString(str);
|
||||||
address.network = 'mainnet';
|
address.networkstr = 'mainnet';
|
||||||
address.type = 'p2sh';
|
address.typestr = 'p2sh';
|
||||||
address.fromString(address.toString());
|
address.fromString(address.toString());
|
||||||
address.toString().should.equal('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
address.toString().should.equal('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
||||||
});
|
});
|
||||||
|
@ -61,8 +61,8 @@ describe('Address', function() {
|
||||||
it('should derive from this known address string testnet p2sh', function() {
|
it('should derive from this known address string testnet p2sh', function() {
|
||||||
var address = new Address();
|
var address = new Address();
|
||||||
address.fromString(str);
|
address.fromString(str);
|
||||||
address.network = 'testnet';
|
address.networkstr = 'testnet';
|
||||||
address.type = 'p2sh';
|
address.typestr = 'p2sh';
|
||||||
address.fromString(address.toString());
|
address.fromString(address.toString());
|
||||||
address.toString().should.equal('2MxjnmaMtsJfyFcyG3WZCzS2RihdNuWqeX4');
|
address.toString().should.equal('2MxjnmaMtsJfyFcyG3WZCzS2RihdNuWqeX4');
|
||||||
});
|
});
|
||||||
|
@ -80,14 +80,14 @@ describe('Address', function() {
|
||||||
it('should describe this address with unknown network as invalid', function() {
|
it('should describe this address with unknown network as invalid', function() {
|
||||||
var address = new Address();
|
var address = new Address();
|
||||||
address.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
address.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
||||||
address.network = 'unknown';
|
address.networkstr = 'unknown';
|
||||||
address.isValid().should.equal(false);
|
address.isValid().should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should describe this address with unknown type as invalid', function() {
|
it('should describe this address with unknown type as invalid', function() {
|
||||||
var address = new Address();
|
var address = new Address();
|
||||||
address.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
address.fromString('37BahqRsFrAd3qLiNNwLNV3AWMRD7itxTo');
|
||||||
address.type = 'unknown';
|
address.typestr = 'unknown';
|
||||||
address.isValid().should.equal(false);
|
address.isValid().should.equal(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -124,19 +124,19 @@ describe('Address', function() {
|
||||||
it('should throw an error on this invalid network', function() {
|
it('should throw an error on this invalid network', function() {
|
||||||
var address = new Address();
|
var address = new Address();
|
||||||
address.fromString(str);
|
address.fromString(str);
|
||||||
address.network = 'unknown';
|
address.networkstr = 'unknown';
|
||||||
(function() {
|
(function() {
|
||||||
address.validate();
|
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() {
|
it('should throw an error on this invalid type', function() {
|
||||||
var address = new Address();
|
var address = new Address();
|
||||||
address.fromString(str);
|
address.fromString(str);
|
||||||
address.type = 'unknown';
|
address.typestr = 'unknown';
|
||||||
(function() {
|
(function() {
|
||||||
address.validate();
|
address.validate();
|
||||||
}).should.throw('type must be "pubkeyhash" or "p2sh"');
|
}).should.throw('typestr must be "pubkeyhash" or "p2sh"');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue