Skip to content

alediscoveringcoding/Caishen

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

95 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Caishen - AI Copilot Wallet for Sui

Status: Active Development | Version: 0.4.1
Deployed: caishen.iseethereaper.com

A Telegram-first AI wallet assistant for the Sui blockchain with zkLogin (Google OAuth), Slush/Wallet Standard support, AI-driven natural language intents, and a React-based signing web dApp.


πŸš€ Quick Start

Prerequisites

  • Python 3.11+ (bot backend)
  • Node.js 20+ (web dApp)
  • PostgreSQL 16 (database)
  • Docker (recommended for deployment)

Local Development

# 1. Bot (Python/aiogram)
cd bot
python -m venv .venv && source .venv/bin/activate  # or .venv\Scripts\activate on Windows
pip install -r requirements.txt
cp .env.example .env  # Configure: TELEGRAM_BOT_TOKEN, POSTGRES_*, GEMINI_API_KEY
python -m src.bot.bot

# 2. Web dApp (React/Vite)
cd services/web-dapp
npm install
cp .env.example .env  # Configure: VITE_API_BASE_URL, VITE_GOOGLE_CLIENT_ID, VITE_SUI_NETWORK
npm run dev

# 3. Database
docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=caishen postgres:16
psql -U postgres -d caishen -f database/init/001_schema.sql

πŸ“– Full guides: QUICKSTART.md | INSTALLATION.md


⚑ Features

  • πŸ’¬ Natural Language Interface - Chat or voice commands powered by Gemini 2.0 Flash
  • πŸ” zkLogin + Wallet Standard - Google OAuth zkLogin or connect Slush/any Sui wallet
  • πŸ”— Persistent Wallet Linking - Bind Telegram account to wallet via secure web flow
  • πŸ’° Sui Blockchain Integration - Balance, send, NFTs, transaction history
  • 🎯 AI Function Calling - 5+ tools: balance, send, contacts, history, portfolio
  • πŸ“± Contact Management - Store friendly names for addresses (database + on-chain registry)
  • πŸ“œ Smart Contracts - Batch transfers, contact registry, spending guardrails (Move)
  • 🎀 Voice Input - Gemini multimodal transcription (replaces Whisper)

πŸ—οΈ Architecture (v0.4.1)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     HTTPS/Webhook    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Telegram      β”‚ ─────────────────────▢│  nginx (SSL)     β”‚
β”‚   Users         β”‚                       β”‚  Port 443/80     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                   β”‚
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β–Ό                    β–Ό                    β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Python Bot     β”‚  β”‚  PostgreSQL     β”‚  β”‚  React Web dApp β”‚
                    β”‚  (aiogram)      β”‚<<β”‚  Database       β”‚  β”‚  (Vite)         β”‚
                    β”‚  Port 3001      β”‚  β”‚  Port 5432      β”‚  β”‚  Static Build   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚                                          β”‚
                             β”‚ Sui RPC                                  β”‚ zkLogin OAuth
                             β–Ό                                          β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  Sui Blockchain β”‚                        β”‚  Mysten Labs    β”‚
                    β”‚  (testnet)      β”‚                        β”‚  Salt/Prover    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Tech Stack

  • Bot: Python 3.11, aiogram, aiohttp, httpx (Sui RPC), google-genai (Gemini)
  • Web dApp: React 18, Vite, @mysten/dapp-kit, @mysten/sui/zklogin
  • Database: PostgreSQL 16 (users, wallet_links, contacts, linking_sessions)
  • Chain: Sui testnet/mainnet via JSON-RPC
  • AI: Gemini 2.0 Flash (NLP + voice transcription + function calling)
  • Infra: nginx (reverse proxy + SSL), Docker Compose

πŸ”„ User Flow

1. Wallet Linking (One-Time Setup)

User sends /start in Telegram
         ↓
Bot creates 15-min token, returns link:
https://caishen.iseethereaper.com/link/@username?token=abc123
         ↓
User chooses on web page:
  [Create zkLogin wallet]  or  [Connect Slush/other wallet]
         ↓
If zkLogin: Google OAuth β†’ Mysten salt/prover β†’ zkLogin address
If Slush: Wallet Standard connection β†’ address
         ↓
Telegram Login Widget verifies Telegram account (HMAC)
         ↓
Bot API binds: telegram_id + wallet_address + zkLogin salt/sub
         ↓
