Skip to main content
This page uses the witness-derived keypair pattern from Access Control. The caller authenticates by proving knowledge of a witness secret whose hash matches the stored UserPublicKey. ownPublicKey() is not used for authentication.

The Contract

How It Works

Authentication

The caller proves knowledge of a witness secret whose hash equals their UserPublicKey. The contract then writes an allowance entry at _allowances[owner][spender] = amount.

Nested Map structure

A two-level map: the outer key is the owner’s authenticated public key; the inner map records how much each spender may spend. Reading the nested map must be done inline — you cannot bind the inner map to a const:

Initializing the inner Map

default<Map<K, V>> produces an empty Map, used to initialize the slot for a new owner before the first allowance is recorded.

Privacy Note

Every approve call reveals the owner, the spender, and the amount on the public ledger. This is appropriate for transparent tokens (game items, public registries) but unsuitable for private finance. Midnight’s native shielded primitives provide UTXO-based privacy that hides participants and amounts.

What’s Next

Allowance & Spending

Learn how to spend approved tokens

ERC20 Token

Complete token with approvals