Cross-Chain Swaps

A cross-chain swap enables trading tokens across different blockchains without using an intermediary party (e.g. an exchange service) in the process.

../_images/cross-chain-swap.png

Cross-chain swap between public and private network

Symbol follows the Hashed TimeLock Contract (HTLC) protocol to create a trustless environment for the decentralized exchange of assets. The protocol guarantees that if all participants agree, the swap will take place. On the contrary, each participant should receive their locked funds back if some of them decide not to conclude the process.

HTLC uses hashlocks and timelocks to reduce the counterparty risk. Every participant in the exchange of tokens needs to present proof (hashlock) to complete it. Failing to do so, the locked assets are released back to each original owner after the timelock expires.

A thorough explanation of the protocol can be found on the Bitcoin Wiki.

Protocol

Alice and Bob want to exchange 10 alice.tokens for 10 bob.tokens.

The problem is that they are not in the same network: alice.token is defined in Symbol’s public chain whereas bob.token is only present in a private chain using Symbol tech.

Trading tokens directly from one blockchain to the other is not possible, due to the technological differences between them.

In the case of Symbol public and private chain, the same mosaic name could have a different definition, distribution, or even not exist. Between Bitcoin and Symbol, the difference is even more evident, as each blockchain uses an entirely different technology.

Instead of transferring tokens between different chains literally, the trade will be performed inside each chain.

The cross chan swap protocol will ensure that the token swap occurs atomically.

sequenceDiagram participant Alice participant Private Chain participant Public Chain participant Bob Note over Alice: proof = 'random' Note over Alice: h = sha_256() Note over Alice: secret = h(proof) Alice ->> Private Chain: announces TX1(secret) Note right of Alice: TX1 waits for proof Alice -->> Bob: tells secret Bob ->> Public Chain: announces TX2(secret) Note left of Bob: TX2 waits for proof Alice ->> Public Chain: announces TX3(proof, secret) Note left of Public Chain: proof becomes public Note left of Bob: TX2 executes Note left of Bob: Alice receives funds Bob ->> Private Chain: announces TX4(proof, secret) Note right of Alice: TX1 executes Note right of Alice: Bob receives funds

Cross-chain swap sequence diagram

Before starting with the swap, all participants involved int the swap must own at least one account in each blockchain.

  1. Alice generates a random set of bytes called proof. The proof should have a size between 10 and 1000 bytes.
  2. Alice hashes the obtained proof with one of the available algorithms to generate the secret.
  3. Alice defines the SecretLockTransaction TX1:
TX1 Property Value
Type SecretLockTransaction
Mosaic 10 alice.token
Recipient Bob’s address (Private Chain)
Algorithm h
Duration 96 h
Secret h(proof)
Network Private Chain

Once announced, this transaction will remain locked until someone discovers the proof that matches the secret. If no one unlocks it before the duration set is reached, the locked funds will be returned back to Alice.

  1. Alice announces TX1 to the private network and shares with Bob the secret.
  2. Bob should retrieve the secret from the chain and announces the following SecretLockTransaction TX2 to the public network:
TX2 Property Value
Type SecretLockTransaction
Mosaic 10 bob.token
Recipient Alice’s address (Public Chain)
Algorithm h
Duration 84 h
Secret h(proof)
Network Public Chain
  1. Alice announces the SecretProofTransaction TX3 to the public network. This transaction defines the encrypting algorithm used, the original proof and the secret:
TX3 Property Value
Type SecretProofTransaction
Recipient Alice’s address (Public Chain)
Algorithm h
Secret h(proof)
Proof proof
Network Public Chain
  1. Once TX3 is confirmed, the proof is revealed. TX2 transaction is unlocked, and Alice receives the locked funds.
  2. Bob picks the proof and announces the SecretProofTransaction TX4 to the private network, receiving the locked funds from TX1.
TX4 Property Value
Type SecretProofTransaction
Recipient Bob’s address (Private Chain)
Algorithm h
Secret h(proof)
Proof proof
Network Private Chain

The process is atomic, but should be completed with lots of time before the deadlines:

  • TX1’s duration should be significantly bigger than TX2’s to guarantee that the second participant will have time to unlock TX1 after the first one reveals the proof.
  • Each participant must wait for at least maxRollBackBlocks between announcements to prevent experiencing transaction rollbacks.

Guides

Continue: Cryptography.