1.28°C
Hilliard
11-21-2024
BSV
$66.95
Vol 199.89m
-1.1%
BTC
$97980
Vol 114985.41m
3.87%
BCH
$486.39
Vol 2150.55m
10.96%
LTC
$89.23
Vol 1404.05m
6.23%
DOGE
$0.38
Vol 9785.92m
2.83%
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

Blockchain enables autonomous AI agents to learn
Utilizing blockchain tech, a group of Belgian scientists enabled autonomous AI agents to learn and communicate securely, contributing to the...
September 17, 2024
WhatsOnChain gets own UTXO endpoints for BSV blockchain services
With ElectrumX set to retire in October, WhatsOnChain is gearing up to implement a new UTXO set of API endpoints,...
September 16, 2024
Advertisement