Skip to content

Instantly share code, notes, and snippets.

@MikeLuDev
Last active March 29, 2018 17:31
Show Gist options
  • Save MikeLuDev/c810ca3680cc2bdb7d784056e1149721 to your computer and use it in GitHub Desktop.
Save MikeLuDev/c810ca3680cc2bdb7d784056e1149721 to your computer and use it in GitHub Desktop.
Waves address validation script
const blake2b = require("blake2b");
const keccak = require('keccak');
validWavesAddress(address) {
console.log(`Checking address validity...`)
let bytes = this.fromBase58(address);
const _blake2b = (data) => {
return blake2b(32).update(data).digest();
}
const _keccak = (data) => {
return keccak('keccak256').update(Buffer.from(data)).digest();
}
const _hashChain = (key) => {
return _keccak(_blake2b(key));
}
const key = bytes.slice(0, 22);
const checkSum = bytes.slice(22, 26);
const keyHash = Array.from(_hashChain(key));
let validChecksum = (bytes) => {
for (let i = 0; i < 4; i++) {
if (checkSum[i] !== keyHash[i]) {
return false;
}
}
return true;
}
let response = (
bytes.byteLength === 26 &&
bytes[0] === 1 &&
bytes[1] === 87 &&
validChecksum(bytes)) ?
(true) : (false);
(response === true) ?
(console.log(`Valid address!`)) :
(console.log(`Invalid address!`));
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment