From e2e191402175858bacdff1d137a59e0f307d0a80 Mon Sep 17 00:00:00 2001 From: Koby Date: Tue, 26 Sep 2023 14:40:06 +0200 Subject: [PATCH 1/4] fix: lack of cjs package version --- tooling/noir_js/.mocharc.cjs.json | 6 ++ tooling/noir_js/.mocharc.json | 2 +- tooling/noir_js/package.json | 17 ++++-- tooling/noir_js/test/node/cjs.test.cjs | 76 ++++++++++++++++++++++++++ tooling/noir_js/tsc-multi.json | 7 +++ 5 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 tooling/noir_js/.mocharc.cjs.json create mode 100644 tooling/noir_js/test/node/cjs.test.cjs create mode 100644 tooling/noir_js/tsc-multi.json diff --git a/tooling/noir_js/.mocharc.cjs.json b/tooling/noir_js/.mocharc.cjs.json new file mode 100644 index 00000000000..c1c37373f18 --- /dev/null +++ b/tooling/noir_js/.mocharc.cjs.json @@ -0,0 +1,6 @@ +{ + "extensions": ["cjs"], + "spec": [ + "test/node/**/*.test.cjs" + ] +} \ No newline at end of file diff --git a/tooling/noir_js/.mocharc.json b/tooling/noir_js/.mocharc.json index 9e1a6acc542..c2e70b73d0f 100644 --- a/tooling/noir_js/.mocharc.json +++ b/tooling/noir_js/.mocharc.json @@ -1,7 +1,7 @@ { "require": "ts-node/register", "loader": "ts-node/esm", - "extensions": ["ts"], + "extensions": ["ts", "cjs"], "spec": [ "test/node/**/*.test.ts*" ] diff --git a/tooling/noir_js/package.json b/tooling/noir_js/package.json index 0168e381dce..0d96918886b 100644 --- a/tooling/noir_js/package.json +++ b/tooling/noir_js/package.json @@ -16,13 +16,21 @@ "lib", "package.json" ], - "main": "lib/index.js", + "source": "src/index.ts", + "main": "lib/index.cjs", + "module": "lib/index.js", + "exports": { + "require": "./lib/index.cjs", + "default": "./lib/index.js", + "types": "./lib/index.d.ts" + }, "types": "lib/index.d.ts", "scripts": { - "dev": "tsc --watch", - "build": "tsc", + "dev": "tsc-multi --watch", + "build": "tsc-multi", "test": "yarn test:node", - "test:node": "mocha --timeout 25000", + "test:node:esm": "mocha --timeout 25000 --config ./.mocharc.json", + "test:node:cjs": "mocha --timeout 25000 --config ./.mocharc.cjs.json", "prettier": "prettier 'src/**/*.ts'", "prettier:fix": "prettier --write 'src/**/*.ts' 'test/**/*.ts'", "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0" @@ -39,6 +47,7 @@ "mocha": "^10.2.0", "prettier": "3.0.3", "ts-node": "^10.9.1", + "tsc-multi": "^1.1.0", "typescript": "^5.2.2" } } diff --git a/tooling/noir_js/test/node/cjs.test.cjs b/tooling/noir_js/test/node/cjs.test.cjs new file mode 100644 index 00000000000..7ad604aa5bc --- /dev/null +++ b/tooling/noir_js/test/node/cjs.test.cjs @@ -0,0 +1,76 @@ +/* eslint-disable no-undef */ +/* eslint-disable @typescript-eslint/no-var-requires */ +const chai = require('chai'); +const assert_lt_json = require('../noir_compiled_examples/assert_lt/target/assert_lt.json'); +const noirjs = require('@noir-lang/noir_js'); + +it('001 generates witnesses successfully', async () => { + const inputs = { + x: '2', + y: '3', + }; + const _solvedWitness = await noirjs.generateWitness(assert_lt_json, inputs); +}); + +it('string input and number input are the same', async () => { + const inputsString = { + x: '2', + y: '3', + }; + const inputsNumber = { + x: 2, + y: 3, + }; + const solvedWitnessString = await noirjs.generateWitness(assert_lt_json, inputsString); + const solvedWitnessNumber = await noirjs.generateWitness(assert_lt_json, inputsNumber); + chai.expect(solvedWitnessString).to.deep.equal(solvedWitnessNumber); +}); + +it('string input and number input are the same', async () => { + const inputsString = { + x: '2', + y: '3', + }; + const inputsNumber = { + x: 2, + y: 3, + }; + + const solvedWitnessString = await noirjs.generateWitness(assert_lt_json, inputsString); + const solvedWitnessNumber = await noirjs.generateWitness(assert_lt_json, inputsNumber); + chai.expect(solvedWitnessString).to.deep.equal(solvedWitnessNumber); +}); + +it('0x prefixed string input for inputs will throw', async () => { + const inputsHexPrefix = { + x: '0x2', + y: '0x3', + }; + + try { + await noirjs.generateWitness(assert_lt_json, inputsHexPrefix); + chai.expect.fail( + 'Expected generatedWitness to throw, due to inputs being prefixed with 0x. Currently not supported', + ); + } catch (error) { + // Successfully errored due to 0x not being supported. Update this test once/if we choose + // to support 0x prefixed inputs. + } +}); + +describe('input validation', () => { + it('x should be a uint64 not a string', async () => { + const inputs = { + x: 'foo', + y: '3', + }; + + try { + await noirjs.generateWitness(assert_lt_json, inputs); + chai.expect.fail('Expected generatedWitness to throw, due to x not being convertible to a uint64'); + } catch (error) { + const knownError = error; + chai.expect(knownError.message).to.equal('Input for x is the wrong type, expected uint64, got "foo"'); + } + }); +}); diff --git a/tooling/noir_js/tsc-multi.json b/tooling/noir_js/tsc-multi.json new file mode 100644 index 00000000000..af26d1e4aa6 --- /dev/null +++ b/tooling/noir_js/tsc-multi.json @@ -0,0 +1,7 @@ +{ + "targets": [ + { "extname": ".cjs", "module": "commonjs" }, + { "extname": ".mjs", "module": "esnext" } + ], + "projects": ["packages/*/tsconfig.json"] +} \ No newline at end of file From 35eca845a001e9fa3d38d31cd6f6d30dbf52ddf5 Mon Sep 17 00:00:00 2001 From: Koby Date: Tue, 26 Sep 2023 14:40:32 +0200 Subject: [PATCH 2/4] chore: adds `tsc-multi` to lock --- yarn.lock | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/yarn.lock b/yarn.lock index 06bba81428d..e3d41c00fb5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -410,6 +410,7 @@ __metadata: mocha: ^10.2.0 prettier: 3.0.3 ts-node: ^10.9.1 + tsc-multi: ^1.1.0 typescript: ^5.2.2 languageName: unknown linkType: soft @@ -3544,7 +3545,7 @@ __metadata: languageName: node linkType: hard -"fast-glob@npm:^3.2.9, fast-glob@npm:^3.3.0": +"fast-glob@npm:^3.2.12, fast-glob@npm:^3.2.9, fast-glob@npm:^3.3.0": version: 3.3.1 resolution: "fast-glob@npm:3.3.1" dependencies: @@ -3908,6 +3909,13 @@ __metadata: languageName: node linkType: hard +"get-stdin@npm:^8.0.0": + version: 8.0.0 + resolution: "get-stdin@npm:8.0.0" + checksum: 40128b6cd25781ddbd233344f1a1e4006d4284906191ed0a7d55ec2c1a3e44d650f280b2c9eeab79c03ac3037da80257476c0e4e5af38ddfb902d6ff06282d77 + languageName: node + linkType: hard + "get-stream@npm:^5.1.0": version: 5.2.0 resolution: "get-stream@npm:5.2.0" @@ -5817,6 +5825,15 @@ __metadata: languageName: node linkType: hard +"p-all@npm:^3.0.0": + version: 3.0.0 + resolution: "p-all@npm:3.0.0" + dependencies: + p-map: ^4.0.0 + checksum: 267a620c2330b14246b92008f4be8758debe74e1454c8fb5808544f51fd038ac4597dbeeaa1542f237794e613cd42e4f1a58c01e5a0a6a6b21340fef616257df + languageName: node + linkType: hard + "p-cancelable@npm:^3.0.0": version: 3.0.0 resolution: "p-cancelable@npm:3.0.0" @@ -6953,6 +6970,15 @@ __metadata: languageName: node linkType: hard +"string-to-stream@npm:^3.0.1": + version: 3.0.1 + resolution: "string-to-stream@npm:3.0.1" + dependencies: + readable-stream: ^3.4.0 + checksum: e8ac7f7497f8f101196e39dd529e98bb97165c532cc4cae5003083a420db62f46ffd67ddff7112b45f9f8d0f9ff1cc6cda9b06362236d43fa6b1685e8b0d446e + languageName: node + linkType: hard + "string-width-cjs@npm:string-width@^4.2.0, string-width@npm:^1.0.2 || 2 || 3 || 4, string-width@npm:^4.1.0, string-width@npm:^4.2.0, string-width@npm:^4.2.3": version: 4.2.3 resolution: "string-width@npm:4.2.3" @@ -7032,6 +7058,13 @@ __metadata: languageName: node linkType: hard +"superstruct@npm:^1.0.3": + version: 1.0.3 + resolution: "superstruct@npm:1.0.3" + checksum: 761790bb111e6e21ddd608299c252f3be35df543263a7ebbc004e840d01fcf8046794c274bcb351bdf3eae4600f79d317d085cdbb19ca05803a4361840cc9bb1 + languageName: node + linkType: hard + "supertap@npm:^3.0.1": version: 3.0.1 resolution: "supertap@npm:3.0.1" @@ -7278,6 +7311,28 @@ __metadata: languageName: node linkType: hard +"tsc-multi@npm:^1.1.0": + version: 1.1.0 + resolution: "tsc-multi@npm:1.1.0" + dependencies: + debug: ^4.3.4 + fast-glob: ^3.2.12 + get-stdin: ^8.0.0 + p-all: ^3.0.0 + picocolors: ^1.0.0 + signal-exit: ^3.0.7 + string-to-stream: ^3.0.1 + superstruct: ^1.0.3 + tslib: ^2.5.0 + yargs: ^17.7.1 + peerDependencies: + typescript: ">=4.3.0" + bin: + tsc-multi: bin/tsc-multi.js + checksum: a82c0358611ac15667aa148ade33b6ad64cc0a94299fb9afc01e3e6224a994dff8812960a43643f25e4c0dac8419707027c3096d0e60bff3522591c06d5f4eeb + languageName: node + linkType: hard + "tslib@npm:^1.8.1": version: 1.14.1 resolution: "tslib@npm:1.14.1" @@ -7940,7 +7995,7 @@ __metadata: languageName: node linkType: hard -"yargs@npm:^17.7.2": +"yargs@npm:^17.7.1, yargs@npm:^17.7.2": version: 17.7.2 resolution: "yargs@npm:17.7.2" dependencies: From 75e1b3c9fef17e30d5e3c4f03af34909fe77d307 Mon Sep 17 00:00:00 2001 From: Koby Hall <102518238+kobyhallx@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:52:27 +0200 Subject: [PATCH 3/4] Update tooling/noir_js/test/node/cjs.test.cjs Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> --- tooling/noir_js/test/node/cjs.test.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/noir_js/test/node/cjs.test.cjs b/tooling/noir_js/test/node/cjs.test.cjs index 7ad604aa5bc..0eddb3cc4af 100644 --- a/tooling/noir_js/test/node/cjs.test.cjs +++ b/tooling/noir_js/test/node/cjs.test.cjs @@ -4,7 +4,7 @@ const chai = require('chai'); const assert_lt_json = require('../noir_compiled_examples/assert_lt/target/assert_lt.json'); const noirjs = require('@noir-lang/noir_js'); -it('001 generates witnesses successfully', async () => { +it('generates witnesses successfully', async () => { const inputs = { x: '2', y: '3', From 85e86665eb9af0a2475a80131e7976730dde9a59 Mon Sep 17 00:00:00 2001 From: Koby Date: Tue, 26 Sep 2023 14:54:34 +0200 Subject: [PATCH 4/4] fix: appropriate test script --- tooling/noir_js/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tooling/noir_js/package.json b/tooling/noir_js/package.json index 0d96918886b..13613cdb38b 100644 --- a/tooling/noir_js/package.json +++ b/tooling/noir_js/package.json @@ -28,7 +28,7 @@ "scripts": { "dev": "tsc-multi --watch", "build": "tsc-multi", - "test": "yarn test:node", + "test": "yarn test:node:esm && yarn test:node:cjs", "test:node:esm": "mocha --timeout 25000 --config ./.mocharc.json", "test:node:cjs": "mocha --timeout 25000 --config ./.mocharc.cjs.json", "prettier": "prettier 'src/**/*.ts'",