- Bitcoin
- Ethereum
- Hemi
- Learn Center
- October 17, 2024
How the Hemi Virtual Machine (hVM) Works
The hVM is a central part of the Hemi Network. Here’s how it works.
The Hemi Virtual Machine (hVM) is an extended version of the Ethereum Virtual Machine (EVM), equipped with native Bitcoin interoperability. It allows developers to build decentralized applications (dApps) that can access and interact with Bitcoin’s state directly within Ethereum-like environments. By leveraging hVM, developers can bypass limitations of previous Bitcoin interoperability solutions and build a wide range of Bitcoin-aware applications. This guide explains the key features of hVM, how it works, and how developers can use it in their dApps.
What Is the hVM?
At its core, hVM upgrades the EVM by embedding a full, indexed Bitcoin node into the EVM environment. This is achieved through a custom “Tiny Bitcoin” (TBC) process, which synchronizes with the Bitcoin network and feeds data into the Hemi network. This enables all Hemi nodes to share a synchronized view of the Bitcoin state, referred to as the “Processed Bitcoin View,” ensuring consistency and reliability across the network.
Tiny Bitcoin (TBC) Process
The TBC process is a critical part of hVM. It connects to the Bitcoin network and syncs with Bitcoin blocks, keeping the network updated in real-time. When a Hemi Sequencer creates a block, they can include new Bitcoin block headers, allowing the Hemi network to maintain an up-to-date and indexed Bitcoin state. The TBC daemon runs directly inside each hVM node, making Bitcoin data accessible via precompiles instead of relying on third-party systems.
Processed Bitcoin View
The Processed Bitcoin View is a shared state across all Hemi nodes, created when the Hemi network processes Bitcoin blocks at the same Hemi block height. This ensures that all Bitcoin-aware smart contracts on Hemi execute in a deterministic manner, meaning the outcome will always be the same, regardless of which node processes the contract.
The Processed Bitcoin View offers a number of advantages over older Bitcoin-Ethereum interoperability solutions, such as header relay systems. With hVM, developers can access the full Bitcoin state, including UTXOs, balances, and transaction data, without relying on external relayers or validators.
Accessing Bitcoin Data with hVM
To enable smart contracts to interact with Bitcoin, hVM introduces new precompiles. These are special addresses that smart contracts can call to fetch data from the embedded Bitcoin node. The current set of precompiles, referred to as “Phase 0,” includes:
- BtcBalAddr (0x40): Fetches the Bitcoin balance of any address in satoshis. Supports various Bitcoin address formats.
- BtcUtxosAddrList (0x41): Retrieves the list of Unspent Transaction Outputs (UTXOs) for a given address.
- BtcTxByTxid (0x42): Returns parts of a Bitcoin transaction based on its TxID.
- BtcTxConfirmations (0x43): Fetches the number of confirmations a transaction has received.
- BtcLastHeader (0x44): Returns the most recent Bitcoin block header known to hVM.
- BtcHeaderN (0x45): Returns the Bitcoin header at a specific block height.
- BtcAddrToScript (0x46): Converts a Bitcoin address into its corresponding script format.
These precompiles allow developers to interact with Bitcoin data in their dApps, offering powerful new capabilities for Bitcoin-Ethereum interoperability.
How to Use the Precompiles
To use hVM directly, developers must call the appropriate precompile address, passing in serialized bytes as parameters. Each precompile call will return serialized bytes that contain the requested data, such as Bitcoin balances, UTXO lists, or transaction details.
Hemi Bitcoin Kit (hBK)
While developers can interact with hVM directly, most will prefer to use the Hemi Bitcoin Kit (hBK), which abstracts much of the complexity involved in working with Bitcoin data on hVM. hBK provides a set of tools and libraries that make it easier to develop Bitcoin-aware dApps without having to manually handle the serialization and precompile calls. However, for advanced users looking to maximize gas efficiency, using hVM directly can reduce overhead and offer more granular control.
Benefits of hVM over Existing Solutions
hVM overcomes many limitations of previous Bitcoin-Ethereum interoperability systems:
- No Relayer System: hVM does not rely on third-party relayers to provide Bitcoin data, removing the need for trust assumptions and external dependencies.
- Efficient and Trustless: hVM directly processes Bitcoin blocks, eliminating the need for costly inclusion proofs or zk-SNARKs.
- Comprehensive Bitcoin State Access: Developers can access the entire Bitcoin state, including UTXOs, balances, and transaction metadata, enabling a wide range of applications that were previously infeasible.
Use Cases for hVM
hVM opens the door to numerous potential applications that leverage Bitcoin’s decentralized network in combination with the flexibility of the EVM:
- Non-custodial BTC-ETH exchanges: Seamlessly swap Bitcoin and Ethereum assets without relying on centralized custodians.
- Bitcoin MEV (Miner Extractable Value) marketplaces: Create new markets for Bitcoin transaction ordering.
- Bitcoin lending markets: Enable decentralized lending and borrowing using Bitcoin as collateral.
- Custom Bitcoin smart-wallets: Build wallets with configurable validator sets and enhanced security.
- Bitcoin tunnels: Create custom interoperability solutions that allow assets and data to move between Bitcoin and Ethereum networks in a trustless way.
Getting Started
To start developing on hVM, developers can either call precompiles directly or use the Hemi Bitcoin Kit (hBK) for a more streamlined experience. The hBK provides a more user-friendly interface for interacting with Bitcoin data while still leveraging the power of hVM.
Future protocol upgrades will introduce additional features, including support for Bitcoin metaprotocols like Ordinals, BRC-20s, and Runes. These upgrades will further expand the range of dApps that can be built on Hemi, solidifying hVM as the leading platform for Bitcoin-Ethereum interoperability.
hVM represents a breakthrough in Bitcoin interoperability, offering developers a powerful, trustless platform to build Bitcoin-aware dApps. By embedding a full indexed Bitcoin node directly into the EVM, hVM provides a flexible, efficient, and comprehensive solution for accessing Bitcoin data on Ethereum-like networks. Whether you’re building non-custodial exchanges, custom wallets, or lending platforms, hVM gives you the tools to bring Bitcoin into the world of smart contracts.