Skip to Content
KryptWarden Developers
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)

FunctionReturnsNotes
register_agent(agent_key: BytesN<32>, policy: Policy)()AgentAlreadyExists if the key is registered. Validates the policy
update_policy(agent_key, policy)u32New 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

FunctionAuthReturnsNotes
request_hold(input: HoldRequestInput)Agent signature (KW_HOLD1)u64 hold IDChecks 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

FunctionAuthNotes
withdraw(to: Address, amount: i128)OwnerOnly up to the unreserved balance
pause(caller: Address) / unpause(caller: Address)OwnerWallet-wide kill switch

Reads

FunctionReturns
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) then accept_ownership() by the new owner: a two-step handover.
  • renounce_ownership() always fails with RenounceDisabled (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

EventEmitted byFields
WalletInitializedconstructorowner, usdc_token, schema_version
AgentRegisteredregister_agentagent_key, daily_budget, auto_approve_limit, expires_at, policy_version
PolicyUpdatedupdate_policysame as above
AgentStatusChangedpause / resume / revokeagent_key, status
HoldCreatedrequest_holdhold_id, agent_key, seller, amount, expires_at, request_id, policy_version
HoldApprovedapprove_holdhold_id, agent_key, seller, amount, expires_at
HoldDenieddeny_holdhold_id, agent_key, amount
HoldExpiredEventexpire_hold, revoke_agenthold_id, agent_key, amount
HoldCancelledrevoke_agenthold_id, agent_key, amount
PaymentRejectedrecord_rejected_attemptagent_key, seller, amount, reason_code, request_id, policy_version
FundsWithdrawnwithdrawto, 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

ConstantValue
MAX_ALLOWED_SELLERS32
MAX_ACTIVE_HOLDS_PER_AGENT8
MIN_APPROVAL_TIMEOUT_SECS / MAX_…10 / 86,400
APPROVED_SETTLEMENT_GRACE_SECS300
SECONDS_PER_DAY86,400 (UTC days)
Request ID retention7 days (7 × 17,280 ledgers)

On this page