Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Authorization and program-derived accounts

A handler receives a set of grants, input.grants, and one question derived from it, input.is_authorized(), which asks whether the set contains the receiver.

A grant is an Actor (an account id paired with a program account id) in a set carried by one delivery, so authority is per actor and per message, not per account or per transaction.

Where a grant comes from

Only the engine mints one (Executor::root_grants, authorize and queue_fee, lee/state_machine/core/src/execution_state.rs). There are four sources:

SourceGrantsReaches
A signer markeach declared actor marked Authority::Signer whose account signedthe transaction’s sync root call only
A private witness’s marksthe actors an authorized private witness lists in signer_programs (for a public root, only those the transaction declares in private_signers)the sync root call only
A PDA seedthe destination actor of the seeded messagethat one message
The fee entrythe payer’s native balance actorthe fee delivery only

A program cannot mint a grant, only pass on one it holds: by naming it on an outgoing sync message with with_grants, or by handing back the one it was lent with with_lent_grant. Attaching a grant the sender does not hold fails the transaction with UnheldGrant.

A grant reaches a callee only because the caller attached it, and each hop must hold what it attaches, so a chain is explicit, with no fixed hop limit: gas bounds the hops of the public part. grant_relay (test programs) is that rule as working code. An AsyncMessage never carries a grant, and neither does a claim, a stored message collected as the root, or a promoted async message (AsyncMessage).

In the wallet CLI an actor authorizes only when it is written :sign, as in wallet program call --program <id> --to <account>:sign ... (Sending a message), and the root program can pass on everything it is handed (rules).

Program-derived accounts

A PDA is an account with no key, its id derived from a program’s id and a seed:

AccountId::for_public_pda(&program_account_id, &seed)

Only that program can issue a grant over it, by attaching the seed to a SyncMessage:

const PDA_SEED: PdaSeed = PdaSeed::new([37; 32]);

fn handle_message(input: &ReceiveInput, callee: AccountId) -> Response {
    let greeting: Vec<u8> = b"Hello from tail call with Program Derived Account ID".to_vec();
    Response::keep_state().send(
        SyncMessage::new(Actor::new(input.receiver.account_id, callee), &greeting)
            .with_pda_seeds(vec![PDA_SEED]),
    )
}

Abbreviated from examples/program_deployment/methods/guest/src/bin/tail_call_with_pda.rs. The engine checks that the seed, hashed with the sending program’s id, gives the receiving account; if so, the receiver sees is_authorized == true.

A private PDA works the same way, at a different address: AccountId::for_private_pda uses its own prefix and also hashes the holder’s public keys, so the public and private PDA of one program and seed differ, and several groups can each have their own private PDA for the same program and seed.

Custody

Custody is the main use of a PDA. A program holding funds keeps them in the native balance actor of a PDA it controls, and releases them with a seed grant, as lee_core::native_token::custody_transfer does:

pub fn custody_transfer(from: AccountId, seed: PdaSeed, to: AccountId, amount: Balance) -> SyncMessage {
    SyncMessage::new(Actor::native_balance(from), &Message::Transfer { to, amount })
        .with_pda_seeds(vec![seed])
}

The bridge pays deposits this way (lez/programs/bridge/src/main.rs). No key can sign for those funds: they move only through the program’s code, and the sequencer committee can replace the bridge’s code, as it can that of any system program not made immutable.

When several users share one program, derive the seed from something that identifies the instance, or one user’s instance could grant the seed of another’s.

Knowing who sent a message

Authorization is about the receiving account, identity about the sender: separate checks.

  • input.from is the sending actor, or None when the transaction’s author submitted the message.
  • input.from_own_program() is true when the sender is an actor of the same program.

Use the first to accept messages only from a known actor, the second for a program’s internal messages:

Message::Credit(amount) => {
    if !input.from_own_program() {
        return Err(TransferError::ForeignCredit { account_id });
    }
    ...
}

A credit must come from the native token program itself, or anyone could mint.

An account id alone identifies nobody. Anyone can deliver a message to any program’s actor on any public account, without that account’s authorization. A program that forwards whatever it receives would then send messages “from” that account. So to know that a specific person is speaking, a handler compares the whole sending Actor: the account, and a program it trusts to have checked is_authorized(). The usual choice is the program itself: each participant speaks through their own actor of the program (a “seat”), and the receiving side checks from_own_program() plus the account id.

What a grant does not mean

It does not mean “the owner asked for this message”. It means this delivery carries a grant over me: I am an actor the signature marked and this is the root call, the program my PDA derives from attached its seed, or someone holding my grant attached it here.

That gap is the model’s main footgun.

A program you handed a grant to: Alice signs a transaction marking her balance actor and sends the root to program P. P holds a grant over that actor for the root delivery, and it can attach it to anything it sends, so a hostile or buggy P can debit her through the native token program. It cannot act with authority over an actor Alice did not mark: it holds no grant over her token holding, her other programs or her other accounts.

A grant you lend is spendable before it comes back. If the root hands a counterparty a grant for a round trip, the counterparty can use it meanwhile, and it returns on the callback only if the counterparty chooses to attach it.

A stored message carries no authority. A claim, a stored message collected as the root and a promoted async message all arrive with an empty grant set, so a collected message cannot debit its recipient even if the recipient marks the account: a Transfer { to: mallory, .. } stored at Alice’s private balance actor cannot move her funds, however she collects it. Collecting consumes a message; it does not consent to it. Consent is marking an actor and sending a root call yourself.

Rules that follow:

  • For program authors. If an operation must be started by the owner and nobody else, check both input.is_authorized() and input.from.is_none(). At a sync root from is None, and the grant exists only because the actor was marked, so the pair says the owner started it. If another program may start it, decide which one and check input.from.
  • For program authors sending on. send_sync attaches no grant, so a program using it builds cleanly and then fails at runtime wherever the callee checks authorization (the native token refuses with UnauthorizedSender). Use SyncMessage::new(..).with_grants([actor]) when the callee needs authority, and with_lent_grant(input) when replying to a reader so it can act on what it read.
  • For transaction builders. Mark the fewest actors the call needs: the root program holds everything you mark and can pass it on.
  • For users. A transaction is only as trustworthy as the programs you hand grants to, and the damage is bounded by the actors you marked.