Migrate from Address() to Identity().

This commit is contained in:
Eric Martindale 2014-10-01 16:26:09 -04:00
parent be95a3f3a7
commit 5f60d26a2b
1 changed files with 17 additions and 17 deletions

View File

@ -4,9 +4,9 @@ var Hash = require('./hash');
var Pubkey = require('./pubkey'); var Pubkey = require('./pubkey');
var Script = require('./script'); var Script = require('./script');
function Address(buf) { function Identity(buf) {
if (!(this instanceof Address)) if (!(this instanceof Identity))
return new Address(buf); return new Identity(buf);
if (Buffer.isBuffer(buf)) { if (Buffer.isBuffer(buf)) {
this.fromBuffer(buf); this.fromBuffer(buf);
} else if (typeof buf === 'string') { } else if (typeof buf === 'string') {
@ -18,16 +18,16 @@ function Address(buf) {
} }
}; };
Address.prototype.set = function(obj) { Identity.prototype.set = function(obj) {
this.hashbuf = obj.hashbuf || this.hashbuf || null; this.hashbuf = obj.hashbuf || this.hashbuf || null;
this.networkstr = obj.networkstr || this.networkstr || 'mainnet'; this.networkstr = obj.networkstr || this.networkstr || 'mainnet';
this.typestr = obj.typestr || this.typestr || 'pubkeyhash'; this.typestr = obj.typestr || this.typestr || 'pubkeyhash';
return this; return this;
}; };
Address.prototype.fromBuffer = function(buf) { Identity.prototype.fromBuffer = function(buf) {
if (buf.length !== 1 + 20) if (buf.length !== 1 + 20)
throw new Error('Address buffers must be exactly 21 bytes'); throw new Error('Identity 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.networkstr = 'mainnet'; this.networkstr = 'mainnet';
@ -51,7 +51,7 @@ Address.prototype.fromBuffer = function(buf) {
return this; return this;
}; };
Address.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) { Identity.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) {
if (hashbuf.length !== 20) if (hashbuf.length !== 20)
throw new Error('hashbuf must be exactly 20 bytes'); throw new Error('hashbuf must be exactly 20 bytes');
this.hashbuf = hashbuf; this.hashbuf = hashbuf;
@ -60,35 +60,35 @@ Address.prototype.fromHashbuf = function(hashbuf, networkstr, typestr) {
return this; return this;
}; };
Address.prototype.fromPubkey = function(pubkey, networkstr) { Identity.prototype.fromPubkey = function(pubkey, networkstr) {
this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer()); this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer());
this.networkstr = networkstr || 'mainnet'; this.networkstr = networkstr || 'mainnet';
this.typestr = 'pubkeyhash'; this.typestr = 'pubkeyhash';
return this; return this;
}; };
Address.prototype.fromScript = function(script, networkstr) { Identity.prototype.fromScript = function(script, networkstr) {
this.hashbuf = Hash.sha256ripemd160(script.toBuffer()); this.hashbuf = Hash.sha256ripemd160(script.toBuffer());
this.networkstr = networkstr || 'mainnet'; this.networkstr = networkstr || 'mainnet';
this.typestr = 'scripthash'; this.typestr = 'scripthash';
return this; return this;
}; };
Address.prototype.fromString = function(str) { Identity.prototype.fromString = function(str) {
var buf = base58check.decode(str); var buf = base58check.decode(str);
return this.fromBuffer(buf); return this.fromBuffer(buf);
} }
Address.isValid = function(addrstr) { Identity.isValid = function(addrstr) {
try { try {
var address = new Address().fromString(addrstr); var address = new Identity().fromString(addrstr);
} catch (e) { } catch (e) {
return false; return false;
} }
return address.isValid(); return address.isValid();
}; };
Address.prototype.isValid = function() { Identity.prototype.isValid = function() {
try { try {
this.validate(); this.validate();
return true; return true;
@ -97,17 +97,17 @@ Address.prototype.isValid = function() {
} }
}; };
Address.prototype.toBuffer = function() { Identity.prototype.toBuffer = function() {
version = new Buffer([constants[this.networkstr][this.typestr]]); 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;
}; };
Address.prototype.toString = function() { Identity.prototype.toString = function() {
return base58check.encode(this.toBuffer()); return base58check.encode(this.toBuffer());
}; };
Address.prototype.validate = function() { 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 (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet') if (this.networkstr !== 'mainnet' && this.networkstr !== 'testnet')
@ -117,4 +117,4 @@ Address.prototype.validate = function() {
return this; return this;
}; };
module.exports = Address; module.exports = Identity;