@ensdomains/merkle-builder 0.0.3 → 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/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, exact) {
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((child, i) => {
835
- if (!child) return;
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), 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
- ]);
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 node = trunk;
872
+ let parent = trunk;
870
873
  while (index < path.length - 1) {
871
- if (isBranch(node)) {
872
- const child = node.children[path[index]];
873
- if (!child) {
874
- start = index;
875
- index = path.length - 1;
876
- break;
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 (!isBranch(node)) throw new RangeError("invalid graft");
889
- if (start === index) {
890
- node.children[path[start]] = limb;
891
- } else if (isBranch(limb)) {
892
- node.children[path[start]] = {
893
- path: path.slice(start + 1),
894
- child: limb
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
- const copy = { ...limb };
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, exact?: boolean): {
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: Uint8Array, limb: Node): Node;
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, exact?: boolean): {
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: Uint8Array, limb: Node): Node;
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, exact) {
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((child, i) => {
773
- if (!child) return;
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), 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
- ]);
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 node = trunk;
810
+ let parent = trunk;
808
811
  while (index < path.length - 1) {
809
- if (isBranch(node)) {
810
- const child = node.children[path[index]];
811
- if (!child) {
812
- start = index;
813
- index = path.length - 1;
814
- break;
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 (!isBranch(node)) throw new RangeError("invalid graft");
827
- if (start === index) {
828
- node.children[path[start]] = limb;
829
- } else if (isBranch(limb)) {
830
- node.children[path[start]] = {
831
- path: path.slice(start + 1),
832
- child: limb
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
- const copy = { ...limb };
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ensdomains/merkle-builder",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Low-level Merkle-Patricia storage trie implementation",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",