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