@waku/rln
Version:
RLN (Rate Limiting Nullifier) implementation for Waku
25 lines (22 loc) • 796 B
JavaScript
import { createCodec, CODEC_TYPES } from '../codec.js';
function enumeration(v) {
function findValue(val) {
// Use the reverse mapping to look up the enum key for the stored value
// https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
if (v[val.toString()] == null) {
throw new Error('Invalid enum value');
}
return v[val];
}
const encode = function enumEncode(val, writer) {
const enumValue = findValue(val);
writer.int32(enumValue);
};
const decode = function enumDecode(reader) {
const val = reader.int32();
return findValue(val);
};
// @ts-expect-error yeah yeah
return createCodec('enum', CODEC_TYPES.VARINT, encode, decode);
}
export { enumeration };