[Exchange] Polygon Mumbai integration with on-chain artifact provenance hashes

← All Specs

Goal

scidex/exchange/wallet_manager.py already issues per-agent EVM
keypairs but every chain interaction so far has been a no-op — wallet_chain defaults to "ethereum" and nothing is ever broadcast.
Wire wallets to Polygon Mumbai testnet (now Amoy after the 2024
deprecation — pick whichever has live RPC) so SciDEX can write content
hashes for top-ranked artifacts on-chain. This gives every elevated
artifact an immutable provenance receipt that anyone outside SciDEX can
verify without trusting our DB.

Acceptance Criteria

scidex/exchange/chain_client.py exposing publish_provenance(artifact_id, content_sha256, agent_id) -> tx_hash and verify_provenance(artifact_id) -> dict.
☐ Uses web3.py against Polygon Amoy RPC (configurable via POLYGON_RPC_URL).
☐ Smart contract contracts/ArtifactProvenance.sol with two methods: publish(bytes32 artifactId, bytes32 contentHash) (only signer wallet) and getProvenance(bytes32) -> (address, bytes32, uint256).
☐ Migration adds artifact_provenance_chain(artifact_id PK, tx_hash, block_number, content_sha256, published_at).
☐ Auto-trigger on artifact promotion to T0/T1 epistemic tier (hook into scidex/atlas/artifact_quality_gates.py) — only top-ranked artifacts pay gas.
☐ Gas budget: hard cap 0.5 MATIC/day per agent_registry row; hit-cap → queue, don't fail.
/artifact/{id} adds a "On-chain receipt" row with tx link to PolygonScan.
GET /api/artifacts/{id}/provenance/verify re-reads the chain and returns {matches: bool, on_chain_hash, db_hash}.
☐ Test: deploy contract to local hardhat; publish a hash; verify reads same hash; tampered DB content makes verify return matches: false.

Approach

  • Read wallet_manager.create_agent_wallet (line 140) — already produces an EVM signer; reuse for tx signing.
  • Add web3==6.x to requirements; pin RPC URL in env, never code.
  • Compute content_sha256 once at promotion time using the canonical artifact bytes (use scidex.atlas.artifact_commit.commit_artifact to get the committed blob).
  • Tx confirmation: poll for 2 blocks; mark pending/confirmed/failed in artifact_provenance_chain.status.
  • Use eth_account.Account.sign_transaction — never expose decrypted keys outside wallet_manager.decrypt_key.
  • Dependencies

    • 8a6c314c699a (chain selection — already done, picked EVM).
    • 2147d16ef177 (per-agent wallets).

    Work Log

    Tasks using this spec (1)
    [Exchange] Polygon Mumbai integration with on-chain artifact
    File: q-cw-polygon-testnet-provenance_spec.md
    Modified: 2026-04-27 03:19
    Size: 2.5 KB