DEV Community

Cover image for Trustless Bitcoin Swaps with DID, Nostr, and HTLCs
Melvin Carvalho
Melvin Carvalho

Posted on

Trustless Bitcoin Swaps with DID, Nostr, and HTLCs

๐Ÿค” The Problem

Say Alice wants to send 100,000 sats to Bob.

But:

  • Alice only has on-chain Bitcoin.
  • Bob only uses the Lightning Network.

They want to swap โ€” but without using a centralized exchange or trusted custodian.

Can they do it trustlessly?

Yes โ€” with:

  • DIDs (Decentralized Identifiers)
  • Nostr (a decentralized messaging protocol)
  • Bitcoin + Lightning
  • HTLCs (Hashed Time-Locked Contracts)

๐Ÿ”— A Simple Use Case

Letโ€™s walk through a real-world example that almost anyone can understand:


๐Ÿ’ก Scenario: On-chain to Lightning Swap

  1. Alice wants to pay 100k sats on-chain.
  2. Bob wants to receive 100k sats on Lightning.
  3. They donโ€™t trust each other and want to swap trustlessly.

๐Ÿงฉ Enter DID + Nostr + HTLCs

โœ… 1. Identity via DIDs on Nostr

  • Both Alice and Bob have DIDs tied to their Nostr pubkeys.
  • DIDs make it possible to verify identities and sign messages.
  • Alice posts a signed message to Nostr offering the swap.
{
  "type": "swap-offer",
  "from": "did:nostr:pubkeyalice",
  "to": "any",
  "amount": 100000,
  "direction": "onchain-to-lightning",
  "hashlock": "abcdef123456...",
  "expires_in": 3600
}
Enter fullscreen mode Exit fullscreen mode

Bob sees it and replies with a signed acceptance.


๐Ÿ“ก 2. Coordinating via Nostr

  • The offer and acceptance are broadcast as ephemeral Nostr events.
  • Both parties agree on:
    • The amount
    • The hashlock
    • Timeouts
    • Where to send funds

No servers, no intermediaries โ€” just pubkey-signed messages over a decentralized relay network.


๐Ÿ” 3. The HTLC Swap

They now execute the swap using the same secret hash:

Alice (on-chain):

  • Sends 100k sats to a Bitcoin HTLC (can only be claimed with the secret before timeout).

Bob (Lightning):

  • Sends 100k sats to a Lightning HTLC (also locked with the same hash).

As soon as one party claims the funds, the preimage is revealed, and the other can claim theirs too.


โœ… 4. Atomic Swap Complete

  • Bob claims the on-chain BTC โ†’ reveals the secret.
  • Alice uses the same secret to claim her Lightning BTC.
  • No one can cheat โ€” itโ€™s enforced by the HTLCs and timeouts.

โœจ Why This Matters

This setup gives us:

โœ… Trustless swaps

โœ… No custodians or centralized services

โœ… Cross-layer interoperability

โœ… Identity-based coordination via Nostr DIDs

And it all works with:

  • Bitcoin
  • Lightning
  • DID documents
  • Open message relays

๐Ÿ”ฎ Whatโ€™s Next?

  • Add wallet support for DID-authenticated swap offers over Nostr.
  • Explore multi-party or ring swaps.

๐Ÿ”— TL;DR

DID + Nostr + HTLCs = trustless swaps across Bitcoin and Lightning, with identity and coordination baked in.

No middleman. Just keys, hashes, and the open web.

Top comments (0)