Skip to main content

This project defines a protocol for trustlessly swapping XCH and BTC using bitcoin lightning network payments.

Requirements

  • Chia Wallet (XCH)
  • Bitcoin lightning wallet satisfying the following:
    • supports viewing the pre-image receipt (if selling BTC)
    • lightning payment requests that are valid for at least five minutes (if selling XCH) to give the XCH time to confirm.

Wallets Tested

Lightning:

  • Muun confirmed on both iOS and Android to work in both directions
  • BlueWallet confirmed not working, as BlueWallet to BlueWallet payments do not reveal the pre-image needed to sweep the XCH funds. You can use BlueWallet if you're selling BTC for XCH, but not the other way unless you are 100% sure your counterparty is not also using BlueWallet.
  • Strike confirmed not working. There's no way to see the receipt pre-image, and the 57s timeout for payment requests is too low.

XCH:

  • Nucle has been confirmed to work in both directions.
  • The official Chia wallet has been confirmed to work in both directions.

Install

Create and activate a virtualenv (first two lines), then install.

$ python3 -m venv venv
$ source venv/bin/activate
$ pip install chiaswap

Use

Find a counterparty and negotiate the exchange quantities. Run the script.

$ xchswap

Answer the questions.

If you are claiming XCH (either through clawback for a failed swap or on the XCH receiving end of a completed swap), you will be given a SpendBundle hex dump and the option to push it to the network.

You can push a SpendBundle to the network manually using the pushtx tool.

$ source venv/bin/activate  # if you're not in the venv yet
$ pushtx 000000012...(lots of SpendBundle hex)...f4

How It Works

A lightning network payment request includes a pre-image of a secret value which is revealed atomically when the request is paid. Essentially, you're buying the pre-image of a secret.

If this same secret is also bound to another asset (in this case, XCH) you can then take that pre-image to claim the other asset. This is also how bitcoin submarine swaps work.

Custom ChiaLisp

A custom puzzle p2_delayed_or_preimage acts as an extension of p2_delegated_puzzle_or_hidden_puzzle. It takes two public keys: one for each counterparty. It commits to the hash of the pre-image, both public keys, and a clawback timeout (24 hours for now).

There are two payment paths in p2_delayed_or_preimage:

  • after the clawback timeout using the XCH holder's key
  • with the pre-image reveal using the BTC holder's key

Each of these uses p2_delegated_puzzle_or_hidden_puzzle internally.

The protocol locks XCH into a standard p2_delegated_puzzle_or_hidden_puzzle puzzle with p2_delayed_or_preimage as the hidden puzzle. The delegated puzzle is tied to the sum of the two participants public key, so one party can grant the XCH to the other by revealing their private key, allowing the other to make a spend that looks just like a "standard" chia spend.

Protocol Summary

We call the participants XCH and BTC

  • XCH has chia and wants bitcoin
  • BTC has bitcoin and wants chia
  1. XCH & BTC create disposable private keys, and exchange corresponding signed public keys. They are signed to prove the private key is held; otherwise, the second to reveal a pubkey could do a subtraction attack.

  2. XCH generates and shares a lightning invoice. The lightning invoice contains the hash of a secret. That value, along with the public keys and the clawback timeout (defaulting to 24 hours) is used to generate a chia address.

  3. XCH sends coins to the chia address.

  • if BTC decides not to go through with the transaction, instead of paying the lightning invoice, BTC can share the disposable private key to XCH can claw back the funds immediately without waiting for the timeout.

  • if BTC vanishes, XCH can claw back these coins after the clawback timeout.

  1. BTC waits for sufficient confirmations of the XCH coin, then pays the lightning invoice, revealing the secret. This secret can be used to claim the coins if XCH vanishes, so step 5 is optional.

  2. XCH sees the invoice has been paid, and shares the disposable private key corresponding to his public key. BTC uses this key along with their own to claim the XCH using the "clean" case, which is indistinguishable from a standard XCH spend.

Open Questions

Is a 24 hour clawback timeout enough time? Is it too much time? What do bitcoin lightning payment failure cases look like?

Metadata

Release files for chiaswap 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chiaswap 0.1.3
File Size Uploaded Provenance Actions
chiaswap-0.1.3.tar.gz 14.6 kB Without provenance Details for chiaswap-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chiaswap 0.1.3
File Interpreter ABI Platform Actions
chiaswap-0.1.3-py3-none-any.whl Python 3 none any Details for chiaswap-0.1.3-py3-none-any.whl

Total release size: 30.5 kB

Release files / chiaswap-0.1.3.tar.gz

Download URL chiaswap-0.1.3.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
43b775c1d9c08e730126008da573e6f227a22d339be9a3d1c71eea3e5ffebeb0
BLAKE2b-256 checksum
How to use checksums
01608e101891ecbdbc001dfe5898ebc3cc225713f386daa88a6d140f52c6d21b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / chiaswap-0.1.3-py3-none-any.whl

Download URL chiaswap-0.1.3-py3-none-any.whl
Size 16.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48cb5c6e10bd0501a8584d6f984c0221a1d4741d22de24d20d2c35053cb77e74
BLAKE2b-256 checksum
How to use checksums
ac0952d897f32223245500905b73355d7e7e4854c3454fb14612928615317e1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for chiaswap 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page