A Peer-to-peer Decentralized Exchange and Atomic Swaps

OnTokens
5 min readNov 30, 2022

--

Abstract. A purely peer-to-peer version of centralized exchanges would allow 
cryptocurrency of different blockchains to be exchanges with each other
without going through a financial institution or another 3rd-party. Digital
signatures provide part of the solution, but the main benefits are lost
if a trusted third party is still required to prevent not-sending.
We propose a solution to the not-sending problem using a peer-to-peer network.
The network records the transactions from other chains by hashing them into
an ongoing chain of hash-based proof-of-send, forming a record that cannot
be changed without redoing the proof-of-send.

1. Introduction

Trade on the Internet has come to rely almost exclusively on central exchanges serving as trusted third parties to process electronic payments. While the system works well enough for most transactions, it still suffers from the inherent weaknesses of the trust based model. Complete trades are not really possible, since central exchanges cannot avoid mediating disputes.

2. Trades

We define an electronic coin as a chain of digital signatures. Each owner transfers the coin to the next by digitally signing a hash of the previous transaction and the public key of the next owner and adding these to the end of the coin. A payee can verify the signatures to verify the chain of ownership.

3. Escrow in blockchain

Escrow services are now playing a major role in the field of Blockchain. The network of these chains has been doing their tasks differently. The duty which is not meant for the financial escrow institutions. Generally, an escrow does duties as a representative of a concerned account. Escrow can always pay obligations, with the help of signature funds, allocated to pay those debts. Escrow main duty is to help to dispense money or documents as a neutral third party in different exchanges. They do not release their contents until agreed conditions are met. These duty acting in the blockchain are not transferring and holding funds for next payment but rather bartering public and private keys to do their function as an escrow.

Laura has a total of 10 BTC that she wants to use for a self-construction donation campaign, under the tutelage of the ConstruyeHogar Foundation. In this sense, Laura and ConstruyeHogar have created a 3-of-3 multisig wallet for each of their beneficiaries. In this way, Laura, the foundation and the beneficiary have a digital signature to be able to access the money in question. In fact, mobilizing that money will only be possible with the digital signature of all of them.

Atomicwallet https://atomicwallet.io/

4. Atomic Swaps

When performing an atomic swap between Ether and Bitcoin, the AtomicSwapEther contract should be used. For this example, Alice holds Ether and Bob also holds Bitcoin. Alice is looking to give her Ether to Bob in exchange for his Bitcoins.

  1. Bob generates a random secret key and hashes it using SHA256 to generate a secret lock.
  2. Bob uses the secret lock, and a Bitcoin Script, to setup a transaction to Alice on the condition that she produces the secret key. If she does not do so within 48 hours then Bob can withdraw the funds.
  3. Bob sends the secret lock to Alice along with the address of his transaction on the Bitcoin blockchain.
  4. Alice checks Bob’s transaction, verifying the details of the trade. If she does not agree then she does not need to do anything. After 48 hours, Bob can withdraw his funds.
  5. Alice calls open using a unique _swapID that has been negotiated between both traders. She also uses the secret lock that was provided by Bob. This is a payable call and Alice must send her Ether when she makes this call.
  6. Bob calls check to verify the details of the trade. If he does not agree, then he does not need to do anything. After 24 hours, Alice can call expire, getting a refund of her Ether.
  7. Bob calls close, which requires that he submits the secret key associated with the secret lock. If he has provided the correct secret key, it will transfer Alice's Ether to Bob and store the secret key.
  8. Alice calls checkSecretKey, acquiring the secret key.
  9. Alice provides the secret key to Bob’s Bitcoin Script, and receives his Bitcoin.

5. Problem of Simultaneously

Trades cannot be simultaneous as one needs to be the first to send the other party verifies if it’s on the smart contract then the other party creates a multi-sig scripting contract on Bitcoin too and sends the funds there. Bob creates a smart contract with 2 signatures for both Bob and Alice.

There are two ways where we can solve this though. One requires trust-based implement in trustless systems. This can be done by adding a parameter for each node or address based on the number of successful transactions they have executed so far (+1 if the transaction was good and -1 if the transaction was bad, adding a proof-of-karma) and levels up as they go along. I know you guys are now frowning on the idea as basically it’s just a cheap amazon review implementation but that’s why we have another solution.

Another implementation is allowing both users to utilized the smart contracts and having them store both the tokens that they want to exchange and allowing the contract to do the swap based if both the conditions were met for each other. I know this would try to solve the problem of simultaneously granting no one trusts nobody, and in reality when your exchanging something for example in a meetup you can always do a simultaneous swap but in the process you would still have to check if it’s the right stuff that you were wanting to exchange for. Allowing the contract to verify the transaction would probably make it for a better solution moving forward.

6. How does it work?

The idea of P2P Swap is to allow sellers to list their trades and not depend on being online everytime thus letting the smart contract do all of the verification and processes. At the start, Jestoni has 0.1 Optimism ETH and puts up a listing trade for 0.1 ETH Mainnet. The smart contract accepts the deposit (and verifies his mainnet address) and waits for any takers of the trade. Soon after Sarah accepts the trade offer and was asked to deposit the 0.1 ETH Mainnet. The smart contract verifies the transaction as well using merkle trees and zk-snarks and then if all of the conditions are met, performs the swap and opens the channel for both parties to withdraw. In the end, Jestoni gets 0.1 ETH Mainnet and Sarah withdraws 0.1 ETH Optimism.

--

--

OnTokens
OnTokens

Written by OnTokens

We empower people with Web3 technology and push the frontiers of technological possibilities. 🧪

No responses yet