> For the complete documentation index, see [llms.txt](https://quantumwing.gitbook.io/quantumwing/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumwing.gitbook.io/quantumwing/smart-contracts/verification.md).

# Contract Verification

Source-code verification for QWVM and EVM contracts

Verification associates a contract's deployed bytecode with the source code that produced it. Once verified, the explorer renders the source inline and exposes a function-level UI for read/write calls.

## Backend

The beacon API (`blockchain/api/contract_verification.go`) registers:

```
POST /api/v1/contract/verify              # submit source for review
GET  /api/v1/contract/{address}/verification   # verification record
GET  /api/v1/contract/{address}/source         # source code only
```

Submission body:

```json
{
  "address":          "0xfbe6fdad648b861ad8fed358a3985c3fbba58de5",
  "source_code":      "<full source>",
  "compiler_version": "solc 0.8.24+commit.e11b9ed9",
  "optimization":     "yes,200",
  "constructor_args": "0x...",
  "bytecode_hash":    "<hex>",
  "submitted_by":     "0x..."
}
```

Acceptance criteria:

```
1. Address resolves to a deployed contract.
2. submitted bytecode_hash == on-chain contract.BytecodeHash
3. source_code, compiler_version, bytecode_hash all non-empty.
```

A successful submission stores a `VerifiedContract` record, which the explorer subsequently surfaces. The API does NOT compile source itself; it trusts the submitter's declared `bytecode_hash` and rejects on mismatch.

## Frontend

The explorer flow lives at `explorer/app/contracts/[address]/verify/page.tsx` (commit `809d7dd95`).

Page layout:

```
1. Address banner            ← GET /api/v1/contract/{address}
2. Form
   - Source code (multiline, monospace)
   - Compiler version (dropdown — solc / qwvm-rust / qwvm-asc)
   - Optimization settings
   - Constructor args (hex)
3. Submit button → POST /api/v1/contract/verify
4. Result panel (success/failure + bytecode hash diff)
```

After submission, the contract detail page renders:

```
∎ Verified ✓                 ← top-right badge
  Source code (collapsible)
  ABI (decoded)
  Read functions (UI: input fields + "Query")
  Write functions (UI: input fields + "Send")
```

## Bytecode-hash derivation

```
QWVM (WASM):   bytecode_hash = SHA3-256(wasm_bytes)
EVM (Solidity): bytecode_hash = SHA3-256(deployedBytecode)
```

The `deployedBytecode` (not `creationBytecode`) is the runtime image loaded by the VM. Tools that hash creation bytecode will fail verification — match the on-chain blob exactly.

## Reproducible builds

For Solidity:

```bash
solc --optimize --optimize-runs 200 \
     --combined-json bin-runtime,abi MyContract.sol > out.json
jq -r '.contracts[].bin-runtime' out.json | xxd -r -p | sha3sum -a 256
```

For QWVM (Rust → WASM):

```bash
cargo build --release --target wasm32-unknown-unknown
sha3sum -a 256 target/wasm32-unknown-unknown/release/my_contract.wasm
```

## Verifying a contract end-to-end

```bash
# 1. Deploy
./build/quantum-wing-blockchain contract deploy \
  --wasm my.wasm --from 0xe20E... --gas 1000000
# Output:  Contract Address: 0xfbe6f...

# 2. Compute bytecode hash locally
HASH=$(sha3sum -a 256 my.wasm | awk '{print $1}')

# 3. Submit verification
curl -X POST http://localhost:8080/api/v1/contract/verify \
  -H 'Content-Type: application/json' \
  -d "{
        \"address\":          \"0xfbe6f...\",
        \"source_code\":      \"$(cat my.rs | jq -Rs .)\",
        \"compiler_version\": \"rustc 1.75.0 (wasm32-unknown-unknown)\",
        \"optimization\":     \"release\",
        \"bytecode_hash\":    \"$HASH\",
        \"submitted_by\":     \"0xe20E...\"
      }"

# 4. Render in explorer
open https://explorer.quantumwing.io/contracts/0xfbe6f...
```

## Limitations

* No automated re-compilation (yet). Operators trust the submitter's declared `bytecode_hash`; mismatches are rejected at submission, but a malicious submitter could publish source that does NOT correspond to the bytecode if their hash happens to match. Mitigation: explorer badges "Verified by" and surfaces the submitter address.
* No multi-source / library linking UI.
* No ABI auto-derivation — the explorer parses ABI from the source block on render.

## Code paths

* `blockchain/api/contract_verification.go` — REST handlers + storage
* `explorer/app/contracts/[address]/verify/page.tsx` — submit form
* `explorer/app/contracts/[address]/page.tsx` — verified-source render
* Commit `809d7dd95` — initial verification flow

## Related

* [qwvm.md](/quantumwing/smart-contracts/qwvm.md) — QWVM contract bytecode layout
* [wasm-gas-metering.md](/quantumwing/smart-contracts/wasm-gas-metering.md) — WASM execution
* [../EVM\_INTEGRATION\_ROADMAP.md](https://github.com/dolfrin/QuantumWing/blob/master/docs/EVM_INTEGRATION_ROADMAP.md) — Solidity status


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://quantumwing.gitbook.io/quantumwing/smart-contracts/verification.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `build a script that syncs our docs to a CMS` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
