# Atomic Swap
[TOC]
## Ref
1. https://www.binance.vision/blockchain/atomic-swaps-explained
## Tech
Scenario:
Alice -> Bob: 1 BTC
Bob -> Alice: 9000 USDT
https://tinyurl.com/t7emy3a
```mermaid
sequenceDiagram
rect rgb(233, 249, 210)
Alice ->> +Randomness/Blockchains: random()
Randomness/Blockchains-->> -Alice: S = 5KtaLuhRmQD6ra5X
Alice ->> Alice: H=Hash(S)
Alice ->> Bob: [H, PublicKey(Alice)]
Bob -->> Alice: PublickKey(Bob)
end
Alice ->> Alice: Build TrxA: <br> [output-amount] = 1 BTC <br> [output-condition] = <br> (Hash(S) == H and Signautre(Bob)) or <br> (48hr and Siganture(Alice))
Bob ->> Bob: Build TrxB: <br> [output-amount] = 9K USDT <br> [output-condition] = <br> (Hash(S) == H and Signautre(Alice)) or <br> (24hr and Siganture(Bob))
Note left of Bob: If Bob broadcasts <br> his contract trx now, <br> Alice can take the $$ <br> and run away!
Alice ->> Randomness/Blockchains: Broadcast trxA
Bob ->> Bob: Create trxA by himself to verify <br> on-chain trxA
Randomness/Blockchains -> Bob: trxA is confirmed
Bob -->> Randomness/Blockchains: broadcast trxB
Randomness/Blockchains -> Alice: trxB is confirmed
rect rgb(233, 249, 210)
Alice ->> +Randomness/Blockchains: trxSwap(S): spends trxB
Randomness/Blockchains -->> -Alice: Alice claims her 9K USDT
Bob ->> +Randomness/Blockchains: trxSwap(S): spends trxA
Randomness/Blockchains -->> -Bob: Bob claims his 1 BTC
end
```
