downloadget_viewing_key

Fetch your viewing key from the Directory contract

Fetch your viewing key from the Directory genesis contract using signed read authentication.

Overview

get_viewing_key() and async_get_viewing_key() retrieve your registered viewing key from the Directory contract at 0x1000...0004. The function uses a signed read to authenticate msg.sender, ensuring only the key owner can retrieve their key.

This function is typically used internally by watch_src20_events(), but can also be called directly when you need access to your viewing key.

Signature

def get_viewing_key(
    w3: Web3,
    encryption: EncryptionState,
    private_key: PrivateKey,
) -> Bytes32

async def async_get_viewing_key(
    w3: AsyncWeb3,
    encryption: EncryptionState,
    private_key: PrivateKey,
) -> Bytes32

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 authentication

Returns

Type
Description

32-byte AES-256 viewing key

Raises

  • ValueError - If no viewing key is registered for the caller's address

Examples

Basic Usage (Sync)

With Error Handling

Use with Event Watcher

Async Usage

Cache the Key

Compare with Stored Key

Export for Sharing

How It Works

  1. Encode calldata - Encodes getKey() with no arguments

  2. Signed read - Calls signed_call() to the Directory contract at 0x1000...0004

  3. Authentication - The contract validates msg.sender matches the key owner

  4. Response - Returns the 32-byte viewing key

  5. Validation - Checks that the key is not zero (indicates no key registered)

Notes

  • Uses signed read for authentication (ensures only the owner can retrieve the key)

  • Makes one RPC call to fetch the key

  • The key is returned as a Bytes32 instance

  • Raises ValueError if no key is registered

  • The function validates that the returned key is non-zero

  • The key is stored encrypted on-chain and decrypted in the TEE

Warnings

  • Key security - The viewing key allows decrypting all SRC20 events; keep it secure

  • No key registered - Raises ValueError if you haven't registered a key yet

  • Authentication required - Requires private key and encryption state

  • RPC call - Makes a network call; cache the result if using frequently

See Also

Last updated