keyregister_viewing_key

Register a viewing key in the Directory contract

Register a viewing key in the Directory genesis contract for SRC20 event decryption.

Overview

register_viewing_key() and async_register_viewing_key() register a 32-byte AES-256 viewing key in the Directory contract at 0x1000...0004. This key is used to decrypt SRC20 Transfer and Approval events.

The function uses a shielded write (setKey(suint256)) to securely store the key on-chain. Once registered, the key can be retrieved via get_viewing_key() or used directly with event watchers.

Signature

def register_viewing_key(
    w3: Web3,
    encryption: EncryptionState,
    private_key: PrivateKey,
    key: Bytes32,
) -> HexBytes

async def async_register_viewing_key(
    w3: AsyncWeb3,
    encryption: EncryptionState,
    private_key: PrivateKey,
    key: Bytes32,
) -> HexBytes

Parameters

Parameter
Type
Required
Description

w3

Web3 or AsyncWeb3

Yes

Web3 instance with Seismic support

encryption

Yes

Encryption state from wallet client

private_key

Yes

32-byte signing key for the transaction

key

Yes

32-byte AES-256 viewing key to register

Returns

Type
Description

HexBytes

Transaction hash

Examples

Basic Usage (Sync)

With Fixed Key

Store Key Securely

Async Usage

Check Before Registering

Derive from Seed

How It Works

  1. Encode calldata - Encodes setKey(suint256) with the viewing key as an unsigned 256-bit integer

  2. Shielded write - Calls send_shielded_transaction() to the Directory contract at 0x1000...0004

  3. Transaction - The transaction is signed, encrypted, and sent to the node

  4. Storage - The key is stored on-chain and mapped to the sender's address

Gas Cost

  • Base cost: ~21,000 gas (standard transaction)

  • Storage cost: ~20,000 gas (first-time registration)

  • Total: ~41,000 gas for first registration

  • Update cost is similar to initial registration

Notes

  • The key is stored per address (sender's address)

  • Registering a new key overwrites the previous one

  • The key is stored encrypted on-chain

  • Only the key owner can retrieve it via signed read

  • Anyone can check if an address has a key via check_has_key()

  • The key hash can be queried publicly via get_key_hash()

Warnings

  • Key security - Keep viewing keys secure; they allow decrypting all SRC20 events

  • No recovery - If you lose the viewing key, you cannot decrypt past events

  • Overwriting - Registering a new key overwrites the previous one permanently

  • Gas costs - Requires a transaction with gas fees

  • Confirmation - Wait for transaction confirmation before using the key

See Also

Last updated