diff options
| author | Adam Soltys <[email protected]> | 2012-11-14 14:31:19 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2012-11-14 14:31:19 -0800 |
| commit | 02b4129f6beaec8eb0ca584e4ba3aec686f43e79 (patch) | |
| tree | 09a1fe6e09b8588ba4ab1f33f0e33b0cc908f21a /public/js/check_address.js | |
| parent | bb5a18653075ed57e89ce1965eccd8893d9e6458 (diff) | |
| download | coinos-server-02b4129f6beaec8eb0ca584e4ba3aec686f43e79.tar.gz coinos-server-02b4129f6beaec8eb0ca584e4ba3aec686f43e79.zip | |
Address validation
Diffstat (limited to 'public/js/check_address.js')
| -rw-r--r-- | public/js/check_address.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/public/js/check_address.js b/public/js/check_address.js new file mode 100644 index 00000000..0ec3de3c --- /dev/null +++ b/public/js/check_address.js @@ -0,0 +1,66 @@ +BigInteger.valueOf = nbv; +BigInteger.prototype.toByteArrayUnsigned = function () { + var ba = this.toByteArray(); + if (ba.length) { + if (ba[0] == 0) + ba = ba.slice(1); + return ba.map(function (v) { + return (v < 0) ? v + 256 : v; + }); + } else + return ba; +}; +var Bitcoin = {}; +(function () { + var B58 = Bitcoin.Base58 = { + alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz", + base: BigInteger.valueOf(58), + decode: function (input) { + bi = BigInteger.valueOf(0); + var leadingZerosNum = 0; + for (var i = input.length - 1; i >= 0; i--) { + var alphaIndex = B58.alphabet.indexOf(input[i]); + if (alphaIndex < 0) { + throw "Invalid character"; + } + bi = bi.add(BigInteger.valueOf(alphaIndex) + .multiply(B58.base.pow(input.length - 1 - i))); + if (input[i] == "1") leadingZerosNum++; + else leadingZerosNum = 0; + } + var bytes = bi.toByteArrayUnsigned(); + while (leadingZerosNum-- > 0) bytes.unshift(0); + return bytes; + } + }; +})(); +Bitcoin.Address = function (bytes) { + if ("string" == typeof bytes) + bytes = Bitcoin.Address.decodeString(bytes); + this.hash = bytes; + this.version = Bitcoin.Address.networkVersion; +}; +Bitcoin.Address.networkVersion = 0x00; // mainnet +Bitcoin.Address.decodeString = function (string) { + var bytes = Bitcoin.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 "Checksum validation failed!"; + var version = hash.shift(); + if (version != 0) + throw "Version " + version + " not supported!"; + return hash; +}; + +function check_address(address) { + try { + Bitcoin.Address(address); + return true; + } catch (err) { + return false; + } +} |
