lms
11 years ago
3 changed files with 70 additions and 52 deletions
@ -1,48 +1,62 @@ |
|||||
var base58 = require('./base58'); |
var base58 = require('./base58') |
||||
var convert = require('./convert'); |
var convert = require('./convert') |
||||
var util = require('./util'); |
var error = require('./util').error |
||||
var mainnet = require('./network').mainnet.addressVersion; |
var mainnet = require('./network').mainnet.addressVersion |
||||
|
|
||||
var Address = function (bytes, version) { |
function Address(bytes, version) { |
||||
if (!(this instanceof Address)) { return new Address(bytes, version); } |
if (!(this instanceof Address)) |
||||
if (arguments[0] instanceof Address) { |
return new Address(bytes, version) |
||||
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'); |
|
||||
|
|
||||
this.version = version || this.hash.version || mainnet; |
if (bytes instanceof Address) { |
||||
} |
this.hash = bytes.hash |
||||
else { |
this.version = bytes.version |
||||
this.hash = bytes; |
} |
||||
this.version = version || mainnet; |
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. |
* Serialize this object as a standard Bitcoin address. |
||||
* |
|
||||
* Returns the address as a base58-encoded string in the standardized format. |
* Returns the address as a base58-encoded string in the standardized format. |
||||
*/ |
*/ |
||||
Address.prototype.toString = function () { |
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 { |
* Returns true if a bitcoin address is a valid address, otherwise false. |
||||
base58.checkDecode(string); |
*/ |
||||
return true; |
Address.validate = function (address) { |
||||
} catch (e) { |
try { |
||||
return false; |
base58.checkDecode(address) |
||||
} |
return true |
||||
}; |
} catch (e) { |
||||
|
return false |
||||
|
} |
||||
|
} |
||||
|
|
||||
module.exports = Address; |
module.exports = Address |
||||
|
Loading…
Reference in new issue