Done! Now user can use /balance, /send, /history, AI commands

2. Daily Usage (After Linking)

# Balance check
User: "/balance"
Bot: "πŸ’° Your balance: 1,234.56 SUI"

# Natural language send
User: "Send 10 SUI to Alice"
Bot: [Gemini parses intent] β†’ [Builds unsigned tx] β†’ [Returns signing link]
User clicks link β†’ Web dApp opens β†’ Wallet signs β†’ Tx confirmed

# Voice command
User: [🎀 Voice note] "What's my transaction history?"
Bot: [Gemini transcribes] β†’ [Fetches history] β†’ "πŸ“œ Last 5 transactions..."

# Contact management
User: "/contacts add Alice 0x1234..."
Bot: "βœ… Added Alice"
User: "Send 5 SUI to Alice"
Bot: [Resolves Alice β†’ 0x1234...] β†’ [Builds tx]

πŸ“œ Smart Contracts (Move)

On-chain smart contracts deployed to Sui. See SMART_CONTRACTS.md for full documentation.

Contract Purpose Key Benefit Status
BatchTransfer Multi-recipient payments "Pay 10 people" in 1 tx βœ… Implemented, but not used yet
ContactRegistry On-chain address book Portable, shared contacts βœ… Implemented, but not used yet
SpendingGuardian Rate-limited transfers Anti-theft, spending limits βœ… Implemented, but not used yet

Quick Commands

# Build contracts
cd move && sui move build

# Run tests
sui move test

# Deploy to testnet
sui client publish --gas-budget 100000000

Example Use Cases

"Send 5 SUI to alice, bob, and carol"    β†’ BatchTransfer.batch_send_sui()
"Add alice as 0x123..."                   β†’ ContactRegistry.add_contact()
"Set my daily limit to 100 SUI"           β†’ SpendingGuardian.set_limit()
"Freeze my account"                       β†’ SpendingGuardian.freeze()

πŸ“‚ Project Structure

.
β”œβ”€β”€ bot/                                # Python Telegram Bot
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ bot/
β”‚   β”‚   β”‚   β”œβ”€β”€ bot.py                  # Main bot entry point (aiogram)
β”‚   β”‚   β”‚   └── handlers/               # Command handlers
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   β”œβ”€β”€ gemini.py               # Gemini AI integration
β”‚   β”‚   β”‚   └── sui.py                  # Sui RPC client (httpx)
β”‚   β”‚   β”œβ”€β”€ database/
β”‚   β”‚   β”‚   └── postgres.py             # PostgreSQL connection
β”‚   β”‚   └── utils/
β”‚   β”‚       └── audio_processor.py      # Voice transcription
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── Dockerfile
β”‚
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ web-dapp/                       # React Signing Interface
β”‚   β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”‚   β”œβ”€β”€ App.tsx                 # Main app with wallet connection
β”‚   β”‚   β”‚   β”œβ”€β”€ LinkPage.tsx            # /link/:handle page (zkLogin/Slush)
β”‚   β”‚   β”‚   └── main.tsx                # Vite entry point
β”‚   β”‚   β”œβ”€β”€ package.json
β”‚   β”‚   └── vite.config.ts
β”‚   β”‚
β”‚   β”œβ”€β”€ transaction-builder/            # (Legacy - now in bot)
β”‚   β”œβ”€β”€ user-service/                   # (Legacy - now in bot)
β”‚   β”œβ”€β”€ zklogin-service/                # (External Mysten APIs)
β”‚   β”œβ”€β”€ nlp-service/                    # (Now Gemini in bot)
β”‚   └── notification-service/           # (Future - webhooks)
β”‚
β”œβ”€β”€ move/                               # Smart Contracts (Sui Move)
β”‚   β”œβ”€β”€ sources/
β”‚   β”‚   β”œβ”€β”€ batch_transfer.move         # Multi-recipient transfers
β”‚   β”‚   β”œβ”€β”€ contact_registry.move       # On-chain address book
β”‚   β”‚   └── spending_guardian.move      # Rate-limited spending
β”‚   β”œβ”€β”€ tests/
β”‚   └── Move.toml
β”‚
β”œβ”€β”€ database/
β”‚   └── init/
β”‚       └── 001_schema.sql              # PostgreSQL schema (users, wallet_links, contacts)
β”‚
β”œβ”€β”€ nginx/
β”‚   β”œβ”€β”€ nginx.conf                      # Main nginx config (SSL termination)
β”‚   β”œβ”€β”€ caishen.iseethereaper.com.conf  # Site-specific config
β”‚   └── ssl/                            # SSL certificates (Let's Encrypt)
β”‚
β”œβ”€β”€ docker-compose.yml                  # Multi-service orchestration
β”œβ”€β”€ .env.example                        # Environment template
└── README.md                           # This file

