Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validate a MaxCoin address
// 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