Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: rfc-0240 XTR-BTC atomic swap #3485

Merged
merged 6 commits into from
Nov 3, 2021

Conversation

SWvheerden
Copy link
Collaborator

Description

This PR adds in RFC-0240 to allow us to do BTC-XTR atomic swaps.

RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
@SWvheerden SWvheerden force-pushed the sw_btc_xtr_swap branch 2 times, most recently from f9ece9a to 021e6d3 Compare October 26, 2021 09:41
@SWvheerden SWvheerden changed the title [WIP] XTR-BTC atomic swap feat!: rfc-0240 XTR-BTC atomic swap Oct 26, 2021
@SWvheerden SWvheerden force-pushed the sw_btc_xtr_swap branch 2 times, most recently from 4b0f384 to fe3cb40 Compare October 26, 2021 09:52
@philipr-za
Copy link
Contributor

Pretty sure this is not a breaking feature but rather documentation

@SWvheerden SWvheerden changed the title feat!: rfc-0240 XTR-BTC atomic swap feat: rfc-0240 XTR-BTC atomic swap Oct 26, 2021
Copy link
Contributor

@delta1 delta1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, sorry for the many comments but just some suggestions


## Description

Atomic swaps are atomic transactions that allow users to trustlessly exchange different crypto assets and or coins without the use of a central exchange. This makes it much more private and secure to do and swap because no third party is required for this to be secure. Atomic swaps work on the principle of using [Hashed Time Lock Contract](https://en.bitcoin.it/wiki/Hash_Time_Locked_Contracts)(HTLC). In short, it requires some hash pre-image to unlock the contract or the time-lock can be used to reclaim the funds.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Atomic swaps are atomic transactions that allow users to trustlessly exchange different crypto assets and or coins without the use of a central exchange. This makes it much more private and secure to do and swap because no third party is required for this to be secure. Atomic swaps work on the principle of using [Hashed Time Lock Contract](https://en.bitcoin.it/wiki/Hash_Time_Locked_Contracts)(HTLC). In short, it requires some hash pre-image to unlock the contract or the time-lock can be used to reclaim the funds.
Atomic swaps are atomic transactions that allow users to trustlessly exchange different crypto assets and or coins without the use of a central exchange. This makes it much more private because no third party is required. Atomic swaps work by using [Hashed Time Lock Contracts](https://en.bitcoin.it/wiki/Hash_Time_Locked_Contracts)(HTLC). In short, it requires some hash pre-image to unlock the contract, or the time-lock can be used to reclaim the funds.


Atomic swaps are atomic transactions that allow users to trustlessly exchange different crypto assets and or coins without the use of a central exchange. This makes it much more private and secure to do and swap because no third party is required for this to be secure. Atomic swaps work on the principle of using [Hashed Time Lock Contract](https://en.bitcoin.it/wiki/Hash_Time_Locked_Contracts)(HTLC). In short, it requires some hash pre-image to unlock the contract or the time-lock can be used to reclaim the funds.

In an Atomic swap, both users lock up the funds to be exchanged on their respective chains in an HTLC-type contract. However, the pre-image, or spending secret, of the two contracts is the same, but only one party knows the correct pre-image. When the first HTLC contract is spent, then this publicly reveals the pre-image for the other party to use in spending the second HTLC. If the first HTLC is never spent, the second transaction's time-lock will allow the user to respend the funds back to themselves after a time.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it "publicly" revealed? It is revealed to the counterparty I presume. Maybe some clarification about "after a time" as well: how much time? is it the length of the time lock? can it only be spent after that?

Suggested change
In an Atomic swap, both users lock up the funds to be exchanged on their respective chains in an HTLC-type contract. However, the pre-image, or spending secret, of the two contracts is the same, but only one party knows the correct pre-image. When the first HTLC contract is spent, then this publicly reveals the pre-image for the other party to use in spending the second HTLC. If the first HTLC is never spent, the second transaction's time-lock will allow the user to respend the funds back to themselves after a time.
In an Atomic swap, both users lock up the funds to be exchanged on their respective chains in an HTLC-type contract. The pre-image, or spending secret, of the two contracts is the same, but is only known to the initiator. When the first HTLC is spent, then this reveals the pre-image for the other party to use in spending the second HTLC. If the first HTLC is never spent, the second transaction's time-lock will allow the user to spend the funds back to themselves after a time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, its publicly revealed on the blockchain.
But yeah the time is the time lock, and there is no limit to what it should be or what a minimum is. It is completely user-defined.

RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved

BTC uses a scripting language to enable smart contracts on transactions which enables [atomic swaps](https://tlu.tarilabs.com/protocols/atomic-swaps/AtomicSwaps.html) on the BTC chain. Traditionally [Mimblewimble] coins do not implement scripts, this makes Atomic swaps harder to implement but not impossible. Grin has implemented atomic swaps as a version of a 2-of-2 multi-signature transaction [mimblewimble atomic swaps](https://tlu.tarilabs.com/protocols/grin-protocol-overview/MainReport.html#atomic-swaps). Fortunately, Tari does have scripting with [TariScript] which works a lot like BTC scripts, this makes adding in scripts a lot easier. Because of the same type of scripting, the scripts to both HTLCs will look very similar and we only need to ensure that we use the same Hashing function in both.

To do an Atomic swap from BTC to XTR we need 4 wallets, 2 BTC wallets, and 2 XTR wallets.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest clarifying where these numbers come from. Is it 1 of each type of wallet for each party?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yip, will clarify

RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
```
_relative locktime_ is a time sequence that Alice chooses to lock up the funds in order to give Bob time to claim this.

#### XTR - HTLC script
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these 2 headings should have the same formatting, either both with the dash or both without

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with this, lets make them consistent.

RFC/src/RFC-0240_AtomicSwap.md Show resolved Hide resolved
RFC/src/SUMMARY.md Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Show resolved Hide resolved

## Goals

The aim of this Request for Comment (RFC) is to describe how an Atomic swap will be created.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The aim of this Request for Comment (RFC) is to describe how an Atomic swap will be created.
The aim of this Request for Comment (RFC) is to describe how BTC/XTR Atomic swaps will be created.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically avoided calling this a BTC/XTR Atomic swap document so that we can expand this to include all other crypto atomics swaps here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s fine but at the moment that’s all it is 🤷

@SWvheerden SWvheerden changed the title feat: rfc-0240 XTR-BTC atomic swap docs: rfc-0240 XTR-BTC atomic swap Oct 26, 2021
Copy link
Contributor

@hansieodendaal hansieodendaal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SWvheerden, looking good, just a suggestion to make the example text a bit clearer, also rectified a math rendering gremlin.

Comment on lines 77 to 91
As an example, Alice wants to trade some of her XTR for Bob's BTC. Alice and Bob need to agree on an amount of XTR and BTC to swap. If this is agreed on the following steps will happen:

* Alice chooses a random string as the pre-image (\\( \preimage \\)), she hashes this with SHA256. She then sends the hash to Bob. Along with het BTC address.

* Bob sends his a public version of the [script key], \\( K_{Sb} \\) for use in the XTR transaction.

* Alice creates a XTR transaction with an HTLC contract requiring \\( \preimage \\) as the input, to pay out to Bob's address or it pays back to her after a certain time.

* Bob waits for this transaction to be mined. When it is mined, he checks the spending script and verifies that the script checks (\\ \hash{\preimage} \\), he has the [script key], \\( K_{Sb} \\) to claim it and that it has a sufficiently long time-lock to give him time to claim the transaction.

* Upon verification, Bob creates a BTC transaction with the same \\( \preimage \\). But this HTLC spends to Alice's BTC address she gave him.

* Alice checks the Bitcoin blockchain and upon seeing that the transaction is mined, she claims the transaction. But by claiming it, she makes public what (\\( \preimage \\)) is.

* Bob sees that his BTC is spent, and looks at the witness to get \\( \preimage \\). Bob can then use \\( \preimage \\) to claim the XTR transaction.
Copy link
Contributor

@hansieodendaal hansieodendaal Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
As an example, Alice wants to trade some of her XTR for Bob's BTC. Alice and Bob need to agree on an amount of XTR and BTC to swap. If this is agreed on the following steps will happen:
* Alice chooses a random string as the pre-image (\\( \preimage \\)), she hashes this with SHA256. She then sends the hash to Bob. Along with het BTC address.
* Bob sends his a public version of the [script key], \\( K_{Sb} \\) for use in the XTR transaction.
* Alice creates a XTR transaction with an HTLC contract requiring \\( \preimage \\) as the input, to pay out to Bob's address or it pays back to her after a certain time.
* Bob waits for this transaction to be mined. When it is mined, he checks the spending script and verifies that the script checks (\\ \hash{\preimage} \\), he has the [script key], \\( K_{Sb} \\) to claim it and that it has a sufficiently long time-lock to give him time to claim the transaction.
* Upon verification, Bob creates a BTC transaction with the same \\( \preimage \\). But this HTLC spends to Alice's BTC address she gave him.
* Alice checks the Bitcoin blockchain and upon seeing that the transaction is mined, she claims the transaction. But by claiming it, she makes public what (\\( \preimage \\)) is.
* Bob sees that his BTC is spent, and looks at the witness to get \\( \preimage \\). Bob can then use \\( \preimage \\) to claim the XTR transaction.
As an example, Alice wants to trade some of her XTR for Bob's BTC. Alice and Bob need to agree on an amount of XTR and
BTC to swap. If this is agreed on the following steps will happen:
* Alice chooses a random string, \\( \preimage \\), as the pre-image and hashes it with SHA256. She then sends the hash
of the pre-image, \\( \hash{\preimage} \\), to Bob along with her BTC address.
* Bob sends her a public version of his [script key], \\( K_{Sb} \\), for use in the XTR transaction, which we can refer
to as Bob's script address.
* Alice creates a one-sided XTR transaction with an HTLC contract requiring \\( \preimage \\) as the input, which will
either pay out to Bob's script address or to her script address, \\( K_{Sa} \\), after a certain "time" has elapsed
(block height has been reached).
* Bob waits for this transaction to be mined. When it is mined, he verifies that the UTXO spending script expects a
comparison of \\( \hash{\preimage} \\) as the first instruction, and that his public [script key], \\( K_{Sb} \\), will
be the final value remaining after executing the script. He has the private [script key], \\( k_{Sb} \\), to enable him
to produce a signature to claim the funds if he can get hold of the expected pre-image input value, \\( \preimage \\).
He also verifies that the UTXO has a sufficiently long time-lock to give him time to claim the transaction.
* Upon verification, Bob creates a Segwit HTLC BTC transaction with the same \\( \hash{\preimage} \\), which will spend
to Alice's BTC address she gave him. It is important that the time lock for this HTLC has to expire prior to the time
lock of the XTR HTLC that Alice created.
* Alice checks the Bitcoin blockchain and upon seeing that the transaction is mined, she claims the transaction, but,
in order for her to do so, she has to make public what \\( \preimage \\) is as she has to use it as the witness of the
claiming transaction.
* Bob sees that his BTC is spent, and looks at the witness to get \\( \preimage \\). Bob can then use \\( \preimage \\)
to claim the XTR transaction.

hansieodendaal
hansieodendaal previously approved these changes Oct 27, 2021
Copy link
Contributor

@hansieodendaal hansieodendaal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@philipr-za philipr-za left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there, just had one change I would like to see about the description of the pre-image.

RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
RFC/src/RFC-0240_AtomicSwap.md Outdated Show resolved Hide resolved
```
_relative locktime_ is a time sequence that Alice chooses to lock up the funds in order to give Bob time to claim this.

#### XTR - HTLC script
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with this, lets make them consistent.

Co-authored-by: Philip Robinson <simian@tari.com>
Co-authored-by: Philip Robinson <simian@tari.com>
@aviator-app aviator-app bot added mq-failed and removed mq-failed labels Nov 2, 2021
@aviator-app aviator-app bot merged commit 299eaae into tari-project:development Nov 3, 2021
@SWvheerden SWvheerden deleted the sw_btc_xtr_swap branch November 16, 2021 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants