|
|
@ -4,11 +4,18 @@ var Hash = require('./hash'); |
|
|
|
var Pubkey = require('./pubkey'); |
|
|
|
var Script = require('./script'); |
|
|
|
|
|
|
|
function Address(obj) { |
|
|
|
function Address(buf) { |
|
|
|
if (!(this instanceof Address)) |
|
|
|
return new Address(obj); |
|
|
|
if (obj) |
|
|
|
return new Address(buf); |
|
|
|
if (Buffer.isBuffer(buf)) { |
|
|
|
this.fromBuffer(buf); |
|
|
|
} else if (typeof buf === 'string') { |
|
|
|
var str = buf; |
|
|
|
this.fromString(str); |
|
|
|
} else if (buf) { |
|
|
|
var obj = buf; |
|
|
|
this.set(obj); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
Address.prototype.set = function(obj) { |
|
|
@ -18,22 +25,7 @@ Address.prototype.set = function(obj) { |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
Address.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) { |
|
|
|
this.hashbuf = Hash.sha256ripemd160(script.toBuffer()); |
|
|
|
this.networkstr = networkstr || 'mainnet'; |
|
|
|
this.typestr = 'scripthash'; |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
Address.prototype.fromString = function(str) { |
|
|
|
var buf = base58check.decode(str); |
|
|
|
Address.prototype.fromBuffer = function(buf) { |
|
|
|
if (buf.length !== 1 + 20) |
|
|
|
throw new Error('Address buffers must be exactly 21 bytes'); |
|
|
|
var version = buf[0]; |
|
|
@ -57,6 +49,25 @@ Address.prototype.fromString = function(str) { |
|
|
|
this.hashbuf = buf.slice(1); |
|
|
|
|
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
Address.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) { |
|
|
|
this.hashbuf = Hash.sha256ripemd160(script.toBuffer()); |
|
|
|
this.networkstr = networkstr || 'mainnet'; |
|
|
|
this.typestr = 'scripthash'; |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
|
Address.prototype.fromString = function(str) { |
|
|
|
var buf = base58check.decode(str); |
|
|
|
return this.fromBuffer(buf); |
|
|
|
} |
|
|
|
|
|
|
|
Address.isValid = function(addrstr) { |
|
|
|