Skip to content

Commit

Permalink
Add teardown to cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ScreamingHawk committed Oct 3, 2024
1 parent 48bc6b9 commit 054592e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Circomkit} from './core';
import {existsSync, readFileSync, readdirSync, writeFileSync} from 'fs';
import {prettyStringify} from './utils';
import {exec} from 'child_process';
import {teardown} from './utils/teardown';

const CONFIG_PATH = './circomkit.json';

Expand All @@ -18,6 +19,7 @@ function cli(args: string[]) {
.action(async circuit => {
const path = await circomkit.compile(circuit);
circomkit.log.info('Built at:', path);
teardown();

// TODO: pattern matching https://github.com/erhant/circomkit/issues/79
});
Expand All @@ -29,6 +31,7 @@ function cli(args: string[]) {
.action(async circuit => {
const path = circomkit.instantiate(circuit);
circomkit.log.info('Created at:', path);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -44,6 +47,7 @@ function cli(args: string[]) {
console.log(`Number of Public Inputs: ${info.publicInputs}`);
console.log(`Number of Public Outputs: ${info.publicOutputs}`);
console.log(`Number of Labels: ${info.labels}`);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -53,6 +57,7 @@ function cli(args: string[]) {
.action(async circuit => {
await circomkit.clear(circuit);
circomkit.log.info('Cleaned.');
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -73,6 +78,7 @@ function cli(args: string[]) {
}

circomkit.log.info('Circomkit project initialized! ✨');
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -88,6 +94,7 @@ function cli(args: string[]) {
const {json, path} = await circomkit.json('r1cs', circuit);
writeFileSync(path, prettyStringify(json));
circomkit.log.info('Exported R1CS at: ' + path);
teardown();
})
)
.addCommand(
Expand All @@ -98,6 +105,7 @@ function cli(args: string[]) {
const {json, path} = await circomkit.json('zkey', circuit);
writeFileSync(path, prettyStringify(json));
circomkit.log.info('Exported prover key at: ' + path);
teardown();
})
)
.addCommand(
Expand All @@ -109,6 +117,7 @@ function cli(args: string[]) {
const {json, path} = await circomkit.json('wtns', circuit, input);
writeFileSync(path, prettyStringify(json));
circomkit.log.info('Exported prover key at: ' + path);
teardown();
})
);

Expand All @@ -119,6 +128,7 @@ function cli(args: string[]) {
.action(async circuit => {
const path = await circomkit.contract(circuit);
circomkit.log.info('Created at: ' + path);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -129,6 +139,7 @@ function cli(args: string[]) {
.action(async (circuit, input) => {
const calldata = await circomkit.calldata(circuit, input);
circomkit.log.info(calldata);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -139,6 +150,7 @@ function cli(args: string[]) {
.action(async (circuit, pkeyPath) => {
const path = await circomkit.vkey(circuit, pkeyPath);
circomkit.log.info('Created at: ' + path);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -149,6 +161,7 @@ function cli(args: string[]) {
.action(async (circuit, input) => {
const path = await circomkit.prove(circuit, input);
circomkit.log.info('Generated at: ' + path);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -163,6 +176,7 @@ function cli(args: string[]) {
} else {
circomkit.log.info('Verification failed!');
}
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -173,6 +187,7 @@ function cli(args: string[]) {
.action(async (circuit, input) => {
const path = await circomkit.witness(circuit, input);
circomkit.log.info('Witness created: ' + path);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -184,6 +199,7 @@ function cli(args: string[]) {
const {proverKeyPath, verifierKeyPath} = await circomkit.setup(circuit, ptauPath);
circomkit.log.info('Prover key created: ' + proverKeyPath);
circomkit.log.info('Verifier key created: ' + verifierKeyPath);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -193,6 +209,7 @@ function cli(args: string[]) {
.action(async circuit => {
const path = await circomkit.ptau(circuit);
circomkit.log.info('PTAU ready at: ' + path);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -211,12 +228,16 @@ function cli(args: string[]) {
.map((c, i) => ` ${i + 1}. ${c}`)
.join('\n')
);
teardown();
});

///////////////////////////////////////////////////////////////////////////////
const config = new Command('config')
.description('print configuration')
.action(() => circomkit.log.info(circomkit.config));
.action(() => {
circomkit.log.info(circomkit.config)
teardown();
});

///////////////////////////////////////////////////////////////////////////////
new Command()
Expand Down
8 changes: 8 additions & 0 deletions src/utils/teardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare global {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-var
var curve_bn128: any;
}

export function teardown() {
if (globalThis.curve_bn128) globalThis.curve_bn128.terminate();
}

0 comments on commit 054592e

Please sign in to comment.