Payment requests
Shows: accept by answering (10)
Goal
Bob asks Alice for money; Alice sees the request, decides, and pays with one action.
A stored message carries no authority, so collecting a request can never pay it. Alice consents by answering the request with a transaction of her own, marking the one actor the payment needs.
Actors
| Actor | Placement | Authorized by |
|---|---|---|
| Alice’s balance | private | a grant Alice marks on her own transaction |
Alice’s mandate actor | private | Alice |
Bob’s requester actor | public or private | Bob |
Messages
The request is a record that someone asked:
enum Message {
/// Bob -> Alice's mandate, async. Anyone may send one; it carries no authority.
Ask { to: AccountId, amount: u128 },
/// Alice -> her own mandate, as a transaction root. Pays a recorded ask.
Pay { index: u32 },
}
Bob needs a program to send the Ask for him, because an async message comes from an actor:
/// The whole requester program: send the given bytes to the given actor, async.
fn handle_message(input: &ReceiveInput, (to, message): (Actor, Vec<u8>)) -> Response {
assert!(input.is_authorized() && input.from.is_none(), "only the owner sends requests");
Response::keep_state().send(AsyncMessage { to, message })
}
Alice’s mandate records asks, and pays one only when she is the one asking it to:
fn handle_message(input: &ReceiveInput, message: Message) -> Response {
match message {
// Anyone may ask. Recording is all this does.
Message::Ask { to, amount } => {
let mut asks = Asks::from_bytes(&input.pre_state);
asks.push(to, amount);
Response::set_state(asks.to_bytes())
}
// Only Alice, and only as a root call: `from` is None at a root, and the
// grant over her balance exists only because her transaction marked it.
Message::Pay { index } => {
assert!(input.is_authorized() && input.from.is_none(), "only the owner pays");
let mut asks = Asks::from_bytes(&input.pre_state);
let (to, amount) = asks.take(index);
let payer = Actor::native_balance(input.receiver.account_id);
Response::set_state(asks.to_bytes()).send(
SyncMessage::new(payer, &native_token::Message::Transfer { to, amount })
.with_grants([payer]),
)
}
}
}
How it runs
- Request. Bob’s requester sends
Askto Alice’s mandate as an async message. It is stored, then collected into her mandate’s state. If the ask is published in the clear (always from a public requester, and from a private one that does not seal it), the mandate’s address needs its recovery binding on chain, or introduced by the same privacy-preserving transaction, or the ask is charged and reverted withUnboundAsyncDestination. Nothing has happened to Alice’s money, and nothing could have. - Read. Alice’s client reads the recorded asks from her mandate’s state: “pay 10 to this account”. The mandate does not record who asked.
- Decide.
| Alice | Effect |
|---|---|
| ignores it | nothing, forever |
sends Pay without marking her mandate | the handler refuses: is_authorized() is false |
sends Pay marking the mandate but not her balance actor | attaching the grant fails with UnheldGrant, so the Transfer is never sent |
sends Pay marking both the mandate and her balance | her balance is debited and a credit goes to Bob |
The ask is removed from the mandate’s state when paid, so it cannot be paid twice.
Why it works
A Transfer needs a grant over the balance actor it debits. Only Alice’s own transaction can mint that grant, by marking the actor, and her mandate can pass it on because the root call handed it one.
So the authority to pay exists only once Alice makes it, and reaches one actor: her native balance. The mandate cannot touch her token holdings, other programs or other accounts, because she did not mark them.
With the wallet CLI, a public Alice pays in one command, and the :sign marks are the consent. Both actors need one: the mandate because its own handler checks is_authorized(), and the balance because that is the grant it passes on. An unmarked actor gets Authority::None and no grant. With no --payer, a signing account pays the fee. A private Alice, as in the table above, needs a client of her own (What works with the reference wallet today).
wallet program call --program <mandate> --to alice:sign \
--actor alice@<native token id>:sign --message-hex <Pay{index}>
Public and private
| Requester | What an observer sees of the request |
|---|---|
| Public | who asked, the payer’s address, the amount |
| Private | a sealed message, if it seals; in the clear, the same as public, with the sender shown as the address it presents |
The payment itself is a private transaction. If Bob is public, the credit to him crosses to the public side and is visible, with Alice shown only as the address her client presents for her balance (a fresh blinded alias, or her account id if her client chooses to present it). If Bob is private, the ask carries only his address, so Alice’s wallet needs more from him off chain: his keys and factor to seal the credit, or an address of his whose recovery binding is on chain, to send it in the clear.
A public payer can be asked this way too, and answers with a signed public transaction marking their mandate and balance actor, as in the command above.
Going further
- Limits on chain. Let the mandate accept an allowance: a sender and a maximum, set by Alice in an authorized call. It can then pay matching asks on a later root call without her reviewing each one, still spending only through the grant her transaction supplies.
- Requests for anything. The same move works for anything an owner might approve: a swap offer, a vote delegation, a multisig proposal. Record the ask, authorize the answer.
- What does not work: forcing payment. Nobody can make Alice answer.