Skip to main content

Adding Non-Custodial Bitcoin Swaps to a Wallet You Already Run

Oliver Bennett 7 min read

Every new partner opens with the same question: who holds the keys during a non-custodial swap? The user does, the whole way through. No private key material reaches our servers at any point. That part takes a sentence to explain. The question that eats most of the integration effort is state: how your app follows a swap that spans several transactions, two blockchains, and 30 to 60 minutes of wall-clock time.

What Non-Custody Changes in Your Stack

Custodial infrastructure takes the assets, runs the swap, and credits the result. Key handling, chain interactions, state bookkeeping all live inside the custodian. Your integration is little more than request and response.

Non-custodial infrastructure inverts that. Signing happens locally in the user's client, using the user's own keys. The protocol sets swap parameters, finds liquidity, and watches chain state, yet it never holds the funds. Your client therefore has to be able to sign Bitcoin transactions and the matching transactions on the other chain.

For a wallet team, that is probably already true. You sign Bitcoin transactions today; cross-chain support adds the HTLC-specific transaction types to your signing logic, which the SDK covers, along with state tracking in your application layer, which needs deliberate design.

For a platform that has never held key material, the decision is architectural rather than incidental. You are introducing key management to your stack. Treat that as its own project with its own review, not as a footnote to the integration.

The Swap Lifecycle as Your App Sees It

Stripped of the chain-level mechanics the protocol handles, a swap moves through your application in this order.

Someone requests a swap with an amount, a source chain, and a destination. Your app hits the quote endpoint, gets back a rate and an estimated settlement window, and the user confirms. A call to the initiate endpoint creates the swap and returns a swap ID plus the HTLC lock parameters.

Your app then prompts the user to sign and broadcast the initiator lock on the source chain. The protocol sees the lock enter the mempool and tracks confirmations. At the required depth, it matches a liquidity provider who locks counterparty funds on the destination chain. The protocol then orchestrates the preimage reveal, and your app receives a status update: preimage revealed, claim transaction pending.

The user signs and broadcasts the claim on the destination chain. SETTLEMENT_COMPLETE marks the swap finished.

Every step carries a transaction your app handles, a state it tracks, and a status it shows. The work is finite, though: the number of states and transitions is fixed, and the SDK implements the machine for you.

States Your App Will See

The state machine runs through these values: QUOTE_ACCEPTED, INITIATOR_LOCK_PENDING, INITIATOR_LOCK_CONFIRMED, COUNTERPARTY_LOCK_PENDING, COUNTERPARTY_LOCK_CONFIRMED, PREIMAGE_REVEALED, CLAIM_PENDING, SETTLEMENT_COMPLETE, TIMEOUT_PENDING, and REFUND_COMPLETE.

Your app has to walk the transitions and map each to a sensible piece of interface. The state endpoint is one HTTP GET by swap ID; it returns the current state plus whatever parameters matter at that point: estimated completion, next action required, transaction IDs on both chains. Poll it, or subscribe over WebSocket for live updates.

Two states demand action from your app rather than patience. INITIATOR_LOCK_PENDING needs the lock broadcast. CLAIM_PENDING needs the claim broadcast. Everything else waits on chain confirmation or protocol coordination.

Typed transaction builders in the SDK cover Bitcoin and EVM chains and produce the correct HTLC structure for each state. Your signing logic receives a transaction object, the user signs it, and the signed result goes to the broadcast endpoint. Script construction, preimage handling, and timeout embedding all stay inside the SDK.

Refund Paths and Timeouts

A swap drifts into the timeout path in two situations: the initiator lock never confirms in time, or the counterparty lock lands but the preimage reveal does not finish before expiry. Both end in a refund rather than a completed swap.

The two cases are not equivalent for your app. A timed-out initiator lock means the user's funds were never locked, so there is nothing to send back. A counterparty lock that lands before a mid-protocol failure strands the user's funds in the initiator HTLC, and recovering them takes a refund transaction.

The state machine names both paths. TIMEOUT_PENDING signals that a refund is required. The SDK ships a refund builder shaped exactly like the claim builder. Your app asks the user to sign and broadcast, and REFUND_COMPLETE confirms the money is back.

One practical rule: do not wait for expiry itself before showing TIMEOUT_PENDING. The machine flips to that state slightly ahead of the timelock, which leaves your app room to build and broadcast a refund with enough fee headroom to confirm inside the window. Apps that only check state when the user touches the screen need background polling, because the transition will often arrive while nobody is looking at the session.

Swaps Outlive Sessions

Thirty to sixty minutes is long enough for a session to die. The user starts a BTC-to-ETH swap, your app confirms the initiator lock went out, then the browser closes or the process is killed.

When the user comes back, your app has to pick the swap up where it stopped. The swap ID is the handle. Persist it in durable storage and you can hit the state endpoint on resume, read the current state, and draw the right screen.

The usual failure is storing the swap ID in memory only. The session ends, the ID evaporates, the user returns to a UI showing no active swap while their funds sit locked in an HTLC on-chain. Without the swap ID, recovery becomes a manual support job.

Do this instead: persist the swap ID the instant QUOTE_ACCEPTED comes back, before the user takes any on-chain step. Write it to durable storage. On every session start, look up active swap IDs and restore their status view before the main interface appears. The state machine can resume from every scenario it can enter; your app only has to supply the ID.

What the TypeScript SDK Handles

The TypeScript SDK covers what an application needs to run the swap lifecycle without hand-building HTLC transactions or chain-specific scripts. It offers quote generation, swap initiation, transaction builders for initiator lock, claim, and refund on each supported chain, helpers for state polling and WebSocket subscription, and local state serialisation for session persistence.

It holds no private keys and signs nothing. Signing stays with your application. The SDK hands back unsigned transaction objects; your existing infrastructure signs them. Because of that split, the same SDK fits browser wallets, React Native apps, and server-side Node code without touching your key management design.

A few things sit outside its scope: custom timeout configuration, overriding liquidity provider selection, and chain support beyond the current set. Those are configuration decisions exposed through the API to Builder and Protocol tier accounts. If you need to override default confirmation depths or limit chain selection, the API documentation covers the configuration endpoints the SDK wraps.

Back to Protocol Notes

Explore the SDK

The TypeScript SDK and testnet work on every plan, and testnet access needs no approval.