# Hemi Viem

[![NPM version](https://img.shields.io/npm/v/hemi-viem)](https://www.npmjs.com/package/hemi-viem) [![Package size](https://img.shields.io/bundlephobia/minzip/hemi-viem)](https://bundlephobia.com/package/hemi-viem) [![Follow Hemi on X](https://img.shields.io/twitter/url?url=https%3A%2F%2Fx.com%2Fhemi_xyz&style=flat&logo=x&label=%40hemi_xyz&labelColor=%23ff6c15&color=%230a0a0a)](https://x.com/intent/follow?screen_name=hemi_xyz)

[Viem](https://viem.sh/) extension for the [Hemi](https://hemi.xyz/) network.
It includes:

- Chain definitions!
- [Bitcoin finality](https://docs.hemi.xyz/foundational-topics/pop-consensus-and-bitcoin-finality) helper!
- [Bitcoin Kit](https://docs.hemi.xyz/building-bitcoin-apps/hemi-bitcoin-kit-hbk) wrappers!

## Installation

```sh
npm install viem hemi-viem
```

### Example

```js
// example.js

import { createPublicClient, http } from "viem";

import {
  hemiPublicBitcoinKitActions,
  hemiPublicOpNodeActions,
  hemiSepolia,
} from "hemi-viem";

const parameters = { chain: hemiSepolia, transport: http() };
const client = createPublicClient(parameters)
  .extend(hemiPublicOpNodeActions())
  .extend(hemiPublicBitcoinKitActions());

const blockNumber = await client.getBlockNumber();
const block = await client.getBlock({ blockNumber: blockNumber - 100n });

const btcFinality = await client.getBtcFinalityByBlockHash(block);
console.log(btcFinality);

const bitcoinKitAddress = "0x0000000000000000000000000000000000000000"; // example address
const btcHeader = await client.getLastHeader({ bitcoinKitAddress });
console.log(btcHeader);
```

Output:

```console
$ node example.js
{
  l2_keystone: {
    version: 1,
    l1_block_number: 5868140,
    l2_block_number: 596350,
    parent_ep_hash: '3fe407f9eec38ce9fa0f6d159adca4ac8739013d380e1d7215b721c7e345ae88',
    prev_ep_keystone_hash: '6b65018eb33048494f2e21ffdd081ae376b125f12e62ab88e77b1461503540fb',
    state_root: 'b3b9f5b810038466290a7c4bff08a260a472d67fed230a27d89de1322490fb51',
    ep_hash: 'd186c2da80f2c79490f644e4af714ee1f5790a4e806028cac0e64a2010a41974'
  },
  btc_pub_height: 2814228,
  btc_pub_header_hash: '1a262fa78618f4b7fa8d5d37eed32f3a0bb67ab09d808e035500000000000000',
  btc_finality: 8
}
{
  height: 2814236,
  blockHash: '0x00000000f71ba71af42fb2a356affa1439bc2adea0bf67880fc30f28237622e7',
  version: 536870912,
  previousBlockHash: '0x000000000000002f3cc7a5f9ed589b734808fff6d67c0f8a9385f6146c80297b',
  merkleRoot: '0x1799d4002cd58d15a764cee0919edf358bf36ace4fa4c6e78250336eafe12184',
  timestamp: 1715273381,
  bits: 486604799,
  nonce: 4137790000
}
```

## Development

This project uses [pnpm](https://pnpm.io/) as its package manager. The exact version is pinned in the `packageManager` field of `package.json`.

### Enabling pnpm with corepack

[Corepack](https://nodejs.org/api/corepack.html) ships with Node.js and manages the pnpm version for you, so it always matches the one pinned in this repo. Just enable it once:

```sh
corepack enable
```

From then on, running any `pnpm` command inside the project automatically uses the pinned version—no manual install needed.

### Common commands

```sh
pnpm install         # install dependencies
pnpm run build       # build ESM, CJS and type declarations
pnpm test            # run unit tests
pnpm run test:e2e    # run end-to-end tests
pnpm run lint        # lint with ESLint
pnpm run format:check # check formatting with Prettier
pnpm run tsc         # type-check without emitting
```
