@ensdomains/merkle-builder 0.0.3 → 0.0.5
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/dist/index.cjs +46 -81
- package/dist/index.d.cts +3 -5
- package/dist/index.d.ts +3 -5
- package/dist/index.js +46 -81
- package/package.json +1 -1
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,96 +810,58 @@ function mimcHash(v) {
|
|
|
807
810
|
}
|
|
808
811
|
|
|
809
812
|
// src/surgery.ts
|
|
810
|
-
function pluckLimbs(node, depth
|
|
811
|
-
if (!node || depth <= 0) return { trunk: node, limbs: [] };
|
|
812
|
-
const queue = [];
|
|
813
|
-
if (isBranch(node)) {
|
|
814
|
-
queue.push([node, []]);
|
|
815
|
-
} else if (isExtension(node)) {
|
|
816
|
-
if (node.path.length >= depth) {
|
|
817
|
-
const extension = node.path.length === depth ? node.child : { path: node.path.subarray(depth), child: node.child };
|
|
818
|
-
return {
|
|
819
|
-
trunk: void 0,
|
|
820
|
-
limbs: [[node.path.subarray(0, depth), extension]]
|
|
821
|
-
};
|
|
822
|
-
}
|
|
823
|
-
queue.push([node.child, [...node.path]]);
|
|
824
|
-
} else if (node.path.length >= depth) {
|
|
825
|
-
const leaf = newLeaf(node.path.subarray(depth), node.data);
|
|
826
|
-
return {
|
|
827
|
-
trunk: void 0,
|
|
828
|
-
limbs: [[node.path.subarray(0, depth), leaf]]
|
|
829
|
-
};
|
|
830
|
-
}
|
|
813
|
+
function pluckLimbs(node, depth) {
|
|
831
814
|
const limbs = [];
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
if (
|
|
837
|
-
|
|
838
|
-
limbs.push([Uint8Array.of(...path, i), child]);
|
|
839
|
-
} else if (isBranch(child)) {
|
|
840
|
-
queue.push([child, [...path, i]]);
|
|
841
|
-
} else if (isExtension(child) && path.length + child.path.length <= depth) {
|
|
842
|
-
queue.push([child.child, [...path, i, ...child.path]]);
|
|
843
|
-
} else if (exact) {
|
|
844
|
-
parent.children[i] = void 0;
|
|
845
|
-
const full = [...path, i, ...child.path];
|
|
846
|
-
const rest = new Uint8Array(full.slice(depth));
|
|
847
|
-
limbs.push([
|
|
848
|
-
new Uint8Array(full.slice(0, depth)),
|
|
849
|
-
isLeaf(child) ? newLeaf(rest, child.data) : rest.length ? { path: rest, child: child.child } : child.child
|
|
850
|
-
]);
|
|
815
|
+
if (node && depth > 0) pluck(node, []);
|
|
816
|
+
return limbs;
|
|
817
|
+
function pluck(node2, path, ext = false) {
|
|
818
|
+
if (path.length >= depth) {
|
|
819
|
+
if (ext && "cache" in node2) {
|
|
820
|
+
node2 = { children: node2.children };
|
|
851
821
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
const copy = { ...limb };
|
|
863
|
-
copy.path = concat(path, copy.path);
|
|
864
|
-
return copy;
|
|
822
|
+
limbs.push([new Uint8Array(path.slice(0, depth)), node2]);
|
|
823
|
+
return true;
|
|
824
|
+
} else if (isBranch(node2)) {
|
|
825
|
+
node2.children.forEach((x, i, v) => {
|
|
826
|
+
if (x && pluck(x, [...path, i])) {
|
|
827
|
+
v[i] = void 0;
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
} else if (isExtension(node2) && pluck(node2.child, [...path, ...node2.path], true)) {
|
|
831
|
+
node2.child = { ...node2.child, ...newBranch() };
|
|
865
832
|
}
|
|
866
833
|
}
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
834
|
+
}
|
|
835
|
+
function graftLimb(trunk, [path, limb]) {
|
|
836
|
+
if (!trunk || !path.length) throw new Error("invalid graft");
|
|
837
|
+
let part = [];
|
|
838
|
+
let parent = void 0;
|
|
839
|
+
let cursor = trunk;
|
|
840
|
+
while (part.length < path.length) {
|
|
841
|
+
if (isBranch(cursor)) {
|
|
842
|
+
const i = path[part.length];
|
|
843
|
+
part.push(i);
|
|
844
|
+
parent = cursor;
|
|
845
|
+
cursor = cursor.children[i];
|
|
846
|
+
if (isExtension(cursor)) {
|
|
847
|
+
parent = cursor;
|
|
848
|
+
part.push(...cursor.path);
|
|
849
|
+
cursor = cursor.child;
|
|
877
850
|
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
index += node.path.length;
|
|
883
|
-
node = node.child;
|
|
851
|
+
} else if (isExtension(cursor)) {
|
|
852
|
+
parent = cursor;
|
|
853
|
+
part.push(...cursor.path);
|
|
854
|
+
cursor = cursor.child;
|
|
884
855
|
} else {
|
|
885
856
|
break;
|
|
886
857
|
}
|
|
887
858
|
}
|
|
888
|
-
if (!
|
|
889
|
-
if (
|
|
890
|
-
|
|
891
|
-
} else if (
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
child: limb
|
|
895
|
-
};
|
|
896
|
-
} else {
|
|
897
|
-
const copy = { ...limb };
|
|
898
|
-
copy.path = concat(path.subarray(start + 1), copy.path);
|
|
899
|
-
node.children[path[start]] = copy;
|
|
859
|
+
if (!path.every((x, i) => x === part[i])) throw new Error("invalid path");
|
|
860
|
+
if (isBranch(parent)) {
|
|
861
|
+
parent.children[path[path.length - 1]] = limb;
|
|
862
|
+
} else if (isExtension(parent)) {
|
|
863
|
+
if (!isBranch(limb)) throw new Error("invalid limb");
|
|
864
|
+
parent.child.children = limb.children;
|
|
900
865
|
}
|
|
901
866
|
return trunk;
|
|
902
867
|
}
|
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,8 @@ 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
|
-
|
|
79
|
-
limbs: Limb[];
|
|
80
|
-
};
|
|
81
|
-
declare function graftLimb(trunk: MaybeNode, path: Uint8Array, limb: Node): Node;
|
|
78
|
+
declare function pluckLimbs(node: MaybeNode, depth: number): Limb[];
|
|
79
|
+
declare function graftLimb(trunk: MaybeNode, [path, limb]: Limb): Node;
|
|
82
80
|
|
|
83
81
|
type Hex = `0x${string}`;
|
|
84
82
|
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,8 @@ 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
|
-
|
|
79
|
-
limbs: Limb[];
|
|
80
|
-
};
|
|
81
|
-
declare function graftLimb(trunk: MaybeNode, path: Uint8Array, limb: Node): Node;
|
|
78
|
+
declare function pluckLimbs(node: MaybeNode, depth: number): Limb[];
|
|
79
|
+
declare function graftLimb(trunk: MaybeNode, [path, limb]: Limb): Node;
|
|
82
80
|
|
|
83
81
|
type Hex = `0x${string}`;
|
|
84
82
|
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,96 +748,58 @@ function mimcHash(v) {
|
|
|
745
748
|
}
|
|
746
749
|
|
|
747
750
|
// src/surgery.ts
|
|
748
|
-
function pluckLimbs(node, depth
|
|
749
|
-
if (!node || depth <= 0) return { trunk: node, limbs: [] };
|
|
750
|
-
const queue = [];
|
|
751
|
-
if (isBranch(node)) {
|
|
752
|
-
queue.push([node, []]);
|
|
753
|
-
} else if (isExtension(node)) {
|
|
754
|
-
if (node.path.length >= depth) {
|
|
755
|
-
const extension = node.path.length === depth ? node.child : { path: node.path.subarray(depth), child: node.child };
|
|
756
|
-
return {
|
|
757
|
-
trunk: void 0,
|
|
758
|
-
limbs: [[node.path.subarray(0, depth), extension]]
|
|
759
|
-
};
|
|
760
|
-
}
|
|
761
|
-
queue.push([node.child, [...node.path]]);
|
|
762
|
-
} else if (node.path.length >= depth) {
|
|
763
|
-
const leaf = newLeaf(node.path.subarray(depth), node.data);
|
|
764
|
-
return {
|
|
765
|
-
trunk: void 0,
|
|
766
|
-
limbs: [[node.path.subarray(0, depth), leaf]]
|
|
767
|
-
};
|
|
768
|
-
}
|
|
751
|
+
function pluckLimbs(node, depth) {
|
|
769
752
|
const limbs = [];
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
if (
|
|
775
|
-
|
|
776
|
-
limbs.push([Uint8Array.of(...path, i), child]);
|
|
777
|
-
} else if (isBranch(child)) {
|
|
778
|
-
queue.push([child, [...path, i]]);
|
|
779
|
-
} else if (isExtension(child) && path.length + child.path.length <= depth) {
|
|
780
|
-
queue.push([child.child, [...path, i, ...child.path]]);
|
|
781
|
-
} else if (exact) {
|
|
782
|
-
parent.children[i] = void 0;
|
|
783
|
-
const full = [...path, i, ...child.path];
|
|
784
|
-
const rest = new Uint8Array(full.slice(depth));
|
|
785
|
-
limbs.push([
|
|
786
|
-
new Uint8Array(full.slice(0, depth)),
|
|
787
|
-
isLeaf(child) ? newLeaf(rest, child.data) : rest.length ? { path: rest, child: child.child } : child.child
|
|
788
|
-
]);
|
|
753
|
+
if (node && depth > 0) pluck(node, []);
|
|
754
|
+
return limbs;
|
|
755
|
+
function pluck(node2, path, ext = false) {
|
|
756
|
+
if (path.length >= depth) {
|
|
757
|
+
if (ext && "cache" in node2) {
|
|
758
|
+
node2 = { children: node2.children };
|
|
789
759
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
const copy = { ...limb };
|
|
801
|
-
copy.path = concat(path, copy.path);
|
|
802
|
-
return copy;
|
|
760
|
+
limbs.push([new Uint8Array(path.slice(0, depth)), node2]);
|
|
761
|
+
return true;
|
|
762
|
+
} else if (isBranch(node2)) {
|
|
763
|
+
node2.children.forEach((x, i, v) => {
|
|
764
|
+
if (x && pluck(x, [...path, i])) {
|
|
765
|
+
v[i] = void 0;
|
|
766
|
+
}
|
|
767
|
+
});
|
|
768
|
+
} else if (isExtension(node2) && pluck(node2.child, [...path, ...node2.path], true)) {
|
|
769
|
+
node2.child = { ...node2.child, ...newBranch() };
|
|
803
770
|
}
|
|
804
771
|
}
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
772
|
+
}
|
|
773
|
+
function graftLimb(trunk, [path, limb]) {
|
|
774
|
+
if (!trunk || !path.length) throw new Error("invalid graft");
|
|
775
|
+
let part = [];
|
|
776
|
+
let parent = void 0;
|
|
777
|
+
let cursor = trunk;
|
|
778
|
+
while (part.length < path.length) {
|
|
779
|
+
if (isBranch(cursor)) {
|
|
780
|
+
const i = path[part.length];
|
|
781
|
+
part.push(i);
|
|
782
|
+
parent = cursor;
|
|
783
|
+
cursor = cursor.children[i];
|
|
784
|
+
if (isExtension(cursor)) {
|
|
785
|
+
parent = cursor;
|
|
786
|
+
part.push(...cursor.path);
|
|
787
|
+
cursor = cursor.child;
|
|
815
788
|
}
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
index += node.path.length;
|
|
821
|
-
node = node.child;
|
|
789
|
+
} else if (isExtension(cursor)) {
|
|
790
|
+
parent = cursor;
|
|
791
|
+
part.push(...cursor.path);
|
|
792
|
+
cursor = cursor.child;
|
|
822
793
|
} else {
|
|
823
794
|
break;
|
|
824
795
|
}
|
|
825
796
|
}
|
|
826
|
-
if (!
|
|
827
|
-
if (
|
|
828
|
-
|
|
829
|
-
} else if (
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
child: limb
|
|
833
|
-
};
|
|
834
|
-
} else {
|
|
835
|
-
const copy = { ...limb };
|
|
836
|
-
copy.path = concat(path.subarray(start + 1), copy.path);
|
|
837
|
-
node.children[path[start]] = copy;
|
|
797
|
+
if (!path.every((x, i) => x === part[i])) throw new Error("invalid path");
|
|
798
|
+
if (isBranch(parent)) {
|
|
799
|
+
parent.children[path[path.length - 1]] = limb;
|
|
800
|
+
} else if (isExtension(parent)) {
|
|
801
|
+
if (!isBranch(limb)) throw new Error("invalid limb");
|
|
802
|
+
parent.child.children = limb.children;
|
|
838
803
|
}
|
|
839
804
|
return trunk;
|
|
840
805
|
}
|