Skip to content

Instantly share code, notes, and snippets.

@RubenSomsen
Last active November 23, 2023 21:16
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RubenSomsen/8853a66a64825716f51b409be528355f to your computer and use it in GitHub Desktop.
Save RubenSomsen/8853a66a64825716f51b409be528355f to your computer and use it in GitHub Desktop.

SAS: Succinct Atomic Swap

Works today with single signer ECDSA adaptor signatures, or with Schnorr + MuSig.
Other than the explanation below, there's also a diagram and a video.

 
Advantages:

  • Requires merely two on-chain transactions for successful completion, as opposed to four
  • Scriptless, and one of the chains doesn't need to support timelocks
  • Can be used for efficient privacy swaps, e.g. Payswap

 
Disadvantages:

  • Access to money is contingent on remembering secrets (backup complexity)
  • Online/watchtower requirement for the timelock supporting chain (not needed with 3 tx protocol)

 
Protocol steps:

 
0.) Alice & Bob pre-sign the following transactions, with exception of the signatures in [brackets]:

  • success_tx (money to Bob): [sigSuccessAlice] + [sigSuccessBob]
  • revoke_tx (timelock): sigRevokeAlice + sigRevokeBob, which must then be spent by:
    • refund_tx (relative timelock, refund to Alice): [sigRefundAlice] + {sigRefundBob}
    • timeout_tx (longer relative timelock, money to Bob): sigTimeoutAlice + [sigTimeoutBob]

{sigRefundBob} is an adaptor signature, which requires secretAlice to complete

 
1.) Alice proceeds to lock up 1 BTC with Bob, using keyAlice & keyBob as pubkeys

If protocol is aborted after step 1:

  • Alice publishes the revoke_tx, followed by the refund_tx & sigRefundBob, to get her BTC back
  • If Alice neglects to publish the refund_tx in time, Bob will claim the BTC with the timeout_tx

 
2.) Bob locks up altcoins with Alice, using secretAlice & secretBob as pubkeys

If protocol is aborted after step 2:

  • Once Alice publishes sigRefundBob, Bob learns secretAlice and regains control over the altcoins

 
3.) Protocol completion:

  • Alice hands adaptor signature {sigSuccessAlice} to Bob, which requires secretBob to complete
  • Bob could now claim the BTC via the success_tx, reveal secretBob, and thus give Alice control over the altcoins (= 3 tx protocol)
  • Instead, Bob simply hands secretBob to Alice
  • Likewise, Alice hands keyAlice to Bob to forego her claim on the refund_tx
  • Bob continues to monitor the chain, because he'll have to respond if Alice ever publishes the revoke_tx

 
More graceful protocol failure:

If the protocol aborts after step 1, Alice would have been forced to make three transactions in total, while Bob has made none. We can reduce that to two by introducing a second refund_tx with timelock that can be published ahead of the revoke_tx and directly spends from the funding transaction. Publishing this transaction would also reveal secretAlice to Bob via an adaptor signature. In the 3 tx protocol, this output can go directly to Alice. In the 2 tx protocol with online/watchtower requirement, this output needs a script: spendable by Alice + Bob right away OR by Alice after a relative timelock. It is important to note that this transaction must NOT be published during step 3. Once Bob can complete the success_tx, the revoke_tx is needed to invalidate the success_tx prior to revealing secretAlice.

 
FAQ:

  • Why not allow Alice to still claim the altcoins if she accidentally lets Bob publish the timeout_tx?

    Alice could send the revoke_tx at the same time, revealing both secrets and causing likely losses. This can be solved by adding yet another transaction, but it wouldn't be efficient and wouldn't motivate Alice to behave.

  • Is it possible to implement this protocol on chains which only support absolute timelocks?

    Yes, but then Bob must spend his swapped coins before the timelock expires (or use the 3 tx protocol). Be aware that the revoke_tx MUST confirm before the timeout_tx becomes valid, which may become a problem if fees suddenly rise. The refund_tx can also not be allowed to CPFP the timeout_tx, as they must confirm independently in order to invalidate the success_tx first.

  • Can't Alice just publish the revoke_tx after protocol completion?

    Yes, she'd first have to move the altcoins (to invalidate secretAlice), and could then try to claim the BTC by publishing the revoke_tx, forcing Bob to react on-chain before the refund_tx becomes valid. The eltoo method of paying for fees (requires sighash_anyprevout) or a second CPFP-able output may be an improvement here (and also mitigates fee rising issues), but note that this also increases the required amount of tx data if the protocol doesn't complete successfully.

  • Can this be made to work with hash locks?

    Yes, by making the altcoins spendable via sigAlice + preimageBob OR sigBob + preimageAlice, and ensuring the contracts on the BTC side reveal either pre-image. Do note that this is not scriptless and will thus increase the transaction size.

 
