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