|
| 1 | +import { ThirdwebStorage } from "@thirdweb-dev/storage"; |
| 2 | +import { providers, utils } from "ethers"; |
| 3 | +import invariant from "tiny-invariant"; |
| 4 | +import { extractConstructorParamsFromAbi } from "../common/feature-detection/extractConstructorParamsFromAbi"; |
| 5 | +import { getChainProvider } from "../constants/urls"; |
| 6 | +import { Abi } from "../schema/contracts/custom"; |
| 7 | +import { twProxyArtifactZK } from "./temp-artifact/TWProxy"; |
| 8 | +import { fetchSourceFilesFromMetadata } from "../common/fetchSourceFilesFromMetadata"; |
| 9 | +import { fetchRawPredeployMetadata } from "../common/feature-detection/fetchRawPredeployMetadata"; |
| 10 | +import { fetchContractMetadata } from "../common/fetchContractMetadata"; |
| 11 | +import { checkVerificationStatus } from "../common/verification"; |
| 12 | + |
| 13 | +const RequestStatus = { |
| 14 | + OK: "1", |
| 15 | + NOTOK: "0", |
| 16 | +}; |
| 17 | + |
| 18 | +// |
| 19 | +// ================================== |
| 20 | +// ======== Core Functions ========== |
| 21 | +// ================================== |
| 22 | +// |
| 23 | + |
| 24 | +export async function zkVerify( |
| 25 | + contractAddress: string, |
| 26 | + chainId: number, |
| 27 | + explorerAPIUrl: string, |
| 28 | + explorerAPIKey: string, |
| 29 | + storage: ThirdwebStorage, |
| 30 | + contractUri?: string, |
| 31 | + encodedConstructorArgs?: string, |
| 32 | +) { |
| 33 | + try { |
| 34 | + const provider = getChainProvider(chainId, {}); |
| 35 | + const contractBytecode = await provider.getCode(contractAddress); |
| 36 | + let compilerMetadata: any = {}; |
| 37 | + if (contractBytecode === twProxyArtifactZK.bytecode) { |
| 38 | + compilerMetadata = { |
| 39 | + name: twProxyArtifactZK.contractName, |
| 40 | + abi: twProxyArtifactZK.abi, |
| 41 | + metadata: JSON.parse(twProxyArtifactZK.metadata.solc_metadata), |
| 42 | + zk_version: twProxyArtifactZK.metadata.zk_version, |
| 43 | + }; |
| 44 | + compilerMetadata.metadata.sources = twProxyArtifactZK.sources; |
| 45 | + } else { |
| 46 | + invariant(contractUri, "No contract URI provided"); |
| 47 | + const rawMeta = await fetchRawPredeployMetadata(contractUri, storage); |
| 48 | + const metadataUri = rawMeta.compilers?.zksolc?.metadataUri; |
| 49 | + |
| 50 | + invariant(metadataUri, "ZkSolc metadata not found"); |
| 51 | + const parsedMetadata = await fetchContractMetadata(metadataUri, storage); |
| 52 | + |
| 53 | + compilerMetadata = { |
| 54 | + name: parsedMetadata.name, |
| 55 | + abi: parsedMetadata.abi, |
| 56 | + metadata: parsedMetadata.metadata, |
| 57 | + zk_version: rawMeta.zk_version, |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + const compilerVersion = compilerMetadata.metadata.compiler.version; |
| 62 | + const sources = await fetchSourceFilesFromMetadata( |
| 63 | + compilerMetadata, |
| 64 | + storage, |
| 65 | + ); |
| 66 | + |
| 67 | + const sourcesWithUrl = compilerMetadata.metadata.sources; |
| 68 | + const sourcesWithContents: Record<string, { content: string }> = {}; |
| 69 | + for (const path of Object.keys(sourcesWithUrl)) { |
| 70 | + const sourceCode = sources.find((source) => path === source.filename); |
| 71 | + if (!sourceCode) { |
| 72 | + throw new Error(`Could not find source file for ${path}`); |
| 73 | + } |
| 74 | + sourcesWithContents[path] = { |
| 75 | + content: sourceCode.source, |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + const compilerInput: any = { |
| 80 | + language: "Solidity", |
| 81 | + settings: compilerMetadata.metadata.settings.optimizer, |
| 82 | + sources: sourcesWithContents, |
| 83 | + }; |
| 84 | + |
| 85 | + const compilationTarget = |
| 86 | + compilerMetadata.metadata.settings.compilationTarget; |
| 87 | + const targets = Object.keys(compilationTarget); |
| 88 | + const contractPath = targets[0]; |
| 89 | + |
| 90 | + const encodedArgs = encodedConstructorArgs |
| 91 | + ? encodedConstructorArgs |
| 92 | + : await zkFetchConstructorParams( |
| 93 | + explorerAPIUrl, |
| 94 | + explorerAPIKey, |
| 95 | + contractAddress, |
| 96 | + compilerMetadata.abi, |
| 97 | + provider, |
| 98 | + ); |
| 99 | + |
| 100 | + const requestBody: Record<string, string> = { |
| 101 | + module: "contract", |
| 102 | + action: "verifysourcecode", |
| 103 | + contractaddress: contractAddress, |
| 104 | + sourceCode: compilerInput, |
| 105 | + codeformat: "solidity-standard-json-input", |
| 106 | + contractname: `${contractPath}:${compilerMetadata.name}`, |
| 107 | + compilerversion: `${compilerVersion.split("+")[0]}`, |
| 108 | + zkCompilerVersion: `v${compilerMetadata.zk_version}`, |
| 109 | + runs: compilerMetadata.metadata.settings.optimizer.runs, |
| 110 | + optimizationUsed: compilerMetadata.metadata.settings.optimizer.enabled |
| 111 | + ? "1" |
| 112 | + : "0", |
| 113 | + constructorArguements: encodedArgs, |
| 114 | + }; |
| 115 | + |
| 116 | + const result = await fetch(`${explorerAPIUrl}`, { |
| 117 | + method: "POST", |
| 118 | + headers: { "Content-Type": "application/json" }, |
| 119 | + body: JSON.stringify(requestBody), |
| 120 | + }); |
| 121 | + |
| 122 | + const data = await result.json(); |
| 123 | + if (data.status === RequestStatus.OK) { |
| 124 | + console.info("Checking verification status..."); |
| 125 | + const verificationStatus = await checkVerificationStatus( |
| 126 | + explorerAPIUrl, |
| 127 | + explorerAPIKey, |
| 128 | + data.result, |
| 129 | + ); |
| 130 | + console.info(verificationStatus); |
| 131 | + } else { |
| 132 | + throw new Error(`${data.result}`); |
| 133 | + } |
| 134 | + } catch (e: any) { |
| 135 | + throw new Error(e.toString()); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// |
| 140 | +// ================================== |
| 141 | +// ======== Helper Functions ======== |
| 142 | +// ================================== |
| 143 | +// |
| 144 | + |
| 145 | +async function zkFetchConstructorParams( |
| 146 | + explorerAPIUrl: string, |
| 147 | + explorerAPIKey: string, |
| 148 | + contractAddress: string, |
| 149 | + abi: Abi, |
| 150 | + provider: providers.Provider, |
| 151 | +): Promise<string> { |
| 152 | + const constructorParamTypes = extractConstructorParamsFromAbi(abi); |
| 153 | + if (constructorParamTypes.length === 0) { |
| 154 | + return ""; |
| 155 | + } |
| 156 | + |
| 157 | + const result = await fetch( |
| 158 | + `${explorerAPIUrl}?module=contract&action=getcontractcreation&contractaddresses=${contractAddress}&apikey=${explorerAPIKey}`, |
| 159 | + ); |
| 160 | + const creationTx = await result.json(); |
| 161 | + |
| 162 | + if ( |
| 163 | + creationTx && |
| 164 | + creationTx.status === RequestStatus.OK && |
| 165 | + creationTx.result[0] !== undefined |
| 166 | + ) { |
| 167 | + const txHash = creationTx.result[0].txHash; |
| 168 | + const transaction = await provider.getTransaction(txHash); |
| 169 | + if (transaction.to === "0x0000000000000000000000000000000000008006") { |
| 170 | + const decoded = utils.defaultAbiCoder.decode( |
| 171 | + ["bytes32", "bytes32", "bytes"], |
| 172 | + utils.hexDataSlice(transaction.data, 4), |
| 173 | + ); |
| 174 | + |
| 175 | + return decoded[2]; |
| 176 | + } else { |
| 177 | + // TODO: decode for create2 deployments via factory |
| 178 | + return ""; |
| 179 | + } |
| 180 | + } else { |
| 181 | + // Could not retrieve constructor parameters, using empty parameters as fallback |
| 182 | + return ""; |
| 183 | + } |
| 184 | +} |
0 commit comments