Open questions:

  • Perhaps it's possible to perform an atomic swap in and out of Lightning with only a single on-chain transaction. This would require some kind of secondary set of HTLCs, allowing the sender to cancel a Lightning payment by revealing a secret after a certain period of time.

 
-- Ruben Somsen

 
 
Thanks to Lloyd Fournier for feedback and review.

 
Related work:

Tier Nolan Atomic Swap: https://bitcointalk.org/index.php?topic=193281.msg2224949#msg2224949
Monero Atomic Swap: https://github.com/h4sh3d/xmr-btc-atomic-swap/blob/master/README.md
Private Key Turnover: AdamISZ/CoinSwapCS#53  

Display the source blob
Display the rendered blob
Raw
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="761px" viewBox="-0.5 -0.5 761 601" style="max-width:100%;max-height:601px;"><defs/><g><rect x="0" y="0" width="760" height="600" fill="#ffffff" stroke="none" pointer-events="all"/><path d="M 240 260 L 278.46 106.18" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 279.73 101.08 L 281.43 108.72 L 278.46 106.18 L 274.64 107.03 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><path d="M 240 260 L 278.46 413.82" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 279.73 418.92 L 274.64 412.97 L 278.46 413.82 L 281.43 411.28 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><path d="M 240 260 L 273.63 260" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 278.88 260 L 271.88 263.5 L 273.63 260 L 271.88 256.5 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="40" y="200" width="200" height="120" fill="#ffffff" stroke="#000000" pointer-events="all"/><rect x="50" y="225" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 260px; margin-left: 51px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">in: 1 BTC Alice</div></div></div></foreignObject><text x="90" y="264" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">in: 1 BTC Ali...</text></switch></g><rect x="150" y="225" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 260px; margin-left: 151px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">out: 1 BTC<br /><b>Alice &amp; Bob</b></div></div></div></foreignObject><text x="190" y="264" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">out: 1 BTC...</text></switch></g><path d="M 480 420 L 517.15 345.7" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 519.5 341 L 519.5 348.83 L 517.15 345.7 L 513.24 345.7 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><path d="M 480 420 L 517.15 494.3" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 519.5 499 L 513.24 494.3 L 517.15 494.3 L 519.5 491.17 Z" fill="#000000" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><rect x="280" y="360" width="200" height="120" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 420px; margin-left: 281px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "><div>Abs. timelock 2 days (nLocktime)</div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><br /></div><div><br /></div></div></div></div></foreignObject><text x="380" y="424" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Abs. timelock 2 days (nLocktime)...</text></switch></g><rect x="290" y="385" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 420px; margin-left: 291px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">in: 1 BTC Alice &amp; Bob</div></div></div></foreignObject><text x="330" y="424" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">in: 1 BTC Ali...</text></switch></g><rect x="390" y="385" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 420px; margin-left: 391px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">out: 1 BTC<br /><b>Alice &amp; Bob</b></div></div></div></foreignObject><text x="430" y="424" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">out: 1 BTC...</text></switch></g><rect x="280" y="40" width="200" height="120" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 100px; margin-left: 281px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "><div> </div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><br /></div><div>Adaptor sig: reveals <b>secretBob</b></div></div></div></div></foreignObject><text x="380" y="104" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">...</text></switch></g><rect x="290" y="65" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 100px; margin-left: 291px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">in: 1 BTC Alice &amp; Bob</div></div></div></foreignObject><text x="330" y="104" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">in: 1 BTC Ali...</text></switch></g><rect x="390" y="65" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 100px; margin-left: 391px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">out: 1 BTC<br /><b>Bob</b></div></div></div></foreignObject><text x="430" y="104" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">out: 1 BTC...</text></switch></g><rect x="520" y="280" width="200" height="120" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 340px; margin-left: 521px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "><div>Rel. timelock +1 day (nSequence)</div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><br /></div><div>Adaptor sig: reveals <b>secretAlice</b><br /></div></div></div></div></foreignObject><text x="620" y="344" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Rel. timelock +1 day (nSequence)...</text></switch></g><rect x="530" y="305" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 340px; margin-left: 531px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">in: 1 BTC Alice &amp; Bob</div></div></div></foreignObject><text x="570" y="344" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">in: 1 BTC Ali...</text></switch></g><rect x="630" y="305" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 340px; margin-left: 631px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">out: 1 BTC<br /><b>Alice</b></div></div></div></foreignObject><text x="670" y="344" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">out: 1 BTC...</text></switch></g><rect x="520" y="440" width="200" height="120" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 500px; margin-left: 521px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "><div>Rel. timelock +2 days (nSequence)</div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><br /></div><div><br /></div></div></div></div></foreignObject><text x="620" y="504" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Rel. timelock +2 days (nSequence)...</text></switch></g><rect x="530" y="465" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 500px; margin-left: 531px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">in: 1 BTC Alice &amp; Bob</div></div></div></foreignObject><text x="570" y="504" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">in: 1 BTC Ali...</text></switch></g><rect x="630" y="465" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 500px; margin-left: 631px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">out: 1 BTC<br /><b>Bob</b></div></div></div></foreignObject><text x="670" y="504" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">out: 1 BTC...</text></switch></g><rect x="40" y="440" width="200" height="120" fill="#ffffff" stroke="#000000" pointer-events="all"/><rect x="50" y="465" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 500px; margin-left: 51px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">in: 200 LTC Bob</div></div></div></foreignObject><text x="90" y="504" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">in: 200 LTC B...</text></switch></g><rect x="150" y="465" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 500px; margin-left: 151px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">out: 200 LTC<br /><b>secretAlice &amp; secretBob</b></div></div></div></foreignObject><text x="190" y="504" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">out: 200 LTC...</text></switch></g><rect x="280" y="200" width="200" height="120" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 260px; margin-left: 281px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "><div>Abs. timelock 1 day (nLockTime)</div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><span><br /></span></div><div><br /></div><div>Adaptor sig: reveals <b>secretAlice</b><br /></div></div></div></div></foreignObject><text x="380" y="264" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Abs. timelock 1 day (nLockTime)...</text></switch></g><rect x="290" y="225" width="80" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 78px; height: 1px; padding-top: 260px; margin-left: 291px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">in: 1 BTC Alice &amp; Bob</div></div></div></foreignObject><text x="330" y="264" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">in: 1 BTC Ali...</text></switch></g><rect x="380" y="225" width="90" height="70" fill="#ffffff" stroke="#000000" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 88px; height: 1px; padding-top: 260px; margin-left: 381px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">out: 1 BTC<br /><b>Alice &amp; Bob</b> OR<br /><b>Alice in +1 day</b></div></div></div></foreignObject><text x="425" y="264" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">out: 1 BTC...</text></switch></g><path d="M 542.5 137.5 C 492.5 137.5 480 175 520 182.5 C 480 199 525 235 557.5 220 C 580 250 655 250 680 220 C 730 220 730 190 698.75 175 C 730 145 680 115 636.25 130 C 605 107.5 555 107.5 542.5 137.5 Z" fill="#ffffff" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 248px; height: 1px; padding-top: 175px; margin-left: 481px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">This transaction is not strictly<br />needed, but allows Alice to<br />recover more efficiently if Bob<br />never locks up any LTC.<br />Note that this contains a script.</div></div></div></foreignObject><text x="605" y="179" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">This transaction is not strictly...</text></switch></g><path d="M 480 260 Q 520 260 505 240 Q 490 220 495 215 Q 500 210 512.5 215.5" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><rect x="280" y="20" width="200" height="20" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 30px; margin-left: 281px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; font-style: italic; white-space: normal; word-wrap: normal; ">Success transaction</div></div></div></foreignObject><text x="380" y="34" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" font-style="italic">Success transaction</text></switch></g><rect x="280" y="180" width="200" height="20" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 190px; margin-left: 281px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; font-style: italic; white-space: normal; word-wrap: normal; ">Refund transaction #1</div></div></div></foreignObject><text x="380" y="194" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" font-style="italic">Refund transaction #1</text></switch></g><rect x="520" y="260" width="200" height="20" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 270px; margin-left: 521px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; font-style: italic; white-space: normal; word-wrap: normal; ">Refund transaction #2</div></div></div></foreignObject><text x="620" y="274" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" font-style="italic">Refund transaction #2</text></switch></g><rect x="280" y="340" width="200" height="20" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 350px; margin-left: 281px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; font-style: italic; white-space: normal; word-wrap: normal; ">Revoke transaction</div></div></div></foreignObject><text x="380" y="354" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" font-style="italic">Revoke transaction</text></switch></g><rect x="520" y="420" width="200" height="20" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 430px; margin-left: 521px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; font-style: italic; white-space: normal; word-wrap: normal; ">Timeout transaction</div></div></div></foreignObject><text x="620" y="434" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" font-style="italic">Timeout transaction</text></switch></g><rect x="40" y="180" width="200" height="20" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 190px; margin-left: 41px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; font-style: italic; white-space: normal; word-wrap: normal; ">On-chain transaction BTC</div></div></div></foreignObject><text x="140" y="194" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" font-style="italic">On-chain transaction BTC</text></switch></g><rect x="40" y="420" width="200" height="20" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 198px; height: 1px; padding-top: 430px; margin-left: 41px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; font-style: italic; white-space: normal; word-wrap: normal; ">On-chain transaction LTC</div></div></div></foreignObject><text x="140" y="434" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle" font-style="italic">On-chain transaction LTC</text></switch></g><rect x="30" y="40" width="240" height="120" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 238px; height: 1px; padding-top: 100px; margin-left: 32px;"><div style="box-sizing: border-box; font-size: 0; text-align: left; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "><b>1. </b>Sign and share all BTC transactions<br />    except the success transaction<br /><div><b>2.</b> Alice sends the BTC on-chain</div><div><b>3. </b>Bob sends the LTC on-chain</div><div><b>4. </b>Sign and share the success transaction</div><div><b>5. </b>Bob gives Alice secretBob</div><div><b>6. </b>Alice gives Bob her key ("Alice")</div><div><b>7. </b>Bob continues to watch the chain so he</div><div>   can respond against refund attempts</div></div></div></div></foreignObject><text x="32" y="104" fill="#000000" font-family="Helvetica" font-size="12px">1. Sign and share all BTC transactions...</text></switch></g><path d="M 542.5 42.5 C 492.5 42.5 480 65 520 69.5 C 480 79.4 525 101 557.5 92 C 580 110 655 110 680 92 C 730 92 730 74 698.75 65 C 730 47 680 29 636.25 38 C 605 24.5 555 24.5 542.5 42.5 Z" fill="#ffffff" stroke="#000000" stroke-miterlimit="10" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 248px; height: 1px; padding-top: 65px; margin-left: 481px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; ">Alice signed this transaction in<br />such a way that it's only valid<br />if Bob reveals secretBob</div></div></div></foreignObject><text x="605" y="69" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">Alice signed this transaction in...</text></switch></g><path d="M 471 150 Q 520 140 505 120 Q 490 100 495 95 Q 500 90 512.5 89.3" fill="none" stroke="#000000" stroke-miterlimit="10" pointer-events="stroke"/><rect x="35" y="330" width="210" height="80" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 208px; height: 1px; padding-top: 370px; margin-left: 36px;"><div style="box-sizing: border-box; font-size: 0; text-align: center; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "><b>GOAL</b>:<br />Alice gets her BTC refunded<br />and reveals secretAlice...<br /><i>OR: </i>Bob takes the BTC<br />and reveals secretBob</div></div></div></foreignObject><text x="140" y="374" fill="#000000" font-family="Helvetica" font-size="12px" text-anchor="middle">GOAL:...</text></switch></g><rect x="340" y="490" width="80" height="70" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject style="overflow: visible; text-align: left;" pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 78px; height: 1px; padding-top: 525px; margin-left: 342px;"><div style="box-sizing: border-box; font-size: 0; text-align: left; "><div style="display: inline-block; font-size: 12px; font-family: Helvetica; color: #000000; line-height: 1.2; pointer-events: all; white-space: normal; word-wrap: normal; "><font style="font-size: 20px"><b>S</b>uccinct<br /><b>A</b>tomic<br /><b>S</b>wap</font></div></div></div></foreignObject><text x="342" y="529" fill="#000000" font-family="Helvetica" font-size="12px">Succinct...</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://desk.draw.io/support/solutions/articles/16000042487" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Viewer does not support full SVG 1.1</text></a></switch></svg>
@xrqin
Copy link

