address: refactor.

This commit is contained in:
Christopher Jeffrey 2016-12-12 06:50:13 -08:00
parent 8108ff3eb5
commit fcf6254d29
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -47,6 +47,13 @@ function Address(options) {
this.fromOptions(options); this.fromOptions(options);
} }
/**
* Address types.
* @enum {Number}
*/
Address.types = scriptTypes;
/** /**
* Inject properties from options object. * Inject properties from options object.
* @private * @private
@ -129,16 +136,11 @@ Address.prototype.getType = function getType() {
*/ */
Address.prototype.getSize = function getSize() { Address.prototype.getSize = function getSize() {
var size = 0; var size = 5 + this.hash.length;
size += 1;
if (this.version !== -1) if (this.version !== -1)
size += 2; size += 2;
size += this.hash.length;
size += 4;
return size; return size;
}; };
@ -152,21 +154,17 @@ Address.prototype.getSize = function getSize() {
Address.prototype.toRaw = function toRaw(network) { Address.prototype.toRaw = function toRaw(network) {
var size = this.getSize(); var size = this.getSize();
var bw = new StaticWriter(size); var bw = new StaticWriter(size);
var prefix; var prefix = this.getPrefix(network);
if (!network)
network = this.network;
network = Network.get(network);
prefix = Address.getPrefix(this.type, network);
assert(prefix !== -1, 'Not a valid address prefix.'); assert(prefix !== -1, 'Not a valid address prefix.');
bw.writeU8(prefix); bw.writeU8(prefix);
if (this.version !== -1) { if (this.version !== -1) {
bw.writeU8(this.version); bw.writeU8(this.version);
bw.writeU8(0); bw.writeU8(0);
} }
bw.writeBytes(this.hash); bw.writeBytes(this.hash);
bw.writeChecksum(); bw.writeChecksum();
@ -186,17 +184,11 @@ Address.prototype.toBase58 = function toBase58(network) {
/** /**
* Convert the Address to a string. * Convert the Address to a string.
* @returns {Base58String} * @returns {Base58Address}
*/ */
Address.prototype.toString = function toString(enc) { Address.prototype.toString = function toString() {
if (enc === 'hex') return this.toBase58();
return this.getHash('hex');
if (enc === 'base58')
enc = null;
return this.toBase58(enc);
}; };
/** /**