@waku/rln
Version:
RLN (Rate Limiting Nullifier) implementation for Waku
32 lines (29 loc) • 1.88 kB
JavaScript
import { cipherDecrypt as cipherDecrypt_1 } from '../../../../node_modules/@chainsafe/bls-keystore/lib/cipher.js';
import { kdf as kdf_2 } from '../../../../node_modules/@chainsafe/bls-keystore/lib/kdf.js';
import { normalizePassword as normalizePassword_1 } from '../../../../node_modules/@chainsafe/bls-keystore/lib/password.js';
import { keccak256 } from '../../../../node_modules/ethereum-cryptography/esm/keccak.js';
import { hexToBytes } from '../../../../node_modules/ethereum-cryptography/esm/utils.js';
import { concatBytes, bytesToHex } from '../../../../node_modules/@noble/hashes/esm/utils.js';
// eipKeystore supports only sha256 checksum so we just make an assumption it is keccak256
const validateChecksum = async (password, eipKeystore) => {
const computedChecksum = await keccak256Checksum(password, eipKeystore);
return computedChecksum === eipKeystore.crypto.checksum.message;
};
// decrypt from @chainsafe/bls-keystore supports only sha256
// but nwaku uses keccak256
// https://github.com/waku-org/nwaku/blob/25d6e52e3804d15f9b61bc4cc6dd448540c072a1/waku/waku_keystore/keyfile.nim#L367
const decryptEipKeystore = async (password, eipKeystore) => {
const decryptionKey = await kdf_2(eipKeystore.crypto.kdf, normalizePassword_1(password));
const isChecksumValid = await validateChecksum(password, eipKeystore);
if (!isChecksumValid) {
throw Error("Password is invalid.");
}
return cipherDecrypt_1(eipKeystore.crypto.cipher, decryptionKey.slice(0, 16));
};
const keccak256Checksum = async (password, eipKeystore) => {
const key = await kdf_2(eipKeystore.crypto.kdf, normalizePassword_1(password));
const payload = concatBytes(key.slice(16), hexToBytes(eipKeystore.crypto.cipher.message));
const ciphertext = keccak256(payload);
return bytesToHex(ciphertext);
};
export { decryptEipKeystore, keccak256Checksum };