Smart contracts
Wallet contract
KryptWardenWallet functions, types, events and limits.
A Soroban custom account. USDC transfers out of it are authorised by __check_auth, which runs the policy gates.
Setup
__constructor(owner: Address, usdc_token: Address)
Called once at deploy time by the factory. Sets the owner and the only token agents may spend.
Agents (owner only)
| Function | Returns | Notes |
|---|---|---|
register_agent(agent_key: BytesN<32>, policy: Policy) | () | AgentAlreadyExists if the key is registered. Validates the policy |
update_policy(agent_key, policy) | u32 | New policy_version |
pause_agent(agent_key) | () | Status → Paused |
resume_agent(agent_key) | () | Status → Active |
revoke_agent(agent_key) | () | Status → Revoked, permanently. Open holds become Cancelled (or Expired if past their deadline) |
Holds
| Function | Auth | Returns | Notes |
|---|---|---|---|
request_hold(input: HoldRequestInput) | Agent signature (KW_HOLD1) | u64 hold ID | Checks status, expiry, seller, open-hold limit and budget; reserves the amount |
approve_hold(hold_id: u64) | Owner | () | Pending → Approved. Guarantees at least 300 s to settle |
deny_hold(hold_id: u64) | Owner | () | Pending/Approved → Denied; frees the reservation |
expire_hold(hold_id: u64) | Anyone | () | Marks a past-deadline hold Expired |
Audit
record_rejected_attempt(input: RejectionInput)
Agent-signed (KW_REJ1), usually submitted by the relayer. Moves no money; emits PaymentRejected with the reason code.
Funds
| Function | Auth | Notes |
|---|---|---|
withdraw(to: Address, amount: i128) | Owner | Only up to the unreserved balance |
pause(caller: Address) / unpause(caller: Address) | Owner | Wallet-wide kill switch |
Reads
| Function | Returns |
|---|---|
get_config() | WalletConfig { usdc_token, next_hold_id, schema_version } |
get_agent(agent_key) | AgentRecord |
get_agent_summary(agent_key) | AgentSummary, with today's spend rolled over if the UTC day changed |
get_agent_keys() | Vec<BytesN<32>> |
get_hold(hold_id) | HoldRecord |
get_unreserved_balance() | i128, balance minus open holds |
paused() | bool |
get_owner() | Option<Address> |
Ownership and upgrades
From OpenZeppelin:
transfer_ownership(new_owner, live_until_ledger)thenaccept_ownership()by the new owner: a two-step handover.renounce_ownership()always fails withRenounceDisabled(128).upgrade(new_wasm_hash, operator): owner only.
Types
pub struct Policy {
pub daily_budget: i128, // stroops
pub auto_approve_limit: i128, // stroops, 0..=daily_budget
pub allowed_sellers: Vec<Address>, // 1..=32
pub expires_at: u64, // unix seconds
pub approval_timeout_secs: u64, // 10..=86_400
}
pub enum AgentStatus { Active, Paused, Revoked }
pub enum HoldStatus { Pending, Approved, Denied, Consumed, Expired, Cancelled }
pub struct AgentSummary {
pub agent_key: BytesN<32>,
pub status: AgentStatus,
pub is_expired: bool,
pub policy: Policy,
pub policy_version: u32,
pub spent_today: i128,
pub reserved_holds: i128,
pub remaining_budget: i128,
pub active_hold_count: u32,
}
pub struct HoldRecord {
pub id: u64,
pub agent_key: BytesN<32>,
pub seller: Address,
pub amount: i128,
pub status: HoldStatus,
pub created_at: u64,
pub expires_at: u64,
pub request_id: BytesN<32>,
pub policy_version: u32,
}
/// The signature the agent attaches to a USDC transfer.
pub struct KwSignature {
pub agent_key: BytesN<32>,
pub signature: BytesN<64>,
pub request_id: BytesN<32>,
pub hold_id: Option<u64>, // set when settling an approved hold
}
pub struct HoldRequestInput {
pub agent_key: BytesN<32>,
pub seller: Address,
pub amount: i128,
pub request_id: BytesN<32>,
pub max_ledger: u32,
pub signature: BytesN<64>,
}
pub struct RejectionInput {
pub agent_key: BytesN<32>,
pub seller: Address,
pub amount: i128,
pub reason_code: u32,
pub request_id: BytesN<32>,
pub max_ledger: u32,
pub signature: BytesN<64>,
}
/// What hold and rejection signatures cover (SHA-256 of its XDR).
pub struct SignedAgentIntent {
pub domain: Symbol, // "KW_HOLD1" or "KW_REJ1"
pub network_id: BytesN<32>,
pub wallet: Address,
pub agent_key: BytesN<32>,
pub seller: Address,
pub amount: i128,
pub reason_code: u32, // 0 for holds
pub request_id: BytesN<32>,
pub max_ledger: u32,
}Events
| Event | Emitted by | Fields |
|---|---|---|
WalletInitialized | constructor | owner, usdc_token, schema_version |
AgentRegistered | register_agent | agent_key, daily_budget, auto_approve_limit, expires_at, policy_version |
PolicyUpdated | update_policy | same as above |
AgentStatusChanged | pause / resume / revoke | agent_key, status |
HoldCreated | request_hold | hold_id, agent_key, seller, amount, expires_at, request_id, policy_version |
HoldApproved | approve_hold | hold_id, agent_key, seller, amount, expires_at |
HoldDenied | deny_hold | hold_id, agent_key, amount |
HoldExpiredEvent | expire_hold, revoke_agent | hold_id, agent_key, amount |
HoldCancelled | revoke_agent | hold_id, agent_key, amount |
PaymentRejected | record_rejected_attempt | agent_key, seller, amount, reason_code, request_id, policy_version |
FundsWithdrawn | withdraw | to, amount |
Settled payments are standard USDC transfer events from the token contract, with the wallet as sender. __check_auth emits nothing, so any x402 facilitator can settle without changes.
Limits
| Constant | Value |
|---|---|
MAX_ALLOWED_SELLERS | 32 |
MAX_ACTIVE_HOLDS_PER_AGENT | 8 |
MIN_APPROVAL_TIMEOUT_SECS / MAX_… | 10 / 86,400 |
APPROVED_SETTLEMENT_GRACE_SECS | 300 |
SECONDS_PER_DAY | 86,400 (UTC days) |
| Request ID retention | 7 days (7 × 17,280 ledgers) |