BSV
$71.25
Vol 64.31m
-1.64%
BTC
$98014
Vol 46063.11m
-0.63%
BCH
$508.73
Vol 1030.51m
-4.48%
LTC
$99.78
Vol 1735.25m
-2.2%
DOGE
$0.43
Vol 15524.78m
-8.08%
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

David Case gets technical with Bitcoin masterclass coding sessions
Whether you're a coding pro or a novice, David Case's livestream sessions on the X platform are not to be...
November 21, 2024
NY Supreme Court’s ruling saves BTC miner Greenidge from closing
However, the judge also ruled that Greenidge must reapply for the permit and that the Department of Environmental Conservation has...
November 20, 2024
Advertisement
Advertisement
Advertisement