Be better at TypeScript

This commit is contained in:
dancoffman 2022-11-04 13:29:52 -07:00
parent 1d20797874
commit 84bd55b304
No known key found for this signature in database
GPG Key ID: 47B1F53E36A9B3CC
2 changed files with 21 additions and 32 deletions

View File

@ -9,6 +9,12 @@ const FIELD_ELEMENTS_PER_BLOB = kzg.FIELD_ELEMENTS_PER_BLOB;
const BYTES_PER_FIELD_ELEMENT = kzg.BYTES_PER_FIELD_ELEMENT;
// Stored as internal state
let setupHandle;
function requireSetupHandle() {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup to initialize KZG.");
}
return setupHandle;
}
function loadTrustedSetup(filePath) {
if (setupHandle) {
throw new Error("Call freeTrustedSetup before loading a new trusted setup.");
@ -16,29 +22,17 @@ function loadTrustedSetup(filePath) {
setupHandle = kzg.loadTrustedSetup(filePath);
}
function freeTrustedSetup() {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup before freeTrustedSetup.");
}
kzg.freeTrustedSetup(setupHandle);
kzg.freeTrustedSetup(requireSetupHandle());
setupHandle = undefined;
}
function blobToKzgCommitment(blob) {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup to initialize KZG.");
}
return kzg.blobToKzgCommitment(blob, setupHandle);
return kzg.blobToKzgCommitment(blob, requireSetupHandle());
}
function computeAggregateKzgProof(blobs) {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup to initialize KZG.");
}
return kzg.computeAggregateKzgProof(blobs, setupHandle);
return kzg.computeAggregateKzgProof(blobs, requireSetupHandle());
}
function verifyAggregateKzgProof(blobs, expectedKzgCommitments, kzgAggregatedProof) {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup to initialize KZG.");
}
return kzg.verifyAggregateKzgProof(blobs, expectedKzgCommitments, kzgAggregatedProof, setupHandle);
return kzg.verifyAggregateKzgProof(blobs, expectedKzgCommitments, kzgAggregatedProof, requireSetupHandle());
}
exports.BYTES_PER_FIELD_ELEMENT = BYTES_PER_FIELD_ELEMENT;

View File

@ -50,6 +50,13 @@ export const BYTES_PER_FIELD_ELEMENT = kzg.BYTES_PER_FIELD_ELEMENT;
// Stored as internal state
let setupHandle: SetupHandle | undefined;
function requireSetupHandle(): SetupHandle {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup to initialize KZG.");
}
return setupHandle;
}
export function loadTrustedSetup(filePath: string): void {
if (setupHandle) {
throw new Error(
@ -60,25 +67,16 @@ export function loadTrustedSetup(filePath: string): void {
}
export function freeTrustedSetup(): void {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup before freeTrustedSetup.");
}
kzg.freeTrustedSetup(setupHandle);
kzg.freeTrustedSetup(requireSetupHandle());
setupHandle = undefined;
}
export function blobToKzgCommitment(blob: Blob): KZGCommitment {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup to initialize KZG.");
}
return kzg.blobToKzgCommitment(blob, setupHandle);
return kzg.blobToKzgCommitment(blob, requireSetupHandle());
}
export function computeAggregateKzgProof(blobs: Blob[]): KZGProof {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup to initialize KZG.");
}
return kzg.computeAggregateKzgProof(blobs, setupHandle);
return kzg.computeAggregateKzgProof(blobs, requireSetupHandle());
}
export function verifyAggregateKzgProof(
@ -86,13 +84,10 @@ export function verifyAggregateKzgProof(
expectedKzgCommitments: KZGCommitment[],
kzgAggregatedProof: KZGProof,
): boolean {
if (!setupHandle) {
throw new Error("You must call loadTrustedSetup to initialize KZG.");
}
return kzg.verifyAggregateKzgProof(
blobs,
expectedKzgCommitments,
kzgAggregatedProof,
setupHandle,
requireSetupHandle(),
);
}