@chainsafe/persistent-merkle-tree
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -0,1 +1,7 @@ | ||
## 0.1.3 (2020-06-07) | ||
### Chores | ||
* remove bigint literals ([461fb7](https://github.com/persistent-merkle-tree/commit/461fb7)) | ||
## 0.1.2 (2020-02-26) | ||
@@ -2,0 +8,0 @@ |
@@ -8,3 +8,3 @@ "use strict"; | ||
function toGindex(index, depth) { | ||
const anchor = 1n << BigInt(depth); | ||
const anchor = BigInt(1) << BigInt(depth); | ||
if (index >= anchor) { | ||
@@ -32,3 +32,3 @@ throw new Error("index too large for depth"); | ||
} | ||
return (count - 1n).toString(2).length; | ||
return (count - BigInt(1)).toString(2).length; | ||
} | ||
@@ -40,3 +40,3 @@ exports.countToDepth = countToDepth; | ||
function iterateAtDepth(startIndex, count, depth) { | ||
const anchor = 1n << BigInt(depth); | ||
const anchor = BigInt(1) << BigInt(depth); | ||
if (startIndex + count >= anchor) { | ||
@@ -43,0 +43,0 @@ throw new Error("Too large for depth"); |
{ | ||
"name": "@chainsafe/persistent-merkle-tree", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Merkle tree implemented as a persistent datastructure", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,2 +5,66 @@ # Persistent Merkle Tree | ||
## Example | ||
```typescript | ||
// LeafNode and BranchNode are used to build nodes in a tree | ||
// Nodes may not be changed once initialized | ||
import {LeafNode, BranchNode} from "@chainsafe/persistent-merkle-tree"; | ||
const leaf = new LeafNode(Uint8Array.from([...])); | ||
const otherLeaf = new LeafNode(Uint8Array.from([...])); | ||
const branch = new BranchNode(leaf, otherLeaf); | ||
// The `root` property returns the merkle root of a Node | ||
const r: Uint8Array = branch.root; // == hash(leaf.root, otherLeaf.root)); | ||
// The `isLeaf` method returns true if the Node is a LeafNode | ||
branch.isLeaf() === false; | ||
leaf.isLeaf() === true; | ||
// Well-known zero nodes are provided | ||
import {zeroNode} from "@chainsafe/persistent-merkle-tree"; | ||
// 0x0 | ||
const zero0 = zeroNode(0); | ||
// hash(0, 0) | ||
const zero1 = zeroNode(1); | ||
// hash(hash(0, 0), hash(0, 0)) | ||
const zero1 = zeroNode(2); | ||
// Tree provides a mutable wrapper around a "root" Node | ||
import {Tree} from "@chainsafe/persistent-merkle-tree"; | ||
const tree = new Tree(zeroNode(10)); | ||
// `rootNode` property returns the root Node of a Tree | ||
const rootNode: Node = tree.rootNode; | ||
// `root` property returns the merkle root of a Tree | ||
const rr: Uint8Array = tree.root; | ||
// A Tree is navigated by Gindex | ||
const gindex = BigInt(...); | ||
const n: Node = tree.getNode(gindex); // the Node at gindex | ||
const rrr: Uint8Array = tree.getRoot(gindex); // the Uint8Array root at gindex | ||
const subtree: Tree = tree.getSubtree(gindex); // the Tree wrapping the Node at gindex | ||
// A merkle proof for a gindex can be generated | ||
const proof: Uint8Array[] = tree.getSingleProof(gindex); | ||
``` | ||
## Motivation | ||
@@ -27,4 +91,12 @@ | ||
## See also: | ||
https://github.com/protolambda/remerkleable | ||
### Audit | ||
This repo was audited by Least Authority as part of [this security audit](https://github.com/ChainSafe/lodestar/blob/master/audits/2020-03-23_UTILITY_LIBRARIES.pdf), released 2020-03-23. Commit [`8b5ad7`](https://github.com/ChainSafe/bls-hd-key/commit/8b5ad7) verified in the report. | ||
## License | ||
Apache-2.0 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30959
101