You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

var base58 = require('./base58');
var Crypto = require('./crypto-js/crypto');
var conv = require('./convert');
var util = require('./util');
var Address = function (bytes, version) {
if (!(this instanceof Address)) { return new Address(bytes, version); }
11 years ago
if (arguments[0] instanceof Address) {
this.hash = arguments[0].hash;
this.version = arguments[0].version;
}
else if (typeof bytes === 'string') {
this.hash =
bytes.length <= 34 ? base58.checkDecode(bytes)
: bytes.length <= 40 ? conv.hexToBytes(bytes)
: util.error('Bad input');
this.version = version || this.hash.version || Address.address_types.prod;
}
else {
this.hash = bytes;
this.version = version || bytes.version || Address.address_types.prod;
}
14 years ago
};
13 years ago
/**
* 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);
};
Address.prototype.getHash = function () {
return conv.bytesToHex(this.hash);
14 years ago
};
Address.getVersion = function(string) {
return base58.decode(string)[0];
}
Address.validate = function(string) {
try {
base58.checkDecode(string);
return true;
} catch (e) {
return false;
}
14 years ago
};
13 years ago
/**
* Parse a Bitcoin address contained in a string.
*/
Address.decodeString = function (string) {
return base58.checkDecode(string);
14 years ago
};
Address.address_types = {
prod: 0,
testnet: 111
};
Address.p2sh_types = {
prod: 5,
testnet: 196
};
module.exports = Address;