import type {SecretKey} from "@chainsafe/bls/types"; import {toHexString} from "@chainsafe/ssz"; export function memoOnce(fn: () => R): () => R { let value: R | null = null; return function () { if (value === null) { value = fn(); } return value; }; } const signCache = new WeakMap>(); /** * **USE FOR TESTING ONLY** * * Caches the signatures associated to a `SecretKey` instance. * Usefull to speed up testing if the same message is signed multiple times. */ export function signCached(sk: SecretKey, message: Uint8Array): Uint8Array { const messageHex = toHexString(message); let cache = signCache.get(sk); if (!cache) { cache = new Map(); signCache.set(sk, cache); } const prevSig = cache.get(messageHex); if (prevSig) { return prevSig; } const sig = sk.sign(message).toBytes(); cache.set(messageHex, sig); return sig; }