Created
September 19, 2016 12:28
-
-
Save bkawk/5f84e2e0852cbe2b63affac13aff3beb to your computer and use it in GitHub Desktop.
Check BTC Address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.getScript("../scripts/Sha256.js"); | |
$.getScript("../scripts/BigInt.js"); | |
// ----------------- FUNCTIONS------------------// | |
function check(address) { | |
var decoded = base58_decode(address); | |
if (decoded.length != 25) return false; | |
var cksum = decoded.substr(decoded.length - 4); | |
var rest = decoded.substr(0, decoded.length - 4); | |
var good_cksum = hex2a(sha256_digest(hex2a(sha256_digest(rest)))).substr(0, 4); | |
if (cksum != good_cksum) return false; | |
return true; | |
} // End check | |
function base58_decode(string) { | |
var table = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | |
var tableArray = new Array(); | |
var i; | |
for (i = 0; i < 58; i++) { | |
tableArray[table[i]] = int2bigInt(i, 8, 0); | |
} | |
var l = string.length; | |
var long_value = int2bigInt(0, 1, 0); | |
var num_58 = int2bigInt(58, 8, 0); | |
var c; | |
for(i = 0; i < l; i++) { | |
c = string[l - i - 1]; | |
long_value = add(long_value, mult(tableArray[c], pow(num_58, i))); | |
} | |
var hex = bigInt2str(long_value, 16); | |
var str = hex2a(hex); | |
var numberPad; | |
for (numberPad = 0; string[numberPad] == table[0]; numberPad++); | |
var output = str; | |
if (numberPad > 0) output = repeat("\0", numberPad) + str; | |
return output; | |
} // End base58_decode | |
function hex2a(hex) { | |
var str = ''; | |
for (var i = 0; i < hex.length; i += 2) | |
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); | |
return str; | |
} // End hex2a | |
function a2hex(str) { | |
var aHex = "0123456789abcdef"; | |
var l = str.length; | |
var nBuf; | |
var strBuf; | |
var strOut = ""; | |
for (var i = 0; i < l; i++) { | |
nBuf = str.charCodeAt(i); | |
strBuf = aHex[Math.floor(nBuf/16)]; | |
strBuf += aHex[nBuf % 16]; | |
strOut += strBuf; | |
} | |
return strOut; | |
} // End a2hex | |
function pow(big, exp) { | |
if (exp == 0) return int2bigInt(1, 1, 0); | |
var i; | |
var newbig = big; | |
for (i = 1; i < exp; i++) { | |
newbig = mult(newbig, big); | |
} | |
return newbig; | |
} // End pow | |
function repeat(s, n){ | |
var a = []; | |
while(a.length < n){ | |
a.push(s); | |
} | |
return a.join(''); | |
} // End repeat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Check BTC Address</title> | |
<script src="../scripts/jquery-1.11.1.js"></script> | |
<script src="../scripts/check-address.js"></script> | |
</head> | |
<body> | |
</body> | |
<script> | |
$(document).ready(function() { | |
setTimeout(function(){ | |
var address = "1Eym7pyJcaambv8FG4ZoU8A4xsiL9us2zz"; | |
var result = check(address); | |
console.log(result); | |
var address2 = "1Eym7pyJcaambv8FG4ZoU8A4xsiL9us2za"; | |
var result2 = check(address2); | |
console.log(result2); | |
}, 10); | |
}); | |
</script> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment