diff --git a/.changeset/verify-without-cbor-metadata.md b/.changeset/verify-without-cbor-metadata.md new file mode 100644 index 00000000000..cdb5e98aedd --- /dev/null +++ b/.changeset/verify-without-cbor-metadata.md @@ -0,0 +1,5 @@ +--- +"@nomicfoundation/hardhat-verify": patch +--- + +Fixed verification of contracts compiled with Solidity's `metadata.appendCBOR` setting disabled. diff --git a/packages/hardhat-verify/src/internal/bytecode.ts b/packages/hardhat-verify/src/internal/bytecode.ts index a80be1ba434..19c9ac54921 100644 --- a/packages/hardhat-verify/src/internal/bytecode.ts +++ b/packages/hardhat-verify/src/internal/bytecode.ts @@ -6,16 +6,9 @@ import { assertHardhatInvariant, HardhatError, } from "@nomicfoundation/hardhat-errors"; -import { - getUnprefixedHexString, - hexStringToBytes, -} from "@nomicfoundation/hardhat-utils/hex"; +import { getUnprefixedHexString } from "@nomicfoundation/hardhat-utils/hex"; -import { - getMetadataSectionBytesLength, - inferSolcVersion, - METADATA_LENGTH_FIELD_SIZE, -} from "./metadata.js"; +import { parseBytecodeMetadata } from "./metadata.js"; interface ByteOffset { start: number; @@ -29,18 +22,6 @@ export class Bytecode { public readonly executableSection: string, ) {} - static async #parse(bytecode: string): Promise { - const bytecodeBytes = hexStringToBytes(bytecode); - - const solcVersion = await inferSolcVersion(bytecodeBytes); - const executableSection = bytecode.slice( - 0, - bytecode.length - getMetadataSectionBytesLength(bytecodeBytes) * 2, - ); - - return new Bytecode(bytecode, solcVersion, executableSection); - } - public static async getDeployedContractBytecode( provider: EthereumProvider, address: string, @@ -66,11 +47,9 @@ export class Bytecode { ); } - return await Bytecode.#parse(deployedBytecode); - } - - public hasVersionRange(): boolean { - return this.solcVersion.type !== "exact"; + const { solcVersion, executableSection } = + parseBytecodeMetadata(deployedBytecode); + return new Bytecode(deployedBytecode, solcVersion, executableSection); } /** @@ -91,9 +70,8 @@ export class Bytecode { * equivalent, `false` otherwise. */ public compare(compilerOutputBytecode: CompilerOutputBytecode): boolean { - const unlinkedExecutableSection = inferExecutableSection( - compilerOutputBytecode.object, - ); + const { executableSection: unlinkedExecutableSection } = + parseBytecodeMetadata(compilerOutputBytecode.object); // If the lengths differ, the bytecodes cannot match, so we can return early if (this.executableSection.length !== unlinkedExecutableSection.length) { @@ -114,47 +92,6 @@ export class Bytecode { } } -/** - * Extracts the executable portion of a contract's bytecode without - * decoding it. - * - * Solidity appends metadata to the end of the bytecode. This metadata - * includes a two-byte field indicating its total length. This function - * removes that metadata segment and returns only the executable code. - * - * This approach avoids decoding issues that can occur if the bytecode - * includes linker placeholders or non-hex characters. - * - * @param bytecode The full contract bytecode as a hex string - * (with or without `0x` prefix). - * @returns The hex string of the executable code, excluding metadata. - */ -export function inferExecutableSection(bytecode: string): string { - const rawBytecode = getUnprefixedHexString(bytecode); - - // Read the last 2 bytes (4 hex chars) that encode the length of - // the metadata section. - const metadataLengthBytes = hexStringToBytes( - rawBytecode.slice(-METADATA_LENGTH_FIELD_SIZE * 2), - ); - - // If the bytecode is too short to contain a metadata length field, - // return the entire bytecode. - if (metadataLengthBytes.length !== METADATA_LENGTH_FIELD_SIZE) { - return rawBytecode; - } - - const metadataSectionLength = - getMetadataSectionBytesLength(metadataLengthBytes); - - // invalid length (metadata + length field doesn't fit) - if (metadataSectionLength * 2 > rawBytecode.length) { - return rawBytecode; - } - - return rawBytecode.slice(0, rawBytecode.length - metadataSectionLength * 2); -} - /** * Replaces all known dynamic offset segments in the bytecode with zeros. * diff --git a/packages/hardhat-verify/src/internal/contract.ts b/packages/hardhat-verify/src/internal/contract.ts index 611c064e351..acf3b4b5b1e 100644 --- a/packages/hardhat-verify/src/internal/contract.ts +++ b/packages/hardhat-verify/src/internal/contract.ts @@ -117,9 +117,11 @@ export class ContractInformationResolver { const formattedSolcVersion = formatInferredSolcVersion( deployedBytecode.solcVersion, ); - const versionDetails = deployedBytecode.hasVersionRange() - ? `a Solidity version in the range ${formattedSolcVersion}` - : `the Solidity version ${formattedSolcVersion}`; + const versionDetails = { + exact: `the Solidity version ${formattedSolcVersion}`, + between: `a Solidity version in the range ${formattedSolcVersion}`, + unknown: "an unknown Solidity version", + }[deployedBytecode.solcVersion.type]; throw new HardhatError( HardhatError.ERRORS.HARDHAT_VERIFY.GENERAL diff --git a/packages/hardhat-verify/src/internal/metadata.ts b/packages/hardhat-verify/src/internal/metadata.ts index 27da7fe08ae..035d04ad80a 100644 --- a/packages/hardhat-verify/src/internal/metadata.ts +++ b/packages/hardhat-verify/src/internal/metadata.ts @@ -1,9 +1,10 @@ import type { SemverVersion } from "@nomicfoundation/hardhat-utils/fast-semver"; -import util from "node:util"; - -import { bytesToHexString } from "@nomicfoundation/hardhat-utils/bytes"; import { createDebug } from "@nomicfoundation/hardhat-utils/debug"; +import { + getUnprefixedHexString, + hexStringToBytes, +} from "@nomicfoundation/hardhat-utils/hex"; import { decode } from "cbor2"; const log = createDebug("hardhat:verify:metadata"); @@ -11,138 +12,114 @@ const log = createDebug("hardhat:verify:metadata"); export const METADATA_LENGTH_FIELD_SIZE = 2; /** - * The Solidity compiler version inferred from a contract's deployed bytecode. - * - * Bytecode metadata was introduced in Solidity v0.4.7, and the explicit - * version field was added in v0.5.9. Below those thresholds we can only - * narrow the version down to a range. + * The Solidity compiler version inferred from a contract's bytecode metadata. + * Solidity introduced metadata in v0.4.7 and the version field in v0.5.9. + * Bytecode without metadata may also come from a modern compiler with CBOR + * metadata disabled. */ export type InferredSolcVersion = | { type: "exact"; version: SemverVersion } - | { type: "lessThan"; bound: SemverVersion } - | { type: "between"; min: SemverVersion; max: SemverVersion }; - -const MISSING_METADATA: InferredSolcVersion = { - type: "lessThan", - bound: [0, 4, 7], -}; + | { type: "between"; min: SemverVersion; max: SemverVersion } + | { type: "unknown" }; -const SOLC_NOT_FOUND_IN_METADATA: InferredSolcVersion = { - type: "between", - min: [0, 4, 7], - max: [0, 5, 8], -}; +interface ParsedBytecode { + executableSection: string; + solcVersion: InferredSolcVersion; +} /** - * Attempts to infer the Solidity compiler version from the bytecode metadata. - * - * - Metadata was introduced in Solidity v0.4.7. - * See: https://docs.soliditylang.org/en/v0.4.7/miscellaneous.html#contract-metadata - * - The version field was first added in v0.5.9. - * See https://docs.soliditylang.org/en/v0.5.9/metadata.html#encoding-of-the-metadata-hash-in-the-bytecode - * - * @param bytecode The deployed bytecode as a Uint8Array. - * @returns The inferred solc version, either as an exact `x.y.z` or, when - * the metadata is missing or incomplete, as a fallback range. + * Strips a recognized Solidity CBOR section from the end of the bytecode. + * Only the suffix is decoded, so the executable code may contain unlinked + * library placeholders. */ -export async function inferSolcVersion( - bytecode: Uint8Array, -): Promise { - let solcMetadata: unknown; +export function parseBytecodeMetadata(bytecode: string): ParsedBytecode { + const rawBytecode = getUnprefixedHexString(bytecode); + + return ( + decodeSolcMetadata(rawBytecode) ?? { + executableSection: rawBytecode, + solcVersion: { type: "unknown" }, + } + ); +} + +function decodeSolcMetadata(bytecode: string): ParsedBytecode | undefined { + // The final two bytes give the size of the CBOR payload in bytes. + const lengthFieldStart = bytecode.length - METADATA_LENGTH_FIELD_SIZE * 2; + const lengthField = bytecode.slice(lengthFieldStart); + if (!/^[0-9a-f]{4}$/i.test(lengthField)) { + return undefined; + } + + const metadataStart = lengthFieldStart - Number.parseInt(lengthField, 16) * 2; + if (metadataStart < 0 || metadataStart === lengthFieldStart) { + return undefined; + } + + let metadata: unknown; try { - solcMetadata = await decodeSolcMetadata(bytecode); + const payload = hexStringToBytes( + bytecode.slice(metadataStart, lengthFieldStart), + ); + metadata = decode(payload, { preferMap: true }); } catch { - // Decoding failed, likely an older compiler or non-Solidity bytecode log("Failed to decode metadata."); - return MISSING_METADATA; + return undefined; } - if (solcMetadata instanceof Uint8Array) { - if (solcMetadata.length === 3) { - const [major, minor, patch] = solcMetadata; - log( - `Detected Solidity version from metadata: ${major}.${minor}.${patch}`, - ); - return { type: "exact", version: [major, minor, patch] }; - } - // Unexpected length. Log raw metadata for inspection - log( - `Unexpected metadata version format: [${[...solcMetadata].join(", ")}]`, - ); + if (!(metadata instanceof Map)) { + return undefined; + } + + const solcVersion = inferSolcVersion(metadata); + if (solcVersion === undefined) { + return undefined; } - // Metadata was decoded but contained no version field - log("Metadata decoded but Solidity version not found."); - return SOLC_NOT_FOUND_IN_METADATA; + return { + executableSection: bytecode.slice(0, metadataStart), + solcVersion, + }; +} + +function inferSolcVersion( + metadata: Map, +): InferredSolcVersion | undefined { + const solc = metadata.get("solc"); + if (solc instanceof Uint8Array && solc.length === 3) { + const [major, minor, patch] = solc; + return { type: "exact", version: [major, minor, patch] }; + } + + // Pre-release compilers store their full version as a string. + if (typeof solc === "string" && /^\d+\.\d+\.\d+-/.test(solc)) { + return { type: "unknown" }; + } + + // Solidity 0.4.7 through 0.5.8 stored a Swarm hash without a version. + const bzzr0 = metadata.get("bzzr0"); + if ( + solc === undefined && + bzzr0 instanceof Uint8Array && + bzzr0.length === 32 + ) { + return { type: "between", min: [0, 4, 7], max: [0, 5, 8] }; + } + + return undefined; } -/** - * Formats an `InferredSolcVersion` as a human-readable string suitable for - * inclusion in error messages: an exact `x.y.z`, a ` { - const metadataSectionBytesLength = getMetadataSectionBytesLength(bytecode); - const metadataPayload = bytecode.slice( - -metadataSectionBytesLength, - -METADATA_LENGTH_FIELD_SIZE, - ); - - log(`Read metadata section length: ${metadataSectionBytesLength} bytes`); - - const lastBytes = metadataPayload.slice(-100); - log( - `Last ${lastBytes.length} bytes of metadata: ${bytesToHexString(lastBytes)}`, - ); - - const decodedMetadata = decode(metadataPayload); - log(`Metadata decoded: ${util.inspect(decodedMetadata)}`); - - // @ts-expect-error: the metadata is known to contain a `solc` field - return decodedMetadata.solc; -} diff --git a/packages/hardhat-verify/src/internal/solc-versions.ts b/packages/hardhat-verify/src/internal/solc-versions.ts index bbd34672ad0..5498e94a6c0 100644 --- a/packages/hardhat-verify/src/internal/solc-versions.ts +++ b/packages/hardhat-verify/src/internal/solc-versions.ts @@ -81,13 +81,13 @@ export function filterVersionsByInferred( switch (inferred.type) { case "exact": return equals(parsed, inferred.version); - case "lessThan": - return lowerThan(parsed, inferred.bound); case "between": return ( greaterThanOrEqual(parsed, inferred.min) && lowerThanOrEqual(parsed, inferred.max) ); + case "unknown": + return true; } }); } diff --git a/packages/hardhat-verify/test/bytecode.ts b/packages/hardhat-verify/test/bytecode.ts index 139aad50141..ead24ea94d5 100644 --- a/packages/hardhat-verify/test/bytecode.ts +++ b/packages/hardhat-verify/test/bytecode.ts @@ -19,7 +19,6 @@ import { createHardhatRuntimeEnvironment } from "hardhat/hre"; import { Bytecode, - inferExecutableSection, nullifyBytecodeOffsets, getLibraryOffsets, getImmutableOffsets, @@ -80,86 +79,29 @@ describe("bytecode", () => { }); }); - describe("hasVersionRange", () => { - it("should return true when no metadata can be decoded (lessThan fallback)", async () => { - const executableSection = "deadbeefcafebabe"; // dummy bytecode - - const bytecode = buildBytecode(executableSection, ""); - + describe("compare", () => { + it("should compare the full bytecode when a CBOR suffix isn't Solidity metadata", async () => { + const bytecode = "6000a00001"; const provider = new MockEthereumProvider({ eth_getCode: getPrefixedHexString(bytecode), }); - const deployedBytecode = await Bytecode.getDeployedContractBytecode( provider, address, networkName, ); - assert.deepEqual(deployedBytecode.solcVersion, { - type: "lessThan", - bound: [0, 4, 7], - }); - assert.equal(deployedBytecode.hasVersionRange(), true); - }); - - it("should return true when metadata lacks a version field (between fallback)", async () => { - const metadata = {}; - const encodedMetadata = encode(metadata); - const metadataSection = getUnprefixedHexString( - bytesToHexString(encodedMetadata), + assert.equal(deployedBytecode.executableSection, bytecode); + assert.equal( + deployedBytecode.compare(buildCompilerOutputBytecode(bytecode)), + true, ); - const executableSection = "deadbeefcafebabe"; // dummy bytecode - - const bytecode = buildBytecode(executableSection, metadataSection); - - const provider = new MockEthereumProvider({ - eth_getCode: getPrefixedHexString(bytecode), - }); - - const deployedBytecode = await Bytecode.getDeployedContractBytecode( - provider, - address, - networkName, + assert.equal( + deployedBytecode.compare(buildCompilerOutputBytecode("6000800001")), + false, ); - - assert.deepEqual(deployedBytecode.solcVersion, { - type: "between", - min: [0, 4, 7], - max: [0, 5, 8], - }); - assert.equal(deployedBytecode.hasVersionRange(), true); }); - it("should return false when the solc version is exact", async () => { - const metadata = { solc: new Uint8Array([0, 6, 0]) }; // [major, minor, patch] - const encodedMetadata = encode(metadata); - const metadataSection = getUnprefixedHexString( - bytesToHexString(encodedMetadata), - ); - const executableSection = "deadbeefcafebabe"; // dummy bytecode - - const bytecode = buildBytecode(executableSection, metadataSection); - - const provider = new MockEthereumProvider({ - eth_getCode: getPrefixedHexString(bytecode), - }); - - const deployedBytecode = await Bytecode.getDeployedContractBytecode( - provider, - address, - networkName, - ); - - assert.deepEqual(deployedBytecode.solcVersion, { - type: "exact", - version: [0, 6, 0], - }); - assert.equal(deployedBytecode.hasVersionRange(), false); - }); - }); - - describe("compare", () => { it("should return true when executable sections match exactly and metadata is the same", async () => { const metadata = { solc: new Uint8Array([0, 8, 30]) }; // [major, minor, patch] const encodedMetadata = encode(metadata); @@ -205,8 +147,11 @@ describe("bytecode", () => { networkName, ); + const differentMetadata = getUnprefixedHexString( + bytesToHexString(encode({ ...metadata, experimental: true })), + ); const output = buildCompilerOutputBytecode( - buildBytecode(executableSection, ""), + buildBytecode(executableSection, differentMetadata), ); assert.equal(deployedBytecode.compare(output), true); @@ -232,9 +177,9 @@ describe("bytecode", () => { networkName, ); - // compiler output with one extra hex char in executable section + // Compiler output with one extra byte in the executable section. const output = buildCompilerOutputBytecode( - buildBytecode(executableSection + "ae", ""), + buildBytecode(executableSection + "ae", metadataSection), ); assert.equal(deployedBytecode.compare(output), false); @@ -309,45 +254,6 @@ describe("bytecode", () => { }); }); - describe("inferExecutableSection", () => { - it("should strip the metadata section from the bytecode", () => { - const executableSection = "deadbeef"; - const metadataSection = "cafebabe"; - const bytecode = buildBytecode(executableSection, metadataSection); - - assert.equal(inferExecutableSection(bytecode), executableSection); - }); - - it("should handle bytecode prefixed with 0x", () => { - const executableSection = "cafed00d"; - const metadataSection = "112233"; - const bytecode = getPrefixedHexString( - buildBytecode(executableSection, metadataSection), - ); - - assert.equal(inferExecutableSection(bytecode), executableSection); - }); - - it("should return the entire bytecode if it's less than the metadata length field size", () => { - const bytecode = "0xab"; // less than METADATA_LENGTH_FIELD_SIZE * 2 chars - - assert.equal( - inferExecutableSection(bytecode), - getUnprefixedHexString(bytecode), - ); - }); - - it("should return the entire bytecode if the metadata length field size is invalid", () => { - const fakeLength = "ffff"; // huge length - const bytecode = "0123" + fakeLength; - - assert.equal( - inferExecutableSection(bytecode), - getUnprefixedHexString(bytecode), - ); - }); - }); - describe("nullifyBytecodeOffsets", () => { useEphemeralFixtureProject("default"); diff --git a/packages/hardhat-verify/test/metadata.ts b/packages/hardhat-verify/test/metadata.ts index d46d4e94f34..a6cf0477654 100644 --- a/packages/hardhat-verify/test/metadata.ts +++ b/packages/hardhat-verify/test/metadata.ts @@ -1,70 +1,69 @@ import assert from "node:assert/strict"; import { describe, it } from "node:test"; -import { - hexStringToBytes, - utf8StringToBytes, -} from "@nomicfoundation/hardhat-utils/bytes"; - import { formatInferredSolcVersion, - getMetadataSectionBytesLength, - inferSolcVersion, + parseBytecodeMetadata, } from "../src/internal/metadata.js"; describe("metadata", () => { - describe("inferSolcVersion", () => { - it("should return an exact compiler version", async () => { + describe("parseBytecodeMetadata", () => { + it("should return an exact compiler version", () => { // Bytecode generated with Solidity 0.5.9 - const solc059Bytecode = hexStringToBytes( - "0x6080604052348015600f57600080fd5b50603e80601d6000396000f3fe6080604052600080fdfea265627a7a723058201d7a6a3b37a66e79d9dd0abb90d493ba6ba82af0d76bf7bcbd53aa63fad4316664736f6c63430005090032", - ); + const solc059Bytecode = + "0x6080604052348015600f57600080fd5b50603e80601d6000396000f3fe6080604052600080fdfea265627a7a723058201d7a6a3b37a66e79d9dd0abb90d493ba6ba82af0d76bf7bcbd53aa63fad4316664736f6c63430005090032"; // Bytecode generated with Solidity 0.8.19 - const solc0819Bytecode = hexStringToBytes( - "0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea26469706673582212205a428cebe312fc68fec73aec069d4922dd88c8404183b33f93424ee0ee18423b64736f6c63430008130033", - ); + const solc0819Bytecode = + "0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea26469706673582212205a428cebe312fc68fec73aec069d4922dd88c8404183b33f93424ee0ee18423b64736f6c63430008130033"; - assert.deepEqual(await inferSolcVersion(solc059Bytecode), { + assert.deepEqual(parseBytecodeMetadata(solc059Bytecode).solcVersion, { type: "exact", version: [0, 5, 9], }); - assert.deepEqual(await inferSolcVersion(solc0819Bytecode), { + assert.deepEqual(parseBytecodeMetadata(solc0819Bytecode).solcVersion, { type: "exact", version: [0, 8, 19], }); }); - it("should return the lessThan-0.4.7 fallback when metadata can't be decoded", async () => { - const emptyBytecode = new Uint8Array([0, 0]); - const noBytecode = utf8StringToBytes("This is no contract bytecode."); - // Bytecode generated with Solidity 0.4.6 - const noMetadataBytecode = hexStringToBytes( - "0x6060604052346000575b60098060156000396000f360606040525b600056", - ); - - const expected = { type: "lessThan", bound: [0, 4, 7] }; - assert.deepEqual(await inferSolcVersion(emptyBytecode), expected); - assert.deepEqual(await inferSolcVersion(noBytecode), expected); - assert.deepEqual(await inferSolcVersion(noMetadataBytecode), expected); + it("should keep the entire bytecode when it has no Solidity metadata", () => { + for (const bytecode of [ + "", + "ab", // Too short to contain a length field. + "0123ffff", // The claimed metadata doesn't fit. + "6000fd0001", // The claimed payload isn't valid CBOR. + "6000a00001", // The claimed payload is valid CBOR, but isn't Solidity metadata. + // Bytecode generated with Solidity 0.4.6. + "6060604052346000575b60098060156000396000f360606040525b600056", + ]) { + assert.deepEqual(parseBytecodeMetadata(`0x${bytecode}`), { + executableSection: bytecode, + solcVersion: { type: "unknown" }, + }); + } }); - it("should return the 0.4.7-to-0.5.8 fallback when metadata has no version field", async () => { + it("should return the 0.4.7-to-0.5.8 fallback when metadata has no version field", () => { // Bytecode generated with Solidity 0.4.7 - const solc047Bytecode = hexStringToBytes( - "0x6060604052346000575b60358060166000396000f30060606040525b60005600a165627a7a7230582074ab9e158d4cb2b2c83bd9efe2ee6bd219f3e2cda7d80ddfe56a1e844f81a2950029", - ); + const solc047Bytecode = + "0x6060604052346000575b60358060166000396000f30060606040525b60005600a165627a7a7230582074ab9e158d4cb2b2c83bd9efe2ee6bd219f3e2cda7d80ddfe56a1e844f81a2950029"; // Bytecode generated with Solidity 0.5.8 - const solc058Bytecode = hexStringToBytes( - "0x6080604052348015600f57600080fd5b50603580601d6000396000f3fe6080604052600080fdfea165627a7a723058209251767f58375bfe02f871bf86ffdae7ab9f6503c50146effb0e7bf96e5a0db90029", - ); + const solc058Bytecode = + "0x6080604052348015600f57600080fd5b50603580601d6000396000f3fe6080604052600080fdfea165627a7a723058209251767f58375bfe02f871bf86ffdae7ab9f6503c50146effb0e7bf96e5a0db90029"; const expected = { type: "between", min: [0, 4, 7], max: [0, 5, 8], }; - assert.deepEqual(await inferSolcVersion(solc047Bytecode), expected); - assert.deepEqual(await inferSolcVersion(solc058Bytecode), expected); + assert.deepEqual( + parseBytecodeMetadata(solc047Bytecode).solcVersion, + expected, + ); + assert.deepEqual( + parseBytecodeMetadata(solc058Bytecode).solcVersion, + expected, + ); }); }); @@ -76,11 +75,8 @@ describe("metadata", () => { ); }); - it("should format a lessThan bound as { - assert.equal( - formatInferredSolcVersion({ type: "lessThan", bound: [0, 4, 7] }), - "<0.4.7", - ); + it("should format an unknown version", () => { + assert.equal(formatInferredSolcVersion({ type: "unknown" }), "unknown"); }); it("should format a between range as 'x.y.z - x.y.z'", () => { @@ -94,14 +90,4 @@ describe("metadata", () => { ); }); }); - - describe("getMetadataSectionBytesLength", () => { - it("should return the length of the metadata section", () => { - const solc0819Bytecode = hexStringToBytes( - "0x6080604052348015600f57600080fd5b50603f80601d6000396000f3fe6080604052600080fdfea26469706673582212205a428cebe312fc68fec73aec069d4922dd88c8404183b33f93424ee0ee18423b64736f6c63430008130033", - ); - - assert.equal(getMetadataSectionBytesLength(solc0819Bytecode), 53); - }); - }); }); diff --git a/packages/hardhat-verify/test/solc-versions.ts b/packages/hardhat-verify/test/solc-versions.ts index 672fc7eb02e..b8905f232b9 100644 --- a/packages/hardhat-verify/test/solc-versions.ts +++ b/packages/hardhat-verify/test/solc-versions.ts @@ -235,24 +235,11 @@ describe("solc-versions", () => { }); }); - describe("lessThan", () => { - it("should keep versions strictly below the bound", () => { + describe("unknown", () => { + it("should keep all configured versions", () => { assert.deepEqual( - filterVersionsByInferred(versions, { - type: "lessThan", - bound: [0, 4, 11], - }), - ["0.4.10"], - ); - }); - - it("should exclude the bound itself", () => { - assert.deepEqual( - filterVersionsByInferred(["0.4.7", "0.4.6"], { - type: "lessThan", - bound: [0, 4, 7], - }), - ["0.4.6"], + filterVersionsByInferred(versions, { type: "unknown" }), + versions, ); }); }); @@ -304,10 +291,9 @@ describe("solc-versions", () => { ); assert.deepEqual( filterVersionsByInferred(mixed, { - type: "lessThan", - bound: [0, 5, 0], + type: "unknown", }), - ["0.4.11"], + ["0.5.9", "0.4.11"], ); assert.deepEqual( filterVersionsByInferred(mixed, { diff --git a/packages/hardhat-verify/test/verification.ts b/packages/hardhat-verify/test/verification.ts index 88075870a49..7527edc6fb9 100644 --- a/packages/hardhat-verify/test/verification.ts +++ b/packages/hardhat-verify/test/verification.ts @@ -64,6 +64,55 @@ describe("verification", () => { assert.ok(result, "Verification should return true"); }); + it("should verify a contract compiled without CBOR metadata", async () => { + const localHre = await createHardhatRuntimeEnvironment({ + ...hardhatUserConfig, + paths: { + artifacts: "artifacts-no-metadata", + cache: "cache-no-metadata", + }, + solidity: { + profiles: { + default: { version: "0.8.33" }, + production: { + version: "0.8.33", + settings: { metadata: { appendCBOR: false } }, + }, + }, + }, + }); + await localHre.tasks.getTask("build").run({ + defaultBuildProfile: "production", + }); + const { provider } = await localHre.network.create(); + const address = await deployContract( + "Counter", + [], + {}, + localHre, + provider, + ); + + const result = await verifyContract( + { address }, + localHre, + () => {}, + testDispatcher.dispatcher, + provider, + ); + + assert.ok(result, "Verification should return true"); + const sourceCode = etherscanMock.verifyRequestBody?.get("sourceCode"); + assert.ok( + typeof sourceCode === "string", + "Verification should submit the Solidity compiler input", + ); + assert.equal( + JSON.parse(sourceCode).settings.metadata.appendCBOR, + false, + ); + }); + it("should verify a contract with constructor arguments", async () => { const constructorArgs = [0, true]; const { provider } = await hre.network.create();