Skip to main content

The subswap Protocol

The subswap protocol is a peer-to-peer (P2P) protocol for facilitating trustless atomic swaps of Bitcoin-based assets, such as Alkanes, using Partially Signed Bitcoin Transactions (PSBTs). It is built on top of the subp2p protocol, which provides a secure and decentralized communication layer for the marketplace.

The Swap Process

The swap process is divided into two main phases: the PSBT negotiation phase and the mempool gossip phase.

1. PSBT Negotiation

The PSBT negotiation phase is where the two parties in a trade agree on the terms of the swap and create a mutually signed transaction. This is a three-step process that is handled by the PsbtNegotiationHandler in the ``subswap-core library.

a. Start

The taker initiates the swap by sending a Start message to the maker. This message contains the taker's unsigned PSBT, which represents their side of the trade.

b. Propose

The maker receives the Start message and creates a proposal PSBT by combining the taker's PSBT with their own. The maker then signs their inputs in the proposal PSBT and sends it back to the taker in a Propose message.

c. Finalize

The taker receives the Propose message and, if they agree to the terms, signs their inputs in the proposal PSBT. The taker then sends the fully signed PSBT back to the maker in a Finalize message.

2. Mempool Gossip

Once the PSBT has been finalized, it is broadcast to the Bitcoin network to be included in a block. This is done through the mempool gossip phase, where the finalized transaction is gossiped to all of the nodes in the subp2p network.

This ensures that the transaction is quickly and efficiently propagated throughout the network, increasing the chances of it being included in a block in a timely manner.

The Role of Runestones

The subswap protocol uses Runestones to represent the Alkanes in a swap. A Runestone is a special type of output in a Bitcoin transaction that contains metadata about the assets being traded.

In the case of a subswap, the Runestone contains a Protostone with an edict that specifies the ID and amount of the Alkane being traded, as well as the output index of the taker's output. This ensures that the Alkanes are correctly assigned to the taker upon the successful completion of the swap.

Fee Calculation

The fees for a subswap are paid by the maker from their BTC output. The fee is calculated by subtracting the total output value from the total input value.

This ensures that the taker receives the exact amount of Alkanes that they requested, without having to worry about paying any fees.