πŸ› οΈ Development

Bot Development (Python)

cd bot
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install -r requirements.txt

# Run with hot reload
python -m src.bot.bot

# Run tests
pytest tests/

Web dApp Development (Node.js)

cd services/web-dapp
npm install
npm run dev  # Runs on http://localhost:5173

# Build for production
npm run build  # Output: out/

Smart Contracts (Move)

cd move
sui move build
sui move test
sui client publish --gas-budget 100000000

πŸ” Environment Variables

Bot (.env)

# Telegram
TELEGRAM_BOT_TOKEN=your_bot_token_from_BotFather
TELEGRAM_WEBHOOK_SECRET=random_secret_for_webhook_validation
WEBHOOK_BASE_URL=https://caishen.iseethereaper.com
WEBAPP_URL=https://caishen.iseethereaper.com

# Database
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=caishen
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_secure_password

# AI
GEMINI_API_KEY=your_google_ai_api_key

# Sui
SUI_NETWORK=testnet  # or mainnet
SUI_RPC_URL=https://fullnode.testnet.sui.io:443

Web dApp (.env)

VITE_API_BASE_URL=https://caishen.iseethereaper.com
VITE_TELEGRAM_BOT_USERNAME=your_bot_username
VITE_GOOGLE_CLIENT_ID=your_google_oauth_client_id
VITE_SUI_NETWORK=testnet
VITE_ZKLOGIN_SALT_SERVICE_URL=https://salt.api.mystenlabs.com/get_salt
VITE_ZKLOGIN_PROVER_URL=https://prover-dev.mystenlabs.com/v1  # testnet

🚒 Deployment

Docker Compose (Recommended)

# 1. Clone and configure
git clone https://github.com/your-org/caishen.git
cd caishen
cp .env.example .env
# Edit .env with production values

# 2. Build and deploy
docker-compose up -d --build

# 3. Sync web dApp static files
docker-compose exec web-dapp npm run build
sudo rsync -av --delete services/web-dapp/out/ /var/www/caishen/web/

# 4. Set webhook
curl -X POST "https://api.telegram.org/bot<YOUR_TOKEN>/setWebhook" \
  -d "url=https://caishen.iseethereaper.com/webhook" \
  -d "secret_token=<TELEGRAM_WEBHOOK_SECRET>"

# 5. Reload nginx
sudo systemctl reload nginx

Manual VPS Deployment

See DEPLOY_TO_VPS.md for detailed instructions.


πŸ§ͺ Testing

# Bot tests (Python)
cd bot && pytest

# Move contract tests
cd move && sui move test

# Web dApp tests
cd services/web-dapp && npm test

πŸ› Troubleshooting

Expired Token (Web dApp)

Error: "Token expired or invalid"

Cause: Linking token is valid for 15 minutes only

Fix: Return to Telegram bot and run /start again for a fresh link

Webhook 404/405

Error: Telegram webhook returns 404 or 405

Cause: nginx not proxying correctly to bot on port 3001

Fix:

# Check nginx config
sudo nginx -t

# Verify bot is running
docker-compose logs telegram-bot

# Check port binding
sudo netstat -tlnp | grep 3001

Chat Not Found

Error: "Chat not found" when bot tries to message user

Cause: User never sent /start to bot

Fix: User must initiate conversation with bot first

zkLogin Setup Failed

Error: Google OAuth fails or prover errors

Fix:

  1. Verify VITE_GOOGLE_CLIENT_ID matches OAuth consent screen
  2. Add authorized redirect URI: https://caishen.iseethereaper.com/*
  3. Check Mysten prover URL matches network (testnet vs mainnet)

Invalid Parameters

Error: Gemini function call fails with "invalid parameters"

Cause: Tool schema mismatch or missing required fields

Fix: Check bot/src/services/gemini.py tool definitions match handlers

Static Build Not Updating

Error: Web dApp shows old version after deployment

Fix:

