12-25-2024
BSV
$56.21
Vol 27.8m
-2.03%
BTC
$98114
Vol 37138.08m
0.07%
BCH
$457.32
Vol 224.17m
-3.78%
LTC
$107.21
Vol 547.14m
-1.96%
DOGE
$0.32
Vol 2849.88m
-1.91%
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

Google unveils ‘Willow’; Bernstein downplays quantum threat to Bitcoin
Google claims that Willow can eliminate common errors associated with quantum computing, while Bernstein analysts noted that Willow’s 105 qubits...
December 18, 2024
WhatsOnChain adds support for 1Sat Ordinals with new API set
WhatsOnChain now supports the 1Sat Ordinals with a set of APIs in beta testing; with this new development, developers can...
December 13, 2024
Advertisement
Advertisement
Advertisement