multicoin-address-validator-ts
Version:
Multicoin address validator for Bitcoin and other Altcoins. TS version
29 lines (28 loc) • 956 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyChecksum = exports.isValidAddress = void 0;
var cryptoUtils = require('./crypto/utils');
function hexToBytes(hex) {
var bytes = [];
for (var c = 0; c < hex.length; c += 2) {
// @ts-ignore
bytes.push(parseInt(hex.substr(c, 2), 16));
}
return bytes;
}
function isValidAddress(address) {
if (address.length !== 76) {
// Check if it has the basic requirements of an address
return false;
}
// Otherwise check each case
return verifyChecksum(address);
}
exports.isValidAddress = isValidAddress;
function verifyChecksum(address) {
var checksumBytes = address.slice(0, 32 * 2);
var check = address.slice(32 * 2, 38 * 2);
var blakeHash = cryptoUtils.blake2b(checksumBytes, 32).slice(0, 6 * 2);
return blakeHash === check;
}
exports.verifyChecksum = verifyChecksum;