# Force rebuild and sync
cd services/web-dapp
rm -rf out/
npm run build
sudo rsync -av --delete out/ /var/www/caishen/web/
sudo systemctl reload nginx

πŸ“š Documentation


πŸ—ΊοΈ Roadmap

See IMPLEMENTATION_STATUS.md for detailed checkpoints.

Phase 1: Foundation (🚧 In Progress - ~60% complete)

  • βœ… Python bot with aiogram
  • βœ… Gemini AI integration
  • βœ… Sui RPC client
  • βœ… PostgreSQL database
  • 🚧 Transaction builder
  • 🚧 NFT service

Phase 2: Smart Contracts (βœ… Complete)

  • βœ… BatchTransfer contract
  • βœ… ContactRegistry contract
  • βœ… SpendingGuardian contract
  • ⏸️ Deployment to testnet
  • ⏸️ Bot integration

Phase 3: Web dApp (🚧 In Progress - ~70% complete)

  • βœ… React + Vite setup
  • βœ… zkLogin flow
  • βœ… Wallet Standard connection
  • βœ… Telegram verification
  • 🚧 Transaction signing UI
  • πŸ“‹ Transaction history view

Phase 4: Production (πŸ“‹ Planned)

  • πŸ“‹ Rate limiting
  • πŸ“‹ Error monitoring (Sentry)
  • πŸ“‹ Analytics
  • πŸ“‹ Backup/restore
  • πŸ“‹ Multi-language support

🀝 Contributing

This is an active development project. Contributions welcome!

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit changes: git commit -m 'Add amazing feature'
  4. Push to branch: git push origin feature/amazing-feature
  5. Open a Pull Request

πŸ“ License

MIT License - see LICENSE file


πŸ“ž Support & Resources


Built with ❀️ for the Sui ecosystem

  • src/services/llm/toolHandlers.ts β€” tool execution stubs hooking Sui/contact services.
  • src/services/sui/client.ts β€” SuiClient factory pinned to SUI_RPC_URL.
  • src/services/sui/suiService.ts β€” balance lookups via Sui JSON-RPC.
  • src/services/sui/txBuilder.ts β€” unsigned transaction building for SUI, tokens, NFTs (SDK-based).
  • src/services/sui/utils.ts β€” metadata cache + amount conversions.
  • src/services/sui/nftService.ts β€” NFT listing (ownership view + display metadata).
  • src/services/sui/historyService.ts β€” transaction history fetcher with basic sent/received classification.
  • src/services/contacts/contactStore.ts β€” in-memory contacts and resolution helper.
  • src/utils/security.ts β€” bcrypt hashing + nonce helper.
  • src/utils/logger.ts β€” pino logger config.

Infrastructure quick start

  • Docker/Docker Compose definitions for all microservices + infra live in docker-compose.yml (nginx front-end, api-gateway, Telegram gateway, NLP, transaction-builder, user, notification, Redis). SQLite3 is used instead of Postgres to fit the 2GB footprint.
  • nginx reverse proxy configs are in nginx/nginx.conf (public) and nginx/api-gateway.conf (internal service mesh). Self-signed cert instructions are in nginx/ssl/README.md.
  • For now, zkLogin uses external Mysten Labs services (salt + prover); no local zklogin-service container.
  • See DOCKER_SETUP.md for WSL-friendly commands, SSL generation steps, and the 2GB-optimized deployment notes.

Tooling

  • Lint: npm run lint
  • Format: npm run format
  • Type check: npm run check
  • Build: npm run build

Next steps (suggested)

  • Wire llmService to OpenAI/Gemini (function calling) using the schemas from the spec.
  • Extend Sui layer for transaction building (send SUI/tokens, NFTs) and deep-link generation to the signing dApp (bytes are already built).
  • Persist sessions/contacts in SQLite/Redis (per the 2GB compose) instead of memory; reuse tokenService helpers to store only hashed secrets.
  • Add end-to-end tests around the webhook flow and Telegram client mocking.
  • Harden RPC usage (timeouts/retries), expand history/NFT queries via indexer, and add rate limiting.

About

Caishen is a Telegram-based assistant that simplifies the Sui user experience without compromising on security. It brings hands-free accessibility, turning natural voice commands into secure, on-chain transactions.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 45.9%
  • HTML 27.7%
  • Python 13.9%
  • Move 8.7%
  • JavaScript 1.2%
  • CSS 1.0%
  • Other 1.6%