Last active
March 29, 2018 17:31
-
-
Save MikeLuDev/c810ca3680cc2bdb7d784056e1149721 to your computer and use it in GitHub Desktop.
Waves address validation script
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
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