|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Build & Run |
| 6 | + |
| 7 | +```bash |
| 8 | +# Build the project (generates protobuf sources into src/main/gen/) |
| 9 | +./gradlew build |
| 10 | + |
| 11 | +# Build fat JAR (output: build/libs/wallet-cli.jar) |
| 12 | +./gradlew shadowJar |
| 13 | + |
| 14 | +# Run in REPL 交互模式 (human-friendly, interactive prompts) |
| 15 | +./gradlew run |
| 16 | +# Or after building: java -jar build/libs/wallet-cli.jar |
| 17 | + |
| 18 | +# Run in standard CLI mode (non-interactive, scriptable) |
| 19 | +java -jar build/libs/wallet-cli.jar --network nile get-account --address TXyz... |
| 20 | +java -jar build/libs/wallet-cli.jar --output json --network nile get-account --address TXyz... |
| 21 | + |
| 22 | +# Run tests |
| 23 | +./gradlew test |
| 24 | + |
| 25 | +# Run a single test class |
| 26 | +./gradlew test --tests "org.tron.keystore.StringUtilsTest" |
| 27 | + |
| 28 | +# Clean (also removes src/main/gen/) |
| 29 | +./gradlew clean |
| 30 | +``` |
| 31 | + |
| 32 | +Java 8 source/target compatibility. Protobuf sources are in `src/main/protos/` and generate into `src/main/gen/` — this directory is git-tracked but rebuilt on `clean`. |
| 33 | + |
| 34 | +## QA Verification |
| 35 | + |
| 36 | +The `qa/` directory contains shell-based parity tests that compare interactive REPL output vs standard CLI (text and JSON modes). Requires a funded Nile testnet account. |
| 37 | + |
| 38 | +```bash |
| 39 | +# Run QA verification (needs TRON_TEST_PRIVATE_KEY env var for private key) |
| 40 | +TRON_TEST_PRIVATE_KEY=<nile-private-key> bash qa/run.sh verify |
| 41 | + |
| 42 | +# QA config is in qa/config.sh; test commands are in qa/commands/*.sh |
| 43 | +# MASTER_PASSWORD env var is used for keystore auto-login (default: testpassword123A) |
| 44 | +``` |
| 45 | + |
| 46 | +## Architecture |
| 47 | + |
| 48 | +This is a **TRON blockchain CLI wallet** built on the [Trident SDK](https://github.com/tronprotocol/trident). It communicates with TRON nodes via gRPC. |
| 49 | + |
| 50 | +### Two CLI Modes |
| 51 | + |
| 52 | +1. **REPL 交互模式** (human-friendly) — `Client` class with JCommander `@Parameters` inner classes. Entry point: `org.tron.walletcli.Client`. Features tab completion, interactive prompts, and conversational output. This is the largest file (~4700 lines). Best for manual exploration and day-to-day wallet management by humans. |
| 53 | +2. **Standard CLI 模式** (AI-agent-friendly) — `StandardCliRunner` with `CommandRegistry`/`CommandDefinition` pattern in `org.tron.walletcli.cli.*`. Supports `--output json`, `--network`, `--quiet` flags. Commands are registered in `cli/commands/` classes (e.g., `WalletCommands`, `TransactionCommands`, `QueryCommands`). Designed for automation: deterministic exit codes, structured JSON output, no interactive prompts, and env-var-based authentication — ideal for AI agents, scripts, and CI/CD pipelines. |
| 54 | + |
| 55 | +The standard CLI suppresses all stray stdout/stderr in JSON mode to ensure machine-parseable output. Authentication is automatic via `MASTER_PASSWORD` env var + keystore files in `Wallet/`. |
| 56 | + |
| 57 | +### Standard CLI Contract |
| 58 | + |
| 59 | +Before changing parser behavior, auth flow, JSON output, command success/failure semantics, or `qa/` expectations for |
| 60 | +the standard CLI, read: |
| 61 | + |
| 62 | +- `docs/standard-cli-contract-spec.md` |
| 63 | + |
| 64 | +Treat that file as the source of truth for the standard CLI contract unless the repository owner explicitly decides to |
| 65 | +revise it. |
| 66 | + |
| 67 | +### Request Flow |
| 68 | + |
| 69 | +``` |
| 70 | +# Standard CLI mode: |
| 71 | +User Input → GlobalOptions → StandardCliRunner → CommandRegistry → CommandHandler → WalletApiWrapper → WalletApi → Trident SDK → gRPC → TRON Node |
| 72 | +
|
| 73 | +# Interactive REPL mode: |
| 74 | +User Input → Client (JCommander) → WalletApiWrapper → WalletApi → Trident SDK → gRPC → TRON Node |
| 75 | +``` |
| 76 | + |
| 77 | +### Key Classes |
| 78 | + |
| 79 | +- **`org.tron.walletcli.Client`** — Legacy REPL entry point and CLI command dispatcher. Each command is a JCommander `@Parameters` inner class. |
| 80 | +- **`org.tron.walletcli.cli.StandardCliRunner`** — New standard CLI executor. Handles network init, auto-authentication, JSON stream suppression, and command dispatch. |
| 81 | +- **`org.tron.walletcli.cli.CommandRegistry`** — Maps command names/aliases to `CommandDefinition` instances. Supports fuzzy suggestion on typos. |
| 82 | +- **`org.tron.walletcli.cli.CommandDefinition`** — Immutable command metadata (name, aliases, options, handler). Built via fluent `Builder` API. |
| 83 | +- **`org.tron.walletcli.cli.OutputFormatter`** — Formats output as text or JSON. In JSON mode, wraps results in `{"success":true,"data":...}` envelope. |
| 84 | +- **`org.tron.walletcli.WalletApiWrapper`** — Orchestration layer between CLI and core wallet logic. Handles transaction construction, signing, and broadcasting. |
| 85 | +- **`org.tron.walletserver.WalletApi`** — Core wallet operations: account management, transaction creation, proposals, asset operations. Delegates gRPC calls to Trident. |
| 86 | +- **`org.tron.walletcli.ApiClientFactory`** — Creates gRPC client instances for different networks (mainnet, Nile testnet, Shasta testnet, custom). |
| 87 | + |
| 88 | +### Adding a New Standard CLI Command |
| 89 | + |
| 90 | +1. Create or extend a class in `cli/commands/` (e.g., `TransactionCommands.java`) |
| 91 | +2. Build a `CommandDefinition` via `CommandDefinition.builder()` with name, aliases, options, and handler |
| 92 | +3. Register it in the appropriate `register(CommandRegistry)` method |
| 93 | +4. The handler receives `(ParsedOptions, WalletApiWrapper, OutputFormatter)` — use `formatter.success()/error()` for output |
| 94 | + |
| 95 | +### Package Organization |
| 96 | + |
| 97 | +| Package | Purpose | |
| 98 | +|---------|---------| |
| 99 | +| `walletcli` | CLI entry points, API wrapper | |
| 100 | +| `walletcli.cli` | Standard CLI framework: registry, definitions, options, formatter | |
| 101 | +| `walletcli.cli.commands` | Standard CLI command implementations by domain | |
| 102 | +| `walletserver` | Core wallet API and gRPC communication | |
| 103 | +| `common` | Crypto utilities, encoding, enums, shared helpers | |
| 104 | +| `core` | Configuration, data converters, DAOs, exceptions, managers | |
| 105 | +| `keystore` | Wallet file encryption/decryption, key management | |
| 106 | +| `ledger` | Ledger hardware wallet integration via HID | |
| 107 | +| `mnemonic` | BIP39 mnemonic seed phrase support | |
| 108 | +| `multi` | Multi-signature transaction handling | |
| 109 | +| `gasfree` | GasFree transaction API (transfer tokens without gas) | |
| 110 | + |
| 111 | +### Configuration |
| 112 | + |
| 113 | +- **Network config:** `src/main/resources/config.conf` (HOCON format via Typesafe Config) |
| 114 | +- **Logging:** `src/main/resources/logback.xml` (Logback, INFO level console + rolling file) |
| 115 | +- **Lombok:** `lombok.config` — uses `logger` as the log field name (not the default `log`) |
| 116 | + |
| 117 | +### Key Frameworks & Libraries |
| 118 | + |
| 119 | +- **Trident SDK 0.10.0** — All gRPC API calls to TRON nodes |
| 120 | +- **JCommander 1.82** — CLI argument parsing (REPL 交互模式) |
| 121 | +- **JLine 3.25.0** — Interactive terminal/readline |
| 122 | +- **BouncyCastle** — Cryptographic operations |
| 123 | +- **Protobuf 3.25.5 / gRPC 1.60.0** — Protocol definitions and transport |
| 124 | +- **Lombok** — `@Getter`, `@Setter`, `@Slf4j` etc. (annotation processing) |
0 commit comments