bookDirectory

Manage viewing keys in the Directory genesis contract

Manage viewing keys stored in the Directory genesis contract at 0x1000...0004.

Overview

The Directory is a genesis contract that stores per-address AES-256 viewing keys used to decrypt SRC20 Transfer and Approval events. It provides:

  • Key registration - Store your viewing key on-chain via shielded write

  • Key retrieval - Fetch your key via authenticated signed read

  • Public queries - Check if addresses have registered keys (no authentication)

Quick Start

Register a Viewing Key

from seismic_web3 import create_wallet_client, PrivateKey, Bytes32
from seismic_web3.src20 import register_viewing_key
import os

private_key = PrivateKey.from_hex_str(os.environ["PRIVATE_KEY"])
w3 = create_wallet_client("https://gcp-1.seismictest.net/rpc", private_key=private_key)

viewing_key = Bytes32(os.urandom(32))
tx_hash = register_viewing_key(
    w3,
    encryption=w3.seismic.encryption,
    private_key=private_key,
    key=viewing_key,
)

w3.eth.wait_for_transaction_receipt(tx_hash)
print("Key registered")

Fetch Your Viewing Key

Check if Address Has Key

Functions

Key Management

Function
Description

Register a viewing key (shielded write)

Fetch your viewing key (signed read)

Public Queries

Function
Description

Check if address has a key (public)

Get keccak256 hash of address's key (public)

Pure Helper

Function
Description

compute_key_hash

Compute keccak256(viewing_key) locally

Directory Contract

  • Address: 0x1000000000000000000000000000000000000004

  • Type: Genesis contract (pre-deployed)

  • Methods:

    • setKey(suint256) - Register viewing key (shielded write)

    • getKey() - Fetch your key (signed read)

    • checkHasKey(address) - Check if address has key (public read)

    • keyHash(address) - Get key hash (public read)

Key Properties

Storage

  • Keys are stored per address (sender's address)

  • Keys are encrypted on-chain

  • Only the owner can retrieve their key via signed read

Key Hash

  • Event topics use keccak256(viewing_key) for filtering

  • The hash is publicly visible but does not expose the key

  • Used by event watchers to filter for relevant events

Security

  • Viewing keys allow decrypting all SRC20 events for that address

  • Keep viewing keys secure (similar to private keys)

  • Losing the viewing key means you cannot decrypt past events

  • Keys can be overwritten by registering a new one

Common Workflows

First-Time Setup

Key Rotation

Async Operations

All functions have async variants:

Gas Costs

Operation
Gas Cost (approx)

Register key (first time)

~41,000 gas

Register key (update)

~41,000 gas

Fetch key (signed read)

Free (off-chain)

Check has key

Free (off-chain)

Get key hash

Free (off-chain)

Notes

  • Viewing keys are separate from wallet private keys

  • Keys can be derived from seed phrases or generated randomly

  • Consider backing up viewing keys to secure storage

  • Share viewing keys cautiously (they allow decrypting all events)

See Also

Last updated