BSV
$51.58
Vol 41.24m
2.63%
BTC
$75686
Vol 66736.33m
1.89%
BCH
$381.3
Vol 365.69m
3.36%
LTC
$71.09
Vol 456.53m
2.34%
DOGE
$0.19
Vol 5677.01m
1.05%
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.

Bitcoin Atomic swap concept adapted from Gemini
Adapted from Gemini

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.

Here is also an example of a swap that did not go through. Both Alice and Bob recovered their funds.

All of the code shown in this article can be found at our GitHub.

Watch: Why atomic swaps are necessary

Recommended for you

BSV joins Linux Foundation to advance open standards
The BSV Association has partnered with the Linux Foundation to advance its objective of promoting development that adheres to BSV...
November 6, 2024
How to construct transactions on BSV blockchain with Python
Python coders, it's time to start learning how to build Bitcoin transactions as nChain's Senior Software Engineer, Arthur Gordon, recently...
November 5, 2024
Advertisement
Advertisement
Advertisement