Advertisement
joric

Javascript bitcoin address validity checker

Feb 21st, 2012
1,903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>JavaScript Bitcoin Address Validity Checker</title>
  5.     <script src="http://crypto-js.googlecode.com/files/2.5.3-crypto-sha256.js"></script>
  6.     <script src="http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js"></script>
  7.     <script src="http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn2.js"></script>
  8.     <!-- taken from bitcoinjs almost verbatim -->
  9.     <script>   
  10.         BigInteger.valueOf = nbv;
  11.         BigInteger.prototype.toByteArrayUnsigned = function () {
  12.             var ba = this.toByteArray();
  13.             if (ba.length) {
  14.                 if (ba[0] == 0)
  15.                     ba = ba.slice(1);
  16.                 return ba.map(function (v) {
  17.                     return (v < 0) ? v + 256 : v;
  18.                 });
  19.             } else
  20.                 return ba;
  21.         };        
  22.         var Bitcoin = {};
  23.         (function () {
  24.             var B58 = Bitcoin.Base58 = {
  25.                 alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
  26.                 base: BigInteger.valueOf(58),
  27.                 decode: function (input) {
  28.                     bi = BigInteger.valueOf(0);
  29.                     var leadingZerosNum = 0;
  30.                     for (var i = input.length - 1; i >= 0; i--) {
  31.                         var alphaIndex = B58.alphabet.indexOf(input[i]);
  32.                         if (alphaIndex < 0) {
  33.                             throw "Invalid character";
  34.                         }
  35.                         bi = bi.add(BigInteger.valueOf(alphaIndex)
  36.                         .multiply(B58.base.pow(input.length - 1 - i)));
  37.                         if (input[i] == "1") leadingZerosNum++;
  38.                         else leadingZerosNum = 0;
  39.                     }
  40.                     var bytes = bi.toByteArrayUnsigned();
  41.                     while (leadingZerosNum-- > 0) bytes.unshift(0);
  42.                     return bytes;
  43.                 }
  44.             };
  45.         })();
  46.         Bitcoin.Address = function (bytes) {
  47.             if ("string" == typeof bytes)
  48.                 bytes = Bitcoin.Address.decodeString(bytes);
  49.             this.hash = bytes;
  50.             this.version = Bitcoin.Address.networkVersion;
  51.         };
  52.         Bitcoin.Address.networkVersion = 0x00; // mainnet
  53.         Bitcoin.Address.decodeString = function (string) {
  54.             var bytes = Bitcoin.Base58.decode(string);
  55.             var hash = bytes.slice(0, 21);
  56.             var checksum = Crypto.SHA256(Crypto.SHA256(hash, { asBytes: true }), { asBytes: true });
  57.             if (checksum[0] != bytes[21] ||
  58.                 checksum[1] != bytes[22] ||
  59.                 checksum[2] != bytes[23] ||
  60.                 checksum[3] != bytes[24])
  61.                 throw "Checksum validation failed!";
  62.             var version = hash.shift();
  63.             if (version != 0)
  64.                 throw "Version " + version + " not supported!";
  65.             return hash;
  66.         };
  67.         function check_address(address) {
  68.             try {
  69.                 Bitcoin.Address(address);
  70.                 return true;
  71.             } catch (err) {
  72.                 return false;
  73.             }
  74.         }
  75.     </script>
  76. </head>
  77. <body>
  78.     <input type="text" value="1RRi5kDMck5cj2ob4UcjKxnGL2sjknRtv" size="50" autofocus
  79.         oninput="this.style.color=check_address(this.value)?'#000':'#f00'">
  80. </body>
  81. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement