Can someone ELI5 to me for Bitcoin Atomic Swaps? I'm reading up the wiki page here: https://en.bitcoin.it/wiki/Atomic_cross-chain_trading

I understand a user can create a transaction without signing it and not broadcasting it to the network. I know this is the main concept to get atomic swaps to work. But I'm a bit confused on how each step works exactly. Can someone explain each line? 

 A picks a random number x

 A creates TX1: "Pay w BTC to &lt;B's public key&gt; if (x for H(x) known and signed by B) or (signed by A &amp; B)"

 A creates TX2: "Pay w BTC from TX1 to &lt;A's public key&gt;, locked 48 hours in the future, signed by A"

 A sends TX2 to B

 B signs TX2 and returns to A

 1) A submits TX1 to the network

 B creates TX3: "Pay v alt-coins to &lt;A-public-key&gt; if (x for H(x) known and signed by A) or (signed by A &amp; B)"

 B creates TX4: "Pay v alt-coins from TX3 to &lt;B's public key&gt;, locked 24 hours in the future, signed by B"

 B sends TX4 to A

 A signs TX4 and sends back to B

 2) B submits TX3 to the network

 3) A spends TX3 giving x

 4) B spends TX1 using x

 This is atomic (with timeout).  If the process is halted, it can be reversed no matter when it is stopped.

 Before 1: Nothing public has been broadcast, so nothing happens
 Between 1 &amp; 2: A can use refund transaction after 72 hours to get his money back
 Between 2 &amp; 3: B can get refund after 24 hours.  A has 24 more hours to get his refund
 After 3: Transaction is completed by 2
 - A must spend his new coin within 24 hours or B can claim the refund and keep his coins
 - B must spend his new coin within 72 hours or A can claim the refund and keep his coins

 For safety, both should complete the process with lots of time until the deadlines.
I think this is easier to understand if you abstract a little bit. On a high level, an atomic swap is very simple: the two parties set up transactions such that they can both take their coins be revealing the preimage to some (pre-decided) hash H. Initially one party knows the preimage. They must reveal it to take their coins, so the act of taking their coins is sufficient to enable the other party to take their coins.

More specifically the Tier Nolan in the OP works as follows.


A chooses a random x and gives H(x) to B.
A sends her coins to a 2-of-2 output that can be taken by B if he reveals x. Similarly B sends his coins to a 2-of-2 output that can be taken by A if she reveals x. These are TX1 and TX3, respectively.
(Before broadcasting these, they create locktimed "refund" transactions TX2 and TX4. It's important that A's locktime is much longer than B's, so that if she gets her refund, B has had a long time to take his refund, and is not at risk of A revealing x to also take his coins.)
At this point, A takes her coins, revealing x.
Seeing this, B learns x and takes his coins.


There is a variant of this called coinswap where x does not hit the blockchain, where steps (4) and (5) are replaced by A revealing x off-chain and then the signers just give each other the coins, only using x as a fallback. This complicates the locktime structure and also requires differently-shaped transactions to keep x and its hash off the blockchain, which unfortunately make the protocol look super scary, but that's all that's going on.