xrqin commented Sep 19, 2020

I think it is not so succinct. Since you should transfer 1 BTC first. I think this step should count. If so, it still needs four transactions.

@RubenSomsen
Copy link
Author

@mt2Qx3R that step does count. The on-chain steps are:

  1. Alice locks up money.
  2. Bob locks up money.
  3. Bob watches the blockchain and reacts if Alice tries to claim a refund.

Depending on what Alice does, step 3 may or may not occur. Bob can also just perform step 3 if he does not wish to keep watching.

Alice does not have to watch anything. Her side of the swap is instantly settled.

@fresheneesz
Copy link

fresheneesz commented May 12, 2021

I went through all the cases, and after some clarification, convinced myself that it all should work. For anyone that's interested, I wrote up all different edge cases here. I also wrote a node.js script that runs through and validates all the steps (and prints out the state of things after each step). Read the readme for how to run it.

A couple things that tripped me up in my understanding of the cases:

  • I misread that Alice gives Bob serectAlice at the end, when the actual protocol is that Alice gives Bob AliceKey. I think my mindset was that "secrets" were something that could be shared, and keys were private. However, I think the real distinction is that secrets are revealed upon use, whereas keys are not revealed on use (signing).
  • I treated things a little too abstractly and forgot that at the end of the day the main outputs (Created by On-chain transaction BTC and Revoke transaction) are just 2-of-2 addresses that then have things presigned off them. By that I mean, I forgot that once Bob got AliceKey that he could spend from the outputs created by those transactions.
  • The name of the "Success Transaction" is a bit misleading. I took that to mean that in normal operation, Bob would eventually send that transaction. However that's not the case. Instead, Bob will have AliceKey and can send directly from the output created by On-chain transaction BTC to any address he wants. This was compounded by my first two misunderstandings.
  • Also, I was unsure whether refund_tx was presigned by Bob or not (I wasn't sure of the curly braces used there were considered "brackets" that meant it wasn't presigned). It turns out refund_tx is in fact presigned with the adapter signature.

