|
| 1 | +import {readdirSync, statSync, readFileSync} from 'node:fs'; |
| 2 | +import {join, extname, resolve} from 'node:path'; |
| 3 | + |
| 4 | +import {isHex} from 'viem'; |
| 5 | + |
| 6 | +import { |
| 7 | + instanceSizes, |
| 8 | + loadConfig, |
| 9 | + viemChain, |
| 10 | + DEFAULT_CONFIG, |
| 11 | +} from '../utils.js'; |
| 12 | +import {deployAndVerifyContractFromSource} from '../solidity.js'; |
| 13 | + |
| 14 | +import { |
| 15 | + invokeRemoteMachine, |
| 16 | + verifyCircuit, |
| 17 | +} from '../circuitscan.js'; |
| 18 | + |
| 19 | +const DEFAULT_NARGO = "0.33.0"; |
| 20 | +const VERSIONS = { |
| 21 | + "0.34.0": "0.55.0", |
| 22 | + "0.33.0": "0.47.1", |
| 23 | + "0.32.0": "0.46.1", |
| 24 | + "0.31.0": "0.41.0", |
| 25 | +}; |
| 26 | + |
| 27 | +export default function(program) { |
| 28 | + program |
| 29 | + .command('verify:noir <chainId> <verifierContractAddress> [packageDir]') |
| 30 | + .description('Verify verifier contracts by their noir sources. Can also specify chain by name.') |
| 31 | + .option('-v, --nargo-version <version>', 'Specify nargo version') |
| 32 | + .option('-i, --instance <memorySize>', `Specify the memory (GB) of compiler instance: ${Object.keys(instanceSizes).join(', ')} (default: 4 for smallest circuits)`) |
| 33 | + .option('-r, --resume <requestId>', 'In case of errors during compilation, reattach to a job and attempt a new deploy. Overrides all other options.') |
| 34 | + .option('-c, --config <configUrl>', `Specify a different configuration file (default: ${DEFAULT_CONFIG})`) |
| 35 | + .option('-a, --api-key <apiKey>', `Specify your API Key as a command line argument`) |
| 36 | + .action(verify); |
| 37 | + |
| 38 | + program |
| 39 | + .command('deploy:noir <chainId> [packageDir]') |
| 40 | + .description('Deploy verifier contracts by their noir sources. Can also specify chain by name.') |
| 41 | + .option('-v, --nargo-version <version>', 'Specify nargo version') |
| 42 | + .option('-i, --instance <memorySize>', `Specify the memory (GB) of compiler instance: ${Object.keys(instanceSizes).join(', ')} (default: 4 for smallest circuits)`) |
| 43 | + .option('-r, --resume <requestId>', 'In case of errors during compilation, reattach to a job and attempt a new deploy. Overrides all other options.') |
| 44 | + .option('-c, --config <configUrl>', `Specify a different configuration file (default: ${DEFAULT_CONFIG})`) |
| 45 | + .option('-a, --api-key <apiKey>', `Specify your API Key as a command line argument`) |
| 46 | + .option('-b, --browser-wallet', 'Send transaction in browser instead of by passing private key env var (overrides chainId argument)') |
| 47 | + .action(deploy); |
| 48 | +} |
| 49 | + |
| 50 | +async function verify(chainId, contractAddr, packageDir, options) { |
| 51 | + options = await loadConfig(options); |
| 52 | + const chain = viemChain(chainId); |
| 53 | + if(!chain) throw new Error('INVALID_CHAIN'); |
| 54 | + const compiled = await compileFile(packageDir, options); |
| 55 | + await verifyCircuit( |
| 56 | + 'verifyNoir', |
| 57 | + compiled.pkgName, |
| 58 | + chain.id, |
| 59 | + contractAddr, |
| 60 | + options, |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +async function deploy(chainId, packageDir, options) { |
| 65 | + options = await loadConfig(options); |
| 66 | + const chain = viemChain(chainId); |
| 67 | + if(!options.browserWallet && !chain) throw new Error('INVALID_CHAIN'); |
| 68 | + const privateKey = process.env.DEPLOYER_PRIVATE_KEY; |
| 69 | + if(!options.browserWallet && (!privateKey || !isHex(privateKey) || privateKey.length !== 66)) |
| 70 | + throw new Error('INVALID_DEPLOYER_PRIVATE_KEY') |
| 71 | + const compiled = await compileFile(packageDir, options); |
| 72 | + const contractSource = await (await fetch(`${options.config.blobUrl}build/${compiled.pkgName}/verifier.sol`)).text(); |
| 73 | + const deployResult = await deployAndVerifyContractFromSource(contractSource, chain, privateKey, options); |
| 74 | + await verifyCircuit( |
| 75 | + 'verifyNoir', |
| 76 | + compiled.pkgName, |
| 77 | + deployResult.chain.id, |
| 78 | + deployResult.contractAddress, |
| 79 | + options, |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +async function compileFile(packageDir, options) { |
| 84 | + packageDir = resolve(packageDir || '.'); |
| 85 | + const fileList = listFilesWithExtension(packageDir, '.nr').map(x => resolve(x).slice(packageDir.length + 1)); |
| 86 | + const files = fileList.map(filename => { |
| 87 | + return { |
| 88 | + filename, |
| 89 | + content: readFileSync(join(packageDir, filename), 'utf8'), |
| 90 | + }; |
| 91 | + }); |
| 92 | + const nargoToml = readFileSync(join(packageDir, 'Nargo.toml'), 'utf8'); |
| 93 | + const nargoVersion = options.nargoVersion || DEFAULT_NARGO; |
| 94 | + if(!VERSIONS.hasOwnProperty(nargoVersion)) |
| 95 | + throw new error('INVALID_NARGO_VERSION'); |
| 96 | + const payload = { |
| 97 | + pipeline: 'noir', |
| 98 | + files, |
| 99 | + nargoToml, |
| 100 | + nargoVersion, |
| 101 | + bbupVersion: VERSIONS[nargoVersion], |
| 102 | + }; |
| 103 | + const compiled = await invokeRemoteMachine(payload, options); |
| 104 | + if('errorMessage' in compiled) { |
| 105 | + throw new Error(compiled.errorMessage); |
| 106 | + } |
| 107 | + return compiled; |
| 108 | +} |
| 109 | + |
| 110 | +function listFilesWithExtension(dirPath, extension, fileList = []) { |
| 111 | + const files = readdirSync(dirPath); |
| 112 | + |
| 113 | + files.forEach(file => { |
| 114 | + const fullPath = join(dirPath, file); |
| 115 | + const stat = statSync(fullPath); |
| 116 | + |
| 117 | + if (stat.isDirectory()) { |
| 118 | + listFilesWithExtension(fullPath, extension, fileList); |
| 119 | + } else if (extname(fullPath) === extension) { |
| 120 | + fileList.push(fullPath); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + return fileList; |
| 125 | +} |
0 commit comments