Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static validateCheckSum({ signerKey = '' } = {}) {
if (!signerKey) {
return false;
}
const signerKeyBytes = base58.decode(signerKey);
if (signerKeyBytes.length !== 41) {
return false;
}
const prefixBytes = signerKeyBytes.slice(0, 5);
const keyBytes = signerKeyBytes.slice(5, 37);
const checkSum = signerKeyBytes.slice(37, 41);
const tmp = sha256.digest((sha256.digest([...prefixBytes, ...keyBytes])));
const tmpCheckSum = tmp.slice(0, 4);
if (Buffer.compare(Buffer.from(checkSum), Buffer.from(tmpCheckSum)) !== 0) {
return false;
}
return true;
}
static createKeyPair() {
const privateKeyBytes = secureRandom.randomUint8Array(32);
let tmp;
let checkSum;
tmp = sha256.digest((sha256.digest([...privatePrefixBytes, ...privateKeyBytes])));
checkSum = tmp.slice(0, 4);
const privateKey = base58.encode([...privatePrefixBytes, ...privateKeyBytes, ...checkSum]);
const publicKeyBytes = ec.keyFromSecret(privateKeyBytes).getPublic();
tmp = sha256.digest((sha256.digest([...publicPrefixBytes, ...publicKeyBytes])));
checkSum = tmp.slice(0, 4);
const publicKey = base58.encode([...publicPrefixBytes, ...publicKeyBytes, ...checkSum]);
return {
privateKey: privateKey,
publicKey: publicKey,
};
}