Atomic swaps
In general, the scheme of atomic swaps in two independent blockchains looks as follows:
sequenceDiagram
participant Alice
participant Chain 1
participant Chain 2
participant Bob
Note over Alice: secret = 'random'
Note over Alice: hash = h(secret)
Alice ->> Chain 1: announces TX1(hash)
Note over Chain 1: TX1 waits for secret
Alice -->> Bob: tells hash
Bob ->> Chain 2: announces TX2(hash)
Note over Chain 2: TX2 waits for secret
Alice ->> Chain 2: announces TX3(secret, hash)
Note over Chain 2: TX2 executes
Note over Chain 2: secret becomes public
Note over Chain 2: Alice receives funds
Bob ->> Chain 1: announces TX4(secret, hash)
Note over Chain 1: TX1 executes
Note over Chain 1: Bob receives funds
BeginSwap
- Begin exchange operation (signed by the sender)sender
- sendertarget
- recipienttimestamp
- expiration timealg
- hashing algorithmhash
- hash?DATA (amount, color, etc)
- data about the frozen funds
CommitSwap
- complete the exchange operation (does not require a signature)hash
- hash of the secret as exchange operation identifiersecret
- secret phrasesender
- exchange initiatortarget
- recipient
RollbackSwap
- Roll back an exchange operation (does not require a signature)hash
- hash of the secret as exchange operation identifiersender
- exchange initiator
CancelSwap
- Cancel the exchange operation (signed by the sender and recipient).hash
- hash of the secret as exchange operation identifiersender
- exchange initiatortarget
- recipient
Diagram of the asset flow within a single network:
graph TD
Alice((Alice)) -- BeginSwap --> swap
swap -- CancelSwap --> Alice
swap -- CommitSwap --> Bob((Bob))
swap -. time .-> expired_swap
expired_swap -- RollbackSwap --> Alice
expired_swap -- CancelSwap --> Alice
BeginSwap
initiates the exchange operation by freezing the funds in the sender's account. To do this, coins are deducted from the account and information about the operation (secret, lifetime, deducted amount, etc.) is being recorded in the account in a special field with unfinished exchanges.CommitSwap
unfreezes the funds and transfers them to the recipient. For this purpose, the secret, whose hash was specified earlier, is provided before the expiration of the exchange operation. In fact, the operation adds the transaction amount to the recipient's balance (it was deducted from the sender when the exchange was created), and then the information about the incomplete operation gets deleted from the sender's account.CancelSwap
cancels operations by mutual consent. It can be performed at any time, including before the exchange expires. This is an optional operation.Last modified 2yr ago