# sparse-merkle-tree-ts

> An optimized Sparse Merkle Tree.

Latest version **0.2.3** (published 2022-03-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install sparse-merkle-tree-ts
pnpm add sparse-merkle-tree-ts
yarn add sparse-merkle-tree-ts
bun add sparse-merkle-tree-ts
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2022-03-16 |
| First published | 2021-12-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 92.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | daryl |
| Maintainers | darylmoe |

## Links

- npm: https://www.npmjs.com/package/sparse-merkle-tree-ts
- Repository: https://github.com/Daryl-L/sparse-merkle-tree-ts
- Homepage: https://github.com/Daryl-L/sparse-merkle-tree-ts#readme
- Issues: https://github.com/Daryl-L/sparse-merkle-tree-ts/issues
- npm.io page: https://npm.io/package/sparse-merkle-tree-ts

## Dependencies (1)

- [@nervosnetwork/ckb-sdk-utils](https://npm.io/package/@nervosnetwork/ckb-sdk-utils.md) ^0.101.0

## Recent versions

- 0.2.3 (latest) — 2022-03-16
- 0.2.2 — 2022-03-15
- 0.2.1 — 2022-03-15
- 0.2.0 — 2022-03-11
- 0.1.1 — 2022-03-11
- 0.0.5 — 2021-12-16
- 0.0.4 — 2021-12-16
- 0.0.3 — 2021-12-16
- 0.0.2 — 2021-12-16
- 0.0.1 — 2021-12-16

## README

# Sparse Merkle Tree

An optimized Sparse Merkle Tree.

## Install

If you are using npm:

```
npm i sparse-merkle-tree-ts
```

If you are using yarn:

```
yarn add sparse-merkle-tree-ts
```

## Example

All branch nodes, branch keys and the leaf nodes are based on the `H256` class, which extends the `Uint8Array`.

For the hasher, you can use your own implementation, just implements the `Hasher`.

```typescript
import { Blake2b } from '@nervosnetwork/ckb-sdk-utils/lib/crypto/blake2b';
import { H256, Hasher, SparseMerkleTree } from '../lib'
import * as util from '@nervosnetwork/ckb-sdk-utils';

// your own implamentation of hasher.
class Blake2bHasher extends Hasher {
  hasher: Blake2b;

  constructor() {
    super();

    this.hasher = util.blake2b(32, null, null, new TextEncoder().encode('ckb-default-hash'));
  }

  update(h: H256): this {
    this.hasher.update(h);

    return this; 
  }

  final(): H256 {
    return new H256(this.hasher.final('binary') as Uint8Array);
  }
}

let auth_smt_value = H256.zero();
auth_smt_value[0] = 1;

let auth_smt_key = new H256([
  6, 18, 52, 86, 120, 144, 18, 52, 86, 120, 144, 18, 52, 86, 120, 144, 18, 52, 86, 120, 144, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);

let key_on_wl1 = new H256([
  111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0,
]);

let key_on_wl2 = new H256([
  222, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0,
]);


let tree = new SparseMerkleTree(() => new Blake2bHasher);
tree.update(key_on_wl1, auth_smt_value);
tree.update(key_on_wl2, auth_smt_value);
tree.update(auth_smt_key, auth_smt_value);

let root = tree.root;
console.log('0x' + Array.from(root).map(x => x.toString(16).padStart(2, '0')).join(''));

let proof = tree.merkle_proof([auth_smt_key]);
let compiled_proof = proof.compile([[auth_smt_key, auth_smt_value]]);
console.log('0x' + compiled_proof.map(x => x.toString(16).padStart(2, '0')).join(''));
console.log('0x' + Array.from(compiled_proof.compute_root([[auth_smt_key, auth_smt_value]])).map(x => x.toString(16).padStart(2, '0')).join(''));
```

And you can see the output

```
0x4cfe0f79bec46e9b111b6ed4a87a301a2058d63b1cbf9867b581a2cbfebf8c02
0x4c4fa7519f5613e03f3c0ed354d1491b0eb58705a091523f770bcecef276dd902d25d25e4100000000000000000000000000000000000000000000000000000000000000004f58
0x4cfe0f79bec46e9b111b6ed4a87a301a2058d63b1cbf9867b581a2cbfebf8c02
```

To verify the given root, just use

```typescript
compiled_proof.compute_root([[auth_smt_key, auth_smt_value]]).toString() == root.toString();
```

---
_Source: https://npm.io/package/sparse-merkle-tree-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
