2013-02-17 00:39:15 -05:00
|
|
|
var base58 = require('./base58');
|
|
|
|
var Crypto = require('./crypto-js/crypto');
|
|
|
|
var conv = require('./convert');
|
|
|
|
|
|
|
|
var address_types = {
|
|
|
|
prod: 0,
|
|
|
|
testnet: 111
|
|
|
|
};
|
|
|
|
|
|
|
|
var p2sh_types = {
|
|
|
|
prod: 5,
|
|
|
|
testnet: 196
|
|
|
|
};
|
|
|
|
|
|
|
|
var Address = function (bytes) {
|
2013-10-07 08:21:00 -04:00
|
|
|
if (typeof bytes === 'string') {
|
|
|
|
this.hash = base58.decode(bytes);
|
|
|
|
this.version = this.hash.version;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.hash = bytes;
|
|
|
|
this.version = 0x00;
|
|
|
|
}
|
2011-05-04 17:02:56 +01:00
|
|
|
};
|
|
|
|
|
2012-01-11 10:41:52 +01:00
|
|
|
/**
|
|
|
|
* Serialize this object as a standard Bitcoin address.
|
|
|
|
*
|
|
|
|
* Returns the address as a base58-encoded string in the standardized format.
|
|
|
|
*/
|
2013-02-17 00:39:15 -05:00
|
|
|
Address.prototype.toString = function () {
|
2012-01-11 02:40:45 +01:00
|
|
|
// Get a copy of the hash
|
|
|
|
var hash = this.hash.slice(0);
|
2011-05-04 17:02:56 +01:00
|
|
|
|
2013-10-07 08:21:00 -04:00
|
|
|
return base58.checkEncode(hash,this.version);
|
2013-02-17 00:39:15 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Address.prototype.getHashBase64 = function () {
|
|
|
|
return conv.bytesToBase64(this.hash);
|
2011-05-04 17:02:56 +01:00
|
|
|
};
|
|
|
|
|
2013-10-07 08:21:00 -04:00
|
|
|
Address.getVersion = function(string) {
|
|
|
|
return base58.decode(string)[0];
|
|
|
|
}
|
|
|
|
|
2013-02-17 00:39:15 -05:00
|
|
|
// TODO(shtylman) isValid?
|
2013-10-07 08:21:00 -04:00
|
|
|
Address.validate = function(string) {
|
2013-02-17 00:39:15 -05:00
|
|
|
try {
|
2013-10-07 08:21:00 -04:00
|
|
|
base58.checkDecode(string);
|
2013-02-17 00:39:15 -05:00
|
|
|
} catch (e) {
|
2013-10-07 08:21:00 -04:00
|
|
|
return false;
|
2013-02-17 00:39:15 -05:00
|
|
|
}
|
|
|
|
return true;
|
2011-05-04 17:02:56 +01:00
|
|
|
};
|
|
|
|
|
2012-01-11 10:41:52 +01:00
|
|
|
/**
|
|
|
|
* Parse a Bitcoin address contained in a string.
|
|
|
|
*/
|
2013-02-17 00:39:15 -05:00
|
|
|
Address.decodeString = function (string) {
|
2013-10-07 08:21:00 -04:00
|
|
|
return base58.checkDecode(string);
|
2011-05-04 17:02:56 +01:00
|
|
|
};
|
2013-02-17 00:39:15 -05:00
|
|
|
|
|
|
|
module.exports = Address;
|