This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
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.
# Install all dependencies (also runs bootstrap + build)
yarn
# Build all packages (required after code changes to propagate across packages)
yarn build
# Run all tests (uses vitest)
yarn test
# Run tests for a single package
cd packages/cashc && yarn test
cd packages/utils && yarn test
# Run a specific test by name
yarn test -t 'test name from it/describe block'
# Run a single test file directly
yarn vitest run packages/utils/test/bitauth-script.test.ts
# Lint all packages
yarn lint
# Spellcheck
yarn spellcheck
# Regenerate ANTLR parser after grammar changes (from packages/cashc)
yarn antlr
# Run cashscript tests against real chipnet instead of mocknet
TESTS_USE_CHIPNET=true yarn testYarn workspaces + Lerna with three main packages:
packages/cashc— Compiler:.cashsource → artifact JSONpackages/cashscript— SDK: load artifacts, build and send transactionspackages/utils— Shared utilities used by both compiler and SDK
The compilation flow in compiler.ts:
- Lexing/Parsing — ANTLR4 grammar (
src/grammar/CashScript.g4) → parse tree - AST Building —
AstBuilderconverts parse tree → typed AST (src/ast/) - Semantic Analysis — Three traversals in order:
SymbolTableTraversal— resolve identifiers and scopesTypeCheckTraversal— type checkingEnsureFinalRequireTraversal— validate contract structure
- Code Generation —
GenerateTargetTraversal(src/generation/) emits bytecode opcodes, source maps, console logs, requires, and source tags - Optimization —
optimiseBytecode(utils/src/script.ts) applies peephole optimizations, adjusting source maps and metadata indices - Artifact Generation — produces the final JSON artifact with bytecode, ABI, debug info
The compiler uses the visitor pattern throughout — AST nodes accept traversals defined in AstVisitor/AstTraversal.
Contract— loads compiled artifacts, instantiates with constructor argsTransactionBuilder— builds and signs Bitcoin Cash transactions- Network providers:
ElectrumNetworkProvider,MockNetworkProvider src/libauth-template/— integrates with@bitauth/libauthfor transaction evaluation and debugging
Shared between compiler and SDK:
artifact.ts— artifact type definitions,DebugInformationstructurescript.ts— bytecode optimization, opcode manipulationsource-map.ts— source map encoding/decoding (format:sl:sc:el:ec:hper opcode,;-separated, with field inheritance compression)bitauth-script.ts— formats bytecode as human-readable BitAuth script (used in debugging)types.ts— shared types includingSourceTagKind,SourceTagEntryoptimisations.ts/cashproof-optimisations.ts— peephole optimization rules
- ESM modules — all packages use ES modules; file extensions required in imports (e.g.,
./foo.js) - Max line length: 125 characters (strings and template literals exempt)
- Explicit return types on functions (expressions exempt)
- Keep nested code to a minimum — use helper functions to keep code DRY and readable
- Prefer writing code in a way that reads top-down, from the main entry point to the leaves.