@chainsafe/ssz
Version:
Simple Serialize
43 lines • 1.66 kB
JavaScript
import { packedUintNum64sToLeafNodes, subtreeFillToContents } from "@chainsafe/persistent-merkle-tree";
import { addLengthNode } from "./arrayBasic.js";
import { ListBasicType } from "./listBasic.js";
import { UintNumberType } from "./uint.js";
/**
* Specific implementation of ListBasicType for UintNumberType with some optimizations.
*/
export class ListUintNum64Type extends ListBasicType {
constructor(limit, opts) {
super(new UintNumberType(8), limit, opts);
}
/**
* Return a ListBasicTreeViewDU with nodes populated
*/
toViewDU(value) {
// no need to serialize and deserialize like in the abstract class
const { treeNode, leafNodes } = this.packedUintNum64sToNode(value);
// cache leaf nodes in the ViewDU
return this.getViewDU(treeNode, {
nodes: leafNodes,
length: value.length,
nodesPopulated: true,
});
}
/**
* No need to serialize and deserialize like in the abstract class
*/
value_toTree(value) {
const { treeNode } = this.packedUintNum64sToNode(value);
return treeNode;
}
packedUintNum64sToNode(value) {
if (value.length > this.limit) {
throw new Error(`Exceeds limit: ${value.length} > ${this.limit}`);
}
const leafNodes = packedUintNum64sToLeafNodes(value);
// subtreeFillToContents mutates the leafNodes array
const rootNode = subtreeFillToContents([...leafNodes], this.chunkDepth);
const treeNode = addLengthNode(rootNode, value.length);
return { treeNode, leafNodes };
}
}
//# sourceMappingURL=listUintNum64.js.map