Wei Lu
11 years ago
3 changed files with 70 additions and 52 deletions
@ -1,48 +1,62 @@ |
|||
var base58 = require('./base58'); |
|||
var convert = require('./convert'); |
|||
var util = require('./util'); |
|||
var mainnet = require('./network').mainnet.addressVersion; |
|||
var base58 = require('./base58') |
|||
var convert = require('./convert') |
|||
var error = require('./util').error |
|||
var mainnet = require('./network').mainnet.addressVersion |
|||
|
|||
var Address = function (bytes, version) { |
|||
if (!(this instanceof Address)) { return new Address(bytes, version); } |
|||
if (arguments[0] instanceof Address) { |
|||
this.hash = arguments[0].hash; |
|||
this.version = arguments[0].version; |
|||
} |
|||
else if (typeof bytes === 'string') { |
|||
this.hash = |
|||
bytes.length <= 35 ? base58.checkDecode(bytes) |
|||
: bytes.length <= 40 ? convert.hexToBytes(bytes) |
|||
: util.error('invalid or unrecognized input'); |
|||
function Address(bytes, version) { |
|||
if (!(this instanceof Address)) |
|||
return new Address(bytes, version) |
|||
|
|||
this.version = version || this.hash.version || mainnet; |
|||
} |
|||
else { |
|||
this.hash = bytes; |
|||
this.version = version || mainnet; |
|||
} |
|||
}; |
|||
if (bytes instanceof Address) { |
|||
this.hash = bytes.hash |
|||
this.version = bytes.version |
|||
} |
|||
else if (typeof bytes === 'string') { |
|||
this.hash = stringToHash(bytes) |
|||
this.version = version || this.hash.version || mainnet |
|||
} |
|||
else { |
|||
this.hash = bytes |
|||
this.version = version || mainnet |
|||
} |
|||
} |
|||
|
|||
function stringToHash(str) { |
|||
if (str.length <= 35) { |
|||
return base58.checkDecode(str) |
|||
} |
|||
if (str.length <= 40) { |
|||
return convert.hexToBytes(str) |
|||
} |
|||
error('invalid or unrecognized input') |
|||
} |
|||
|
|||
/** |
|||
* Serialize this object as a standard Bitcoin address. |
|||
* |
|||
* Returns the address as a base58-encoded string in the standardized format. |
|||
*/ |
|||
Address.prototype.toString = function () { |
|||
return base58.checkEncode(this.hash.slice(0), this.version); |
|||
}; |
|||
return base58.checkEncode(this.hash.slice(0), this.version) |
|||
} |
|||
|
|||
Address.getVersion = function(string) { |
|||
return base58.decode(string)[0]; |
|||
/** |
|||
* Returns the version of an address, e.g. if the address belongs to the main |
|||
* net or the test net. |
|||
*/ |
|||
Address.getVersion = function (address) { |
|||
return base58.decode(address)[0] |
|||
} |
|||
|
|||
Address.validate = function(string) { |
|||
try { |
|||
base58.checkDecode(string); |
|||
return true; |
|||
} catch (e) { |
|||
return false; |
|||
} |
|||
}; |
|||
/** |
|||
* Returns true if a bitcoin address is a valid address, otherwise false. |
|||
*/ |
|||
Address.validate = function (address) { |
|||
try { |
|||
base58.checkDecode(address) |
|||
return true |
|||
} catch (e) { |
|||
return false |
|||
} |
|||
} |
|||
|
|||
module.exports = Address; |
|||
module.exports = Address |
|||
|
Loading…
Reference in new issue