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.

102 lines
2.2 KiB

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) {
if (typeof bytes === 'string') {
bytes = Address.decodeString(bytes);
13 years ago
}
this.hash = bytes;
13 years ago
this.version = 0x00;
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 () {
13 years ago
// Get a copy of the hash
var hash = this.hash.slice(0);
14 years ago
13 years ago
// Version
hash.unshift(this.version);
14 years ago
13 years ago
var checksum = Crypto.SHA256(Crypto.SHA256(hash, {asBytes: true}), {asBytes: true});
14 years ago
13 years ago
var bytes = hash.concat(checksum.slice(0,4));
14 years ago
return base58.encode(bytes);
};
Address.prototype.getHashBase64 = function () {
return conv.bytesToBase64(this.hash);
14 years ago
};
// TODO(shtylman) isValid?
Address.validate = function(string, type) {
try {
var bytes = base58.decode(string);
} catch (e) {
return false;
}
var hash = bytes.slice(0, 21);
var checksum = Crypto.SHA256(Crypto.SHA256(hash, {asBytes: true}), {asBytes: true});
if (checksum[0] != bytes[21] ||
checksum[1] != bytes[22] ||
checksum[2] != bytes[23] ||
checksum[3] != bytes[24]) {
return false;
}
var version = hash[0];
if (type && version !== address_types[type] && version !== p2sh_types[type]) {
return false;
}
return true;
14 years ago
};
13 years ago
/**
* Parse a Bitcoin address contained in a string.
*/
Address.decodeString = function (string) {
var bytes = base58.decode(string);
14 years ago
13 years ago
var hash = bytes.slice(0, 21);
14 years ago
13 years ago
var checksum = Crypto.SHA256(Crypto.SHA256(hash, {asBytes: true}), {asBytes: true});
14 years ago
13 years ago
if (checksum[0] != bytes[21] ||
checksum[1] != bytes[22] ||
checksum[2] != bytes[23] ||
checksum[3] != bytes[24]) {
throw new Error('Address Checksum validation failed: ' + string);
13 years ago
}
14 years ago
13 years ago
var version = hash.shift();
// TODO(shtylman) allow for specific version decoding same as validate above
13 years ago
if (version != 0) {
throw new Error('Address version not supported: ' + string);
13 years ago
}
14 years ago
13 years ago
return hash;
14 years ago
};
module.exports = Address;