Skip to content

AtomX-Bitcoin/AtomX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AtomX

AtomX is an open-source reference implementation of a trustless, peer-to-peer Bitcoin ↔ Litecoin atomic swap using Hash Time-Locked Contracts (HTLCs) on native Segwit (P2WSH) outputs. It ships three pieces:

Piece Role
@atomx/core HTLC script construction (bitcoinjs-lib), witness stacks, PSBT finalization helpers, dual-chain and single-chain in-memory “regtest” simulators, and cross-chain parameter planning (BTC vs LTC CLTV ordering).
atomx-cli simulate — full BTC + LTC simulated atomic swap (four transactions: two locks, then LTC redeem revealing preimage, then BTC redeem). walk — step-by-step narrative for initiators and followers (interactive on a TTY). relay — WebSocket hub for the GUI or manual JSON coordination.
@atomx/gui Static Vite app that connects to the relay—two parties share a session ID and exchange HTLC metadata (addresses, optional lock txid, notes). No private keys in the browser.

There is no custodial bridge and no wrapped asset: funds move only when valid HTLC witnesses hit chain within the agreed timelocks.

Product brief vs. original idea

Goal Where it lives
HTLC core (bitcoinjs-lib; Litecoin-compatible scripts — specs sometimes cite “litecoinjs-lib”) packages/core — see Tech stack
CLI: step-by-step initiate / participate / redeem npm run walkpackages/cli/src/walk.ts; npm run simulate for full scripted demo
Web GUI + WebSocket coordination npm run relay + npm run gui:devpackages/gui, relay in packages/cli/src/relay.ts
Simulated Bitcoin + Litecoin “regtest” (safe local experiment) demoDualChainAtomicSwap + tests in packages/core (in-memory ledgers, not bitcoind)

Workspace path (this environment): /root/Projects/project-structures/AtomX


Why this matters (2026 context)

Cross-chain interoperability is expanding beyond custodial bridges: ecosystem activity around trust-minimized swaps (including Bitcoin paired with other major L1s—often discussed alongside Cardano-class experiments—and ongoing HTLC / adaptor-signature research) reflects demand to move liquidity without centralized exchanges or wrapped synthetic tokens. AtomX stays intentionally narrow—BTC ↔ LTC—because Litecoin shares Bitcoin’s UTXO script model, making it the standard teaching and integration-test analogue next to Bitcoin itself.


Tech stack: bitcoinjs-lib and “litecoinjs-lib”

Educational specs sometimes cite litecoinjs-lib alongside bitcoinjs-lib. There is no widely maintained npm package named litecoinjs-lib. Litecoin HTLCs use the same Script VM as Bitcoin for this pattern; AtomX implements Litecoin outputs by combining bitcoinjs-lib with an explicit litecoinRegtest Network (version bytes / bech32 prefix). Older standalone packages such as litecoinjs exist on npm but are unmaintained—this repo does not depend on them.


Architecture

HTLC script (Segwit)

The locking redeem script follows the standard branch layout:

  • IF path: SHA256(preimage) == hash and redeemer signature (counterparty reveals secret).
  • ELSE path: CHECKLOCKTIMEVERIFY at refund height/time and funder signature.

Witness ordering for P2WSH is handled in core (witnessStackForRedeem / witnessStackForRefund) and wired through PSBT finalization (finalizeHtlcRedeemInput).

Cross-chain safety ordering

For Alice swapping BTC for Bob’s LTC (Alice locks BTC first in the canonical story), Litecoin’s refund CLTV must unlock strictly before Bitcoin’s refund CLTV. Otherwise Bob could stall until Alice’s patience expires on one chain while extracting value on the other. planBtcLtcSwap enforces ltcRefund < btcRefund.

Simulated mode (Bitcoin + Litecoin regtest objects)

The simulator does not connect to bitcoind/litecoind. It maintains separate in-memory ledgers (ChainSimulator), mints synthetic UTXOs, builds real bitcoinjs-lib transactions and witnesses, and runs a full dual-chain story: BTC lock → LTC lock → LTC redeem (preimage revealed) → BTC redeem. An additional single-chain smoke test exercises one isolated Bitcoin ledger. Connecting to real regtest daemons is left as an operational extension.


Requirements

  • Node.js ≥ 20

Install & build

cd AtomX
npm install
npm run build

Commands

Dual-chain simulation + smoke test

npm run simulate
# or
node packages/cli/dist/index.js simulate

Prints four main-net-style tx ids (BTC lock, LTC lock, LTC redeem, BTC redeem), secret hash, HTLC addresses, CLTV heights, plus an extra single-chain HTLC demo on a fresh simulator.

Interactive walkthrough (atomx-cli walk)

Explains the swap phases (lock ordering, preimage handling, claim order). On a TTY, prompts once then runs the same dual-chain simulation and prints tx ids. In CI/non-interactive environments, prints the script and exits without hanging.

npm run walk

WebSocket relay (for the GUI or any two peers)

npm run relay
# default bind: ws://127.0.0.1:8765

Environment overrides:

  • ATOMX_RELAY_HOST (default 127.0.0.1)
  • ATOMX_RELAY_PORT (default 8765)

Custom port:

node packages/cli/dist/index.js relay --port 9000

Browser GUI (development)

npm run gui:dev

Open two tabs/windows, use the same session ID, choose complementary roles, connect both to the relay, then exchange HTLC metadata. Never paste private keys or mnemonics.

Production assets:

npm run gui:build
# static output: packages/gui/dist

Serve packages/gui/dist with any static file server over HTTPS in production; use wss:// for the relay.


Testing

npm test

Builds core → cli → gui, then runs Node’s test runner (via tsx) for @atomx/core and @atomx/cli (including CLI subprocess smoke tests).


Package layout

AtomX/
  packages/
    core/      # HTLC + simulators + redeem helpers
    cli/       # atomx-cli binary
    gui/       # Vite static client

Security & limitations

  • Reference/educational software—audit before any mainnet use.
  • Simulator ≠ production: no fee markets, no mempool policy, no P2P relay—only script/transaction validity exercises on synthetic UTXOs.
  • GUI relay is not authenticated: run locally or behind TLS + auth for real deployments.
  • Keys: simulation keys are ephemeral; the GUI never handles mainnet wallets.

License

MIT (project-wide; verify individual dependencies at install time).

Contact Info

Telegram: @AuraTerminal

About

AtomX – Open‑source reference implementation of trustless Bitcoin ↔ Litecoin atomic swaps using HTLCs on native Segwit (P2WSH). TypeScript monorepo with core library, CLI (simulate / walk / relay), and WebSocket GUI. No custodial bridge, no wrapped assets.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors