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.

50 lines
1.1 KiB

14 years ago
Bitcoin.Address = function (bytes) {
13 years ago
if ("string" == typeof bytes) {
bytes = Bitcoin.Address.decodeString(bytes);
}
this.hash = bytes;
13 years ago
this.version = 0x00;
14 years ago
};
Bitcoin.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
13 years ago
return Bitcoin.Base58.encode(bytes);
14 years ago
};
Bitcoin.Address.prototype.getHashBase64 = function () {
13 years ago
return Crypto.util.bytesToBase64(this.hash);
14 years ago
};
Bitcoin.Address.decodeString = function (string) {
13 years ago
var bytes = Bitcoin.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 "Checksum validation failed!";
}
14 years ago
13 years ago
var version = hash.shift();
14 years ago
13 years ago
if (version != 0) {
throw "Version "+version+" not supported!";
}
14 years ago
13 years ago
return hash;
14 years ago
};