From 141639e00b6ecabaef7ff1171f4b3539d0880fbe Mon Sep 17 00:00:00 2001 From: Mati OS Date: Fri, 11 Sep 2026 14:15:45 -0300 Subject: [PATCH 1/2] fix(plugin): send only the build info fields the explorer reads Every injected artifact carried its whole build info - the full solc standard JSON input, with every source in the compilation job - and contracts compiled in the same job each carried their own copy. Once a project pulls in OpenZeppelin or forge-std, that runs to megabytes per contract and the localStorage write in the served page fails on quota. A local chain has no Sourcify or Etherscan fallback, so every deployed contract then shows as unverified. The explorer reads four scalar fields of it: solcVersion, solcLongVersion, input.language and input.settings.evmVersion. Source resolution still falls back to input.sources, so both loaders now keep the parsed build info local, resolve the source against it, and attach only those four fields. With forge-std's Test.sol imported into the example project's Counter, the payload injected for three deployments drops from 1.6M characters to 5.6K. Refs openscan-explorer/explorer#402 --- packages/plugin/src/artifacts.ts | 60 +++++++++++++++++++++-- packages/plugin/src/deployment-tracker.ts | 14 ++++-- 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/packages/plugin/src/artifacts.ts b/packages/plugin/src/artifacts.ts index 5b8539a..7a46a39 100644 --- a/packages/plugin/src/artifacts.ts +++ b/packages/plugin/src/artifacts.ts @@ -2,13 +2,28 @@ import { existsSync, readdirSync, readFileSync } from "node:fs"; import path from "node:path"; import { resolveSourceCode } from "./source-resolver.js"; +/** + * The parts of a solc build info file the explorer reads: compiler version, + * source language and EVM version. + */ +export interface ExplorerBuildInfo { + solcVersion?: string; + solcLongVersion?: string; + input: { + language?: string; + settings: { + evmVersion?: string; + }; + }; +} + export interface ArtifactData { abi: unknown[]; contractName: string; sourceName?: string; buildInfoId?: string; sourceCode?: string; - buildInfo?: unknown; + buildInfo?: ExplorerBuildInfo; deployments: string[]; } @@ -22,6 +37,39 @@ const CHAIN_ID = 31337; let hasLoggedArtifacts = false; +/** + * Reduce a solc build info file to the fields the explorer reads. + * + * Build info carries the full standard JSON input - every source in the + * compilation job - and each contract from that job points at the same file. + * Injecting it whole repeats megabytes per contract, overflows the browser's + * localStorage quota and leaves every contract showing as unverified. Source + * resolution falls back to `input.sources`, so trim only after it has run. + * + * @param buildInfo - Parsed build info, shape unverified + * @returns The fields the explorer reads, or undefined without build info + */ +export function toExplorerBuildInfo( + buildInfo: unknown, +): ExplorerBuildInfo | undefined { + if (typeof buildInfo !== "object" || buildInfo === null) return undefined; + + const { solcVersion, solcLongVersion, input } = buildInfo as { + solcVersion?: string; + solcLongVersion?: string; + input?: { language?: string; settings?: { evmVersion?: string } }; + }; + + return { + solcVersion, + solcLongVersion, + input: { + language: input?.language, + settings: { evmVersion: input?.settings?.evmVersion }, + }, + }; +} + export function findIgnitionDeployment(projectRoot: string): string | null { const deploymentPath = path.join( projectRoot, @@ -112,6 +160,7 @@ export function loadArtifacts( }; // Try to load build info + let buildInfo: unknown; if (artifactData.buildInfoId && existsSync(buildInfoDir)) { const buildInfoPath = path.join( buildInfoDir, @@ -119,9 +168,7 @@ export function loadArtifacts( ); if (existsSync(buildInfoPath)) { try { - artifactData.buildInfo = JSON.parse( - readFileSync(buildInfoPath, "utf-8"), - ); + buildInfo = JSON.parse(readFileSync(buildInfoPath, "utf-8")); } catch { // Ignore build info errors } @@ -134,10 +181,13 @@ export function loadArtifacts( projectRoot, sourceName: artifactData.sourceName, inputSourceName: artifact.inputSourceName as string | undefined, - buildInfo: artifactData.buildInfo, + buildInfo, }); } + // Keep only the build info fields the explorer reads + artifactData.buildInfo = toExplorerBuildInfo(buildInfo); + // Store by lowercase address addressMap[deployedAddress.toLowerCase()] = artifactData; } diff --git a/packages/plugin/src/deployment-tracker.ts b/packages/plugin/src/deployment-tracker.ts index 66ceeed..6c8f5f9 100644 --- a/packages/plugin/src/deployment-tracker.ts +++ b/packages/plugin/src/deployment-tracker.ts @@ -1,6 +1,10 @@ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs"; import path from "node:path"; -import type { ArtifactData, AddressMap } from "./artifacts.js"; +import { + toExplorerBuildInfo, + type ArtifactData, + type AddressMap, +} from "./artifacts.js"; import { resolveSourceCode } from "./source-resolver.js"; interface CompiledArtifact { @@ -96,6 +100,7 @@ export class DeploymentTracker { }; // Try to load build info (before source, which falls back to it) + let buildInfo: unknown; if (artifact.buildInfoId) { const buildInfoPath = path.join( this.projectRoot, @@ -105,7 +110,7 @@ export class DeploymentTracker { ); if (existsSync(buildInfoPath)) { try { - entry.buildInfo = JSON.parse(readFileSync(buildInfoPath, "utf-8")); + buildInfo = JSON.parse(readFileSync(buildInfoPath, "utf-8")); } catch { // ignore } @@ -118,10 +123,13 @@ export class DeploymentTracker { projectRoot: this.projectRoot, sourceName: artifact.sourceName, inputSourceName: artifact.inputSourceName, - buildInfo: entry.buildInfo, + buildInfo, }); } + // Keep only the build info fields the explorer reads + entry.buildInfo = toExplorerBuildInfo(buildInfo); + this.trackedDeployments[contractAddress.toLowerCase()] = entry; } From a5e8dc61064f0cbacdd001735b3ea4a8b3b7e1d0 Mon Sep 17 00:00:00 2001 From: Mati OS Date: Fri, 11 Sep 2026 14:15:45 -0300 Subject: [PATCH 2/2] fix(plugin): clear stale artifacts and flag oversized payloads A failed injection was invisible in the terminal and vague in the browser: the inline script reduced any error to a generic console warning, and the previous value stayed in localStorage. Hardhat derives deployment addresses from deployer and nonce, so after a redeploy a new contract could pick up an old contract's ABI and source. Remove the key before writing it, so a failed write leaves nothing behind, and put the error name in the browser warning so QuotaExceededError is recognisable at a glance. Warn in the terminal, once per process, when the serialised payload passes 2M characters - roughly where the tightest localStorage quotas give out. Refs openscan-explorer/explorer#402 --- packages/plugin/src/services/webapp.ts | 43 +++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/plugin/src/services/webapp.ts b/packages/plugin/src/services/webapp.ts index 12bc8ac..0df2364 100644 --- a/packages/plugin/src/services/webapp.ts +++ b/packages/plugin/src/services/webapp.ts @@ -25,6 +25,26 @@ const MIME_TYPES: Record = { ".eot": "application/vnd.ms-fontobject", }; +/** + * localStorage key the explorer reads injected artifacts from. + */ +const ARTIFACTS_STORAGE_KEY = "OPENSCAN_ARTIFACTS_JSON_V1"; + +/** + * Payload size, in characters, at which injection risks exceeding the + * browser's localStorage quota. Quotas run 5-10 MB per origin and a character + * can cost 2 bytes (Chromium stores any value holding a non-Latin-1 character + * as UTF-16), so the tightest quotas fit roughly 2.5M characters. + */ +const ARTIFACTS_WARN_CHARS = 2_000_000; + +/** + * Whether the oversized payload warning has been printed. Artifacts are + * re-injected on every HTML request, so an ungated warning would repeat on + * every page load. + */ +let hasWarnedPayloadSize = false; + /** * WebappService - Custom HTTP server for serving static files * No external dependencies - uses only Node.js built-in modules @@ -217,17 +237,30 @@ export class WebappService extends Service { return html; } - // Double-encode: first JSON.stringify produces the data string, - // second wraps it as a safe JS string literal with proper escaping. - const jsonString = JSON.stringify(JSON.stringify(artifacts)); + const payload = JSON.stringify(artifacts); + + if (!hasWarnedPayloadSize && payload.length > ARTIFACTS_WARN_CHARS) { + hasWarnedPayloadSize = true; + console.warn( + `[openscan] Injected artifacts are ${(payload.length / 1_000_000).toFixed(1)}M characters, near or over the browser's localStorage quota. Contracts may show as unverified.`, + ); + } + + // Double-encode: the serialized payload is wrapped again as a safe JS + // string literal with proper escaping. + const jsonString = JSON.stringify(payload); // Escape to prevent premature tag closure const safeJsonString = jsonString.replace(/<\/script>/gi, "<\\/script>"); + // Remove the key before writing it, so a failed write (e.g. over quota) + // leaves no artifacts from a previous deploy mapped onto reused addresses. + // Log the error name so QuotaExceededError is recognisable at a glance. const scriptTag = ``; return html.replace("", `${scriptTag}`);