This is a Cloudflare Workers project that implements various services for Go Native. It uses Bun for JavaScript and Typescript runtime and package management (instead of Nodejs + npm).
The content is organized into a Bun workspace. See @README.md for:
- High-level architecture and component interactions
- Detailed setup instructions
- Functional flow documentation
- Contributing guidelines
All packages are in the ./packages directory:
| Package | Type | Purpose |
|---|---|---|
btcindexer |
Service (Worker) | Bitcoin-to-Sui bridging and minting |
sui-indexer |
Service (Worker) | Sui blockchain monitoring and redemption |
block-ingestor |
Service (Worker) | Receives Bitcoin blocks via REST API |
compliance |
Service (Worker) | Sanctions and geo-blocking data |
lib |
Shared Library | Common utilities and types |
- Bun
- Cloudflare Workers
- TypeScript
- Cloudflare D1 (SQL Database)
- Cloudflare KV (Key-Value Storage)
- Cloudflare Service Bindings
- Cloudflare Queue
- BitcoinJS Library
- Sui Blockchain integration
- IKA MPC integration
See @README.md for full setup instructions.
Quick commands:
# Install dependencies
bun install
# Run all tests
bun run test
# Run with verbose logging
ENABLE_LOGS=1 bun run test
# Run tests for a specific package
cd packages/<package-name> && bun run test
# Type checking
cd packages/<package-name> && bun run typecheck
# Run a worker locally
cd packages/<package-name> && bun run devWorkers communicate via Cloudflare Service Bindings using RPC interfaces. Each service exports:
RPC- Production RPC interfaceRPCMock- Mock implementation for testing (where applicable)
Service bindings are configured in wrangler.jsonc files:
// Access bound service from environment
const result = await env.SuiIndexer.someMethod(params);See @README.md for Cloudflare RPC documentation and examples.
Location: ./packages/btcindexer
src/index.ts- Main entry with HTTP handlers and scheduled cronsrc/btcindexer.ts- Bitcoin indexing and deposit detection logicsrc/router.ts- HTTP API endpointssrc/sui_client.ts- Sui blockchain integration for mintingsrc/cf-storage.ts- Cloudflare D1/KV storage layersrc/bitcoin-merkle-tree.ts- Merkle proof generation
- Bitcoin Block Processing: Cron job runs every minute to scan blocks, identify nBTC deposits via OP_RETURN outputs
- Cross-Chain Minting: Tracks deposits and mints corresponding nBTC on Sui with Merkle proof validation
- Data Storage: Uses D1 for transaction data, KV for block storage
- Queue Consumption: Consumes blocks from
block-queuepopulated by block-ingestor
- Cron: Every minute (
* * * * *) - D1 Database:
btcindexer-dev - KV Namespaces:
BtcBlocks,nbtc_txs - Queue Consumer:
block-queue - Service Bindings:
SuiIndexer,Compliance - Secrets:
NBTC_MINTING_SIGNER_MNEMONIC(via Secrets Store)
See migration files in packages/btcindexer/db/migrations/:
btc_blocks- Block trackingnbtc_minting- Deposit transactionsnbtc_deposit_addresses- Deposit addressesnbtc_utxos- UTXO tracking (states: available, locked, spent)nbtc_redeem_requests- Redemption requestsnbtc_redeem_solutions- Redemption solutionsindexer_state- Cursor statepresign_objects- IKA presign objects
Location: ./packages/sui-indexer
src/index.ts- Entry point with scheduled tasksrc/processor.ts- Sui event indexingsrc/redeem-service.ts- Redemption processing logicsrc/redeem-sui-client.ts- Sui client for redemptionsrc/ika_client.ts- IKA MPC integrationsrc/storage.ts- D1 storage layersrc/sighash.ts- Bitcoin sighash calculations
The Sui Indexer integrates with IKA (MPC service) for threshold signature operations:
- Uses
@ika.xyz/sdkfor MPC communication - Manages presign objects for Bitcoin transaction signing
- Implements coin selection logic for redemption transactions
- Event Monitoring: Indexes Sui events for nBTC redemption requests
- Redemption Processing: Handles burn-and-redeem flow with IKA MPC
- UTXO Management: Manages UTXO lifecycle (available → locked → spent)
- Cron: Every minute (
* * * * *) - D1 Database: Shared
btcindexer-dev - Service Binding:
BtcIndexer - Environment Variables:
UTXO_LOCK_TIME(1 hour),REDEEM_DURATION_MS(5 min) - Secrets:
NBTC_MINTING_SIGNER_MNEMONIC
Location: ./packages/block-ingestor
See @packages/block-ingestor/README.md for detailed architecture.
src/index.ts- HTTP router and handlerssrc/ingest.ts- Block ingestion logicsrc/api/put-blocks.ts- msgpack encoding/decodingsrc/api/client.ts- Client for sending blocks
Receives Bitcoin blocks via REST API, validates them, and enqueues to block-queue for processing by BTCIndexer.
- KV Namespace:
BtcBlocks(shared with btcindexer) - Queue Producer:
block-queue - Service Binding:
BtcIndexer
Location: ./packages/compliance
src/index.ts- Scheduled worker entry pointsrc/sanction.ts- Sanctions list updating logicsrc/storage.ts- D1 storage for sanctionssrc/rpc.ts- RPC interface for other services to query compliance data
- Sanctions List Updates: Daily cron job fetches and updates sanctions data
- Compliance API: Exposes RPC methods for other services to check addresses
- Geo-blocking: Supports geo-blocking rules
- Cron: Daily at 1am (
0 1 * * *) - D1 Database:
compliance
See migration files in packages/compliance/db/migrations/.
Location: ./packages/lib
Shared library package containing utilities used across all services:
src/logger.ts- Structured JSON loggingsrc/nbtc.ts- Bitcoin network types (BtcNetenum,BlockQueueRecord,BitcoinTxStatus)src/nsui.ts- Sui network types (SuiNet,NbtcPkg)src/setups.ts- Environment-specific configurationssrc/coin-ops.ts- IKA coin selection logicsrc/auth.ts- Authorization utilitiessrc/secrets.ts- Secrets retrieval from Secrets Storesrc/rpc-types.ts- Shared RPC type definitionssrc/test-helpers/- Test utilities including D1 initialization
- Shared Types: Network enums, block records, transaction status types
- Testing Support: Mock RPC implementations and D1 test helpers
- Utilities: Logging, delays, key generation
- TypeScript with strict type checking - run
bun run typecheck - ESLint and Prettier - run
bun run format - Comprehensive type definitions required
- Framework: Bun's built-in test framework with Miniflare for mocking Workers
- Mock RPC: Each service provides
RPCMockimplementation for isolated testing - Test Data: Real Bitcoin regtest blocks from https://learnmeabitcoin.com/explorer/
- Test Helpers: Located in
packages/lib/src/test-helpers/
wrangler.jsonc- Development configurationwrangler-prod.jsonc- Production configuration.dev.vars- Local environment variables- Environment-specific setups in
packages/lib/src/setups.ts
Sensitive data (mnemonics, API keys) is stored using Cloudflare Secrets Store:
# Bind secrets store in wrangler.jsonc
"secrets_store_stubs": [
{
"binding": "SECRETS_STORE",
"store_id": "your-store-id",
"preview_store_id": "your-preview-store-id"
}
]Access via packages/lib/src/secrets.ts.
UTXOs progress through states managed by the Sui Indexer:
- Available - UTXO is ready for use in redemption
- Locked - UTXO reserved for a pending redemption (time-limited via
UTXO_LOCK_TIME) - Spent - UTXO has been used in a redemption transaction
Run make setup-hooks to install pre-commit hooks for code quality.
Regenerate using tree --gitignore.
├── .git/
├── api/
│ └── btcindexer/
├── contrib/
│ └── git-hooks/
├── node_modules/
├── packages/
│ ├── btcindexer/
│ ├── sui-indexer/
│ ├── block-ingestor/
│ ├── compliance/
│ └── lib/
├── .editorconfig
├── .gitignore
├── .markdownlint.yml
├── .prettierignore
├── .sourcery.yaml
├── bun.lock
├── eslint.config.mjs
├── LICENSE
├── Makefile
├── package.json
├── README.md
├── readme.org
├── tsconfig.json
├── wrangler.jsonc
└── wrangler-prod.jsonc