@seresistvanandras
Copy link

Hi Ruben! This is a really nice protocol! Congrats! I agree that the online requirement from Bob's side is kind of suboptimal, but I don't see why statefulness would be an issue as you mention both here and in your video. Wallets, already have a state (they store where you are in the HD-tree used for key-derivation). I think statefulness is not an issue, since this is already the case, no?

To me, it seems that Chris Belcher's CoinSwap proposal got the community's support even though it requires significantly more on-chain space (4 transactions), and hence coordination. Any proposal that relies on interaction over multiple blocks is almost doomed. That's the reason why CoinJoins are deployed way earlier than Coinswaps. Your proposal seems to be optimal in the cooperative case. In the non-cooperative case, we have 3 transactions (with the being online requirement), which is still better than Belcher's proposal. Do you have any insights into why the community wants to go with that proposal instead of this? Anyways, it would be really exciting to see if coinswaps could be deployed and widely adopted soon in the Bitcoin community. You should also try to convince @nopara73 to have Coinswaps in Wasabi soon :))) cc @nothingmuch

@RubenSomsen
Copy link
Author

Thanks for taking the time to understand my protocol, @seresistvanandras :)

they store where you are in the HD-tree used for key-derivation

It's not crucial information though. If you delete your wallet and restore your backup, all your coins will be found, because you just keep searching all keys in deterministic order until you hit a gap of X (I think it's 100) unused keys, and then you stop. If you add state to it, then you continually have to make new back-ups in order to be able to restore all your coins, and I suspect this is also one of the reasons why @chris-belcher preferred to go with a more traditional coinswap method. The uncooperative case in SAS is also a bit awkward, because one person will have to carry the majority of the on-chain cost, so I can imagine that may have played a role as well.

