Building a Self-Custodial AI Trading Wallet for Android: Architecture, Safety, and Lessons Learned
Crypto trading has a structural problem: you can have automation, or you can have custody — historically not both. Trading bots want your private keys. Self-custodial wallets give you keys but no trading intelligence. This post explains how I built an Android wallet that keeps keys on-device while running an autonomous AI trading agent locally, and the safety architecture that makes that safe.
The core design decision
The key insight is separating two concerns that get conflated: the agent that reasons about markets and the keys that authorize transactions.
- Private keys are generated and stored on the device. They are never serialized to disk where the agent can read them, never transmitted, and never given to the LLM.
- The AI expresses intent only. It is given tools, not custody.
- Every write operation — swap, approval, transfer — passes through a mandatory safety gate the agent cannot bypass.
The agent runtime: a bounded thinking loop
The agent runs as a loop: build context → call the LLM → check if the model requested tool use → execute tools → feed results back → repeat until a final response. It is capped at 8 rounds to prevent runaway tool-call chains.
The runtime supports both OpenAI function-calling and Anthropic Claude tool-use protocols, differing in how the system prompt is placed. Transient network failures (timeout, DNS, 5xx, 429) trigger a retry with backoff; auth failures (401/403) do not.
Twelve tools, read-only vs write
The agent is granted exactly twelve tools:
- Read-only (no gas, no state change): wallet address, native balance, token balance, token price, positions, market indicators (RSI/MACD/MA/Bollinger), safety status, arbitrary read-only contract call.
- Write (gas, state change, mandatory gate check): swap tokens, approve token, send native, arbitrary state-changing contract call.
Every write tool requires an operation_desc string stating intent, which is recorded in the audit log.
Three lines of defense
1. TradeAuthManager — AI trading can not even be enabled until the account meets an eligibility threshold (e.g. ≥200 in major assets, or ≥20,000 R-MAB tokens).
2. SafetyGate — five mandatory checks on every write:
- Circuit breaker — 3 consecutive losses = 30-min break; daily error rate >50% = 60-min break.
- Daily limit — rejects once the day total exceeds the configured cap.
- Per-trade limit — rejects single transactions over the cap.
- Whitelist — by default every token is blacklisted; a non-whitelisted token triggers a confirmation dialog.
- Audit — every approved operation is stamped and recorded.
3. RiskManager — execution parameters: max 10% position per token, default stop-loss -5% / take-profit +15%, 3% slippage, 20 trades/day cap.
Cross-chain defaults
Cross-chain transactions carry conservative defaults: ** per-trade, daily cap**. A one-time authorization for a chain+asset+address combo enables subsequent same-chain signals to execute silently when safety conditions are met.
Key lessons
- Chain-specific DEX routers matter. Reusing Ethereum Uniswap V2 address on AVAX/MATIC/FTM breaks swaps. Each EVM chain needs its correct router and wrapped-native token address.
- Separate executors for UI. A single-thread executor for asset loading blocked the UI for 2.5 minutes when computing all-wallet totals. Now the current-wallet display runs on one executor, background totals on another.
- Whitelist verification must use the UI-thread captured origin. WebView.getUrl() called from a background thread returns null, breaking DApp whitelist checks.
Tech stack
- Android (Kotlin/Java), 10 chains (BNB, ETH, MATIC, AVAX, FTM, ARB, OP, BASE, SOL, TRX)
- Local indicators: RSI, MACD, moving averages, Bollinger Bands
- 4 languages: English, 中文, 日本語, Deutsch
Important disclaimer
The AI provides analysis only and is not financial advice. Cryptocurrency trading is highly risky — never risk money you cannot afford to lose.
The project is source-available (BSL-1.1), not OSI open source. You can audit the code and use it personally; commercial use requires a license from the team.
- GitHub: https://github.com/openxcn/AI-Crypto-Wallet
- Whitepaper: https://red-tough-caribou-72.mypinata.cloud/ipfs/bafkreiaczdqmz3tbfza5zu2k3nzbjk7j6p7tug32fw77kidtq2zfn2dqom
Not financial advice. Do your own research.
Top comments (0)