Path: blob/master/src/bitcoinjs-lib.address.js
248 views
//https://raw.github.com/bitcoinjs/bitcoinjs-lib/09e8c6e184d6501a0c2c59d73ca64db5c0d3eb95/src/address.js1Bitcoin.Address = function (bytes) {2if ("string" == typeof bytes) {3bytes = Bitcoin.Address.decodeString(bytes);4}5this.hash = bytes;6this.version = Bitcoin.Address.networkVersion;7};89Bitcoin.Address.networkVersion = 0x00; // mainnet1011/**12* Serialize this object as a standard Bitcoin address.13*14* Returns the address as a base58-encoded string in the standardized format.15*/16Bitcoin.Address.prototype.toString = function () {17// Get a copy of the hash18var hash = this.hash.slice(0);1920// Version21hash.unshift(this.version);22var checksum = Crypto.SHA256(Crypto.SHA256(hash, { asBytes: true }), { asBytes: true });23var bytes = hash.concat(checksum.slice(0, 4));24return Bitcoin.Base58.encode(bytes);25};2627Bitcoin.Address.prototype.getHashBase64 = function () {28return Crypto.util.bytesToBase64(this.hash);29};3031/**32* Parse a Bitcoin address contained in a string.33*/34Bitcoin.Address.decodeString = function (string) {35var bytes = Bitcoin.Base58.decode(string);36var hash = bytes.slice(0, 21);37var checksum = Crypto.SHA256(Crypto.SHA256(hash, { asBytes: true }), { asBytes: true });3839if (checksum[0] != bytes[21] ||40checksum[1] != bytes[22] ||41checksum[2] != bytes[23] ||42checksum[3] != bytes[24]) {43throw "Checksum validation failed!";44}4546var version = hash.shift();4748if (version != 0) {49throw "Version " + version + " not supported!";50}5152return hash;53};5455