Fast withdrawals from eCash (ECX) sidechains back to the eCash main chain.
eCash is a Bitcoin-derived chain with drivechains (BIP300/301). Moving coins from a sidechain (L2) back to eCash (L1) normally means waiting for a withdrawal bundle to be approved on L1, which takes a long time. This service skips the wait: you pay it ECX on the sidechain, and it pays you ECX on L1 right away, minus a fee. The operator fronts the L1 liquidity and collects the L2 coins.
It supports the Thunder sidechain today.
- Quote. You ask for a withdrawal of some amount to your L1 address. The service returns a signed quote: an L2 address to pay, the exact amount, and a deadline.
- Pay. You pay the L2 address.
- Payout. Once the payment has enough L2 confirmations, the service sends your coins on L1.
Underpaid withdrawals aren't paid out, and can be refunded on request.
The API is Connect, so it works over gRPC and
plain HTTP/JSON. The schema is in
proto/fastwithdraw/v1/fastwithdraw.proto,
and servers expose it through reflection:
buf curl https://<host>/fastwithdraw.v1.FastWithdrawalService/GetStatusGetServiceInfo gives the fee, payout limits and the key quotes are signed
with; GetStatus says whether withdrawals are accepted right now.
flowchart LR
user([User]) -- "quote, status" --> api
subgraph operator [Operator]
api["api<br/>public"]
wallet["wallet<br/>internal"]
apidb[(Postgres)]
walletdb[(Postgres)]
quotekey{{"Quote key<br/>KMS"}}
payoutkey{{"Payout key<br/>KMS or HSM"}}
api --- apidb
api -- "signs quotes" --> quotekey
api -- "register quote, pay" --> wallet
wallet --- walletdb
wallet -- "signs payouts" --> payoutkey
end
cold{{"Cold L2 key<br/>offline"}}
api -- "watches payments" --> thunder["Thunder node<br/>(L2)"]
wallet -- "re-verifies payment" --> thunder
wallet -- "sends payout" --> esplora["Esplora<br/>(eCash L1)"]
cold -.->|"xpub only"| api
cold -.->|"sweeps L2 coins, signed offline"| thunder
- api is the only public service. It quotes withdrawals, derives a fresh L2 payment address for each from an xpub, watches the sidechain for payments, and asks the wallet to pay out.
- wallet holds the L1 hot wallet. It doesn't verify callers itself:
keep it on a private network that only the api and the scheduler reach.
It then admits the api to everything and the scheduler only to
Tick, by the identity Cloud Run IAM verified on Google Cloud, or elsewhere by a bearer token per caller (WALLET_API_TOKENandWALLET_SCHEDULER_TOKEN; the api sends its own asWALLET_TOKEN). It pays only what was registered when the quote was made, and checks the L2 payment against Thunder itself, so a compromised api can't redirect or invent payouts. Daily caps limit what it can lose. - Background work runs when a scheduler calls
Tickon each service, eg. once a minute. The api's internal methods (Tick, and listing paid indexes for sweeps) need the operator token (API_INTERNAL_TOKEN) asAuthorization: Bearer, or on Google Cloud (-gcpimages) the scheduler's ID token. - Keys never leave their KMS or HSM in production. The key that spends the collected L2 coins stays offline; only its xpub is deployed.
The services are published as images, ghcr.io/layertwo-labs/fastwithdraw-api
and ghcr.io/layertwo-labs/fastwithdraw-wallet, and run anywhere containers
do. They need Postgres, a Thunder node, an Esplora server for eCash, and a
KMS or HSM for the signing keys. The plain image tags support any
PKCS#11 HSM for the payout key; the -gcp tags
also support Google Cloud KMS for both keys and Cloud Scheduler's ID tokens
(the gcp-kms and gcp-auth build features), and the -aws tags AWS KMS
for both keys and database logins with RDS IAM auth tokens (aws-kms and
aws-rds). Behind a load balancer, set
API_CLIENT_IP_HEADER to the header it puts the client's address in, for
rate limiting: X-Client-IP (the default) as the Google Cloud reference
deployment sets it, X-Forwarded-For behind an AWS ALB.
For development, keys can be given as environment variables. Create the cold
L2 key on an offline machine with
cargo run -p fastwithdraw-thunder-keys --bin thunder-xpub -- new, which
prints the xpub the services need. Every setting is an environment variable;
see --help on each binary.
Thunder 0.19 derives different keys from the same mnemonic than 0.18 did. An xpub made before 0.19 still works, but its addresses aren't the ones Thunder 0.19's wallet shows for that mnemonic. To switch without stranding quotes already handed out:
- Pick a cutover index above every receive index in use, eg.
SELECT last_value + 1000 FROM l2_index_seq, to leave room for quotes made until the deploy. - On the offline machine:
thunder-xpub migrate CUTOVER < mnemonic.txt. Check that the legacy xpub it prints is theTHUNDER_XPUBin use now. - Set the new xpub as
THUNDER_XPUBon both the api and the wallet, and deploy both together. Indexes below the cutover keep their addresses; later ones match Thunder 0.19. The api refuses to start if the newest quote's address doesn't match, eg. because the cutover was too low.
thunder-sweep signs payments to either kind of address with the same
mnemonic.
Needs Rust, just, buf
and, for the integration tests, SoftHSM (brew install softhsm) and Docker.
just test # clippy and unit tests
docker compose up -d # Postgres for the integration tests
just test-it # integration tests against a local regtest stack
just proto # regenerate code after editing proto/On first run, the integration tests download bitcoind, the BIP300/301
enforcer and Thunder and build electrs, and run them alongside the services.
Each test gets databases of its own on the Postgres from docker compose, or
the server at FASTWITHDRAW_IT_DATABASE_URL.