ModularAccount contract performs no signature verification itself. Instead, it stores a set of registered signer addresses and, when __check_auth is called, forwards the authorization context to whichever of those signers the user attached to the transaction. Each delegate runs its own __check_auth independently.
Auth delegation was introduced in soroban-sdk v27 via CAP-71. For a single-key account see the Simple Account example, and for a multi-sig account with spend-limit policies see the Complex Account example.
Implementing a contract account requires a very good understanding of authentication and authorization and requires rigorous testing and review. The example here is not a full-fledged account contract — use it as an API reference only.
Run the Example
- Finish the Setup checklist to install the Stellar CLI, Rust target, and required environment variables.
- Clone the
soroban-examplesrepository:
- Run the tests from the
modular_accountdirectory:
Code
modular_account/src/lib.rs
How it Works
Storage layout
ModularAccountDataKey::Signer(Address) uses one persistent storage entry per allowed signer. Using a per-key entry makes registration and revocation O(1) lookups rather than scanning a list. The () value signals presence.
Constructor
type Signature = ()
ModularAccount verifies no signature of its own — it relies entirely on its delegates. Setting Signature = () tells the Soroban host that no signature data needs to be passed to __check_auth. The user’s wallet still builds a full SorobanAddressCredentialsWithDelegates auth entry (see the test), but the account-level signature field is empty.
__check_auth: get → verify → forward
env.custom_account() drive delegation. Both may only be called from within __check_auth; calling either outside of it panics.
get_delegated_signers() -> Vec<Address>returns the delegate addresses the user attached to the transaction’s auth entry. These are unsanitized user input — the contract must verify each one is registered before forwarding.delegate_auth(&address)forwards the current__check_authauthorization context toaddress. Unlikerequire_auth, this does not start a new contract invocation and does not require a separate auth entry for the delegate in the transaction. Delegation is nestable: a delegate may further delegate.
Tests
Openmodular_account/src/test.rs. The test defines two helper contracts:
DelegateAccount— a simple custom account withtype Signature = ()that always approves, and stores the receivedauth_contextsin instance storage for later assertion.Protected— a contract with one function that callsaccount.require_auth().
ModularAccount with one DelegateAccount as its allowed signer, builds an AddressWithDelegates auth entry by hand, and calls Protected::protected:
env.auths()shows only the account authorizingprotected— delegating toDelegateAccountis not recorded as a separate top-level authorization.DelegateAccount’sApprovedContextsstorage contains the same contract context, confirming the delegation actually reached it.
test_unknown_delegate_is_rejected attaches a delegate the account never registered, and test_empty_delegates_is_rejected attaches none at all. In both, Protected::protected fails. The caller only sees a generic Error(Auth, InvalidAction), because the host escalates any failed __check_auth to that — the account’s own error code does not propagate to the return value. To assert the account rejected the call for its own reason, the tests read the host’s diagnostic events with env.host().get_diagnostic_events() and match the recorded Error(Contract, #n) — UnknownDelegate and InsufficientDelegates respectively.
Testing delegated auth via
env.try_invoke_contract_check_auth is not supported. Use env.set_auths with SorobanAddressCredentialsWithDelegates and a wrapper contract call instead, as shown in the test.Build the Contract
.wasm file will be output in the target directory:
Further Reading
- Simple Account example — the minimal single-key baseline.
- Complex Account example — multisig and spend-limit policies.
- CAP-71 specification — the protocol change that introduced auth delegation.
CustomAccountAPI docs — reference forget_delegated_signersanddelegate_auth.