Skip to content

Commit

Permalink
chore: generate helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cawfree committed Nov 5, 2022
1 parent 05ccab4 commit 416d047
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 36 deletions.
11 changes: 2 additions & 9 deletions apps/web/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {Main as Environment} from 'foundry';
// @ts-expect-error missing-declaration
import * as ffjavascript from 'ffjavascript';

import mainWitnessCalculator from '../public/Main_witness_calculator';
import {ethers} from 'ethers';
import {ConnectButton} from '@rainbow-me/rainbowkit';
import {useAccount, useProvider, useSigner} from "wagmi";
Expand All @@ -12,6 +11,7 @@ const {
contractAddress,
abi,
deployEtherFromFaucet,
createZeroKnowledgeHelpersAsync,
} = Environment;

type Result = {
Expand Down Expand Up @@ -40,14 +40,7 @@ export default function Main(): JSX.Element {

console.log({currentSignerBalance});

// TODO: turn this into a module
const [wasm, verificationKey] = await Promise.all([
fetch('/Main.wasm').then(e => e.arrayBuffer()),
fetch('/Main_verification_key.json').then(e => e.json()),
]);

// https://github.com/iden3/snarkjs/issues/126#issuecomment-1022877878
const witnessCalculator = await mainWitnessCalculator(wasm);
const {witnessCalculator, verificationKey} = await createZeroKnowledgeHelpersAsync();

const witnessBuffer = await witnessCalculator.calculateWTNSBin(
{a: 3, b: 11},
Expand Down
29 changes: 29 additions & 0 deletions packages/foundry/scripts/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const deployContractsAndWriteModule = async ({
readonly contractNames: readonly string[];
readonly url: string;
}) => {

const {
contractNameToDeploymentAddress,
} = await deployContractsByName({contractNames, url});
Expand All @@ -46,6 +47,7 @@ const deployContractsAndWriteModule = async ({
path.resolve('dist', 'index.ts'),
`
import {ethers, Wallet} from 'ethers';
import * as ffjavascript from 'ffjavascript';
const ANVIL_DEFAULT_WALLET_PRIVATE_KEY_DO_NOT_USE_YOU_WILL_GET_REKT = "${ANVIL_DEFAULT_WALLET_PRIVATE_KEY_DO_NOT_USE_YOU_WILL_GET_REKT}";
Expand All @@ -72,6 +74,32 @@ const deployEtherFromFaucet = async ({
return provider.sendTransaction(signedTransaction);
};
${contractNames
.map(
(contractName: string) => {
const witnessCalculatorExport = fs.readFileSync(
path.resolve('..', '..', 'build', `${contractName}_js`, 'witness_calculator.js'),
'utf-8',
);
return [
`function ${contractName}WitnessCalculatorThunk() {`,
` ${witnessCalculatorExport.substring('module.exports = '.length)}`,
' return async () => {',
' const [wasm, verificationKey] = await Promise.all([',
` fetch(\'/${contractName}.wasm\').then(e => e.arrayBuffer()),`,
` fetch(\'/${contractName}_verification_key.json\').then(e => e.json()),`,
' ]);',
' // @ts-expect-error missing-parameter',
' const witnessCalculator = await builder(wasm);',
' return {witnessCalculator, verificationKey};',
' }',
'}',
].join('\n');
},
)
.join('\n')
}
${contractNames.map((contractName: string) => `
export const ${contractName} = Object.freeze({
...${JSON.stringify(JSON.parse(fs.readFileSync(
Expand All @@ -81,6 +109,7 @@ export const ${contractName} = Object.freeze({
rpcUrl: "${url}",
contractAddress: "${contractNameToDeploymentAddress[contractName]}",
deployEtherFromFaucet,
createZeroKnowledgeHelpersAsync: ${contractName}WitnessCalculatorThunk(),
});
`.trim(),
)}
Expand Down
8 changes: 4 additions & 4 deletions scripts/circuits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ circuits.forEach((circuit: string) => {
opts,
);

child_process.execSync(
`cp -rf ${path.resolve('build', `${name}_js`, 'witness_calculator.js')} ${path.resolve(publicDir, `${name}_witness_calculator.js`)}`,
opts,
);
//child_process.execSync(
// `cp -rf ${path.resolve('build', `${name}_js`, 'witness_calculator.js')} ${path.resolve(publicDir, `${name}_witness_calculator.js`)}`,
// opts,
//);

child_process.execSync(
`cp -rf ${path.resolve('build', `${name}_final.zkey`)} ${publicDir}/`,
Expand Down
34 changes: 11 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.20.1.tgz#f2e6ef7790d8c8dbf03d379502dcc246dcce0b30"
integrity sha512-EWZ4mE2diW3QALKvDMiXnbZpRvlj+nayZ112nK93SnhqOtpdsbVD4W+2tEoT3YNBAG9RBR0ISY758ZkOgsn6pQ==

"@babel/[email protected]", "@babel/core@^7.0.0":
"@babel/[email protected]":
version "7.19.6"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.19.6.tgz#7122ae4f5c5a37c0946c066149abd8e75f81540f"
integrity sha512-D2Ue4KHpc6Ys2+AxpIx1BZ8+UegLLLE2p3KJEuJRKmokHOtl49jQ5ny1773KsGLZs8MQvBidAF6yWUJxRqtKtg==
Expand Down Expand Up @@ -894,7 +894,7 @@
dependencies:
tslib "^2.4.0"

"@tailwindcss/typography@^0.5.7":
"@tailwindcss/[email protected]":
version "0.5.7"
resolved "https://registry.yarnpkg.com/@tailwindcss/typography/-/typography-0.5.7.tgz#e0b95bea787ee14c5a34a74fc824e6fe86ea8855"
integrity sha512-JTTSTrgZfp6Ki4svhPA4mkd9nmQ/j9EfE7SbHJ1cLtthKkpW2OxsFXzSmxbhYbEkfNIyAyhle5p4SYyKRbz/jg==
Expand Down Expand Up @@ -980,7 +980,7 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.9.tgz#02d013de7058cea16d36168ef2fc653464cfbad4"
integrity sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==

"@types/[email protected]", "@types/node@^17.0.12":
"@types/[email protected]":
version "17.0.45"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.45.tgz#2c0fafd78705e7a18b7906b5201a522719dc5190"
integrity sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==
Expand All @@ -1002,14 +1002,14 @@
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf"
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==

"@types/[email protected]", "@types/react-dom@^18.0.7":
"@types/[email protected]":
version "18.0.8"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.0.8.tgz#d2606d855186cd42cc1b11e63a71c39525441685"
integrity sha512-C3GYO0HLaOkk9dDAz3Dl4sbe4AKUGTCfFIZsz3n/82dPNN8Du533HzKatDxeUYWu24wJgMP1xICqkWk1YOLOIw==
dependencies:
"@types/react" "*"

"@types/react@*", "@types/[email protected]", "@types/react@^18.0.22":
"@types/react@*", "@types/[email protected]":
version "18.0.24"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.0.24.tgz#2f79ed5b27f08d05107aab45c17919754cc44c20"
integrity sha512-wRJWT6ouziGUy+9uX0aW4YOJxAY0bG6/AOk5AW5QSvZqI7dk6VBIbXvcVgIw/W5Jrl24f77df98GEKTJGOLx7Q==
Expand Down Expand Up @@ -1517,7 +1517,7 @@ async@^3.2.3:
resolved "https://registry.yarnpkg.com/async/-/async-3.2.4.tgz#2d22e00f8cddeb5fde5dd33522b56d1cf569a81c"
integrity sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==

autoprefixer@^10.4.13:
[email protected]:
version "10.4.13"
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-10.4.13.tgz#b5136b59930209a321e9fa3dca2e7c4d223e83a8"
integrity sha512-49vKpMqcZYsJjwotvt4+h/BCjJVnhGwcLpDt5xkcaOG3eLrG/HUYLagrihYsQ+qrIBgIzX1Rw7a6L8I/ZA1Atg==
Expand Down Expand Up @@ -2705,7 +2705,7 @@ ethereumjs-util@^6.0.0:
ethjs-util "0.1.6"
rlp "^2.2.3"

[email protected], ethers@^5.7.2:
[email protected]:
version "5.7.2"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e"
integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg==
Expand Down Expand Up @@ -3981,7 +3981,7 @@ [email protected]:
picocolors "^1.0.0"
source-map-js "^1.0.2"

postcss@^8.4.18:
postcss@8.4.18, postcss@^8.4.18:
version "8.4.18"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.18.tgz#6d50046ea7d3d66a85e0e782074e7203bc7fbca2"
integrity sha512-Wi8mWhncLJm11GATDaQKobXSNEYGUHeQLiQqDFG1qQ5UTDPTEvKw0Xt5NsTpktGTwLps3ByrWsBrG0rB8YQ9oA==
Expand Down Expand Up @@ -4095,7 +4095,7 @@ randombytes@^2.1.0:
dependencies:
safe-buffer "^5.1.0"

[email protected], react-dom@^18.2.0:
[email protected]:
version "18.2.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.2.0.tgz#22aaf38708db2674ed9ada224ca4aa708d821e3d"
integrity sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g==
Expand Down Expand Up @@ -4143,7 +4143,7 @@ react-style-singleton@^2.2.1:
invariant "^2.2.4"
tslib "^2.0.0"

[email protected], react@^18.2.0:
[email protected]:
version "18.2.0"
resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5"
integrity sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==
Expand Down Expand Up @@ -4427,11 +4427,6 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha512-D9cPgkvLlV3t3IzL0D0YLvGA9Ahk4PcvVwUbN0dSGr1aP0Nrt4AEnTUbuGvquEC0mA64Gqt1fzirlRs5ibXx8g==

[email protected]:
version "0.1.40"
resolved "https://registry.yarnpkg.com/std/-/std-0.1.40.tgz#3678a5f65094d9e1b6b5e26edbfc0212b8342b71"
integrity sha512-wUf57hkDGCoVShrhPA8Q7lAg2Qosk+FaMlECmAsr1A4/rL2NRXFHQGBcgMUFKVkPEemJFW9gzjCQisRty14ohg==

stream-browserify@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/stream-browserify/-/stream-browserify-3.0.0.tgz#22b0a2850cdf6503e73085da1fc7b7d0c2122f2f"
Expand Down Expand Up @@ -4758,18 +4753,11 @@ [email protected]:
dependencies:
is-typedarray "^1.0.0"

[email protected], typescript@^4.5.3, typescript@^4.7.4:
[email protected], typescript@^4.7.4:
version "4.8.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6"
integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ==

ui@*:
version "0.2.4"
resolved "https://registry.yarnpkg.com/ui/-/ui-0.2.4.tgz#39b7c8086b2b1b64a72c6437c38ddc7c4e4e836d"
integrity sha512-se2D+frb/i6JwFc7j72IEa0JADF8lfgc6ivOigaYkXHOuZRBtWe8R7UZ2YiXcbJ1dYNsjq40dn39rlNRxmVdPw==
dependencies:
std "0.1.x"

unbox-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/unbox-primitive/-/unbox-primitive-1.0.2.tgz#29032021057d5e6cdbd08c5129c226dff8ed6f9e"
Expand Down

0 comments on commit 416d047

Please sign in to comment.