@ensdomains/merkle-builder 0.0.2 → 0.0.4
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/README.md +2 -0
- package/dist/index.cjs +40 -43
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +40 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
* Compute efficient (branch nodes hash once)
|
|
7
7
|
* Multiple storage encodings (object, [bytes](./src/coder.ts), [JSON](./src/json.ts))
|
|
8
8
|
|
|
9
|
+
`npm i @ensdomains/merkle-builder` [✓](https://www.npmjs.com/package/@ensdomains/merkle-builder)
|
|
10
|
+
|
|
9
11
|
#### Roadmap
|
|
10
12
|
|
|
11
13
|
* Linea
|
package/dist/index.cjs
CHANGED
|
@@ -404,6 +404,9 @@ var Coder = class {
|
|
|
404
404
|
return c.pos;
|
|
405
405
|
}
|
|
406
406
|
pos = 0;
|
|
407
|
+
get remaining() {
|
|
408
|
+
return Math.max(0, this.buf.length - this.pos);
|
|
409
|
+
}
|
|
407
410
|
reset() {
|
|
408
411
|
this.pos = 0;
|
|
409
412
|
}
|
|
@@ -807,7 +810,7 @@ function mimcHash(v) {
|
|
|
807
810
|
}
|
|
808
811
|
|
|
809
812
|
// src/surgery.ts
|
|
810
|
-
function pluckLimbs(node, depth
|
|
813
|
+
function pluckLimbs(node, depth) {
|
|
811
814
|
if (!node || depth <= 0) return { trunk: node, limbs: [] };
|
|
812
815
|
const queue = [];
|
|
813
816
|
if (isBranch(node)) {
|
|
@@ -831,29 +834,30 @@ function pluckLimbs(node, depth, exact) {
|
|
|
831
834
|
const limbs = [];
|
|
832
835
|
while (queue.length) {
|
|
833
836
|
const [parent, path] = queue.pop();
|
|
834
|
-
parent.children.forEach((
|
|
835
|
-
if (!
|
|
837
|
+
parent.children.forEach((x, i) => {
|
|
838
|
+
if (!x) return;
|
|
836
839
|
if (path.length + 1 === depth) {
|
|
837
840
|
parent.children[i] = void 0;
|
|
838
|
-
limbs.push([Uint8Array.of(...path, i),
|
|
839
|
-
} else if (isBranch(
|
|
840
|
-
queue.push([
|
|
841
|
-
} else if (isExtension(
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
new Uint8Array(full.slice(0, depth)),
|
|
849
|
-
|
|
850
|
-
|
|
841
|
+
limbs.push([Uint8Array.of(...path, i), x]);
|
|
842
|
+
} else if (isBranch(x)) {
|
|
843
|
+
queue.push([x, [...path, i]]);
|
|
844
|
+
} else if (isExtension(x)) {
|
|
845
|
+
const full = [...path, i, ...x.path];
|
|
846
|
+
if (full.length >= depth) {
|
|
847
|
+
const { cache, ...branch } = x.child;
|
|
848
|
+
const child = newBranch();
|
|
849
|
+
if (cache) child.cache = cache;
|
|
850
|
+
parent.children[i] = { path: x.path, child };
|
|
851
|
+
limbs.push([new Uint8Array(full.slice(0, depth)), branch]);
|
|
852
|
+
} else {
|
|
853
|
+
queue.push([x.child, full]);
|
|
854
|
+
}
|
|
851
855
|
}
|
|
852
856
|
});
|
|
853
857
|
}
|
|
854
858
|
return { trunk: node, limbs };
|
|
855
859
|
}
|
|
856
|
-
function graftLimb(trunk, path, limb) {
|
|
860
|
+
function graftLimb(trunk, [path, limb]) {
|
|
857
861
|
if (!path.length) return limb;
|
|
858
862
|
if (!trunk) {
|
|
859
863
|
if (isBranch(limb)) {
|
|
@@ -864,39 +868,32 @@ function graftLimb(trunk, path, limb) {
|
|
|
864
868
|
return copy;
|
|
865
869
|
}
|
|
866
870
|
}
|
|
867
|
-
let start = 0;
|
|
868
871
|
let index = 0;
|
|
869
|
-
let
|
|
872
|
+
let parent = trunk;
|
|
870
873
|
while (index < path.length - 1) {
|
|
871
|
-
if (isBranch(
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
}
|
|
878
|
-
start = ++index;
|
|
879
|
-
node = child;
|
|
880
|
-
} else if (isExtension(node)) {
|
|
881
|
-
++start;
|
|
882
|
-
index += node.path.length;
|
|
883
|
-
node = node.child;
|
|
874
|
+
if (isBranch(parent)) {
|
|
875
|
+
parent = parent.children[path[index++]];
|
|
876
|
+
} else if (isExtension(parent)) {
|
|
877
|
+
index += parent.path.length;
|
|
878
|
+
if (index >= path.length) break;
|
|
879
|
+
parent = parent.child;
|
|
884
880
|
} else {
|
|
885
881
|
break;
|
|
886
882
|
}
|
|
887
883
|
}
|
|
888
|
-
if (
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
} else if (isBranch(
|
|
892
|
-
|
|
893
|
-
path: path.slice(
|
|
894
|
-
|
|
895
|
-
|
|
884
|
+
if (isExtension(parent)) {
|
|
885
|
+
if (!isBranch(limb)) throw new RangeError("expected branch");
|
|
886
|
+
parent.child.children = limb.children;
|
|
887
|
+
} else if (isBranch(parent)) {
|
|
888
|
+
if (isBranch(limb)) {
|
|
889
|
+
parent.children[path[index]] = index + 1 == path.length ? limb : { path: path.slice(index + 1), child: limb };
|
|
890
|
+
} else {
|
|
891
|
+
const copy = { ...limb };
|
|
892
|
+
copy.path = concat(path.subarray(index + 1), copy.path);
|
|
893
|
+
parent.children[path[index]] = copy;
|
|
894
|
+
}
|
|
896
895
|
} else {
|
|
897
|
-
|
|
898
|
-
copy.path = concat(path.subarray(start + 1), copy.path);
|
|
899
|
-
node.children[path[start]] = copy;
|
|
896
|
+
throw new RangeError("invalid graft location");
|
|
900
897
|
}
|
|
901
898
|
return trunk;
|
|
902
899
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -41,6 +41,7 @@ declare class Coder {
|
|
|
41
41
|
static getByteCount(node: MaybeNode): number;
|
|
42
42
|
pos: number;
|
|
43
43
|
constructor(buf?: Uint8Array);
|
|
44
|
+
get remaining(): number;
|
|
44
45
|
reset(): void;
|
|
45
46
|
expand(need: number): void;
|
|
46
47
|
require(need: number): void;
|
|
@@ -74,11 +75,11 @@ declare function insertBytes(node: MaybeNode, slot: Uint8Array, value: Uint8Arra
|
|
|
74
75
|
declare function mimcHash(v: bigint[]): bigint;
|
|
75
76
|
|
|
76
77
|
type Limb = [path: Uint8Array, node: Node];
|
|
77
|
-
declare function pluckLimbs(node: MaybeNode, depth: number
|
|
78
|
+
declare function pluckLimbs(node: MaybeNode, depth: number): {
|
|
78
79
|
trunk: MaybeNode;
|
|
79
80
|
limbs: Limb[];
|
|
80
81
|
};
|
|
81
|
-
declare function graftLimb(trunk: MaybeNode, path
|
|
82
|
+
declare function graftLimb(trunk: MaybeNode, [path, limb]: Limb): Node;
|
|
82
83
|
|
|
83
84
|
type Hex = `0x${string}`;
|
|
84
85
|
declare function keccak256(v: Uint8Array): Uint8Array;
|
package/dist/index.d.ts
CHANGED
|
@@ -41,6 +41,7 @@ declare class Coder {
|
|
|
41
41
|
static getByteCount(node: MaybeNode): number;
|
|
42
42
|
pos: number;
|
|
43
43
|
constructor(buf?: Uint8Array);
|
|
44
|
+
get remaining(): number;
|
|
44
45
|
reset(): void;
|
|
45
46
|
expand(need: number): void;
|
|
46
47
|
require(need: number): void;
|
|
@@ -74,11 +75,11 @@ declare function insertBytes(node: MaybeNode, slot: Uint8Array, value: Uint8Arra
|
|
|
74
75
|
declare function mimcHash(v: bigint[]): bigint;
|
|
75
76
|
|
|
76
77
|
type Limb = [path: Uint8Array, node: Node];
|
|
77
|
-
declare function pluckLimbs(node: MaybeNode, depth: number
|
|
78
|
+
declare function pluckLimbs(node: MaybeNode, depth: number): {
|
|
78
79
|
trunk: MaybeNode;
|
|
79
80
|
limbs: Limb[];
|
|
80
81
|
};
|
|
81
|
-
declare function graftLimb(trunk: MaybeNode, path
|
|
82
|
+
declare function graftLimb(trunk: MaybeNode, [path, limb]: Limb): Node;
|
|
82
83
|
|
|
83
84
|
type Hex = `0x${string}`;
|
|
84
85
|
declare function keccak256(v: Uint8Array): Uint8Array;
|
package/dist/index.js
CHANGED
|
@@ -342,6 +342,9 @@ var Coder = class {
|
|
|
342
342
|
return c.pos;
|
|
343
343
|
}
|
|
344
344
|
pos = 0;
|
|
345
|
+
get remaining() {
|
|
346
|
+
return Math.max(0, this.buf.length - this.pos);
|
|
347
|
+
}
|
|
345
348
|
reset() {
|
|
346
349
|
this.pos = 0;
|
|
347
350
|
}
|
|
@@ -745,7 +748,7 @@ function mimcHash(v) {
|
|
|
745
748
|
}
|
|
746
749
|
|
|
747
750
|
// src/surgery.ts
|
|
748
|
-
function pluckLimbs(node, depth
|
|
751
|
+
function pluckLimbs(node, depth) {
|
|
749
752
|
if (!node || depth <= 0) return { trunk: node, limbs: [] };
|
|
750
753
|
const queue = [];
|
|
751
754
|
if (isBranch(node)) {
|
|
@@ -769,29 +772,30 @@ function pluckLimbs(node, depth, exact) {
|
|
|
769
772
|
const limbs = [];
|
|
770
773
|
while (queue.length) {
|
|
771
774
|
const [parent, path] = queue.pop();
|
|
772
|
-
parent.children.forEach((
|
|
773
|
-
if (!
|
|
775
|
+
parent.children.forEach((x, i) => {
|
|
776
|
+
if (!x) return;
|
|
774
777
|
if (path.length + 1 === depth) {
|
|
775
778
|
parent.children[i] = void 0;
|
|
776
|
-
limbs.push([Uint8Array.of(...path, i),
|
|
777
|
-
} else if (isBranch(
|
|
778
|
-
queue.push([
|
|
779
|
-
} else if (isExtension(
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
new Uint8Array(full.slice(0, depth)),
|
|
787
|
-
|
|
788
|
-
|
|
779
|
+
limbs.push([Uint8Array.of(...path, i), x]);
|
|
780
|
+
} else if (isBranch(x)) {
|
|
781
|
+
queue.push([x, [...path, i]]);
|
|
782
|
+
} else if (isExtension(x)) {
|
|
783
|
+
const full = [...path, i, ...x.path];
|
|
784
|
+
if (full.length >= depth) {
|
|
785
|
+
const { cache, ...branch } = x.child;
|
|
786
|
+
const child = newBranch();
|
|
787
|
+
if (cache) child.cache = cache;
|
|
788
|
+
parent.children[i] = { path: x.path, child };
|
|
789
|
+
limbs.push([new Uint8Array(full.slice(0, depth)), branch]);
|
|
790
|
+
} else {
|
|
791
|
+
queue.push([x.child, full]);
|
|
792
|
+
}
|
|
789
793
|
}
|
|
790
794
|
});
|
|
791
795
|
}
|
|
792
796
|
return { trunk: node, limbs };
|
|
793
797
|
}
|
|
794
|
-
function graftLimb(trunk, path, limb) {
|
|
798
|
+
function graftLimb(trunk, [path, limb]) {
|
|
795
799
|
if (!path.length) return limb;
|
|
796
800
|
if (!trunk) {
|
|
797
801
|
if (isBranch(limb)) {
|
|
@@ -802,39 +806,32 @@ function graftLimb(trunk, path, limb) {
|
|
|
802
806
|
return copy;
|
|
803
807
|
}
|
|
804
808
|
}
|
|
805
|
-
let start = 0;
|
|
806
809
|
let index = 0;
|
|
807
|
-
let
|
|
810
|
+
let parent = trunk;
|
|
808
811
|
while (index < path.length - 1) {
|
|
809
|
-
if (isBranch(
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
}
|
|
816
|
-
start = ++index;
|
|
817
|
-
node = child;
|
|
818
|
-
} else if (isExtension(node)) {
|
|
819
|
-
++start;
|
|
820
|
-
index += node.path.length;
|
|
821
|
-
node = node.child;
|
|
812
|
+
if (isBranch(parent)) {
|
|
813
|
+
parent = parent.children[path[index++]];
|
|
814
|
+
} else if (isExtension(parent)) {
|
|
815
|
+
index += parent.path.length;
|
|
816
|
+
if (index >= path.length) break;
|
|
817
|
+
parent = parent.child;
|
|
822
818
|
} else {
|
|
823
819
|
break;
|
|
824
820
|
}
|
|
825
821
|
}
|
|
826
|
-
if (
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
} else if (isBranch(
|
|
830
|
-
|
|
831
|
-
path: path.slice(
|
|
832
|
-
|
|
833
|
-
|
|
822
|
+
if (isExtension(parent)) {
|
|
823
|
+
if (!isBranch(limb)) throw new RangeError("expected branch");
|
|
824
|
+
parent.child.children = limb.children;
|
|
825
|
+
} else if (isBranch(parent)) {
|
|
826
|
+
if (isBranch(limb)) {
|
|
827
|
+
parent.children[path[index]] = index + 1 == path.length ? limb : { path: path.slice(index + 1), child: limb };
|
|
828
|
+
} else {
|
|
829
|
+
const copy = { ...limb };
|
|
830
|
+
copy.path = concat(path.subarray(index + 1), copy.path);
|
|
831
|
+
parent.children[path[index]] = copy;
|
|
832
|
+
}
|
|
834
833
|
} else {
|
|
835
|
-
|
|
836
|
-
copy.path = concat(path.subarray(start + 1), copy.path);
|
|
837
|
-
node.children[path[start]] = copy;
|
|
834
|
+
throw new RangeError("invalid graft location");
|
|
838
835
|
}
|
|
839
836
|
return trunk;
|
|
840
837
|
}
|