@seresistvanandras
Copy link

But the state (i.e., all the keys used throughout the protocol) could be derived from the HD-tree, no? Or not sure what exactly you refer to a state in SAS. I think you just need the ability to derive as many keys as you want, which is already given via BIP-32.

Yes, the uncooperative case is a little bit nasty. Maybe, it could be improved? Either way, I think it is not a big disadvantage of the protocol that one party bears all the on-chain costs in the non-cooperative case. This asymmetry is also implicitly assumed in Belcher's proposal, where you have takers and makers in the CoinSwap protocol akin to JoinMarket. If you don't like this asymmetry, we can make it a bit fairer. Participants could do off-chain a coin-tossing protocol to decide who will be bearing the costs of the non-cooperative case. I would still bet on this protocol in the long run. 4 on-chain txs sound a way bigger burden than the slight suboptimality of the non-cooperative case.

@nopara73
Copy link

You should also try to convince @nopara73 to have Coinswaps in Wasabi soon :)))

Happy to hear out the arguments. But make it good, because me convincing the developers that it's worthy spending their time on will be the hard part :)

@RubenSomsen
Copy link
Author

@seresistvanandras

But the state (i.e., all the keys used throughout the protocol) could be derived from the HD-tree, no?

The keys that come from your counterparty are new to you (e.g. secretBob if you're Alice), and need to be backed up to ensure access.

Participants could do off-chain a coin-tossing protocol to decide who will be bearing the costs

A malicious party would abort as soon as they lose the coin toss.

@seresistvanandras
Copy link

Thanks, Ruben for the clarifications. In both cases you're right!

I'll answer to @nopara73 a bit later to collect all my convincing arguments why we need Succint Atomic Swap-based CoinSwaps in Wasabi.

@chris-belcher
Copy link

My coinswap design (or rather, gmaxwell's) doesn't necessarily require 4 transactions, in the co-operative case it only needs 2, because of private key handover.

A lot of my thinking around the design was about other issues with coinswap such as the "who goes first problem" (which your post alludes to and which my design deals with using fidelity bonds), the amount correlation problem, the problem of a single party being able to unmix a coinswap and payjoin-with-coinswap.
These problems are what I've spent most time implementing. SAS is orthogonal to those.
Right now my project actually uses 2-of-2 multisig in p2sh addresses, which obviously pretty rubbish for privacy. The plan is to first get working all the other hard parts like the maker/taker liquidity market, multiple transactions, routed transactions, recovering from aborts, etc and afterwards we can swap in ECDSA-2P code or taproot code. There's nothing to say we can't swap in SAS as well or instead of.

BTW, has anyone thought much about extending SAS to support multi-transaction coinswaps? That's pretty necessary for good privacy. So if not then a user could be coinswapping say 1.23456 BTC, the adversary will see that amount go in and can scan the blockchain for later transactions for nearly the same amount. To avoid that any coinswap scheme needs to support multiple transactions. That also means you'll never really get on-chain usage of just 2 transactions, it would need to be more like 6+

@RubenSomsen
Copy link
Author

has anyone thought much about extending SAS to support multi-transaction coinswaps?

@chris-belcher, do you mean swapping with more than two participants like Multiparty S6, or two people swapping more than one output each? I suspect the former may be difficult, but the latter should be fine. It's probably easiest to see on the "LTC" side from the diagram, as that's a simple 2-of-2 output with no scripts. Instead of receiving 200 LTC for 1 BTC, you'd receive e.g. 20 + 60 + 40 LTC for 1 BTC, as soon as the key is revealed on the BTC side.

Anonymity wise supporting cross-chain swaps between Liquid may be beneficial, as it means outputs on Liquid also have to be taken into account when analyzing where a swap could potentially have gone, and because of Confidential Transactions the list of suspects will be equivalent to every recent output. And if high-value exposure to Liquid is not desired, you could swap only a fraction of the value to it (e.g. 1 BTC for 0.95 BTC and 0.05 L-BTC), which could help throw off on-chain amount linkage.

@xrqin
Copy link

xrqin commented Sep 27, 2022

why don't you publish your result?

@nikicat
Copy link

nikicat commented Nov 23, 2023

Hello, is this scheme implemented?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment