This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is the web4 project - a NEAR blockchain HTTP gateway that allows smart contracts to serve web content directly. It enables hosting decentralized websites where frontend, backend, and blockchain logic are all managed by a single WebAssembly smart contract.
- HTTP Gateway (
app.js): Main Koa.js server that proxies HTTP requests to NEAR smart contracts - Smart Contract (
contract/assembly/): AssemblyScript contract implementingweb4_getfor serving web content - Wallet Adapter (
wallet-adapter/): Authentication flow for NEAR wallets - Domain Resolution: Maps
.near.pagedomains to NEAR account IDs
- HTTP requests to
*.near.pageor custom domains resolve to NEAR account contracts - Gateway calls
web4_getmethod on the contract with request details - Contract can return HTML, redirect to IPFS content, or request data preloading
- Authentication handled via
/web4/loginand/web4/logoutendpoints
# Build contract and deploy locally
npm run build # Build AssemblyScript contract
npm run start # Start local development server
npm run dev # Watch for contract changes and restart
# Testing
npm run test # Unit tests
npm run test:e2e # End-to-end tests
npm run test:all # All tests including fast-near variant
# Build artifacts
npm run build:website # Generate website from README
npm run build:wallet-adapter # Build wallet adapter bundlecd contract
npm run build:release # Production build
npm run build:debug # Development build with debug symbolsnpm run deploy:contract # Deploy to web4.near
npm run deploy:website # Deploy website content via web4-deploy
npm run deploy # Deploy contract onlySet these environment variables for different network configurations:
NODE_ENVorNEAR_ENV:mainnet,testnet,local,developmentCONTRACT_NAME: NEAR account ID for the contractIPFS_GATEWAY_URL: IPFS gateway URL (default: cloudflare-ipfs.com)FAST_NEAR_URL: Optional fast-near RPC endpoint for better performance
The contract must implement web4_get(request: Web4Request): Web4Response which can:
- Return HTML content directly
- Redirect to IPFS/external URLs via
bodyUrl - Request data preloading via
preloadUrls - Handle authentication via
request.accountId
- Login:
/web4/loginredirects to wallet, setsweb4_account_idcookie - Logout:
/web4/logoutclears authentication cookies - Transaction signing: POST to
/web4/contract/{account}/{method}handles wallet signing
- Direct contract response (
bodyfield) - External URL content (
bodyUrlfield) - supports IPFS and HTTP URLs - Data preloading cycle for dependent API calls
The project uses tape for unit tests and custom E2E scripts. Tests mock NEAR API responses and verify HTTP gateway behavior. Always run npm run test:all before submitting changes.