diff --git a/src/cli.ts b/src/cli.ts index 474be85..1fff4c3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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'; @@ -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 }); @@ -29,6 +31,7 @@ function cli(args: string[]) { .action(async circuit => { const path = circomkit.instantiate(circuit); circomkit.log.info('Created at:', path); + teardown(); }); /////////////////////////////////////////////////////////////////////////////// @@ -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(); }); /////////////////////////////////////////////////////////////////////////////// @@ -53,6 +57,7 @@ function cli(args: string[]) { .action(async circuit => { await circomkit.clear(circuit); circomkit.log.info('Cleaned.'); + teardown(); }); /////////////////////////////////////////////////////////////////////////////// @@ -73,6 +78,7 @@ function cli(args: string[]) { } circomkit.log.info('Circomkit project initialized! ✨'); + teardown(); }); /////////////////////////////////////////////////////////////////////////////// @@ -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( @@ -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( @@ -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(); }) ); @@ -119,6 +128,7 @@ function cli(args: string[]) { .action(async circuit => { const path = await circomkit.contract(circuit); circomkit.log.info('Created at: ' + path); + teardown(); }); /////////////////////////////////////////////////////////////////////////////// @@ -129,6 +139,7 @@ function cli(args: string[]) { .action(async (circuit, input) => { const calldata = await circomkit.calldata(circuit, input); circomkit.log.info(calldata); + teardown(); }); /////////////////////////////////////////////////////////////////////////////// @@ -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(); }); /////////////////////////////////////////////////////////////////////////////// @@ -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(); }); /////////////////////////////////////////////////////////////////////////////// @@ -163,6 +176,7 @@ function cli(args: string[]) { } else { circomkit.log.info('Verification failed!'); } + teardown(); }); /////////////////////////////////////////////////////////////////////////////// @@ -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(); }); /////////////////////////////////////////////////////////////////////////////// @@ -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(); }); /////////////////////////////////////////////////////////////////////////////// @@ -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(); }); /////////////////////////////////////////////////////////////////////////////// @@ -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() diff --git a/src/utils/teardown.ts b/src/utils/teardown.ts new file mode 100644 index 0000000..f5c125f --- /dev/null +++ b/src/utils/teardown.ts @@ -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(); +}