|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +CashScript is a high-level language for Bitcoin Cash smart contracts, consisting of a compiler (`cashc`), a TypeScript SDK (`cashscript`), and shared utilities (`@cashscript/utils`). It compiles `.cash` contract files into Bitcoin Cash bytecode artifacts. |
| 8 | + |
| 9 | +## Common Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +# Install all dependencies (also runs bootstrap + build) |
| 13 | +yarn |
| 14 | + |
| 15 | +# Build all packages (required after code changes to propagate across packages) |
| 16 | +yarn build |
| 17 | + |
| 18 | +# Run all tests (uses vitest) |
| 19 | +yarn test |
| 20 | + |
| 21 | +# Run tests for a single package |
| 22 | +cd packages/cashc && yarn test |
| 23 | +cd packages/utils && yarn test |
| 24 | + |
| 25 | +# Run a specific test by name |
| 26 | +yarn test -t 'test name from it/describe block' |
| 27 | + |
| 28 | +# Run a single test file directly |
| 29 | +yarn vitest run packages/utils/test/bitauth-script.test.ts |
| 30 | + |
| 31 | +# Lint all packages |
| 32 | +yarn lint |
| 33 | + |
| 34 | +# Spellcheck |
| 35 | +yarn spellcheck |
| 36 | + |
| 37 | +# Regenerate ANTLR parser after grammar changes (from packages/cashc) |
| 38 | +yarn antlr |
| 39 | + |
| 40 | +# Run cashscript tests against real chipnet instead of mocknet |
| 41 | +TESTS_USE_CHIPNET=true yarn test |
| 42 | +``` |
| 43 | + |
| 44 | +## Architecture |
| 45 | + |
| 46 | +### Monorepo Structure |
| 47 | + |
| 48 | +Yarn workspaces + Lerna with three main packages: |
| 49 | + |
| 50 | +- **`packages/cashc`** — Compiler: `.cash` source → artifact JSON |
| 51 | +- **`packages/cashscript`** — SDK: load artifacts, build and send transactions |
| 52 | +- **`packages/utils`** — Shared utilities used by both compiler and SDK |
| 53 | + |
| 54 | +### Compiler Pipeline (`cashc`) |
| 55 | + |
| 56 | +The compilation flow in `compiler.ts`: |
| 57 | + |
| 58 | +1. **Lexing/Parsing** — ANTLR4 grammar (`src/grammar/CashScript.g4`) → parse tree |
| 59 | +2. **AST Building** — `AstBuilder` converts parse tree → typed AST (`src/ast/`) |
| 60 | +3. **Semantic Analysis** — Three traversals in order: |
| 61 | + - `SymbolTableTraversal` — resolve identifiers and scopes |
| 62 | + - `TypeCheckTraversal` — type checking |
| 63 | + - `EnsureFinalRequireTraversal` — validate contract structure |
| 64 | +4. **Code Generation** — `GenerateTargetTraversal` (`src/generation/`) emits bytecode opcodes, source maps, console logs, requires, and source tags |
| 65 | +5. **Optimization** — `optimiseBytecode` (`utils/src/script.ts`) applies peephole optimizations, adjusting source maps and metadata indices |
| 66 | +6. **Artifact Generation** — produces the final JSON artifact with bytecode, ABI, debug info |
| 67 | + |
| 68 | +The compiler uses the **visitor pattern** throughout — AST nodes accept traversals defined in `AstVisitor`/`AstTraversal`. |
| 69 | + |
| 70 | +### SDK (`cashscript`) |
| 71 | + |
| 72 | +- `Contract` — loads compiled artifacts, instantiates with constructor args |
| 73 | +- `TransactionBuilder` — builds and signs Bitcoin Cash transactions |
| 74 | +- Network providers: `ElectrumNetworkProvider`, `MockNetworkProvider` |
| 75 | +- `src/libauth-template/` — integrates with `@bitauth/libauth` for transaction evaluation and debugging |
| 76 | + |
| 77 | +### Utils (`@cashscript/utils`) |
| 78 | + |
| 79 | +Shared between compiler and SDK: |
| 80 | +- `artifact.ts` — artifact type definitions, `DebugInformation` structure |
| 81 | +- `script.ts` — bytecode optimization, opcode manipulation |
| 82 | +- `source-map.ts` — source map encoding/decoding (format: `sl:sc:el:ec:h` per opcode, `;`-separated, with field inheritance compression) |
| 83 | +- `bitauth-script.ts` — formats bytecode as human-readable BitAuth script (used in debugging) |
| 84 | +- `types.ts` — shared types including `SourceTagKind`, `SourceTagEntry` |
| 85 | +- `optimisations.ts` / `cashproof-optimisations.ts` — peephole optimization rules |
| 86 | + |
| 87 | +## Code Conventions |
| 88 | + |
| 89 | +- **ESM modules** — all packages use ES modules; file extensions required in imports (e.g., `./foo.js`) |
| 90 | +- **Max line length**: 125 characters (strings and template literals exempt) |
| 91 | +- **Explicit return types** on functions (expressions exempt) |
| 92 | +- **Keep nested code to a minimum** — use helper functions to keep code DRY and readable |
| 93 | +- Prefer writing code in a way that reads top-down, from the main entry point to the leaves. |
0 commit comments