Atomic swaps are processes by which two parties exchange coins using hashed time-locked puzzles, such that no actions by a dishonest party could cause an honest party to end up, at the end of the transaction, in control of coins of fewer value than those which they started with at the beginning of the transaction. In an atomic swap, each party creates a coin in the amount to be exchanged, and the coins are locked both by a hash h = H(s) whose preimage s serves as a key, and by a time limit t. Each coin can be spent in one of two ways: either the coin is spent to the intended recipient’s wallet if that recipient provides the secret s, or the coin is spent to the coin’s creator’s wallet after time t has passed.
Alice and Bob agree to swap coins. Alice will send X1 Chia to Bob, and Bob will send X2 Chia to Alice. One party must arbitrarily act as the swap initiator.
Alice initiates the swap by creating a coin C1 whose value is X1 Chia and whose timeout time is at block height NOW1+B, which is B blocks away from the current block height NOW1. C1 is Alice's "outgoing coin" and Bob's "incoming coin".
Bob adds the swap to his swap list while creating a reciprocal coin C2 whose value is X2 Chia and whose timeout time is at block height NOW2+(B/2), which is B/2 blocks away from the current block height NOW2. C2 is Bob's "outgoing coin" and Alice's "incoming coin". Because NOW1 and NOW2 may be different (and in the case of our impletation are always different), Bob also sets a buffer time BUF which ensures that the timeout time of C2 will occur before the timeout time of C1. The buffer may be between 1 and (B/2)-1 blocks, but otherwise the value of BUF is left to the discretion of Bob. When Bob creates C2, a check is performed to ensure that NOW1+B is greater than or equal to NOW2+(B/2)+ BUF.
C1 and C2 are committed to the blockchain when the next block is farmed.
In the standard case, Alice will spend her incoming coin (C2) to her wallet using the secret s, which Alice used to lock her outgoing coin (C1). When Alice does this and the spend is posted to the blockchain, s becomes public knowledge. Bob then uses s to spend his incoming coin (C1) to his wallet.
In the case that Alice fails to spend C2 to her wallet before C2's timeout time has elapsed (i.e. when block height NOW2+(B/2) is reached), Bob may spend C2 back to his wallet. Because Bob does not have the secret s, he is unable to spend C1 to his wallet. After C1's timeout time has elapsed (i.e. when block height NOW1+B is reached), Alice may spend C1 back to her wallet.
Run a version of ledger-sim
in a background terminal window.
-
Run Ledger-Sim
- Open a terminal window.
- Run
$ . .venv/bin/activate
- Run
$ ledger-sim
- 1 Wallet Details / Generate Puzzlehash
- 2 View Funds
- 3 View Contacts
- 4 Add Contacts
- 5 Edit Contacts
- 6 View Current Atomic Swaps
- 7 Initiate Atomic Swap
- 8 Add Atomic Swap
- 9 Redeem Atomic Swap Coin
- 10 Get Update
- 11 GOD MODE Farm Block / Get Money
- q Quit
Terminal 1 represents Alice's wallet, and Terminal 2 represents Bob's wallet. Alice sends 1000 Chia to Bob, and Bob sends 3000 Chia to Alice.
-
Run Atomic Swap Wallets
- Open two terminal windows.
- In each window run
$ . .venv/bin/activate
- In each window run
$ as_wallet
-
Get Chia
- Terminal 1 (Alice's wallet)
- Type "11" and press enter. (This gives Alice 1 billion Chia.)
- Terminal 2 (Bob's wallet)
- Type "11" and press enter. (This gives Bob 1 billion Chia.)
- Terminal 1 (Alice's wallet)
-
Atomic Swap
- Terminal 1 (Alice's wallet, which initializes the swap)
- Type "7" and press enter.
- Type "Bob" and press enter.
- Terminal 2 (Bob's wallet, which adds the swap)
- Type "8" and press enter.
- Type "Alice" and press enter.
- Terminal 1
- Copy the PubKey from Terminal 1 and then paste it to Terminal 2 and press enter. Go back to Terminal 1 and press enter.
- Terminal 2
- Copy the PubKey from Terminal 2 and then paste it to Terminal 1 and press enter. Go back to Terminal 2 and press enter.
- Terminal 1
- Type "1000" and press enter. (This sets 1000 Chia as Alice's outgoing amount.)
- Terminal 2
- Type "3000" and press enter. (This sets 3000 Chia as Bob's outgoing amount.)
- Terminal 1
- Type "3000" and press enter. (This sets 3000 Chia as Alice's incoming amount.)
- Terminal 2
- Type "1000" and press enter. (This sets 1000 Chia as Bob's incoming amount.)
- Terminal 1
- Copy the hash of the secret from Terminal 1 and then paste it to Terminal 2 and press enter in Terminal 2. Go back to Terminal 1 and press enter.
- Type "10" and press enter. (This sets 10 blocks as the timelock for Alice's outgoing coin [Bob's incoming coin] and consequently sets 10/2=5 blocks as the timelock for Alice's incoming coin [Bob's outgoing coin].)
- Terminal 2
- Type "10" and press enter. (This sets 10 blocks as the timelock for Bob's incoming coin [Alice's outgoing coin] and consequently sets 10/2=5 blocks as the timelock for Bob's outgoing coin [Alice's incoming coin].)
- Type "4" and press enter. (The sets 4 blocks as the minimum buffer time between the timeout blocks of Bob's incoming and outgoing coins.)
- Terminal 1
- Copy the puzzlehash from Terminal 1 and then paste it to Terminal 2 and press enter. Go back to Terminal 1 and press enter. (In our toy model, Alice's wallet automatically farms a block and receives a block reward at this time in order to commit C1 to the blockchain.)
- Terminal 2
- Copy the puzzlehash from Terminal 2 and then paste it to Terminal 1 and press enter. Go back to Terminal 2 and press enter. (In our toy model, Bob's wallet automatically farms a block and receives a block reward at this time in order to commit C2 to the blockchain.)
- Terminal 1 (Alice's wallet, which initializes the swap)
-
View Swap Info
- Terminal 1
- Type "2" and press enter. (This views the available funds in Alice's wallet. The pending atomic swap coins are marked with an asterisk (*).)
- Terminal 2
- Type "2" and press enter. (This views the available funds in Bob's wallet. The pending atomic swap coins are marked with an asterisk (*).)
- Terminal 1
- Type "3" and press enter.
- Type "Bob" and press enter. (This views the atomic swap puzzlehashes associated with the Alice's contact "Bob".)
- Type "menu" and press enter.
- Terminal 2
- Type "3" and press enter.
- Type "Alice" and press enter. (This views the atomic swap puzzlehashes associated with the Bob's contact "Alice".)
- Type "menu" and press enter.
- Terminal 1
- Type "6" and press enter. (This views the atomic swaps in which Alice is currently participating.)
- Terminal 2
- Type "6" and press enter. (This views the atomic swaps in which Bob is currently participating.)
- Terminal 1
-
Spend Coins
- Terminal 1
- Type "9" and press enter.
- Copy the value of "Atomic swap incoming puzzlehash" from Terminal 1 and paste it into Terminal 1. Press enter.
- Type "y" and press enter. (This uses Alice's stored secret to spend Alice's incoming coin.)
- Type "11" and press enter. (This farms a new block and includes Alice's spend in that block. At this moment, the secret is revealed publicly and Bob can use it to spend Bob's incoming coin.)
- Terminal 2
- Type "10" and press enter. (This updates the latest block information. Bob's wallet pulls from the blockchain the secret from the solution Alice used to spend Alice's incoming coin.)
- Type "9" and press enter.
- Copy the value of "Atomic swap incoming puzzlehash" from Terminal 2 and paste it to Terminal 2. Press enter.
- Type "y" and press enter.
- type "11" and press enter (This farms a new block and includes Bob's spend in that block.)
- Terminal 1
- type "10" and press enter (Updates lates block info)
- Terminal 1
-
View Swap Info
- Terminal 1
- Type "2" and press enter. (This views the available funds in Alice's wallet. The pending atomic swap coins are gone, and the coin created by Alice's incoming atomic swap coin is there.)
- Terminal 2
- Type "2" and press enter. (This views the available funds in Bob's wallet. The pending atomic swap coins are gone, and the coin created by Bob's incoming atomic swap coin is there.)
- Terminal 1
- Type "3" and press enter.
- Type "Bob" and press enter. (This views the atomic swap puzzlehashes associated with the Alice's contact "Bob". Alice has no current atomic swap puzzlehashes associated with "Bob".)
- Type "menu" and press enter.
- Terminal 2
- Type "3" and press enter.
- Type "Alice" and press enter. (This views the atomic swap puzzlehashes associated with the Bob's contact "Alice". Bob has no current atomic swap puzzlehashes associated with "Alice".)
- Type "menu" and press enter.
- Terminal 1
- Type "6" and press enter. (This views the atomic swaps in which Alice is currently participating. Alice is no longer participating in any atomic swaps.)
- Terminal 2
- Type "6" and press enter. (This views the atomic swaps in which Bob is currently participating. Bob is no longer participating in any atomic swaps.)
- Terminal 1