Extra-Ordinal! ZenGo’s Rust Library for Bitcoin Ordinals Trading

TL;DR: Bitcoin fees recently surged due to increased activity in Ordinals-enabled projects. In this blog we will explain how Ordinals-based Bitcoin NFTs work, how they can be traded via PSBT, share our contribution to the community in the form of an open source implementation of Ordinals’ PSBT trade, and make some predictions on Bitcoin NFT security.

Fungible and non-fungible tokens (NFTs)

Investopedia defines Fungibility as “the ability of a good or asset to be interchanged with other individual goods or assets of the same type. Fungible assets simplify the exchange and trade processes, as fungibility implies equal value between the assets.”

For a simple example of fungibility, think of dollar bills. Every $1 bill is worth the same as another, although one of them might be shiny and new and the other is already ragged. This makes fungibility a highly desired quality for currency, and therefore most popular cryptocurrencies, such as Bitcoin and Ethereum, are fungible.

However, some assets are not fungible by definition. For example an artwork cannot be interchanged with any other artwork. A ticket for a specific arena seat cannot be interchanged with any other ticket.

To represent this kind of Non-Fungible asset that is bounded to some content (such as images and other types of digital media or a real-world ID such as a concert seat number), Ethereum developers leveraged smart contract infrastructure to extend the blockchain’s capabilities and create NFTs (Non-Fungible Tokens) standards and projects, resulting in a booming industry of NFTs.

In contrast, Bitcoin has no smart contract support to similarly extend, and was left out of the NFT party, to the dismay of at least some Bitcoin parties. So how can non-fungibility and binding to content be introduced to Bitcoin. Enter ordinals!

Ordinals: Adding non-fungibility to Bitcoin

Following our example above, indeed every Satoshi (Bitcoin’s smallest unit: There are 100,000,000 Satoshis in every Bitcoin) can be thought of as a $1 bill and easily interchanged with any other Satoshi (AKA “Sat”). But $1 bills are not identical, each has a serial number that we usually ignore, but if we wanted we could uniquely identify each.

Ordinals do just that!

In a nutshell, Ordinals arbitrarily assigned “serial numbers” to every Sat (Bitcoin’s smallest unit).

Each Sat gets its Ordinal when it’s born (minted). Of course, users can just disregard that “serial number” (in the same manner we usually do with dollar bills) and continue to use Bitcoin as before.

However, if users take Ordinals’ “serial numbers” into account, this Bitcoin UTXO (unspent transaction output, or “bill”) is identified by the ordinal of its first Sat to become unique and can serve as a basis for non-fungible tokens. So how can we bind content to such non-fungible Ordinals Sats? Enter Inscriptions!

Inscriptions: Adding content to Bitcoin

Inscriptions leverage Bitcoin scripts that are present in every Bitcoin transaction, originally meant to be used as a way to add complex spending terms to Bitcoin transactions, to add some arbitrary content into Bitcoin blocks.

Inscription of “Hello, world!” NFT

The script code above never gets executed as it is wrapped within an if statement that is always false, enabling the inscriptor to add arbitrary NFT content as desired, such as all kinds of media types.

Inscriptions examples: https://ordinals.market/collection/ordinals/bitcoin-frogs?tab=items

This content is uniquely identified by the ordinal of the UTXO that created that inscription. This Bitcoin NFT can be transferred to another user by sending the UTXO to that user’s address.

Sending Ordinals example (Source: https://tara-annison.medium.com/a-comprehensive-explanation-of-ordinals-nfts-on-bitcoin-67b11868e74f )

​​As can be seen in the example above, Ordinals are transferred in order (“First-In, First-Out” or FIFO) from inputs to outputs. 

Trading Bitcoin NFTs

As we had just seen, transferring Bitcoin NFTs is easy, however trading introduces another challenge of “the fair exchange problem.”

The fair exchange problem is simple. There are two parties, Alice and Bob, each with some assets. Alice wants Bob’s assets and vice-versa. Alice can give her assets to Bob, but then she’s at a disadvantage because Bob can’t be trusted to provide what he owns. So who will go first?

This problem can be solved by a trusted third party, Charlie: Alice and Bob will both send their assets to Charlie, and then he will send each party the asset of the counterparty. If one fails to send their part before some pre-agreed timeout, Charlie will refund the asset he has received. Of course, Charlie needs to be trusted to behave faithfully and not run away with their assets.

To solve crypto trading without the need of an additional trusted third party, the exchange needs to be “atomic”: Either happen in full or not at all. To solve this problem in a general way, in ZenGo we had invented and patented the “JugglingSwap” protocol, a chain agnostic cross chain MPC based protocol for such atomic swaps. 

However, if we limit ourselves to trading Ordinals for Bitcoin only, we can settle for a simpler solution, based on the fact that a Bitcoin transaction can be composed of multiple inputs and outputs owned by different parties.

To create such an Atomic Swap in Bitcoin, Ordinals creators suggested using the PSBT construction (Partially Signed Bitcoin Transaction) that allows multiple parties to construct a transaction that is composed by multiple signers. Another common use case for PSBT is a MultiSig transaction in which multiple signers need to sign on the same transaction.

The general idea is straightforward: The Ordinal sellers create and sign a PSBT with their NFT as input, bind it to the required price as an output and list it on a marketplace. When buyers want to buy they take the PSBT, add their payment as input and their address to receive the Ordinal as output and complete the transaction to the full satisfaction of all parties.

Ideal ordinal transaction: Seller prepares the PSBT in yellow, Buyer adds the blue input and output

However, some technical difficulties arise as there seems to be some conflicting requirements:

  1. For sellers, to bind the Ordinal input with payment output, they MUST be “aligned”: in the same index of the transaction. As a result, both input and output need to be either first (0 index) or second (1 index).
  2. For buyers, in order to get the NFT and satisfy Ordinal’s FIFO requirement, in practice the “receive NFT” output MUST NOT be aligned with their payment, as otherwise it will cause the NFT to return to the seller.

To solve this problem, Ordinal creators added a dummy transaction to deconflict these requirements.

Practical Ordinal transaction: Dummy input is added

With this dummy input, sellers input and output are aligned, while buyers input and output are not aligned, as required.

ZenGo contribution: The Extra Ordinaire library

As part of our ongoing research in blockchain, we have created a world’s first implementation in Rust, known for its safety and performance, for such PSBT Odrinals’ trading.

Today, we share it as open source to support the community: https://github.com/ZenGo-X/extra-ordinaire

In the video above, you can see this project in action facilitating a Bitcoin NFT trade.

What’s next?

In ZenGo, we are closely monitoring advancements and trends in crypto and blockchains. We take a special interest in Ordinals’ progress, as it can be a natural fit for our multi-chain wallet, supporting Bitcoin on one hand and our NFT gallery on the other. As part of this effort we have contributed an open source implementation of Ordinals PSBT transactions.

We believe that if Bitcoin Ordinals NFTs become more mainstream, it will attract hackers and scammers in the same manner as Ethereum and other Web3 chains, causing multi-millions USD in damages. To mitigate this, we predict Ordinals will need the protection of a Web3 firewall, such as ZenGo’s ClearSign.

Since it is not clear yet if Ordinals NFTs are here to stay, or just a passing episode, we are closely monitoring its progress. Once Ordinals NFTs become mature enough, we plan to offer direct support for it within our wallet.