Created
May 12, 2017 21:58
-
-
Save lukem512/59a31f50252d52151e1a8c8c09c38853 to your computer and use it in GitHub Desktop.
Validate a MaxCoin address
This file contains 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
// node imports | |
var sys = require('sys'); | |
var base58 = require('bs58'); | |
var SHA3 = require('sha3'); | |
// validation function | |
function validateAddress(address) { | |
// network/version byte | |
var version = address.substring(0, 1); | |
if (version != 'm') return false; | |
// decode | |
var decoded = base58.decode(address); | |
// check length | |
if (decoded.length != 25) return false; | |
// checksum | |
var checksum = new Buffer(4); | |
decoded.copy(checksum, 0, decoded.length - 4); | |
var rest = new Buffer(decoded.length - 4); | |
decoded.copy(rest, 0, 0, decoded.length - 4); | |
var start = rest.slice(0, 4); | |
//var start = new Buffer(4); | |
//rest.copy(start, 0, 0, 4); | |
var d = new SHA3.SHA3Hash(256); | |
d.update(start); | |
// THIS IS CORRECT | |
sys.puts(start); | |
sys.puts(base58.encode(start)); | |
sys.puts(start.length); | |
// THIS IS CORRECT | |
sys.puts(checksum); | |
sys.puts(base58.encode(checksum)); | |
sys.puts(checksum.length); | |
// I NEED THE FIRST 4 BYTES ONLY... | |
var checksum2 = new Buffer(4); | |
var hash = d.digest('binary'); | |
sys.puts(hash); | |
checksum2 = hash.slice(0, 4); | |
console.log(checksum2); | |
sys.puts(checksum2); // NOPE | |
sys.puts("checksum2:"+base58.encode(checksum2)); // NOPE | |
sys.puts(checksum2.length); // NOPE | |
if (checksum2 != checksum) return false; | |
return true; | |
} | |
// Validate an address | |
var address = "mQ1MSBXTRvV4ArwDksPiUBLWCmt7t9AkDi"; | |
sys.puts(validateAddress(address)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment