Getting your Trinity Audio player ready...
|
This post was first published on Medium.
We have executed the first atomic swap between BSV blockchain and BTC Core. This achievement represents a major advancement for the BSV blockchain, fostering seamless interoperability with other blockchains in a trustless manner.
In a previous article, we have explained the high-level theory of atomic swaps. We delve into a practical example of an atomic swap between BSV and BTC using Hash Time Locked Contracts (HTLC). Let’s break down this process into four essential steps, each of which contains code snippets that you can run yourself.
Step 1: Alice initiates a transaction on BTC
The process begins with Alice, who selects a random integer x and creates a hash (xHash) using the SHA-256 algorithm. Next, Alice deploys a Pay-to-Witness-Script-Hash (P2WSH) transaction which includes the amount of BTC she wants to trade with Bob.
We construct Alice’s transaction using the bitcoinutils library:
Once executed, the code will print the raw serialized transaction, which we can broadcast to the BTC network using Blockstream’s block explorer.
This P2WSH transaction script comprises of conditional clauses that require either Bob’s public key and the secret x, or Alice’s signature and a time delay to spend the transaction.
Step 2: Bob reciprocates on BSV
In response, Bob deploys an equivalent sCrypt smart contract on the BSV blockchain, containing the BSV amount he is prepared to swap with Alice. This contract includes two public methods: one that Alice can unlock by providing x, and another that Bob can unlock after a predetermined time period, i.e. the timeout, to withdraw his funds. The smart contract is functionally equivalent to the aforementioned script on BTC.
Bob obtains the hash of x, but not x itself, and Alice’s public key. He then deploys the smart contract using the following deployment code:
Step 3: Alice unlocks Bob’s contract
Upon successful deployment of the sCrypt contract, Alice can now call the public method “unlock” of Bobs contract by revealing the secret x. As a result, Alice possesses Bob’s BSV.
Step 4: Bob retrieves Alice’s BTC
Once x has been revealed by Alice, Bob can use it to unlock the P2WSH UTXO on the BTC blockchain. This process allows Bob to claim Alice’s BTC, concluding the atomic swap.
We can broadcast the printed raw transaction and complete the swap.
Dispute resolution
In a situation where a dispute arises, both Alice and Bob have the ability to withdraw their funds after a certain time.
This feature offers a fail-safe mechanism that ensures no funds are permanently locked in the event of an unsuccessful or disputed swap. The temporal constraints provide enough window for both parties to unlock their respective contracts.
In Alice’s case we can modify the construction of the witness:
Passing the OP_FALSE as the first input will execute the script after OP_ELSE, which checks time lock has expired.
Likewise, in Bobs case, we modify the code to call the “cancel” method:
The first successful cross-chain BTC-BSV swap
We have conducted the first ever successful atomic swap between BSV and BTC mainnets.
- The initial P2WSH transaction on BTC: af7a8c31e489c18d6fe46141cf2cbc777aeb23672679eb199bde4813d6fe41d4
- The equivalent sCrypt smart contract transaction on BSV: 8598d203e8bdd45b7025e334cdebdef06be88381dec245d95108e658e8373f9c
- Alice’s invocation of the “unlock” method: 68c2c5a91645112df6b62a5ee6ea9e0e22f29499b88c9ccffc23f8f86ec63d09
- Bob unlocks the BTC: 9c697bf8f0d2c11285cfab3a2b8a856a1c18ae048d481c7bb3f93f9ccd2dbaad
Here is also an example of a swap that did not go through. Both Alice and Bob recovered their funds.
- BTC: 395dde14e489208302c1d6ac3b4fc0db890c000fd79f3e8
36fc74043a54480ff - BSV: 12e701237a1f6da2b69f1d4dab2b1208d3f6003f7b45ef178
c5c7d9e00c54273
All of the code shown in this article can be found at our GitHub.
Watch: Why atomic swaps are necessary