@feelyourprotocol/binarytree 8141.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +46 -0
- package/dist/cjs/binaryTree.d.ts +167 -0
- package/dist/cjs/binaryTree.d.ts.map +1 -0
- package/dist/cjs/binaryTree.js +606 -0
- package/dist/cjs/binaryTree.js.map +1 -0
- package/dist/cjs/constructors.d.ts +4 -0
- package/dist/cjs/constructors.d.ts.map +1 -0
- package/dist/cjs/constructors.js +44 -0
- package/dist/cjs/constructors.js.map +1 -0
- package/dist/cjs/db/checkpoint.d.ts +87 -0
- package/dist/cjs/db/checkpoint.d.ts.map +1 -0
- package/dist/cjs/db/checkpoint.js +257 -0
- package/dist/cjs/db/checkpoint.js.map +1 -0
- package/dist/cjs/db/index.d.ts +2 -0
- package/dist/cjs/db/index.d.ts.map +1 -0
- package/dist/cjs/db/index.js +18 -0
- package/dist/cjs/db/index.js.map +1 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/node/index.d.ts +4 -0
- package/dist/cjs/node/index.d.ts.map +1 -0
- package/dist/cjs/node/index.js +20 -0
- package/dist/cjs/node/index.js.map +1 -0
- package/dist/cjs/node/internalNode.d.ts +37 -0
- package/dist/cjs/node/internalNode.d.ts.map +1 -0
- package/dist/cjs/node/internalNode.js +92 -0
- package/dist/cjs/node/internalNode.js.map +1 -0
- package/dist/cjs/node/stemNode.d.ts +34 -0
- package/dist/cjs/node/stemNode.d.ts.map +1 -0
- package/dist/cjs/node/stemNode.js +75 -0
- package/dist/cjs/node/stemNode.js.map +1 -0
- package/dist/cjs/node/types.d.ts +35 -0
- package/dist/cjs/node/types.d.ts.map +1 -0
- package/dist/cjs/node/types.js +9 -0
- package/dist/cjs/node/types.js.map +1 -0
- package/dist/cjs/node/util.d.ts +9 -0
- package/dist/cjs/node/util.d.ts.map +1 -0
- package/dist/cjs/node/util.js +40 -0
- package/dist/cjs/node/util.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/proof.d.ts +16 -0
- package/dist/cjs/proof.d.ts.map +1 -0
- package/dist/cjs/proof.js +49 -0
- package/dist/cjs/proof.js.map +1 -0
- package/dist/cjs/types.d.ts +45 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +6 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/util.d.ts +17 -0
- package/dist/cjs/util.d.ts.map +1 -0
- package/dist/cjs/util.js +71 -0
- package/dist/cjs/util.js.map +1 -0
- package/dist/esm/binaryTree.d.ts +167 -0
- package/dist/esm/binaryTree.d.ts.map +1 -0
- package/dist/esm/binaryTree.js +602 -0
- package/dist/esm/binaryTree.js.map +1 -0
- package/dist/esm/constructors.d.ts +4 -0
- package/dist/esm/constructors.d.ts.map +1 -0
- package/dist/esm/constructors.js +41 -0
- package/dist/esm/constructors.js.map +1 -0
- package/dist/esm/db/checkpoint.d.ts +87 -0
- package/dist/esm/db/checkpoint.d.ts.map +1 -0
- package/dist/esm/db/checkpoint.js +253 -0
- package/dist/esm/db/checkpoint.js.map +1 -0
- package/dist/esm/db/index.d.ts +2 -0
- package/dist/esm/db/index.d.ts.map +1 -0
- package/dist/esm/db/index.js +2 -0
- package/dist/esm/db/index.js.map +1 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/node/index.d.ts +4 -0
- package/dist/esm/node/index.d.ts.map +1 -0
- package/dist/esm/node/index.js +4 -0
- package/dist/esm/node/index.js.map +1 -0
- package/dist/esm/node/internalNode.d.ts +37 -0
- package/dist/esm/node/internalNode.d.ts.map +1 -0
- package/dist/esm/node/internalNode.js +88 -0
- package/dist/esm/node/internalNode.js.map +1 -0
- package/dist/esm/node/stemNode.d.ts +34 -0
- package/dist/esm/node/stemNode.d.ts.map +1 -0
- package/dist/esm/node/stemNode.js +71 -0
- package/dist/esm/node/stemNode.js.map +1 -0
- package/dist/esm/node/types.d.ts +35 -0
- package/dist/esm/node/types.d.ts.map +1 -0
- package/dist/esm/node/types.js +6 -0
- package/dist/esm/node/types.js.map +1 -0
- package/dist/esm/node/util.d.ts +9 -0
- package/dist/esm/node/util.d.ts.map +1 -0
- package/dist/esm/node/util.js +33 -0
- package/dist/esm/node/util.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/proof.d.ts +16 -0
- package/dist/esm/proof.d.ts.map +1 -0
- package/dist/esm/proof.js +45 -0
- package/dist/esm/proof.js.map +1 -0
- package/dist/esm/types.d.ts +45 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +3 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/util.d.ts +17 -0
- package/dist/esm/util.d.ts.map +1 -0
- package/dist/esm/util.js +66 -0
- package/dist/esm/util.js.map +1 -0
- package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
- package/package.json +77 -0
- package/src/binaryTree.ts +742 -0
- package/src/constructors.ts +50 -0
- package/src/db/checkpoint.ts +297 -0
- package/src/db/index.ts +1 -0
- package/src/index.ts +6 -0
- package/src/node/index.ts +3 -0
- package/src/node/internalNode.ts +112 -0
- package/src/node/stemNode.ts +87 -0
- package/src/node/types.ts +41 -0
- package/src/node/util.ts +38 -0
- package/src/proof.ts +54 -0
- package/src/types.ts +58 -0
- package/src/util.ts +80 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { BinaryNodeType } from './types.ts';
|
|
2
|
+
import type { BinaryNodeOptions } from './types.ts';
|
|
3
|
+
export declare class StemBinaryNode {
|
|
4
|
+
stem: Uint8Array;
|
|
5
|
+
values: (Uint8Array | null)[];
|
|
6
|
+
type: 1;
|
|
7
|
+
constructor(options: BinaryNodeOptions[typeof BinaryNodeType.Stem]);
|
|
8
|
+
static fromRawNode(rawNode: Uint8Array[]): StemBinaryNode;
|
|
9
|
+
/**
|
|
10
|
+
* Generates a new Stem node
|
|
11
|
+
* @param stem the 31 byte stem corresponding to the where the stem node is located in the tree
|
|
12
|
+
* @returns a new Stem node
|
|
13
|
+
*/
|
|
14
|
+
static create(stem: Uint8Array): StemBinaryNode;
|
|
15
|
+
getValue(index: number): Uint8Array | null;
|
|
16
|
+
setValue(index: number, value: Uint8Array | null): void;
|
|
17
|
+
/**
|
|
18
|
+
* @returns the RLP serialized node
|
|
19
|
+
*/
|
|
20
|
+
serialize(): Uint8Array;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the raw serialized representation of the node as an array of Uint8Arrays.
|
|
23
|
+
* The returned array is constructed as follows:
|
|
24
|
+
* - The first element is a Uint8Array containing a single byte that represents the node type,
|
|
25
|
+
* - The second element is the node's `stem` property.
|
|
26
|
+
* - The remaining elements are derived from the node's `values` array:
|
|
27
|
+
* - For each value, if it is `null`, it is converted to an empty Uint8Array.
|
|
28
|
+
* - Otherwise, the value is included as-is.
|
|
29
|
+
*
|
|
30
|
+
* @returns {Uint8Array[]} An array of Uint8Arrays representing the node's raw data.
|
|
31
|
+
*/
|
|
32
|
+
raw(): Uint8Array[];
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=stemNode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stemNode.d.ts","sourceRoot":"","sources":["../../../src/node/stemNode.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAc,MAAM,YAAY,CAAA;AAEvD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEnD,qBAAa,cAAc;IAClB,IAAI,EAAE,UAAU,CAAA;IAChB,MAAM,EAAE,CAAC,UAAU,GAAG,IAAI,CAAC,EAAE,CAAA;IAE7B,IAAI,IAAsB;gBAErB,OAAO,EAAE,iBAAiB,CAAC,OAAO,cAAc,CAAC,IAAI,CAAC;IAKlE,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,cAAc;IAkBzD;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,UAAU,GAAG,cAAc;IAK/C,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI;IAI1C,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI,GAAG,IAAI;IAIvD;;OAEG;IACH,SAAS,IAAI,UAAU;IAIvB;;;;;;;;;;OAUG;IACH,GAAG,IAAI,UAAU,EAAE;CAcpB"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StemBinaryNode = void 0;
|
|
4
|
+
const rlp_1 = require("@feelyourprotocol/rlp");
|
|
5
|
+
const util_1 = require("@feelyourprotocol/util");
|
|
6
|
+
const types_ts_1 = require("./types.js");
|
|
7
|
+
class StemBinaryNode {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.type = types_ts_1.BinaryNodeType.Stem;
|
|
10
|
+
this.stem = options.stem;
|
|
11
|
+
this.values = options.values ?? new Array(256).fill(null);
|
|
12
|
+
}
|
|
13
|
+
static fromRawNode(rawNode) {
|
|
14
|
+
const nodeType = rawNode[0][0];
|
|
15
|
+
if (nodeType !== types_ts_1.BinaryNodeType.Stem) {
|
|
16
|
+
throw (0, util_1.EthereumJSErrorWithoutCode)('Invalid node type');
|
|
17
|
+
}
|
|
18
|
+
// The length of the rawNode should be the # of values (node width) + 2 for the node type and the stem
|
|
19
|
+
if (rawNode.length !== types_ts_1.NODE_WIDTH + 2) {
|
|
20
|
+
throw (0, util_1.EthereumJSErrorWithoutCode)('Invalid node length');
|
|
21
|
+
}
|
|
22
|
+
const stem = rawNode[1];
|
|
23
|
+
const rawValues = rawNode.slice(2, rawNode.length);
|
|
24
|
+
const values = rawValues.map((el) => (el.length === 0 ? null : el));
|
|
25
|
+
return new StemBinaryNode({ stem, values });
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Generates a new Stem node
|
|
29
|
+
* @param stem the 31 byte stem corresponding to the where the stem node is located in the tree
|
|
30
|
+
* @returns a new Stem node
|
|
31
|
+
*/
|
|
32
|
+
static create(stem) {
|
|
33
|
+
return new StemBinaryNode({ stem });
|
|
34
|
+
}
|
|
35
|
+
// Retrieve the value at the provided index from the values array
|
|
36
|
+
getValue(index) {
|
|
37
|
+
return this.values[index];
|
|
38
|
+
}
|
|
39
|
+
setValue(index, value) {
|
|
40
|
+
this.values[index] = value;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @returns the RLP serialized node
|
|
44
|
+
*/
|
|
45
|
+
serialize() {
|
|
46
|
+
return rlp_1.RLP.encode(this.raw());
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns the raw serialized representation of the node as an array of Uint8Arrays.
|
|
50
|
+
* The returned array is constructed as follows:
|
|
51
|
+
* - The first element is a Uint8Array containing a single byte that represents the node type,
|
|
52
|
+
* - The second element is the node's `stem` property.
|
|
53
|
+
* - The remaining elements are derived from the node's `values` array:
|
|
54
|
+
* - For each value, if it is `null`, it is converted to an empty Uint8Array.
|
|
55
|
+
* - Otherwise, the value is included as-is.
|
|
56
|
+
*
|
|
57
|
+
* @returns {Uint8Array[]} An array of Uint8Arrays representing the node's raw data.
|
|
58
|
+
*/
|
|
59
|
+
raw() {
|
|
60
|
+
return [
|
|
61
|
+
new Uint8Array([types_ts_1.BinaryNodeType.Stem]),
|
|
62
|
+
this.stem,
|
|
63
|
+
...this.values.map((val) => {
|
|
64
|
+
switch (val) {
|
|
65
|
+
case null:
|
|
66
|
+
return new Uint8Array();
|
|
67
|
+
default:
|
|
68
|
+
return val;
|
|
69
|
+
}
|
|
70
|
+
}),
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.StemBinaryNode = StemBinaryNode;
|
|
75
|
+
//# sourceMappingURL=stemNode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stemNode.js","sourceRoot":"","sources":["../../../src/node/stemNode.ts"],"names":[],"mappings":";;;AAAA,yCAAqC;AACrC,2CAA6D;AAE7D,yCAAuD;AAIvD,MAAa,cAAc;IAMzB,YAAY,OAAsD;QAF3D,SAAI,GAAG,yBAAc,CAAC,IAAI,CAAA;QAG/B,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC3D,CAAC;IAED,MAAM,CAAC,WAAW,CAAC,OAAqB;QACtC,MAAM,QAAQ,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAC9B,IAAI,QAAQ,KAAK,yBAAc,CAAC,IAAI,EAAE,CAAC;YACrC,MAAM,IAAA,iCAA0B,EAAC,mBAAmB,CAAC,CAAA;QACvD,CAAC;QAED,sGAAsG;QACtG,IAAI,OAAO,CAAC,MAAM,KAAK,qBAAU,GAAG,CAAC,EAAE,CAAC;YACtC,MAAM,IAAA,iCAA0B,EAAC,qBAAqB,CAAC,CAAA;QACzD,CAAC;QAED,MAAM,IAAI,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;QACvB,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAA;QAClD,MAAM,MAAM,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;QAEnE,OAAO,IAAI,cAAc,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAA;IAC7C,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,IAAgB;QAC5B,OAAO,IAAI,cAAc,CAAC,EAAE,IAAI,EAAE,CAAC,CAAA;IACrC,CAAC;IAED,iEAAiE;IACjE,QAAQ,CAAC,KAAa;QACpB,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;IAED,QAAQ,CAAC,KAAa,EAAE,KAAwB;QAC9C,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAA;IAC5B,CAAC;IAED;;OAEG;IACH,SAAS;QACP,OAAO,SAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;IAC/B,CAAC;IAED;;;;;;;;;;OAUG;IACH,GAAG;QACD,OAAO;YACL,IAAI,UAAU,CAAC,CAAC,yBAAc,CAAC,IAAI,CAAC,CAAC;YACrC,IAAI,CAAC,IAAI;YACT,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;gBACzB,QAAQ,GAAG,EAAE,CAAC;oBACZ,KAAK,IAAI;wBACP,OAAO,IAAI,UAAU,EAAE,CAAA;oBACzB;wBACE,OAAO,GAAG,CAAA;gBACd,CAAC;YACH,CAAC,CAAC;SACH,CAAA;IACH,CAAC;CACF;AA/ED,wCA+EC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { InternalBinaryNode } from './internalNode.ts';
|
|
2
|
+
import type { StemBinaryNode } from './stemNode.ts';
|
|
3
|
+
export type BinaryNodeType = (typeof BinaryNodeType)[keyof typeof BinaryNodeType];
|
|
4
|
+
export declare const BinaryNodeType: {
|
|
5
|
+
readonly Internal: 0;
|
|
6
|
+
readonly Stem: 1;
|
|
7
|
+
};
|
|
8
|
+
export interface TypedBinaryNode {
|
|
9
|
+
[BinaryNodeType.Internal]: InternalBinaryNode;
|
|
10
|
+
[BinaryNodeType.Stem]: StemBinaryNode;
|
|
11
|
+
}
|
|
12
|
+
export type BinaryNode = TypedBinaryNode[BinaryNodeType];
|
|
13
|
+
/**
|
|
14
|
+
* @dev A child node in a binary tree internal node.
|
|
15
|
+
* @param hash The hash of the child node.
|
|
16
|
+
* @param path The path to the child node, in bits.
|
|
17
|
+
* */
|
|
18
|
+
export type ChildBinaryNode = {
|
|
19
|
+
hash: Uint8Array;
|
|
20
|
+
path: number[];
|
|
21
|
+
};
|
|
22
|
+
interface InternalBinaryNodeOptions {
|
|
23
|
+
children?: (ChildBinaryNode | null)[];
|
|
24
|
+
}
|
|
25
|
+
interface StemBinaryNodeOptions {
|
|
26
|
+
stem: Uint8Array;
|
|
27
|
+
values?: (Uint8Array | null)[];
|
|
28
|
+
}
|
|
29
|
+
export interface BinaryNodeOptions {
|
|
30
|
+
[BinaryNodeType.Internal]: InternalBinaryNodeOptions;
|
|
31
|
+
[BinaryNodeType.Stem]: StemBinaryNodeOptions;
|
|
32
|
+
}
|
|
33
|
+
export declare const NODE_WIDTH = 256;
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/node/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAEnD,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,OAAO,cAAc,CAAC,CAAA;AACjF,eAAO,MAAM,cAAc;;;CAGjB,CAAA;AAEV,MAAM,WAAW,eAAe;IAC9B,CAAC,cAAc,CAAC,QAAQ,CAAC,EAAE,kBAAkB,CAAA;IAC7C,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,cAAc,CAAA;CACtC;AAED,MAAM,MAAM,UAAU,GAAG,eAAe,CAAC,cAAc,CAAC,CAAA;AAExD;;;;KAIK;AACL,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,UAAU,CAAA;IAChB,IAAI,EAAE,MAAM,EAAE,CAAA;CACf,CAAA;AAED,UAAU,yBAAyB;IACjC,QAAQ,CAAC,EAAE,CAAC,eAAe,GAAG,IAAI,CAAC,EAAE,CAAA;CACtC;AAED,UAAU,qBAAqB;IAC7B,IAAI,EAAE,UAAU,CAAA;IAChB,MAAM,CAAC,EAAE,CAAC,UAAU,GAAG,IAAI,CAAC,EAAE,CAAA;CAC/B;AAED,MAAM,WAAW,iBAAiB;IAChC,CAAC,cAAc,CAAC,QAAQ,CAAC,EAAE,yBAAyB,CAAA;IACpD,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE,qBAAqB,CAAA;CAC7C;AAED,eAAO,MAAM,UAAU,MAAM,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/node/types.ts"],"names":[],"mappings":";;;AAIa,QAAA,cAAc,GAAG;IAC5B,QAAQ,EAAE,CAAC;IACX,IAAI,EAAE,CAAC;CACC,CAAA;AAiCG,QAAA,UAAU,GAAG,GAAG,CAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { InternalBinaryNode } from './internalNode.ts';
|
|
2
|
+
import { StemBinaryNode } from './stemNode.ts';
|
|
3
|
+
import { type BinaryNode } from './types.ts';
|
|
4
|
+
export declare function decodeRawBinaryNode(raw: Uint8Array[]): BinaryNode;
|
|
5
|
+
export declare function decodeBinaryNode(raw: Uint8Array): BinaryNode;
|
|
6
|
+
export declare function isRawBinaryNode(node: Uint8Array | Uint8Array[]): node is Uint8Array[];
|
|
7
|
+
export declare function isInternalBinaryNode(node: BinaryNode): node is InternalBinaryNode;
|
|
8
|
+
export declare function isStemBinaryNode(node: BinaryNode): node is StemBinaryNode;
|
|
9
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/node/util.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,EAAE,KAAK,UAAU,EAAkB,MAAM,YAAY,CAAA;AAE5D,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,UAAU,CAUjE;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,UAAU,cAM/C;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,EAAE,GAAG,IAAI,IAAI,UAAU,EAAE,CAErF;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,IAAI,kBAAkB,CAEjF;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,IAAI,cAAc,CAEzE"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeRawBinaryNode = decodeRawBinaryNode;
|
|
4
|
+
exports.decodeBinaryNode = decodeBinaryNode;
|
|
5
|
+
exports.isRawBinaryNode = isRawBinaryNode;
|
|
6
|
+
exports.isInternalBinaryNode = isInternalBinaryNode;
|
|
7
|
+
exports.isStemBinaryNode = isStemBinaryNode;
|
|
8
|
+
const rlp_1 = require("@feelyourprotocol/rlp");
|
|
9
|
+
const util_1 = require("@feelyourprotocol/util");
|
|
10
|
+
const internalNode_ts_1 = require("./internalNode.js");
|
|
11
|
+
const stemNode_ts_1 = require("./stemNode.js");
|
|
12
|
+
const types_ts_1 = require("./types.js");
|
|
13
|
+
function decodeRawBinaryNode(raw) {
|
|
14
|
+
const nodeType = raw[0][0];
|
|
15
|
+
switch (nodeType) {
|
|
16
|
+
case types_ts_1.BinaryNodeType.Internal:
|
|
17
|
+
return internalNode_ts_1.InternalBinaryNode.fromRawNode(raw);
|
|
18
|
+
case types_ts_1.BinaryNodeType.Stem:
|
|
19
|
+
return stemNode_ts_1.StemBinaryNode.fromRawNode(raw);
|
|
20
|
+
default:
|
|
21
|
+
throw (0, util_1.EthereumJSErrorWithoutCode)('Invalid node type');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function decodeBinaryNode(raw) {
|
|
25
|
+
const decoded = rlp_1.RLP.decode(Uint8Array.from(raw));
|
|
26
|
+
if (!Array.isArray(decoded)) {
|
|
27
|
+
throw (0, util_1.EthereumJSErrorWithoutCode)('Invalid node');
|
|
28
|
+
}
|
|
29
|
+
return decodeRawBinaryNode(decoded);
|
|
30
|
+
}
|
|
31
|
+
function isRawBinaryNode(node) {
|
|
32
|
+
return Array.isArray(node) && !(node instanceof Uint8Array);
|
|
33
|
+
}
|
|
34
|
+
function isInternalBinaryNode(node) {
|
|
35
|
+
return node.type === types_ts_1.BinaryNodeType.Internal;
|
|
36
|
+
}
|
|
37
|
+
function isStemBinaryNode(node) {
|
|
38
|
+
return node.type === types_ts_1.BinaryNodeType.Stem;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/node/util.ts"],"names":[],"mappings":";;AAOA,kDAUC;AAED,4CAMC;AAED,0CAEC;AAED,oDAEC;AAED,4CAEC;AArCD,yCAAqC;AACrC,2CAA6D;AAE7D,uDAAsD;AACtD,+CAA8C;AAC9C,yCAA4D;AAE5D,SAAgB,mBAAmB,CAAC,GAAiB;IACnD,MAAM,QAAQ,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC1B,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,yBAAc,CAAC,QAAQ;YAC1B,OAAO,oCAAkB,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;QAC5C,KAAK,yBAAc,CAAC,IAAI;YACtB,OAAO,4BAAc,CAAC,WAAW,CAAC,GAAG,CAAC,CAAA;QACxC;YACE,MAAM,IAAA,iCAA0B,EAAC,mBAAmB,CAAC,CAAA;IACzD,CAAC;AACH,CAAC;AAED,SAAgB,gBAAgB,CAAC,GAAe;IAC9C,MAAM,OAAO,GAAG,SAAG,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAiB,CAAA;IAChE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC5B,MAAM,IAAA,iCAA0B,EAAC,cAAc,CAAC,CAAA;IAClD,CAAC;IACD,OAAO,mBAAmB,CAAC,OAAO,CAAC,CAAA;AACrC,CAAC;AAED,SAAgB,eAAe,CAAC,IAA+B;IAC7D,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,CAAA;AAC7D,CAAC;AAED,SAAgB,oBAAoB,CAAC,IAAgB;IACnD,OAAO,IAAI,CAAC,IAAI,KAAK,yBAAc,CAAC,QAAQ,CAAA;AAC9C,CAAC;AAED,SAAgB,gBAAgB,CAAC,IAAgB;IAC/C,OAAO,IAAI,CAAC,IAAI,KAAK,yBAAc,CAAC,IAAI,CAAA;AAC1C,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { BinaryTree } from './binaryTree.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Saves the nodes from a proof into the tree.
|
|
4
|
+
* @param proof
|
|
5
|
+
*/
|
|
6
|
+
export declare function binaryTreeFromProof(proof: Uint8Array[]): Promise<BinaryTree>;
|
|
7
|
+
/**
|
|
8
|
+
* Verifies a proof.
|
|
9
|
+
* @param rootHash
|
|
10
|
+
* @param key
|
|
11
|
+
* @param proof
|
|
12
|
+
* @throws If proof is found to be invalid.
|
|
13
|
+
* @returns The value from the key, or null if valid proof of non-existence.
|
|
14
|
+
*/
|
|
15
|
+
export declare function verifyBinaryProof(rootHash: Uint8Array, key: Uint8Array, proof: Uint8Array[]): Promise<Uint8Array | null>;
|
|
16
|
+
//# sourceMappingURL=proof.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proof.d.ts","sourceRoot":"","sources":["../../src/proof.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAIjD;;;GAGG;AACH,wBAAsB,mBAAmB,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAUlF;AAED;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,UAAU,EACpB,GAAG,EAAE,UAAU,EACf,KAAK,EAAE,UAAU,EAAE,GAClB,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAgB5B"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.binaryTreeFromProof = binaryTreeFromProof;
|
|
4
|
+
exports.verifyBinaryProof = verifyBinaryProof;
|
|
5
|
+
const util_1 = require("@feelyourprotocol/util");
|
|
6
|
+
const constructors_ts_1 = require("./constructors.js");
|
|
7
|
+
const index_ts_1 = require("./node/index.js");
|
|
8
|
+
/**
|
|
9
|
+
* Saves the nodes from a proof into the tree.
|
|
10
|
+
* @param proof
|
|
11
|
+
*/
|
|
12
|
+
async function binaryTreeFromProof(proof) {
|
|
13
|
+
const proofTrie = await (0, constructors_ts_1.createBinaryTree)();
|
|
14
|
+
const putStack = proof.map((bytes) => {
|
|
15
|
+
const node = (0, index_ts_1.decodeBinaryNode)(bytes);
|
|
16
|
+
return [proofTrie['merkelize'](node), node];
|
|
17
|
+
});
|
|
18
|
+
await proofTrie.saveStack(putStack);
|
|
19
|
+
const root = putStack[0][0];
|
|
20
|
+
proofTrie.root(root);
|
|
21
|
+
return proofTrie;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Verifies a proof.
|
|
25
|
+
* @param rootHash
|
|
26
|
+
* @param key
|
|
27
|
+
* @param proof
|
|
28
|
+
* @throws If proof is found to be invalid.
|
|
29
|
+
* @returns The value from the key, or null if valid proof of non-existence.
|
|
30
|
+
*/
|
|
31
|
+
async function verifyBinaryProof(rootHash, key, proof) {
|
|
32
|
+
const proofTrie = await binaryTreeFromProof(proof);
|
|
33
|
+
if (!(0, util_1.equalsBytes)(proofTrie.root(), rootHash)) {
|
|
34
|
+
throw (0, util_1.EthereumJSErrorWithoutCode)('rootHash does not match proof root');
|
|
35
|
+
}
|
|
36
|
+
const [value] = await proofTrie.get(key.slice(0, 31), [key[31]]);
|
|
37
|
+
const valueNode = (0, index_ts_1.decodeBinaryNode)(proof[proof.length - 1]);
|
|
38
|
+
const expectedValue = valueNode.values[key[31]];
|
|
39
|
+
if (!expectedValue) {
|
|
40
|
+
if (value) {
|
|
41
|
+
throw (0, util_1.EthereumJSErrorWithoutCode)('Proof is invalid');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else if (value && !(0, util_1.equalsBytes)(value, expectedValue)) {
|
|
45
|
+
throw (0, util_1.EthereumJSErrorWithoutCode)('Proof is invalid');
|
|
46
|
+
}
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=proof.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proof.js","sourceRoot":"","sources":["../../src/proof.ts"],"names":[],"mappings":";;AAaA,kDAUC;AAUD,8CAoBC;AArDD,2CAA0E;AAE1E,uDAAoD;AACpD,8CAAkD;AAMlD;;;GAGG;AACI,KAAK,UAAU,mBAAmB,CAAC,KAAmB;IAC3D,MAAM,SAAS,GAAG,MAAM,IAAA,kCAAgB,GAAE,CAAA;IAC1C,MAAM,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tD,CAAC;IACH,CAAC;SAAM,IAAI,KAAK,IAAI,CAAC,IAAA,kBAAW,EAAC,KAAK,EAAE,aAAa,CAAC,EAAE,CAAC;QACvD,MAAM,IAAA,iCAA0B,EAAC,kBAAkB,CAAC,CAAA;IACtD,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { DB, ValueEncoding } from '@feelyourprotocol/util';
|
|
2
|
+
export interface BinaryTreeOpts {
|
|
3
|
+
/**
|
|
4
|
+
* A database instance.
|
|
5
|
+
*/
|
|
6
|
+
db: DB<string, string | Uint8Array>;
|
|
7
|
+
/**
|
|
8
|
+
* A `Uint8Array` for the root of a previously stored tree
|
|
9
|
+
*/
|
|
10
|
+
root?: Uint8Array;
|
|
11
|
+
/**
|
|
12
|
+
* Store the root inside the database after every `write` operation
|
|
13
|
+
*/
|
|
14
|
+
useRootPersistence: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* LRU cache for tree nodes to allow for faster node retrieval.
|
|
17
|
+
*
|
|
18
|
+
* Default: 0 (deactivated)
|
|
19
|
+
*/
|
|
20
|
+
cacheSize: number;
|
|
21
|
+
/**
|
|
22
|
+
* Hash function used for hashing the tree nodes.
|
|
23
|
+
*/
|
|
24
|
+
hashFunction: (msg: Uint8Array) => Uint8Array;
|
|
25
|
+
}
|
|
26
|
+
export interface CheckpointDBOpts {
|
|
27
|
+
/**
|
|
28
|
+
* A database instance.
|
|
29
|
+
*/
|
|
30
|
+
db: DB<string, string | Uint8Array>;
|
|
31
|
+
/**
|
|
32
|
+
* ValueEncoding of the database (the values which are `put`/`get` in the db are of this type). Defaults to `string`
|
|
33
|
+
*/
|
|
34
|
+
valueEncoding?: ValueEncoding;
|
|
35
|
+
/**
|
|
36
|
+
* Cache size (default: 0)
|
|
37
|
+
*/
|
|
38
|
+
cacheSize?: number;
|
|
39
|
+
}
|
|
40
|
+
export type Checkpoint = {
|
|
41
|
+
keyValueMap: Map<string, Uint8Array | undefined>;
|
|
42
|
+
root: Uint8Array;
|
|
43
|
+
};
|
|
44
|
+
export declare const ROOT_DB_KEY: Uint8Array<ArrayBufferLike>;
|
|
45
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,EAAE,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAEzD,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,EAAE,EAAE,EAAE,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAA;IAEnC;;OAEG;IACH,IAAI,CAAC,EAAE,UAAU,CAAA;IAEjB;;OAEG;IACH,kBAAkB,EAAE,OAAO,CAAA;IAE3B;;;;OAIG;IACH,SAAS,EAAE,MAAM,CAAA;IAEjB;;OAEG;IACH,YAAY,EAAE,CAAC,GAAG,EAAE,UAAU,KAAK,UAAU,CAAA;CAC9C;AAED,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,EAAE,EAAE,EAAE,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAA;IAEnC;;OAEG;IACH,aAAa,CAAC,EAAE,aAAa,CAAA;IAE7B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,MAAM,UAAU,GAAG;IAGvB,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,GAAG,SAAS,CAAC,CAAA;IAChD,IAAI,EAAE,UAAU,CAAA;CACjB,CAAA;AAED,eAAO,MAAM,WAAW,6BAA0B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":";;;AAAA,2CAA8C;AAyDjC,QAAA,WAAW,GAAG,IAAA,kBAAW,EAAC,UAAU,CAAC,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type PrefixedHexString } from '@feelyourprotocol/util';
|
|
2
|
+
import type { BinaryTree } from './binaryTree.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Recursively walks down the tree from a given starting node and returns all the leaf values
|
|
5
|
+
* @param tree - The binary tree
|
|
6
|
+
* @param startingNode - The starting node
|
|
7
|
+
* @returns An array of key-value pairs containing the tree keys and associated values
|
|
8
|
+
*/
|
|
9
|
+
export declare const dumpLeafValues: (tree: BinaryTree, startingNode: Uint8Array) => Promise<[PrefixedHexString, PrefixedHexString][] | undefined>;
|
|
10
|
+
/**
|
|
11
|
+
* Recursively walks down the tree from a given starting node and returns all the node paths and hashes
|
|
12
|
+
* @param tree - The binary tree
|
|
13
|
+
* @param startingNode - The starting node
|
|
14
|
+
* @returns An array of key-value pairs containing the tree paths and associated hashes
|
|
15
|
+
*/
|
|
16
|
+
export declare const dumpNodeHashes: (tree: BinaryTree, startingNode: Uint8Array) => Promise<[string, PrefixedHexString][] | undefined>;
|
|
17
|
+
//# sourceMappingURL=util.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,iBAAiB,EAAwC,MAAM,kBAAkB,CAAA;AAM/F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAGjD;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GACzB,MAAM,UAAU,EAChB,cAAc,UAAU,KACvB,OAAO,CAAC,CAAC,iBAAiB,EAAE,iBAAiB,CAAC,EAAE,GAAG,SAAS,CAsB9D,CAAA;AACD;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GACzB,MAAM,UAAU,EAChB,cAAc,UAAU,KACvB,OAAO,CAAC,CAAC,MAAM,EAAE,iBAAiB,CAAC,EAAE,GAAG,SAAS,CA6BnD,CAAA"}
|
package/dist/cjs/util.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.dumpNodeHashes = exports.dumpLeafValues = void 0;
|
|
4
|
+
const util_1 = require("@feelyourprotocol/util");
|
|
5
|
+
const internalNode_ts_1 = require("./node/internalNode.js");
|
|
6
|
+
const stemNode_ts_1 = require("./node/stemNode.js");
|
|
7
|
+
const util_ts_1 = require("./node/util.js");
|
|
8
|
+
/**
|
|
9
|
+
* Recursively walks down the tree from a given starting node and returns all the leaf values
|
|
10
|
+
* @param tree - The binary tree
|
|
11
|
+
* @param startingNode - The starting node
|
|
12
|
+
* @returns An array of key-value pairs containing the tree keys and associated values
|
|
13
|
+
*/
|
|
14
|
+
const dumpLeafValues = async (tree, startingNode) => {
|
|
15
|
+
if ((0, util_1.equalsBytes)(startingNode, tree.EMPTY_TREE_ROOT) === true)
|
|
16
|
+
return;
|
|
17
|
+
// Retrieve starting node from DB
|
|
18
|
+
const rawNode = await tree['_db'].get(startingNode);
|
|
19
|
+
if (rawNode === undefined)
|
|
20
|
+
return;
|
|
21
|
+
const node = (0, util_ts_1.decodeBinaryNode)(rawNode);
|
|
22
|
+
const entries = [];
|
|
23
|
+
if (node instanceof stemNode_ts_1.StemBinaryNode) {
|
|
24
|
+
for (const [idx, val] of node.values.entries()) {
|
|
25
|
+
if (val !== null) {
|
|
26
|
+
entries.push([(0, util_1.bytesToHex)((0, util_1.concatBytes)(node.stem, Uint8Array.from([idx]))), (0, util_1.bytesToHex)(val)]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return entries;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const childPaths = node.children
|
|
33
|
+
.filter((value) => value !== null)
|
|
34
|
+
.map((value) => (0, exports.dumpLeafValues)(tree, value.hash));
|
|
35
|
+
const res = (await Promise.all(childPaths)).filter((val) => val !== undefined);
|
|
36
|
+
return res.flat(1);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
exports.dumpLeafValues = dumpLeafValues;
|
|
40
|
+
/**
|
|
41
|
+
* Recursively walks down the tree from a given starting node and returns all the node paths and hashes
|
|
42
|
+
* @param tree - The binary tree
|
|
43
|
+
* @param startingNode - The starting node
|
|
44
|
+
* @returns An array of key-value pairs containing the tree paths and associated hashes
|
|
45
|
+
*/
|
|
46
|
+
const dumpNodeHashes = async (tree, startingNode) => {
|
|
47
|
+
let entries = [];
|
|
48
|
+
// Retrieve starting node from DB
|
|
49
|
+
const rawNode = await tree['_db'].get(startingNode);
|
|
50
|
+
if (rawNode === undefined)
|
|
51
|
+
return;
|
|
52
|
+
const node = (0, util_ts_1.decodeBinaryNode)(rawNode);
|
|
53
|
+
// If current node is root, push '0x' for path and node hash for commitment
|
|
54
|
+
(0, util_1.equalsBytes)(startingNode, tree.root()) && entries.push(['0x', (0, util_1.bytesToHex)(startingNode)]);
|
|
55
|
+
if (node instanceof internalNode_ts_1.InternalBinaryNode) {
|
|
56
|
+
const children = node.children.filter((value) => value !== null);
|
|
57
|
+
// Push non-null children paths and hashes
|
|
58
|
+
for (const child of children) {
|
|
59
|
+
entries.push([child.path.join(''), (0, util_1.bytesToHex)(child.hash)]);
|
|
60
|
+
}
|
|
61
|
+
// Recursively call dumpNodeHashes on each child node
|
|
62
|
+
const childPaths = (await Promise.all(children.map((value) => (0, exports.dumpNodeHashes)(tree, value.hash))))
|
|
63
|
+
.filter((val) => val !== undefined)
|
|
64
|
+
.flat(1);
|
|
65
|
+
// Add all child paths and hashes to entries
|
|
66
|
+
entries = [...entries, ...childPaths];
|
|
67
|
+
}
|
|
68
|
+
return entries;
|
|
69
|
+
};
|
|
70
|
+
exports.dumpNodeHashes = dumpNodeHashes;
|
|
71
|
+
//# sourceMappingURL=util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":";;;AAAA,2CAA+F;AAE/F,4DAA2D;AAC3D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AAC,IAAA,sBAAc,EAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAC7E;aACE,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,KAAK,SAAS,CAAC;aAClC,IAAI,CAAC,CAAC,CAAC,CAAA;QAEV,4CAA4C;QAC5C,OAAO,GAAG,CAAC,GAAG,OAAO,EAAE,GAAG,UAAU,CAAkC,CAAA;IACxE,CAAC;IAED,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA;AAhCY,QAAA,cAAc,kBAgC1B"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Lock } from '@feelyourprotocol/util';
|
|
2
|
+
import { CheckpointDB } from './db/index.ts';
|
|
3
|
+
import { StemBinaryNode } from './node/stemNode.ts';
|
|
4
|
+
import { type BinaryTreeOpts } from './types.ts';
|
|
5
|
+
import type { Debugger } from 'debug';
|
|
6
|
+
import type { BinaryNode } from './node/types.ts';
|
|
7
|
+
interface Path {
|
|
8
|
+
node: BinaryNode | null;
|
|
9
|
+
remaining: number[];
|
|
10
|
+
stack: Array<[BinaryNode, number[]]>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The basic binary tree interface, use with `import { BinaryTree } from '@feelyourprotocol/binarytree'`.
|
|
14
|
+
*
|
|
15
|
+
* A BinaryTree object can be created with the constructor method:
|
|
16
|
+
*
|
|
17
|
+
* - {@link createBinaryTree}
|
|
18
|
+
*/
|
|
19
|
+
export declare class BinaryTree {
|
|
20
|
+
/** The options for instantiating the binary tree */
|
|
21
|
+
protected _opts: BinaryTreeOpts;
|
|
22
|
+
/** The root for an empty tree */
|
|
23
|
+
EMPTY_TREE_ROOT: Uint8Array;
|
|
24
|
+
protected _db: CheckpointDB;
|
|
25
|
+
protected _hashLen: number;
|
|
26
|
+
protected _lock: Lock;
|
|
27
|
+
protected _root: Uint8Array;
|
|
28
|
+
protected DEBUG: boolean;
|
|
29
|
+
protected _debug: Debugger;
|
|
30
|
+
protected debug: (...args: any) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new binary tree.
|
|
33
|
+
* @param opts Options for instantiating the binary tree
|
|
34
|
+
*
|
|
35
|
+
* Note: in most cases, the static {@link createBinaryTree} constructor should be used. It uses the same API but provides sensible defaults
|
|
36
|
+
*/
|
|
37
|
+
constructor(opts: BinaryTreeOpts);
|
|
38
|
+
/**
|
|
39
|
+
* Gets and/or Sets the current root of the `tree`
|
|
40
|
+
*/
|
|
41
|
+
root(value?: Uint8Array | null): Uint8Array;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if a given root exists.
|
|
44
|
+
*/
|
|
45
|
+
checkRoot(root: Uint8Array): Promise<boolean>;
|
|
46
|
+
/**
|
|
47
|
+
* Gets values at a given binary tree `stem` and set of suffixes
|
|
48
|
+
* @param stem - the stem of the stem node where we're seeking values
|
|
49
|
+
* @param suffixes - an array of suffixes corresponding to the values desired
|
|
50
|
+
* @returns A Promise that resolves to an array of `Uint8Array`s or `null` depending on if values were found.
|
|
51
|
+
* If the stem is not found, will return an empty array.
|
|
52
|
+
*/
|
|
53
|
+
get(stem: Uint8Array, suffixes: number[]): Promise<(Uint8Array | null)[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Stores a given `value` at the given `key` or performs a deletion if `value` is null.
|
|
56
|
+
* @param stem - the stem (must be 31 bytes) to store the value at.
|
|
57
|
+
* @param suffixes - array of suffixes at which to store individual values.
|
|
58
|
+
* @param values - the value(s) to store (or null for deletion).
|
|
59
|
+
* @returns A Promise that resolves once the value is stored.
|
|
60
|
+
*/
|
|
61
|
+
put(stem: Uint8Array, suffixes: number[], values: (Uint8Array | null)[]): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Helper method for updating or creating the parent internal node for a given stem node.
|
|
64
|
+
* If the nearest node is a stem node with a different stem, a new internal node is created
|
|
65
|
+
* to branch at the first differing bit.
|
|
66
|
+
* If the nearest node is an internal node, its child reference is updated.
|
|
67
|
+
*
|
|
68
|
+
* @param stemNode - The child stem node that will be referenced by the new/updated internal node.
|
|
69
|
+
* @param nearestNode - The nearest node to the new stem node.
|
|
70
|
+
* @param pathToNode - The path (in bits) to `nearestNode` as known from the trie.
|
|
71
|
+
* @returns An array of nodes and their partial paths from the new stem node to the branch parent node
|
|
72
|
+
* or `undefined` if no changes were made.
|
|
73
|
+
*/
|
|
74
|
+
updateBranch(stemNode: StemBinaryNode, nearestNode: BinaryNode, pathToNode: number[], pathToParent: number[]): {
|
|
75
|
+
node: BinaryNode;
|
|
76
|
+
parentPath: number[];
|
|
77
|
+
}[] | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Tries to find a path to the node for the given key.
|
|
80
|
+
* It returns a `Path` object containing:
|
|
81
|
+
* - `node`: the found node (if any),
|
|
82
|
+
* - `stack`: an array of tuples [node, path] representing the nodes encountered,
|
|
83
|
+
* - `remaining`: the bits of the key that were not matched.
|
|
84
|
+
*
|
|
85
|
+
* @param keyInBytes - the search key as a byte array.
|
|
86
|
+
* @returns A Promise that resolves to a Path object.
|
|
87
|
+
*/
|
|
88
|
+
findPath(keyInBytes: Uint8Array): Promise<Path>;
|
|
89
|
+
/**
|
|
90
|
+
* Deletes a given `key` from the tree.
|
|
91
|
+
* @param stem - the stem of the stem node to delete from
|
|
92
|
+
* @param suffixes - the suffixes to delete
|
|
93
|
+
* @returns A Promise that resolves once the key is deleted.
|
|
94
|
+
*/
|
|
95
|
+
del(stem: Uint8Array, suffixes: number[]): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Create empty root node for initializing an empty tree.
|
|
98
|
+
*/
|
|
99
|
+
createRootNode(): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Creates the initial node from an empty tree.
|
|
102
|
+
* @private
|
|
103
|
+
*/
|
|
104
|
+
protected _createInitialNode(stem: Uint8Array, indexes: number[], values: (Uint8Array | null)[]): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Saves a stack of nodes to the database.
|
|
107
|
+
*
|
|
108
|
+
* @param putStack - an array of tuples of keys (the partial path of the node in the trie) and nodes (BinaryNodes)
|
|
109
|
+
*/
|
|
110
|
+
saveStack(putStack: [Uint8Array, BinaryNode | null][]): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Creates a proof from a tree and key that can be verified using {@link BinaryTree.verifyBinaryProof}.
|
|
113
|
+
* @param key a 32 byte binary tree key (31 byte stem + 1 byte suffix)
|
|
114
|
+
*/
|
|
115
|
+
createBinaryProof(key: Uint8Array): Promise<Uint8Array[]>;
|
|
116
|
+
/**
|
|
117
|
+
* The `data` event is given an `Object` that has two properties; the `key` and the `value`. Both should be Uint8Arrays.
|
|
118
|
+
* @return Returns a [stream](https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_class_stream_readable) of the contents of the `tree`
|
|
119
|
+
*/
|
|
120
|
+
createReadStream(): any;
|
|
121
|
+
/**
|
|
122
|
+
* Returns a copy of the underlying tree.
|
|
123
|
+
*
|
|
124
|
+
* Note on db: the copy will create a reference to the
|
|
125
|
+
* same underlying database.
|
|
126
|
+
*
|
|
127
|
+
* Note on cache: for memory reasons a copy will not
|
|
128
|
+
* recreate a new LRU cache but initialize with cache
|
|
129
|
+
* being deactivated.
|
|
130
|
+
*
|
|
131
|
+
* @param includeCheckpoints - If true and during a checkpoint, the copy will contain the checkpointing metadata and will use the same scratch as underlying db.
|
|
132
|
+
*/
|
|
133
|
+
shallowCopy(includeCheckpoints?: boolean): BinaryTree;
|
|
134
|
+
/**
|
|
135
|
+
* Persists the root hash in the underlying database
|
|
136
|
+
*/
|
|
137
|
+
persistRoot(): Promise<void>;
|
|
138
|
+
/**
|
|
139
|
+
* Is the tree during a checkpoint phase?
|
|
140
|
+
*/
|
|
141
|
+
hasCheckpoints(): boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Creates a checkpoint that can later be reverted to or committed.
|
|
144
|
+
* After this is called, all changes can be reverted until `commit` is called.
|
|
145
|
+
*/
|
|
146
|
+
checkpoint(): void;
|
|
147
|
+
/**
|
|
148
|
+
* Commits a checkpoint to disk, if current checkpoint is not nested.
|
|
149
|
+
* If nested, only sets the parent checkpoint as current checkpoint.
|
|
150
|
+
* @throws If not during a checkpoint phase
|
|
151
|
+
*/
|
|
152
|
+
commit(): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Reverts the tree to the state it was at when `checkpoint` was first called.
|
|
155
|
+
* If during a nested checkpoint, sets root to most recent checkpoint, and sets
|
|
156
|
+
* parent checkpoint as current.
|
|
157
|
+
*/
|
|
158
|
+
revert(): Promise<void>;
|
|
159
|
+
/**
|
|
160
|
+
* Flushes all checkpoints, restoring the initial checkpoint state.
|
|
161
|
+
*/
|
|
162
|
+
flushCheckpoints(): void;
|
|
163
|
+
protected hash(msg: Uint8Array | null): Uint8Array;
|
|
164
|
+
protected merkelize(node: BinaryNode | null): Uint8Array;
|
|
165
|
+
}
|
|
166
|
+
export {};
|
|
167
|
+
//# sourceMappingURL=binaryTree.d.ts.map
|