Socket
Socket
Sign inDemoInstall

@chainsafe/ssz

Package Overview
Dependencies
Maintainers
5
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chainsafe/ssz - npm Package Compare versions

Comparing version 0.8.13 to 0.8.15

15

CHANGELOG.md

@@ -0,1 +1,15 @@

## 0.8.15 (2021-08-23)
## Features
- Avoid iterateNodesAtDepth [#167](https://github.com/ChainSafe/ssz/pull/167)
## 0.8.14 (2021-08-19)
## Features
- Add getFixedSerializedLength() [#148](https://github.com/ChainSafe/ssz/pull/148)
- Cache field information for Container type [#146](https://github.com/ChainSafe/ssz/pull/146)
- Use persistent-merkle-tree 0.3.5 and as-sha256 0.2.4 [#165](https://github.com/ChainSafe/ssz/pull/165)
## 0.8.13 (2021-08-04)

@@ -105,2 +119,3 @@

## BREAKING CHANGES
- Use abstract class Type/BasicType/CompositeType ([c91b19](https://github.com/chainsafe/ssz/commit/c91b19))

@@ -107,0 +122,0 @@ - Remove TreeBacked<T>#gindexOfProperty ([d4b141](https://github.com/chainsafe/ssz/commit/d4b141))

1

lib/backings/backedValue.d.ts

@@ -13,1 +13,2 @@ import { CompositeValue } from "../interface";

export declare function isBackedValue<T extends CompositeValue>(value: unknown): value is BackedValue<T>;
//# sourceMappingURL=backedValue.d.ts.map

14

lib/backings/backedValue.js
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isBackedValue = isBackedValue;
var _tree = require("./tree");
Object.defineProperty(exports, "__esModule", { value: true });
exports.isBackedValue = void 0;
const tree_1 = require("./tree");
function isBackedValue(value) {
return (0, _tree.isTreeBacked)(value);
return tree_1.isTreeBacked(value);
}
exports.isBackedValue = isBackedValue;
//# sourceMappingURL=backedValue.js.map
export * from "./tree";
export * from "./backedValue";
export * from "./readonlyIterate";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _tree = require("./tree");
Object.keys(_tree).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _tree[key];
}
});
});
var _backedValue = require("./backedValue");
Object.keys(_backedValue).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _backedValue[key];
}
});
});
var _readonlyIterate = require("./readonlyIterate");
Object.keys(_readonlyIterate).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _readonlyIterate[key];
}
});
});
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./tree"), exports);
__exportStar(require("./backedValue"), exports);
__exportStar(require("./readonlyIterate"), exports);
//# sourceMappingURL=index.js.map

@@ -5,1 +5,2 @@ import { CompositeValue } from "../interface";

export declare function readonlyEntries<T extends CompositeValue>(obj: T): Iterable<[keyof T, ValueOf<T>]>;
//# sourceMappingURL=readonlyIterate.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.readonlyValues = readonlyValues;
exports.readonlyEntries = readonlyEntries;
var _tree = require("./tree");
Object.defineProperty(exports, "__esModule", { value: true });
exports.readonlyEntries = exports.readonlyValues = void 0;
const tree_1 = require("./tree");
function readonlyValues(obj) {
if ((0, _tree.isTreeBacked)(obj) && obj.readonlyValues) {
return obj.readonlyValues();
} else {
return Object.values(obj);
}
if (tree_1.isTreeBacked(obj) && obj.readonlyValues) {
return obj.readonlyValues();
}
else {
return Object.values(obj);
}
}
exports.readonlyValues = readonlyValues;
function readonlyEntries(obj) {
if ((0, _tree.isTreeBacked)(obj) && obj.readonlyEntries) {
return obj.readonlyEntries();
} else {
return Object.entries(obj);
}
if (tree_1.isTreeBacked(obj) && obj.readonlyEntries) {
return obj.readonlyEntries();
}
else {
return Object.entries(obj);
}
}
exports.readonlyEntries = readonlyEntries;
//# sourceMappingURL=readonlyIterate.js.map
export * from "./interface";
export * from "./treeValue";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _interface = require("./interface");
Object.keys(_interface).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _interface[key];
}
});
});
var _treeValue = require("./treeValue");
Object.keys(_treeValue).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _treeValue[key];
}
});
});
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./interface"), exports);
__exportStar(require("./treeValue"), exports);
//# sourceMappingURL=index.js.map

@@ -53,1 +53,2 @@ import { Proof, Tree } from "@chainsafe/persistent-merkle-tree";

}
//# sourceMappingURL=interface.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=interface.js.map

@@ -6,3 +6,3 @@ import { Proof, Tree } from "@chainsafe/persistent-merkle-tree";

import { ITreeBacked, TreeBacked, ValueOf } from "./interface";
declare type TreeValueConstructor<T> = {
declare type TreeValueConstructor<T extends CompositeValue> = {
new (type: CompositeType<T>, tree: Tree): TreeValue<T>;

@@ -78,3 +78,3 @@ };

}
export declare class CompositeListTreeValue<T extends List<object>> extends CompositeArrayTreeValue<T> {
export declare class CompositeListTreeValue<T extends List<unknown>> extends CompositeArrayTreeValue<T> {
type: CompositeListType<T>;

@@ -97,1 +97,2 @@ constructor(type: CompositeListType<T>, tree: Tree);

export {};
//# sourceMappingURL=treeValue.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isTreeBacked = isTreeBacked;
exports.createTreeBacked = createTreeBacked;
exports.getTreeValueClass = getTreeValueClass;
exports.proxyWrapTreeValue = proxyWrapTreeValue;
exports.ContainerTreeValue = exports.CompositeListTreeValue = exports.BasicListTreeValue = exports.CompositeArrayTreeValue = exports.BasicArrayTreeValue = exports.TreeValue = exports.TreeProxyHandler = void 0;
var _types = require("../../types");
var _byteArray = require("../../util/byteArray");
var _tree = require("../../util/tree");
let _Symbol$iterator;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContainerTreeValue = exports.CompositeListTreeValue = exports.BasicListTreeValue = exports.CompositeArrayTreeValue = exports.BasicArrayTreeValue = exports.TreeValue = exports.TreeProxyHandler = exports.proxyWrapTreeValue = exports.getTreeValueClass = exports.createTreeBacked = exports.isTreeBacked = void 0;
const types_1 = require("../../types");
const byteArray_1 = require("../../util/byteArray");
const tree_1 = require("../../util/tree");
function isTreeBacked(value) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return value && value.type && value.tree && (0, _tree.isTree)(value.tree);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return value && value.type && value.tree && tree_1.isTree(value.tree);
}
exports.isTreeBacked = isTreeBacked;
/**
* Return an ES6 Proxy-wrapped tree value (ergonomic getter/setter/iteration)
*/
function createTreeBacked(type, tree) {
const TreeValueClass = getTreeValueClass(type);
return proxyWrapTreeValue(new TreeValueClass(type, tree));
const TreeValueClass = getTreeValueClass(type);
return proxyWrapTreeValue(new TreeValueClass(type, tree));
}
exports.createTreeBacked = createTreeBacked;
function getTreeValueClass(type) {
if ((0, _types.isListType)(type)) {
if ((0, _types.isBasicType)(type.elementType)) {
return BasicListTreeValue;
} else {
return CompositeListTreeValue;
if (types_1.isListType(type)) {
if (types_1.isBasicType(type.elementType)) {
return BasicListTreeValue;
}
else {
return CompositeListTreeValue;
}
}
} else if ((0, _types.isVectorType)(type)) {
if ((0, _types.isBasicType)(type.elementType)) {
return BasicArrayTreeValue;
} else {
return CompositeArrayTreeValue;
else if (types_1.isVectorType(type)) {
if (types_1.isBasicType(type.elementType)) {
return BasicArrayTreeValue;
}
else {
return CompositeArrayTreeValue;
}
}
} else if ((0, _types.isContainerType)(type)) {
return ContainerTreeValue;
}
else if (types_1.isContainerType(type)) {
return ContainerTreeValue;
}
throw Error("No TreeValueClass for type");
}
exports.getTreeValueClass = getTreeValueClass;
/**
* Wrap a TreeValue in a Proxy that adds ergonomic getter/setter
*/
function proxyWrapTreeValue(value) {
return new Proxy(value, TreeProxyHandler);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new Proxy(value, exports.TreeProxyHandler);
}
exports.proxyWrapTreeValue = proxyWrapTreeValue;
/**
* Proxy handler that adds ergonomic get/set and exposes TreeValue methods
*/
const TreeProxyHandler = {
get(target, property) {
if (property in target) {
return target[property];
} else {
return target.getProperty(property);
}
},
set(target, property, value) {
return target.setProperty(property, value);
},
ownKeys(target) {
return target.getPropertyNames();
},
getOwnPropertyDescriptor(target, property) {
if (target.type.getPropertyType(property)) {
return {
configurable: true,
enumerable: true,
writable: true
};
} else {
return undefined;
}
}
exports.TreeProxyHandler = {
get(target, property) {
if (property in target) {
return target[property];
}
else {
return target.getProperty(property);
}
},
set(target, property, value) {
return target.setProperty(property, value);
},
ownKeys(target) {
return target.getPropertyNames();
},
getOwnPropertyDescriptor(target, property) {
if (target.type.getPropertyType(property)) {
return {
configurable: true,
enumerable: true,
writable: true,
};
}
else {
return undefined;
}
},
};

@@ -99,327 +85,256 @@ /**

*/
exports.TreeProxyHandler = TreeProxyHandler;
_Symbol$iterator = Symbol.iterator;
class TreeValue {
constructor(type, tree) {
_defineProperty(this, "type", void 0);
_defineProperty(this, "tree", void 0);
this.type = type;
this.tree = tree;
}
clone() {
const TreeValueClass = Object.getPrototypeOf(this).constructor;
return proxyWrapTreeValue(new TreeValueClass(this.type, this.tree.clone()));
}
valueOf() {
return this.type.tree_convertToStruct(this.tree);
}
equals(other) {
if (isTreeBacked(other)) {
return (0, _byteArray.byteArrayEquals)(this.hashTreeRoot(), other.hashTreeRoot());
} else {
return this.type.struct_equals(this, other);
type;
tree;
constructor(type, tree) {
this.type = type;
this.tree = tree;
}
}
size() {
return this.type.tree_getSerializedLength(this.tree);
}
toStruct() {
return this.type.tree_convertToStruct(this.tree);
}
toBytes(output, offset) {
return this.type.tree_serializeToBytes(this.tree, output, offset);
}
serialize() {
const output = new Uint8Array(this.type.tree_getSerializedLength(this.tree));
this.toBytes(output, 0);
return output;
}
hashTreeRoot() {
return this.tree.root;
}
createProof(paths) {
return this.type.tree_createProof(this.tree, paths);
}
getPropertyNames() {
return this.type.tree_getPropertyNames(this.tree);
}
[_Symbol$iterator]() {
return this.values();
}
clone() {
const TreeValueClass = Object.getPrototypeOf(this).constructor;
return proxyWrapTreeValue(new TreeValueClass(this.type, this.tree.clone()));
}
valueOf() {
return this.type.tree_convertToStruct(this.tree);
}
equals(other) {
if (isTreeBacked(other)) {
return byteArray_1.byteArrayEquals(this.hashTreeRoot(), other.hashTreeRoot());
}
else {
return this.type.struct_equals(this, other);
}
}
size() {
return this.type.tree_getSerializedLength(this.tree);
}
toStruct() {
return this.type.tree_convertToStruct(this.tree);
}
toBytes(output, offset) {
return this.type.tree_serializeToBytes(this.tree, output, offset);
}
serialize() {
const output = new Uint8Array(this.type.tree_getSerializedLength(this.tree));
this.toBytes(output, 0);
return output;
}
hashTreeRoot() {
return this.tree.root;
}
createProof(paths) {
return this.type.tree_createProof(this.tree, paths);
}
getPropertyNames() {
return this.type.tree_getPropertyNames(this.tree);
}
[Symbol.iterator]() {
return this.values();
}
}
exports.TreeValue = TreeValue;
class BasicArrayTreeValue extends TreeValue {
constructor(type, tree) {
super(type, tree);
_defineProperty(this, "type", void 0);
this.type = type;
}
getProperty(property) {
return this.type.tree_getProperty(this.tree, property);
}
setProperty(property, value) {
return this.type.tree_setProperty(this.tree, property, value);
}
*keys() {
const propNames = this.getPropertyNames(); // pop off "length"
propNames.pop();
yield* propNames.map(String);
}
values() {
return this.type.tree_iterateValues(this.tree);
}
*entries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.values()) {
yield [String(keys[i]), value];
i++;
type;
constructor(type, tree) {
super(type, tree);
this.type = type;
}
}
readonlyValues() {
return this.type.tree_readonlyIterateValues(this.tree);
}
*readonlyEntries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.readonlyValues()) {
yield [String(keys[i]), value];
i++;
getProperty(property) {
return this.type.tree_getProperty(this.tree, property);
}
}
setProperty(property, value) {
return this.type.tree_setProperty(this.tree, property, value);
}
*keys() {
const propNames = this.getPropertyNames();
// pop off "length"
propNames.pop();
yield* propNames.map(String);
}
values() {
return this.type.tree_iterateValues(this.tree);
}
*entries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.values()) {
yield [String(keys[i]), value];
i++;
}
}
readonlyValues() {
return this.type.tree_readonlyIterateValues(this.tree);
}
*readonlyEntries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.readonlyValues()) {
yield [String(keys[i]), value];
i++;
}
}
}
exports.BasicArrayTreeValue = BasicArrayTreeValue;
class CompositeArrayTreeValue extends TreeValue {
constructor(type, tree) {
super(type, tree);
_defineProperty(this, "type", void 0);
this.type = type;
}
getProperty(property) {
if (property === "length") {
return this.type.tree_getProperty(this.tree, property);
type;
constructor(type, tree) {
super(type, tree);
this.type = type;
}
return createTreeBacked(this.type.elementType, this.type.tree_getProperty(this.tree, property));
}
setProperty(property, value) {
return this.type.tree_setProperty(this.tree, property, isTreeBacked(value) ? value.tree : this.type.elementType.struct_convertToTree(value));
}
*keys() {
const propNames = this.getPropertyNames(); // pop off "length"
propNames.pop();
yield* propNames.map(String);
}
*values() {
for (const tree of this.type.tree_iterateValues(this.tree)) {
yield createTreeBacked(this.type.elementType, tree);
getProperty(property) {
if (property === "length") {
return this.type.tree_getProperty(this.tree, property);
}
return createTreeBacked(this.type.elementType, this.type.tree_getProperty(this.tree, property));
}
}
*entries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.values()) {
yield [String(keys[i]), value];
i++;
setProperty(property, value) {
return this.type.tree_setProperty(this.tree, property, isTreeBacked(value)
? value.tree
: this.type.elementType.struct_convertToTree(value));
}
}
*readonlyValues() {
for (const tree of this.type.tree_readonlyIterateValues(this.tree)) {
yield createTreeBacked(this.type.elementType, tree);
*keys() {
const propNames = this.getPropertyNames();
// pop off "length"
propNames.pop();
yield* propNames.map(String);
}
}
*readonlyEntries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.readonlyValues()) {
yield [String(keys[i]), value];
i++;
*values() {
for (const tree of this.type.tree_iterateValues(this.tree)) {
yield createTreeBacked(this.type.elementType, tree);
}
}
}
*entries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.values()) {
yield [String(keys[i]), value];
i++;
}
}
*readonlyValues() {
for (const tree of this.type.tree_readonlyIterateValues(this.tree)) {
yield createTreeBacked(this.type.elementType, tree);
}
}
*readonlyEntries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.readonlyValues()) {
yield [String(keys[i]), value];
i++;
}
}
}
exports.CompositeArrayTreeValue = CompositeArrayTreeValue;
class BasicListTreeValue extends BasicArrayTreeValue {
constructor(type, tree) {
super(type, tree);
_defineProperty(this, "type", void 0);
this.type = type;
}
push(...values) {
return this.type.tree_push(this.tree, ...values);
}
pop() {
return this.type.tree_pop(this.tree);
}
type;
constructor(type, tree) {
super(type, tree);
this.type = type;
}
push(...values) {
return this.type.tree_push(this.tree, ...values);
}
pop() {
return this.type.tree_pop(this.tree);
}
}
exports.BasicListTreeValue = BasicListTreeValue;
class CompositeListTreeValue extends CompositeArrayTreeValue {
constructor(type, tree) {
super(type, tree);
_defineProperty(this, "type", void 0);
this.type = type;
}
push(...values) {
const convertedValues = values.map(value => isTreeBacked(value) ? value.tree : this.type.elementType.struct_convertToTree(value));
return this.type.tree_push(this.tree, ...convertedValues);
}
pop() {
return this.type.tree_pop(this.tree);
}
type;
constructor(type, tree) {
super(type, tree);
this.type = type;
}
push(...values) {
const convertedValues = values.map((value) => isTreeBacked(value)
? value.tree
: this.type.elementType.struct_convertToTree(value));
return this.type.tree_push(this.tree, ...convertedValues);
}
pop() {
return this.type.tree_pop(this.tree);
}
}
exports.CompositeListTreeValue = CompositeListTreeValue;
class ContainerTreeValue extends TreeValue {
constructor(type, tree) {
super(type, tree);
_defineProperty(this, "type", void 0);
this.type = type;
}
getProperty(property) {
if (!this.type.fields[property]) {
return undefined;
type;
constructor(type, tree) {
super(type, tree);
this.type = type;
}
const propType = this.type.getPropertyType(property);
const propValue = this.type.tree_getProperty(this.tree, property);
if ((0, _types.isCompositeType)(propType)) {
return createTreeBacked(propType, propValue);
} else {
return propValue;
getProperty(property) {
if (!this.type.fields[property]) {
return undefined;
}
const propType = this.type.getPropertyType(property);
const propValue = this.type.tree_getProperty(this.tree, property);
if (!this.type.fieldInfos.get(property).isBasic) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return createTreeBacked(propType, propValue);
}
else {
return propValue;
}
}
}
setProperty(property, value) {
const propType = this.type.getPropertyType(property);
if ((0, _types.isCompositeType)(propType)) {
if (isTreeBacked(value)) {
return this.type.tree_setProperty(this.tree, property, value.tree);
} else {
return this.type.tree_setProperty(this.tree, property, propType.struct_convertToTree(value));
}
} else {
return this.type.tree_setProperty(this.tree, property, value);
setProperty(property, value) {
if (!this.type.fieldInfos.get(property).isBasic) {
if (isTreeBacked(value)) {
return this.type.tree_setProperty(this.tree, property, value.tree);
}
else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const propType = this.type.getPropertyType(property);
return this.type.tree_setProperty(this.tree, property, propType.struct_convertToTree(value));
}
}
else {
return this.type.tree_setProperty(this.tree, property, value);
}
}
}
*keys() {
yield* this.getPropertyNames();
}
*values() {
for (const [_key, value] of this.entries()) {
yield value;
*keys() {
yield* this.getPropertyNames();
}
}
*entries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.type.tree_iterateValues(this.tree)) {
const propName = keys[i];
const propType = this.type.getPropertyType(propName);
if ((0, _types.isCompositeType)(propType)) {
yield [propName, createTreeBacked(propType, value)];
} else {
yield [propName, value];
}
i++;
*values() {
for (const [_key, value] of this.entries()) {
yield value;
}
}
}
*readonlyValues() {
for (const [_key, value] of this.readonlyEntries()) {
yield value;
*entries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.type.tree_iterateValues(this.tree)) {
const propName = keys[i];
const propType = this.type.getPropertyType(propName);
if (types_1.isCompositeType(propType)) {
yield [propName, createTreeBacked(propType, value)];
}
else {
yield [propName, value];
}
i++;
}
}
}
*readonlyEntries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.type.tree_readonlyIterateValues(this.tree)) {
const propName = keys[i];
const propType = this.type.getPropertyType(propName);
if ((0, _types.isCompositeType)(propType)) {
yield [propName, createTreeBacked(propType, value)];
} else {
yield [propName, value];
}
i++;
*readonlyValues() {
for (const [_key, value] of this.readonlyEntries()) {
yield value;
}
}
}
*readonlyEntries() {
const keys = this.getPropertyNames();
let i = 0;
for (const value of this.type.tree_readonlyIterateValues(this.tree)) {
const propName = keys[i];
const propType = this.type.getPropertyType(propName);
if (types_1.isCompositeType(propType)) {
yield [propName, createTreeBacked(propType, value)];
}
else {
yield [propName, value];
}
i++;
}
}
}
exports.ContainerTreeValue = ContainerTreeValue;
//# sourceMappingURL=treeValue.js.map

@@ -7,1 +7,2 @@ export * from "./interface";

export * from "./util/tree";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
hash: true
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "hash", {
enumerable: true,
get: function get() {
return _compat.hash;
}
});
var _interface = require("./interface");
Object.keys(_interface).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _interface[key];
}
});
});
var _types = require("./types");
Object.keys(_types).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _types[key];
}
});
});
var _backings = require("./backings");
Object.keys(_backings).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _backings[key];
}
});
});
var _compat = require("./util/compat");
var _byteArray = require("./util/byteArray");
Object.keys(_byteArray).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _byteArray[key];
}
});
});
var _tree = require("./util/tree");
Object.keys(_tree).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _tree[key];
}
});
});
Object.defineProperty(exports, "__esModule", { value: true });
exports.hash = void 0;
__exportStar(require("./interface"), exports);
__exportStar(require("./types"), exports);
__exportStar(require("./backings"), exports);
var compat_1 = require("./util/compat");
Object.defineProperty(exports, "hash", { enumerable: true, get: function () { return compat_1.hash; } });
__exportStar(require("./util/byteArray"), exports);
__exportStar(require("./util/tree"), exports);
//# sourceMappingURL=index.js.map

@@ -15,3 +15,3 @@ /**

}
export declare type Container<T extends object> = T;
export declare type Container<T extends Record<string, unknown>> = T;
export declare type ByteVector = Vector<number>;

@@ -23,3 +23,3 @@ export declare type BitVector = Vector<boolean>;

}
export declare type CompositeValue = Record<string, unknown> | ArrayLike<unknown> | {};
export declare type CompositeValue = Record<string, any> | ArrayLike<unknown> | Record<string, never>;
/**

@@ -31,1 +31,2 @@ * The Json interface is used for json-serializable input

} | Json[];
//# sourceMappingURL=interface.d.ts.map
"use strict";
/**
* These interfaces are consistent across all backings.
* As long as these interfaces are respected, the backing can be abstracted entirely.
*/
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=interface.js.map

@@ -19,2 +19,3 @@ import { Type } from "../type";

hasVariableSerializedLength(): boolean;
getFixedSerializedLength(): number;
getMaxSerializedLength(): number;

@@ -26,1 +27,2 @@ getMinSerializedLength(): number;

}
//# sourceMappingURL=abstract.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isBasicType = isBasicType;
exports.BasicType = exports.BASIC_TYPE = void 0;
var _type = require("../type");
/* eslint-disable @typescript-eslint/camelcase */
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasicType = exports.isBasicType = exports.BASIC_TYPE = void 0;
/* eslint-disable @typescript-eslint/member-ordering */
/* eslint-disable @typescript-eslint/no-unused-vars */
const BASIC_TYPE = Symbol.for("ssz/BasicType");
exports.BASIC_TYPE = BASIC_TYPE;
const type_1 = require("../type");
exports.BASIC_TYPE = Symbol.for("ssz/BasicType");
function isBasicType(type) {
return (0, _type.isTypeOf)(type, BASIC_TYPE);
return type_1.isTypeOf(type, exports.BASIC_TYPE);
}
exports.isBasicType = isBasicType;
/**

@@ -27,65 +17,52 @@ * A BasicType is a terminal type, which has no flexibility in its representation.

*/
class BasicType extends _type.Type {
constructor() {
super();
this._typeSymbols.add(BASIC_TYPE);
}
struct_clone(value) {
return value;
}
struct_equals(value1, value2) {
this.assertValidValue(value1);
this.assertValidValue(value2);
return value1 === value2;
}
/**
* Check if type has a variable number of elements (or subelements)
*
* For basic types, this is always false
*/
hasVariableSerializedLength() {
return false;
}
getMaxSerializedLength() {
return this.struct_getSerializedLength();
}
getMinSerializedLength() {
return this.struct_getSerializedLength();
}
bytes_validate(data, offset) {
if (!data) {
throw new Error("Data is null or undefined");
class BasicType extends type_1.Type {
constructor() {
super();
this._typeSymbols.add(exports.BASIC_TYPE);
}
if (data.length === 0) {
throw new Error("Data is empty");
struct_clone(value) {
return value;
}
const length = data.length - offset;
if (length < this.struct_getSerializedLength()) {
throw new Error("Data length of ".concat(length, " is too small, expect ").concat(this.struct_getSerializedLength()));
} // accept data length > this.size()
}
struct_hashTreeRoot(value) {
const output = new Uint8Array(32);
this.struct_serializeToBytes(value, output, 0);
return output;
}
struct_equals(value1, value2) {
this.assertValidValue(value1);
this.assertValidValue(value2);
return value1 === value2;
}
/**
* Check if type has a variable number of elements (or subelements)
*
* For basic types, this is always false
*/
hasVariableSerializedLength() {
return false;
}
getFixedSerializedLength() {
return this.struct_getSerializedLength();
}
getMaxSerializedLength() {
return this.struct_getSerializedLength();
}
getMinSerializedLength() {
return this.struct_getSerializedLength();
}
bytes_validate(data, offset) {
if (!data) {
throw new Error("Data is null or undefined");
}
if (data.length === 0) {
throw new Error("Data is empty");
}
const length = data.length - offset;
if (length < this.struct_getSerializedLength()) {
throw new Error(`Data length of ${length} is too small, expect ${this.struct_getSerializedLength()}`);
}
// accept data length > this.size()
}
struct_hashTreeRoot(value) {
const output = new Uint8Array(32);
this.struct_serializeToBytes(value, output, 0);
return output;
}
}
exports.BasicType = BasicType;
//# sourceMappingURL=abstract.js.map

@@ -16,1 +16,2 @@ import { Json } from "../../interface";

}
//# sourceMappingURL=boolean.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isBooleanType = isBooleanType;
exports.BooleanType = exports.BOOLEAN_TYPE = void 0;
var _type = require("../type");
var _abstract = require("./abstract");
/* eslint-disable @typescript-eslint/camelcase */
const BOOLEAN_TYPE = Symbol.for("ssz/BooleanType");
exports.BOOLEAN_TYPE = BOOLEAN_TYPE;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BooleanType = exports.isBooleanType = exports.BOOLEAN_TYPE = void 0;
const type_1 = require("../type");
const abstract_1 = require("./abstract");
exports.BOOLEAN_TYPE = Symbol.for("ssz/BooleanType");
function isBooleanType(type) {
return (0, _type.isTypeOf)(type, BOOLEAN_TYPE);
return type_1.isTypeOf(type, exports.BOOLEAN_TYPE);
}
class BooleanType extends _abstract.BasicType {
constructor() {
super();
this._typeSymbols.add(BOOLEAN_TYPE);
}
struct_getSerializedLength() {
return 1;
}
struct_assertValidValue(value) {
if (value !== true && value !== false) {
throw new Error("Boolean value must be true or false");
exports.isBooleanType = isBooleanType;
class BooleanType extends abstract_1.BasicType {
constructor() {
super();
this._typeSymbols.add(exports.BOOLEAN_TYPE);
}
}
struct_defaultValue() {
return false;
}
struct_serializeToBytes(value, output, offset) {
output[offset] = value ? 1 : 0;
return offset + 1;
}
struct_deserializeFromBytes(data, offset) {
this.bytes_validate(data, offset);
if (data[offset] === 1) {
return true;
} else if (data[offset] === 0) {
return false;
} else {
throw new Error("Invalid boolean value");
struct_getSerializedLength() {
return 1;
}
}
struct_convertFromJson(data) {
this.struct_assertValidValue(data);
return data;
}
struct_convertToJson(value) {
return value;
}
struct_assertValidValue(value) {
if (value !== true && value !== false) {
throw new Error("Boolean value must be true or false");
}
}
struct_defaultValue() {
return false;
}
struct_serializeToBytes(value, output, offset) {
output[offset] = value ? 1 : 0;
return offset + 1;
}
struct_deserializeFromBytes(data, offset) {
this.bytes_validate(data, offset);
if (data[offset] === 1) {
return true;
}
else if (data[offset] === 0) {
return false;
}
else {
throw new Error("Invalid boolean value");
}
}
struct_convertFromJson(data) {
this.struct_assertValidValue(data);
return data;
}
struct_convertToJson(value) {
return value;
}
}
exports.BooleanType = BooleanType;
//# sourceMappingURL=boolean.js.map

@@ -5,1 +5,2 @@ export * from "./abstract";

export * from "./wellKnown";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _abstract = require("./abstract");
Object.keys(_abstract).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _abstract[key];
}
});
});
var _boolean = require("./boolean");
Object.keys(_boolean).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _boolean[key];
}
});
});
var _uint = require("./uint");
Object.keys(_uint).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _uint[key];
}
});
});
var _wellKnown = require("./wellKnown");
Object.keys(_wellKnown).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _wellKnown[key];
}
});
});
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./abstract"), exports);
__exportStar(require("./boolean"), exports);
__exportStar(require("./uint"), exports);
__exportStar(require("./wellKnown"), exports);
//# sourceMappingURL=index.js.map

@@ -6,2 +6,3 @@ import { Json } from "../../interface";

byteLength: number;
infinityWhenBig?: boolean;
}

@@ -12,2 +13,3 @@ export declare const UINT_TYPE: unique symbol;

byteLength: number;
infinityWhenBig: boolean;
constructor(options: IUintOptions);

@@ -19,3 +21,3 @@ struct_getSerializedLength(): number;

export declare class NumberUintType extends UintType<number> {
_maxBigInt: BigInt;
_maxBigInt?: BigInt;
constructor(options: IUintOptions);

@@ -41,1 +43,2 @@ struct_assertValidValue(value: unknown): asserts value is number;

}
//# sourceMappingURL=uint.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isUintType = isUintType;
exports.isNumberUintType = isNumberUintType;
exports.isBigIntUintType = isBigIntUintType;
exports.BigIntUintType = exports.BIGINT_UINT_TYPE = exports.NumberUintType = exports.NUMBER_UINT_TYPE = exports.UintType = exports.UINT_TYPE = void 0;
var _bigInt = require("../../util/bigInt");
var _type = require("../type");
var _abstract = require("./abstract");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const UINT_TYPE = Symbol.for("ssz/UintType");
exports.UINT_TYPE = UINT_TYPE;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BigIntUintType = exports.isBigIntUintType = exports.BIGINT_UINT_TYPE = exports.NumberUintType = exports.isNumberUintType = exports.NUMBER_UINT_TYPE = exports.UintType = exports.isUintType = exports.UINT_TYPE = void 0;
const bigInt_1 = require("../../util/bigInt");
const type_1 = require("../type");
const abstract_1 = require("./abstract");
exports.UINT_TYPE = Symbol.for("ssz/UintType");
function isUintType(type) {
return (0, _type.isTypeOf)(type, UINT_TYPE);
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
class UintType extends _abstract.BasicType {
constructor(options) {
super();
_defineProperty(this, "byteLength", void 0);
this.byteLength = options.byteLength;
this._typeSymbols.add(UINT_TYPE);
}
struct_getSerializedLength() {
return this.byteLength;
}
return type_1.isTypeOf(type, exports.UINT_TYPE);
}
exports.isUintType = isUintType;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
class UintType extends abstract_1.BasicType {
byteLength;
infinityWhenBig;
constructor(options) {
super();
this.byteLength = options.byteLength;
this.infinityWhenBig = options.infinityWhenBig === true ? true : this.byteLength > 6;
this._typeSymbols.add(exports.UINT_TYPE);
}
struct_getSerializedLength() {
return this.byteLength;
}
}
exports.UintType = UintType;
const NUMBER_UINT_TYPE = Symbol.for("ssz/NumberUintType");
exports.NUMBER_UINT_TYPE = NUMBER_UINT_TYPE;
exports.NUMBER_UINT_TYPE = Symbol.for("ssz/NumberUintType");
const BIGINT_4_BYTES = BigInt(32);
function isNumberUintType(type) {
return (0, _type.isTypeOf)(type, NUMBER_UINT_TYPE);
return type_1.isTypeOf(type, exports.NUMBER_UINT_TYPE);
}
exports.isNumberUintType = isNumberUintType;
class NumberUintType extends UintType {
constructor(options) {
super(options);
_defineProperty(this, "_maxBigInt", void 0);
this._typeSymbols.add(NUMBER_UINT_TYPE);
}
struct_assertValidValue(value) {
if (value !== Infinity && (!Number.isSafeInteger(value) || value > (0, _bigInt.bigIntPow)(BigInt(2), BigInt(8) * BigInt(this.byteLength)))) {
throw new Error("Uint value is not a number");
_maxBigInt;
constructor(options) {
super(options);
this._typeSymbols.add(exports.NUMBER_UINT_TYPE);
}
if (value < 0) {
throw new Error("Uint value must be gte 0");
struct_assertValidValue(value) {
if (value !== Infinity &&
(!Number.isSafeInteger(value) ||
value > bigInt_1.bigIntPow(BigInt(2), BigInt(8) * BigInt(this.byteLength)))) {
throw new Error("Uint value is not a number");
}
if (value < 0) {
throw new Error("Uint value must be gte 0");
}
}
}
struct_defaultValue() {
return 0;
}
struct_getMaxBigInt() {
if (this._maxBigInt === undefined) {
this._maxBigInt = (0, _bigInt.bigIntPow)(BigInt(2), BigInt(this.byteLength * 8)) - BigInt(1);
struct_defaultValue() {
return 0;
}
return this._maxBigInt;
}
struct_serializeToBytes(value, output, offset) {
if (this.byteLength > 6 && value === Infinity) {
for (let i = offset; i < offset + this.byteLength; i++) {
output[i] = 0xff;
}
} else {
let v = value;
const MAX_BYTE = 0xff;
for (let i = 0; i < this.byteLength; i++) {
output[offset + i] = v & MAX_BYTE;
v = Math.floor(v / 256);
}
struct_getMaxBigInt() {
if (this._maxBigInt === undefined) {
this._maxBigInt = bigInt_1.bigIntPow(BigInt(2), BigInt(this.byteLength * 8)) - BigInt(1);
}
return this._maxBigInt;
}
return offset + this.byteLength;
}
struct_deserializeFromBytes(data, offset) {
this.bytes_validate(data, offset);
let isInfinity = true;
let output = 0;
for (let i = 0; i < this.byteLength; i++) {
output += data[offset + i] * 2 ** (8 * i);
if (data[offset + i] !== 0xff) {
isInfinity = false;
}
struct_serializeToBytes(value, output, offset) {
if (this.byteLength > 6 && value === Infinity) {
for (let i = offset; i < offset + this.byteLength; i++) {
output[i] = 0xff;
}
}
else {
let v = value;
const MAX_BYTE = 0xff;
for (let i = 0; i < this.byteLength; i++) {
output[offset + i] = v & MAX_BYTE;
v = Math.floor(v / 256);
}
}
return offset + this.byteLength;
}
if (this.byteLength > 6 && isInfinity) {
return Infinity;
struct_deserializeFromBytes(data, offset) {
this.bytes_validate(data, offset);
let isInfinity = true;
let output = 0;
for (let i = 0; i < this.byteLength; i++) {
output += data[offset + i] * 2 ** (8 * i);
if (data[offset + i] !== 0xff) {
isInfinity = false;
}
}
if (this.infinityWhenBig && isInfinity) {
return Infinity;
}
return Number(output);
}
return Number(output);
}
struct_convertFromJson(data) {
let n;
const bigN = BigInt(data);
if (bigN === this.struct_getMaxBigInt()) {
n = Infinity;
} else if (bigN < Number.MAX_SAFE_INTEGER) {
n = Number(bigN);
} else {
throw new Error("Uint value unsafe");
struct_convertFromJson(data) {
let n;
const bigN = BigInt(data);
if (this.infinityWhenBig && bigN === this.struct_getMaxBigInt()) {
n = Infinity;
}
else if (bigN < Number.MAX_SAFE_INTEGER) {
n = Number(bigN);
}
else {
throw new Error("Uint value unsafe");
}
this.assertValidValue(n);
return n;
}
this.assertValidValue(n);
return n;
}
struct_convertToJson(value) {
if (this.byteLength > 4) {
if (value === Infinity) {
return this.struct_getMaxBigInt().toString();
}
return String(value);
struct_convertToJson(value) {
if (this.byteLength > 4) {
if (value === Infinity) {
return this.struct_getMaxBigInt().toString();
}
return String(value);
}
return value;
}
return value;
}
}
exports.NumberUintType = NumberUintType;
const BIGINT_UINT_TYPE = Symbol.for("ssz/BigIntUintType");
exports.BIGINT_UINT_TYPE = BIGINT_UINT_TYPE;
exports.BIGINT_UINT_TYPE = Symbol.for("ssz/BigIntUintType");
function isBigIntUintType(type) {
return (0, _type.isTypeOf)(type, BIGINT_UINT_TYPE);
return type_1.isTypeOf(type, exports.BIGINT_UINT_TYPE);
}
exports.isBigIntUintType = isBigIntUintType;
class BigIntUintType extends UintType {
constructor(options) {
super(options);
this._typeSymbols.add(BIGINT_UINT_TYPE);
}
struct_assertValidValue(value) {
if (typeof value !== "bigint") {
throw new Error("Uint value is not a bigint");
constructor(options) {
super(options);
this._typeSymbols.add(exports.BIGINT_UINT_TYPE);
}
if (value < 0) {
throw new Error("Uint value must be gte 0");
struct_assertValidValue(value) {
if (typeof value !== "bigint") {
throw new Error("Uint value is not a bigint");
}
if (value < 0) {
throw new Error("Uint value must be gte 0");
}
}
}
struct_defaultValue() {
return BigInt(0);
}
struct_serializeToBytes(value, output, offset) {
// Motivation
// BigInt bit shifting and BigInt allocation is slower compared to number
// For every 4 bytes, we extract value to groupedBytes
// and do bit shifting on the number
let v = value;
let groupedBytes = Number(BigInt.asUintN(32, v));
for (let i = 0; i < this.byteLength; i++) {
output[offset + i] = Number(groupedBytes & 0xff);
if ((i + 1) % 4 !== 0) {
groupedBytes >>= 8;
} else {
v >>= BIGINT_4_BYTES;
groupedBytes = Number(BigInt.asUintN(32, v));
}
struct_defaultValue() {
return BigInt(0);
}
return offset + this.byteLength;
}
struct_deserializeFromBytes(data, offset) {
this.bytes_validate(data, offset); // Motivation:
// Creating BigInts and bitshifting is more expensive than
// number bitshifting.
// Implementation:
// Iterate throuth the bytearray, bitshifting the data into a 'groupOutput' number, byte by byte
// After each 4 bytes, bitshift the groupOutput into the bigint output and clear the groupOutput out
// After iterating through the bytearray,
// There may be additional data in the groupOutput if the bytearray if the bytearray isn't divisible by 4
let output = BigInt(0);
let groupIndex = 0,
groupOutput = 0;
for (let i = 0; i < this.byteLength; i++) {
groupOutput += data[offset + i] << 8 * (i % 4);
if ((i + 1) % 4 === 0) {
// Left shift returns a signed integer and the output may have become negative
// In that case, the output needs to be converted to unsigned integer
if (groupOutput < 0) {
groupOutput >>>= 0;
} // Optimization to set the output the first time, forgoing BigInt addition
if (groupIndex === 0) {
output = BigInt(groupOutput);
} else {
output += BigInt(groupOutput) << BigInt(32 * groupIndex);
struct_serializeToBytes(value, output, offset) {
// Motivation
// BigInt bit shifting and BigInt allocation is slower compared to number
// For every 4 bytes, we extract value to groupedBytes
// and do bit shifting on the number
let v = value;
let groupedBytes = Number(BigInt.asUintN(32, v));
for (let i = 0; i < this.byteLength; i++) {
output[offset + i] = Number(groupedBytes & 0xff);
if ((i + 1) % 4 !== 0) {
groupedBytes >>= 8;
}
else {
v >>= BIGINT_4_BYTES;
groupedBytes = Number(BigInt.asUintN(32, v));
}
}
groupIndex++;
groupOutput = 0;
}
} // if this.byteLength isn't a multiple of 4, there will be additional data
if (groupOutput) {
output += BigInt(groupOutput >>> 0) << BigInt(32 * groupIndex);
return offset + this.byteLength;
}
return output;
}
struct_convertFromJson(data) {
const value = BigInt(data);
this.assertValidValue(value);
return value;
}
struct_convertToJson(value) {
if (this.byteLength > 4) {
return value.toString();
} else {
return Number(value);
struct_deserializeFromBytes(data, offset) {
this.bytes_validate(data, offset);
// Motivation:
// Creating BigInts and bitshifting is more expensive than
// number bitshifting.
// Implementation:
// Iterate throuth the bytearray, bitshifting the data into a 'groupOutput' number, byte by byte
// After each 4 bytes, bitshift the groupOutput into the bigint output and clear the groupOutput out
// After iterating through the bytearray,
// There may be additional data in the groupOutput if the bytearray if the bytearray isn't divisible by 4
let output = BigInt(0);
let groupIndex = 0, groupOutput = 0;
for (let i = 0; i < this.byteLength; i++) {
groupOutput += data[offset + i] << (8 * (i % 4));
if ((i + 1) % 4 === 0) {
// Left shift returns a signed integer and the output may have become negative
// In that case, the output needs to be converted to unsigned integer
if (groupOutput < 0) {
groupOutput >>>= 0;
}
// Optimization to set the output the first time, forgoing BigInt addition
if (groupIndex === 0) {
output = BigInt(groupOutput);
}
else {
output += BigInt(groupOutput) << BigInt(32 * groupIndex);
}
groupIndex++;
groupOutput = 0;
}
}
// if this.byteLength isn't a multiple of 4, there will be additional data
if (groupOutput) {
output += BigInt(groupOutput >>> 0) << BigInt(32 * groupIndex);
}
return output;
}
}
struct_convertFromJson(data) {
const value = BigInt(data);
this.assertValidValue(value);
return value;
}
struct_convertToJson(value) {
if (this.byteLength > 4) {
return value.toString();
}
else {
return Number(value);
}
}
}
exports.BigIntUintType = BigIntUintType;
//# sourceMappingURL=uint.js.map

@@ -6,1 +6,2 @@ import { NumberUintType } from "./uint";

export declare const booleanType: BooleanType;
//# sourceMappingURL=wellKnown.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "__esModule", { value: true });
exports.booleanType = exports.number32Type = exports.byteType = void 0;
var _uint = require("./uint");
var _boolean = require("./boolean");
const byteType = new _uint.NumberUintType({
byteLength: 1
});
exports.byteType = byteType;
const number32Type = new _uint.NumberUintType({
byteLength: 4
});
exports.number32Type = number32Type;
const booleanType = new _boolean.BooleanType();
exports.booleanType = booleanType;
const uint_1 = require("./uint");
const boolean_1 = require("./boolean");
exports.byteType = new uint_1.NumberUintType({ byteLength: 1 });
exports.number32Type = new uint_1.NumberUintType({ byteLength: 4 });
exports.booleanType = new boolean_1.BooleanType();
//# sourceMappingURL=wellKnown.js.map

@@ -12,4 +12,4 @@ import { CompositeValue, Json } from "../../interface";

export declare abstract class CompositeType<T extends CompositeValue> extends Type<T> {
_chunkDepth: number;
_defaultNode: Node;
_chunkDepth?: number;
_defaultNode?: Node;
constructor();

@@ -156,1 +156,2 @@ abstract struct_assertValidValue(value: unknown): asserts value is T;

}
//# sourceMappingURL=abstract.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isCompositeType = isCompositeType;
exports.CompositeType = exports.COMPOSITE_TYPE = void 0;
var _backings = require("../../backings");
var _type = require("../type");
var _persistentMerkleTree = require("@chainsafe/persistent-merkle-tree");
var _compat = require("../../util/compat");
var _byteArray = require("../../util/byteArray");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const COMPOSITE_TYPE = Symbol.for("ssz/CompositeType");
exports.COMPOSITE_TYPE = COMPOSITE_TYPE;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeType = exports.isCompositeType = exports.COMPOSITE_TYPE = void 0;
const backings_1 = require("../../backings");
const type_1 = require("../type");
const persistent_merkle_tree_1 = require("@chainsafe/persistent-merkle-tree");
const compat_1 = require("../../util/compat");
const byteArray_1 = require("../../util/byteArray");
exports.COMPOSITE_TYPE = Symbol.for("ssz/CompositeType");
function isCompositeType(type) {
return (0, _type.isTypeOf)(type, COMPOSITE_TYPE);
return type_1.isTypeOf(type, exports.COMPOSITE_TYPE);
}
exports.isCompositeType = isCompositeType;
/**

@@ -31,389 +18,300 @@ * A CompositeType is a type containing other types, and is flexible in its representation.

*/
class CompositeType extends _type.Type {
constructor() {
super();
_defineProperty(this, "_chunkDepth", void 0);
_defineProperty(this, "_defaultNode", void 0);
this._typeSymbols.add(COMPOSITE_TYPE);
}
tree_equals(tree1, tree2) {
return (0, _byteArray.byteArrayEquals)(tree1.root, tree2.root);
}
bytes_equals(bytes1, bytes2) {
return (0, _byteArray.byteArrayEquals)(bytes1, bytes2);
}
tree_defaultValue() {
return new _persistentMerkleTree.Tree(this.tree_defaultNode());
}
tree_clone(value) {
return value.clone();
}
bytes_clone(value, start = 0, end = value.length) {
const bytes = new Uint8Array(end - start);
value.subarray(start, end).set(bytes);
return bytes;
}
struct_serialize(struct, data) {
const output = new Uint8Array(this.struct_getSerializedLength(struct));
return this.struct_serializeToBytes(struct, output, 0);
}
tree_serialize(tree, data) {
const output = new Uint8Array(this.tree_getSerializedLength(tree));
return this.tree_serializeToBytes(tree, output, 0);
}
bytes_validate(data, start, end) {
if (!data) {
throw new Error("Data is null or undefined");
class CompositeType extends type_1.Type {
_chunkDepth;
_defaultNode;
constructor() {
super();
this._typeSymbols.add(exports.COMPOSITE_TYPE);
}
if (data.length === 0) {
throw new Error("Data is empty");
tree_equals(tree1, tree2) {
return byteArray_1.byteArrayEquals(tree1.root, tree2.root);
}
if (start < 0) {
throw new Error("Start param is negative: ".concat(start));
bytes_equals(bytes1, bytes2) {
return byteArray_1.byteArrayEquals(bytes1, bytes2);
}
if (start > data.length) {
throw new Error("Start param: ".concat(start, " is greater than length: ").concat(data.length));
tree_defaultValue() {
return new persistent_merkle_tree_1.Tree(this.tree_defaultNode());
}
if (end < 0) {
throw new Error("End param is negative: ".concat(end));
tree_clone(value) {
return value.clone();
}
if (end > data.length) {
throw new Error("End param: ".concat(end, " is greater than length: ").concat(data.length));
bytes_clone(value, start = 0, end = value.length) {
const bytes = new Uint8Array(end - start);
value.subarray(start, end).set(bytes);
return bytes;
}
const length = end - start;
if (!this.hasVariableSerializedLength() && length !== this.struct_getSerializedLength(null)) {
throw new Error("Incorrect data length ".concat(length, ", expect ").concat(this.struct_getSerializedLength(null)));
struct_serialize(struct, data) {
const output = new Uint8Array(this.struct_getSerializedLength(struct));
return this.struct_serializeToBytes(struct, output, 0);
}
if (end - start < this.getMinSerializedLength()) {
throw new Error("Data length ".concat(length, " is too small, expect at least ").concat(this.getMinSerializedLength()));
tree_serialize(tree, data) {
const output = new Uint8Array(this.tree_getSerializedLength(tree));
return this.tree_serializeToBytes(tree, output, 0);
}
}
struct_deserialize(data) {
return this.struct_deserializeFromBytes(data, 0, data.length);
}
tree_deserialize(data) {
return this.tree_deserializeFromBytes(data, 0, data.length);
}
struct_getChunkCount(struct) {
return this.getMaxChunkCount();
}
tree_getChunkCount(target) {
return this.getMaxChunkCount();
}
*struct_yieldChunkRoots(struct) {
const chunkCount = this.struct_getChunkCount(struct);
for (let i = 0; i < chunkCount; i++) {
yield this.struct_getRootAtChunkIndex(struct, i);
bytes_validate(data, start, end) {
if (!data) {
throw new Error("Data is null or undefined");
}
if (data.length === 0) {
throw new Error("Data is empty");
}
if (start < 0) {
throw new Error(`Start param is negative: ${start}`);
}
if (start > data.length) {
throw new Error(`Start param: ${start} is greater than length: ${data.length}`);
}
if (end < 0) {
throw new Error(`End param is negative: ${end}`);
}
if (end > data.length) {
throw new Error(`End param: ${end} is greater than length: ${data.length}`);
}
const length = end - start;
const fixedLen = this.getFixedSerializedLength();
if (fixedLen !== null && length !== fixedLen) {
throw new Error(`Incorrect data length ${length}, expect ${fixedLen}`);
}
if (end - start < this.getMinSerializedLength()) {
throw new Error(`Data length ${length} is too small, expect at least ${this.getMinSerializedLength()}`);
}
}
}
getChunkDepth() {
if (!this._chunkDepth) {
this._chunkDepth = (0, _persistentMerkleTree.countToDepth)(BigInt(this.getMaxChunkCount()));
struct_deserialize(data) {
return this.struct_deserializeFromBytes(data, 0, data.length);
}
return this._chunkDepth;
}
getGindexAtChunkIndex(index) {
return (0, _persistentMerkleTree.toGindex)(this.getChunkDepth(), BigInt(index));
}
tree_getSubtreeAtChunkIndex(target, index) {
return target.getSubtree(this.getGindexAtChunkIndex(index));
}
tree_setSubtreeAtChunkIndex(target, index, value, expand = false) {
target.setSubtree(this.getGindexAtChunkIndex(index), value, expand);
}
tree_getRootAtChunkIndex(target, index) {
return target.getRoot(this.getGindexAtChunkIndex(index));
}
tree_setRootAtChunkIndex(target, index, value, expand = false) {
target.setRoot(this.getGindexAtChunkIndex(index), value, expand);
}
/**
* Navigate to a subtype & gindex using a path
*/
getPathInfo(path) {
const gindices = [];
let type = this;
for (const prop of path) {
if (!isCompositeType(type)) {
throw new Error("Invalid path: cannot navigate beyond a basic type");
}
gindices.push(type.getPropertyGindex(prop));
type = type.getPropertyType(prop);
tree_deserialize(data) {
return this.tree_deserializeFromBytes(data, 0, data.length);
}
return {
type,
gindex: (0, _persistentMerkleTree.concatGindices)(gindices)
};
}
getPathGindex(path) {
return this.getPathInfo(path).gindex;
}
/**
* Get leaf gindices
*
* Note: This is a recursively called method.
* Subtypes recursively call this method until basic types / leaf data is hit.
*
* @param target Used for variable-length types.
* @param root Used to anchor the returned gindices to a non-root gindex.
* This is used to augment leaf gindices in recursively-called subtypes relative to the type.
* @returns The gindices corresponding to leaf data.
*/
tree_createProof(target, paths) {
const gindices = paths.map(path => {
const {
type,
gindex
} = this.getPathInfo(path);
if (!isCompositeType(type)) {
return gindex;
} else {
// if the path subtype is composite, include the gindices of all the leaves
return type.tree_getLeafGindices(type.hasVariableSerializedLength() ? target.getSubtree(gindex) : undefined, gindex);
}
}).flat(1);
return target.getProof({
type: _persistentMerkleTree.ProofType.treeOffset,
gindices
});
}
tree_createFromProof(root, proof) {
const tree = _persistentMerkleTree.Tree.createFromProof(proof);
if (!(0, _byteArray.byteArrayEquals)(tree.root, root)) {
throw new Error("Proof does not match trusted root");
struct_getChunkCount(struct) {
return this.getMaxChunkCount();
}
return tree;
}
tree_createFromProofUnsafe(proof) {
return _persistentMerkleTree.Tree.createFromProof(proof);
}
struct_hashTreeRoot(struct) {
return (0, _compat.merkleize)(this.struct_yieldChunkRoots(struct), this.getMaxChunkCount());
}
tree_hashTreeRoot(tree) {
return tree.root;
} // convenience
/**
* Valid value assertion
*/
assertValidValue(value) {
this.struct_assertValidValue(value);
}
/**
* Equality
*/
equals(value1, value2) {
if ((0, _backings.isBackedValue)(value1) && (0, _backings.isBackedValue)(value2)) {
return value1.equals(value2);
} else {
return this.struct_equals(value1, value2);
tree_getChunkCount(target) {
return this.getMaxChunkCount();
}
}
/**
* Default constructor
*/
defaultValue() {
return this.struct_defaultValue();
}
/**
* Clone / copy
*/
clone(value) {
if ((0, _backings.isBackedValue)(value)) {
return value.clone();
} else {
return this.struct_clone(value);
*struct_yieldChunkRoots(struct) {
const chunkCount = this.struct_getChunkCount(struct);
for (let i = 0; i < chunkCount; i++) {
yield this.struct_getRootAtChunkIndex(struct, i);
}
}
} // Serialization / Deserialization
/**
* Serialized byte length
*/
size(value) {
if ((0, _backings.isBackedValue)(value)) {
return value.size();
} else {
return this.struct_getSerializedLength(value);
getChunkDepth() {
if (!this._chunkDepth) {
this._chunkDepth = persistent_merkle_tree_1.countToDepth(BigInt(this.getMaxChunkCount()));
}
return this._chunkDepth;
}
}
/**
* Maximal serialized byte length
*/
maxSize() {
return this.getMaxSerializedLength();
}
/**
* Minimal serialized byte length
*/
minSize() {
return this.getMinSerializedLength();
}
/**
* Low-level deserialization
*/
fromBytes(data, start, end) {
return this.struct_deserializeFromBytes(data, start, end);
}
/**
* Deserialization
*/
deserialize(data) {
return this.fromBytes(data, 0, data.length);
}
/**
* Low-level serialization
*
* Serializes to a pre-allocated Uint8Array
*/
toBytes(value, output, offset) {
if ((0, _backings.isBackedValue)(value)) {
return value.toBytes(output, offset);
} else {
return this.struct_serializeToBytes(value, output, offset);
getGindexAtChunkIndex(index) {
return persistent_merkle_tree_1.toGindex(this.getChunkDepth(), BigInt(index));
}
}
/**
* Serialization
*/
serialize(value) {
if ((0, _backings.isBackedValue)(value)) {
return value.serialize();
} else {
const output = new Uint8Array(this.size(value));
this.toBytes(value, output, 0);
return output;
tree_getSubtreeAtChunkIndex(target, index) {
return target.getSubtree(this.getGindexAtChunkIndex(index));
}
} // Merkleization
/**
* Merkleization
*/
hashTreeRoot(value) {
if ((0, _backings.isBackedValue)(value)) {
return value.hashTreeRoot();
} else {
return this.struct_hashTreeRoot(value);
tree_setSubtreeAtChunkIndex(target, index, value, expand = false) {
target.setSubtree(this.getGindexAtChunkIndex(index), value, expand);
}
}
/**
* Convert from a JSON-serializable object
*/
fromJson(data, options) {
return this.struct_convertFromJson(data, options);
}
/**
* Convert to a JSON-serializable object
*/
toJson(value, options) {
return this.struct_convertToJson(value, options);
}
createTreeBacked(tree) {
return (0, _backings.createTreeBacked)(this, tree);
}
createTreeBackedFromStruct(value) {
return this.createTreeBacked(this.struct_convertToTree(value));
}
createTreeBackedFromBytes(data) {
return this.createTreeBacked(this.tree_deserialize(data));
}
createTreeBackedFromJson(data, options) {
return this.createTreeBackedFromStruct(this.struct_convertFromJson(data, options));
}
createTreeBackedFromProof(root, proof) {
return this.createTreeBacked(this.tree_createFromProof(root, proof));
}
createTreeBackedFromProofUnsafe(proof) {
return this.createTreeBacked(this.tree_createFromProofUnsafe(proof));
}
defaultTreeBacked() {
return (0, _backings.createTreeBacked)(this, this.tree_defaultValue());
}
tree_getRootAtChunkIndex(target, index) {
return target.getRoot(this.getGindexAtChunkIndex(index));
}
tree_setRootAtChunkIndex(target, index, value, expand = false) {
target.setRoot(this.getGindexAtChunkIndex(index), value, expand);
}
/**
* Navigate to a subtype & gindex using a path
*/
getPathInfo(path) {
const gindices = [];
let type = this;
for (const prop of path) {
if (!isCompositeType(type)) {
throw new Error("Invalid path: cannot navigate beyond a basic type");
}
gindices.push(type.getPropertyGindex(prop));
type = type.getPropertyType(prop);
}
return {
type,
gindex: persistent_merkle_tree_1.concatGindices(gindices),
};
}
getPathGindex(path) {
return this.getPathInfo(path).gindex;
}
tree_createProof(target, paths) {
const gindices = paths
.map((path) => {
const { type, gindex } = this.getPathInfo(path);
if (!isCompositeType(type)) {
return gindex;
}
else {
// if the path subtype is composite, include the gindices of all the leaves
return type.tree_getLeafGindices(type.hasVariableSerializedLength() ? target.getSubtree(gindex) : undefined, gindex);
}
})
.flat(1);
return target.getProof({
type: persistent_merkle_tree_1.ProofType.treeOffset,
gindices,
});
}
tree_createFromProof(root, proof) {
const tree = persistent_merkle_tree_1.Tree.createFromProof(proof);
if (!byteArray_1.byteArrayEquals(tree.root, root)) {
throw new Error("Proof does not match trusted root");
}
return tree;
}
tree_createFromProofUnsafe(proof) {
return persistent_merkle_tree_1.Tree.createFromProof(proof);
}
struct_hashTreeRoot(struct) {
return compat_1.merkleize(this.struct_yieldChunkRoots(struct), this.getMaxChunkCount());
}
tree_hashTreeRoot(tree) {
return tree.root;
}
// convenience
/**
* Valid value assertion
*/
assertValidValue(value) {
this.struct_assertValidValue(value);
}
/**
* Equality
*/
equals(value1, value2) {
if (backings_1.isBackedValue(value1) && backings_1.isBackedValue(value2)) {
return value1.equals(value2);
}
else {
return this.struct_equals(value1, value2);
}
}
/**
* Default constructor
*/
defaultValue() {
return this.struct_defaultValue();
}
/**
* Clone / copy
*/
clone(value) {
if (backings_1.isBackedValue(value)) {
return value.clone();
}
else {
return this.struct_clone(value);
}
}
// Serialization / Deserialization
/**
* Serialized byte length
*/
size(value) {
if (backings_1.isBackedValue(value)) {
return value.size();
}
else {
return this.struct_getSerializedLength(value);
}
}
/**
* Maximal serialized byte length
*/
maxSize() {
return this.getMaxSerializedLength();
}
/**
* Minimal serialized byte length
*/
minSize() {
return this.getMinSerializedLength();
}
/**
* Low-level deserialization
*/
fromBytes(data, start, end) {
return this.struct_deserializeFromBytes(data, start, end);
}
/**
* Deserialization
*/
deserialize(data) {
return this.fromBytes(data, 0, data.length);
}
/**
* Low-level serialization
*
* Serializes to a pre-allocated Uint8Array
*/
toBytes(value, output, offset) {
if (backings_1.isBackedValue(value)) {
return value.toBytes(output, offset);
}
else {
return this.struct_serializeToBytes(value, output, offset);
}
}
/**
* Serialization
*/
serialize(value) {
if (backings_1.isBackedValue(value)) {
return value.serialize();
}
else {
const output = new Uint8Array(this.size(value));
this.toBytes(value, output, 0);
return output;
}
}
// Merkleization
/**
* Merkleization
*/
hashTreeRoot(value) {
if (backings_1.isBackedValue(value)) {
return value.hashTreeRoot();
}
else {
return this.struct_hashTreeRoot(value);
}
}
/**
* Convert from a JSON-serializable object
*/
fromJson(data, options) {
return this.struct_convertFromJson(data, options);
}
/**
* Convert to a JSON-serializable object
*/
toJson(value, options) {
return this.struct_convertToJson(value, options);
}
createTreeBacked(tree) {
return backings_1.createTreeBacked(this, tree);
}
createTreeBackedFromStruct(value) {
return this.createTreeBacked(this.struct_convertToTree(value));
}
createTreeBackedFromBytes(data) {
return this.createTreeBacked(this.tree_deserialize(data));
}
createTreeBackedFromJson(data, options) {
return this.createTreeBackedFromStruct(this.struct_convertFromJson(data, options));
}
createTreeBackedFromProof(root, proof) {
return this.createTreeBacked(this.tree_createFromProof(root, proof));
}
createTreeBackedFromProofUnsafe(proof) {
return this.createTreeBacked(this.tree_createFromProofUnsafe(proof));
}
defaultTreeBacked() {
return backings_1.createTreeBacked(this, this.tree_defaultValue());
}
}
exports.CompositeType = CompositeType;
//# sourceMappingURL=abstract.js.map

@@ -82,1 +82,2 @@ import { ArrayLike, CompositeValue, Json } from "../../interface";

}
//# sourceMappingURL=array.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
/* eslint-disable @typescript-eslint/member-ordering */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeArrayType = exports.BasicArrayType = void 0;
var _abstract = require("./abstract");
var _errorPath = require("../../util/errorPath");
var _persistentMerkleTree = require("@chainsafe/persistent-merkle-tree");
var _treeValue = require("../../backings/tree/treeValue");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
class BasicArrayType extends _abstract.CompositeType {
constructor(options) {
super();
_defineProperty(this, "elementType", void 0);
this.elementType = options.elementType;
}
struct_getSerializedLength(value) {
return this.elementType.struct_getSerializedLength() * this.struct_getLength(value);
}
getMaxSerializedLength() {
return this.getMaxLength() * this.elementType.getMaxSerializedLength();
}
getMinSerializedLength() {
return this.getMinLength() * this.elementType.getMinSerializedLength();
}
struct_assertValidValue(value) {
for (let i = 0; i < this.struct_getLength(value); i++) {
try {
this.elementType.struct_assertValidValue(value[i]);
} catch (e) {
throw new Error("Invalid element ".concat(i, ": ").concat(e.message));
}
const abstract_1 = require("./abstract");
const errorPath_1 = require("../../util/errorPath");
const persistent_merkle_tree_1 = require("@chainsafe/persistent-merkle-tree");
const treeValue_1 = require("../../backings/tree/treeValue");
class BasicArrayType extends abstract_1.CompositeType {
elementType;
constructor(options) {
super();
this.elementType = options.elementType;
}
}
struct_equals(value1, value2) {
if (this.struct_getLength(value1) !== this.struct_getLength(value2)) {
return false;
struct_getSerializedLength(value) {
return this.elementType.struct_getSerializedLength() * this.struct_getLength(value);
}
for (let i = 0; i < this.struct_getLength(value1); i++) {
if (!this.elementType.struct_equals(value1[i], value2[i])) {
return false;
}
getMaxSerializedLength() {
return this.getMaxLength() * this.elementType.getMaxSerializedLength();
}
return true;
}
struct_clone(value) {
const newValue = this.struct_defaultValue();
for (let i = 0; i < this.struct_getLength(value); i++) {
newValue[i] = this.elementType.struct_clone(value[i]);
getMinSerializedLength() {
return this.getMinLength() * this.elementType.getMinSerializedLength();
}
return newValue;
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const elementSize = this.elementType.struct_getSerializedLength();
return Array.from({
length: (end - start) / elementSize
}, (_, i) => this.elementType.struct_deserializeFromBytes(data, start + i * elementSize));
}
struct_serializeToBytes(value, output, offset) {
const length = this.struct_getLength(value);
let index = offset;
for (let i = 0; i < length; i++) {
index = this.elementType.struct_serializeToBytes(value[i], output, index);
struct_assertValidValue(value) {
for (let i = 0; i < this.struct_getLength(value); i++) {
try {
this.elementType.struct_assertValidValue(value[i]);
}
catch (e) {
throw new Error(`Invalid element ${i}: ${e.message}`);
}
}
}
return index;
}
struct_getRootAtChunkIndex(value, index) {
const output = new Uint8Array(32);
const itemSize = this.elementType.struct_getSerializedLength();
const itemsInChunk = Math.floor(32 / itemSize);
const firstIndex = index * itemsInChunk; // not inclusive
const lastIndex = Math.min(this.struct_getLength(value), firstIndex + itemsInChunk); // i = array index, grows by 1
// j = data offset, grows by itemSize
for (let i = firstIndex, j = 0; i < lastIndex; i++, j += itemSize) {
this.elementType.struct_serializeToBytes(value[i], output, j);
struct_equals(value1, value2) {
if (this.struct_getLength(value1) !== this.struct_getLength(value2)) {
return false;
}
for (let i = 0; i < this.struct_getLength(value1); i++) {
if (!this.elementType.struct_equals(value1[i], value2[i])) {
return false;
}
}
return true;
}
return output;
}
struct_getPropertyNames(value) {
const length = this.struct_getLength(value);
return Array.from({
length
}, (_, i) => i).concat(["length"]);
}
struct_convertFromJson(data) {
return Array.from({
length: data.length
}, (_, i) => this.elementType.fromJson(data[i]));
}
struct_convertToJson(value) {
return Array.from({
length: this.struct_getLength(value)
}, (_, i) => this.elementType.struct_convertToJson(value[i]));
}
struct_convertToTree(value) {
if ((0, _treeValue.isTreeBacked)(value)) return value.tree.clone();
const contents = [];
for (const chunk of this.struct_yieldChunkRoots(value)) {
contents.push(new _persistentMerkleTree.LeafNode(chunk));
struct_clone(value) {
const newValue = this.struct_defaultValue();
for (let i = 0; i < this.struct_getLength(value); i++) {
newValue[i] = this.elementType.struct_clone(value[i]);
}
return newValue;
}
return new _persistentMerkleTree.Tree((0, _persistentMerkleTree.subtreeFillToContents)(contents, this.getChunkDepth()));
}
tree_convertToStruct(target) {
const value = this.struct_defaultValue();
const length = this.tree_getLength(target);
for (let i = 0; i < length; i++) {
value[i] = this.tree_getValueAtIndex(target, i);
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const elementSize = this.elementType.struct_getSerializedLength();
return Array.from({ length: (end - start) / elementSize }, (_, i) => this.elementType.struct_deserializeFromBytes(data, start + i * elementSize));
}
return value;
}
tree_getSerializedLength(target) {
return this.elementType.struct_getSerializedLength() * this.tree_getLength(target);
}
tree_deserializeFromBytes(data, start, end) {
const target = this.tree_defaultValue();
const byteLength = end - start;
const chunkCount = Math.ceil(byteLength / 32);
for (let i = 0; i < chunkCount; i++) {
// view of the chunk, shared buffer from `data`
const dataChunk = new Uint8Array(data.buffer, data.byteOffset + start + i * 32, Math.min(32, byteLength - i * 32)); // copy chunk into new memory
const chunk = new Uint8Array(32);
chunk.set(dataChunk);
this.tree_setRootAtChunkIndex(target, i, chunk, true // expand tree as needed
);
struct_serializeToBytes(value, output, offset) {
const length = this.struct_getLength(value);
let index = offset;
for (let i = 0; i < length; i++) {
index = this.elementType.struct_serializeToBytes(value[i], output, index);
}
return index;
}
return target;
}
tree_serializeToBytes(target, output, offset) {
const size = this.tree_getSerializedLength(target);
const fullChunkCount = Math.floor(size / 32);
const remainder = size % 32;
let i = 0;
if (fullChunkCount > 0) {
for (const node of target.iterateNodesAtDepth(this.getChunkDepth(), 0, fullChunkCount)) {
output.set(node.root, offset + i * 32);
i++;
}
struct_getRootAtChunkIndex(value, index) {
const output = new Uint8Array(32);
const itemSize = this.elementType.struct_getSerializedLength();
const itemsInChunk = Math.floor(32 / itemSize);
const firstIndex = index * itemsInChunk;
// not inclusive
const lastIndex = Math.min(this.struct_getLength(value), firstIndex + itemsInChunk);
// i = array index, grows by 1
// j = data offset, grows by itemSize
for (let i = firstIndex, j = 0; i < lastIndex; i++, j += itemSize) {
this.elementType.struct_serializeToBytes(value[i], output, j);
}
return output;
}
if (remainder) {
output.set(this.tree_getRootAtChunkIndex(target, fullChunkCount).slice(0, remainder), offset + i * 32);
struct_getPropertyNames(value) {
const length = this.struct_getLength(value);
return Array.from({ length }, (_, i) => i).concat(["length"]);
}
return offset + size;
}
getPropertyGindex(prop) {
return this.getGindexAtChunkIndex(this.getChunkIndex(prop));
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
getPropertyType(prop) {
return this.elementType;
}
*tree_iterateValues(target) {
const length = this.tree_getLength(target);
if (length === 0) {
return;
struct_convertFromJson(data) {
return Array.from({ length: data.length }, (_, i) => this.elementType.fromJson(data[i]));
}
const elementSize = this.elementType.struct_getSerializedLength();
if (32 % elementSize !== 0) {
throw new Error("cannot handle a non-chunk-alignable elementType");
struct_convertToJson(value) {
return Array.from({ length: this.struct_getLength(value) }, (_, i) => this.elementType.struct_convertToJson(value[i]));
}
let left = length;
for (const node of target.iterateNodesAtDepth(this.getChunkDepth(), 0, this.tree_getChunkCount(target))) {
const chunk = node.root;
for (let offset = 0; offset < 32; offset += elementSize) {
yield this.elementType.struct_deserializeFromBytes(chunk, offset);
left--;
if (left === 0) {
return;
struct_convertToTree(value) {
if (treeValue_1.isTreeBacked(value))
return value.tree.clone();
const contents = [];
for (const chunk of this.struct_yieldChunkRoots(value)) {
contents.push(new persistent_merkle_tree_1.LeafNode(chunk));
}
}
return new persistent_merkle_tree_1.Tree(persistent_merkle_tree_1.subtreeFillToContents(contents, this.getChunkDepth()));
}
}
*tree_readonlyIterateValues(target) {
yield* this.tree_iterateValues(target);
}
getChunkOffset(index) {
const elementSize = this.elementType.struct_getSerializedLength();
return index % Math.ceil(32 / elementSize) * elementSize;
}
getChunkIndex(index) {
return Math.floor(index / Math.ceil(32 / this.elementType.struct_getSerializedLength()));
}
tree_getValueAtIndex(target, index) {
const chunk = this.tree_getRootAtChunkIndex(target, this.getChunkIndex(index));
return this.elementType.struct_deserializeFromBytes(chunk, this.getChunkOffset(index));
}
tree_setValueAtIndex(target, index, value, expand = false) {
const chunkGindex = this.getGindexAtChunkIndex(this.getChunkIndex(index)); // copy data from old chunk, use new memory to set a new chunk
const chunk = new Uint8Array(32);
chunk.set(target.getRoot(chunkGindex));
this.elementType.struct_serializeToBytes(value, chunk, this.getChunkOffset(index));
target.setRoot(chunkGindex, chunk, expand);
return true;
}
tree_getProperty(target, property) {
const length = this.tree_getLength(target);
if (property === "length") {
return length;
tree_convertToStruct(target) {
const value = this.struct_defaultValue();
const length = this.tree_getLength(target);
for (let i = 0; i < length; i++) {
value[i] = this.tree_getValueAtIndex(target, i);
}
return value;
}
const index = Number(property);
if (Number.isNaN(index)) {
return undefined;
tree_getSerializedLength(target) {
return this.elementType.struct_getSerializedLength() * this.tree_getLength(target);
}
if (index >= length) {
return undefined;
tree_deserializeFromBytes(data, start, end) {
const target = this.tree_defaultValue();
const byteLength = end - start;
const chunkCount = Math.ceil(byteLength / 32);
for (let i = 0; i < chunkCount; i++) {
// view of the chunk, shared buffer from `data`
const dataChunk = new Uint8Array(data.buffer, data.byteOffset + start + i * 32, Math.min(32, byteLength - i * 32));
// copy chunk into new memory
const chunk = new Uint8Array(32);
chunk.set(dataChunk);
this.tree_setRootAtChunkIndex(target, i, chunk, true // expand tree as needed
);
}
return target;
}
return this.tree_getValueAtIndex(target, index);
}
tree_setProperty(target, property, value, expand = false) {
return this.tree_setValueAtIndex(target, property, value, expand);
}
tree_deleteProperty(target, property) {
return this.tree_setProperty(target, property, this.elementType.struct_defaultValue());
}
tree_getPropertyNames(target) {
return Array.from({
length: this.tree_getLength(target)
}, (_, i) => String(i)).concat("length");
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
bytes_getVariableOffsets(target) {
return [];
}
tree_getLeafGindices(target, root = BigInt(1)) {
const chunkCount = this.tree_getChunkCount(target);
const startIndex = (0, _persistentMerkleTree.concatGindices)([root, (0, _persistentMerkleTree.toGindex)(this.getChunkDepth(), BigInt(0))]);
const gindices = [];
for (let i = 0, gindex = startIndex; i < chunkCount; i++, gindex++) {
gindices.push(gindex);
tree_serializeToBytes(target, output, offset) {
const size = this.tree_getSerializedLength(target);
const fullChunkCount = Math.floor(size / 32);
const remainder = size % 32;
let i = 0;
if (fullChunkCount > 0) {
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, fullChunkCount);
for (; i < nodes.length; i++) {
output.set(nodes[i].root, offset + i * 32);
}
}
if (remainder) {
output.set(this.tree_getRootAtChunkIndex(target, fullChunkCount).slice(0, remainder), offset + i * 32);
}
return offset + size;
}
return gindices;
}
getPropertyGindex(prop) {
return this.getGindexAtChunkIndex(this.getChunkIndex(prop));
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getPropertyType(prop) {
return this.elementType;
}
*tree_iterateValues(target) {
const length = this.tree_getLength(target);
if (length === 0) {
return;
}
const elementSize = this.elementType.struct_getSerializedLength();
if (32 % elementSize !== 0) {
throw new Error("cannot handle a non-chunk-alignable elementType");
}
let left = length;
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, this.tree_getChunkCount(target));
for (let i = 0; i < nodes.length; i++) {
const chunk = nodes[i].root;
for (let offset = 0; offset < 32; offset += elementSize) {
yield this.elementType.struct_deserializeFromBytes(chunk, offset);
left--;
if (left === 0) {
return;
}
}
}
}
*tree_readonlyIterateValues(target) {
yield* this.tree_iterateValues(target);
}
getChunkOffset(index) {
const elementSize = this.elementType.struct_getSerializedLength();
return (index % Math.ceil(32 / elementSize)) * elementSize;
}
getChunkIndex(index) {
return Math.floor(index / Math.ceil(32 / this.elementType.struct_getSerializedLength()));
}
tree_getValueAtIndex(target, index) {
const chunk = this.tree_getRootAtChunkIndex(target, this.getChunkIndex(index));
return this.elementType.struct_deserializeFromBytes(chunk, this.getChunkOffset(index));
}
tree_setValueAtIndex(target, index, value, expand = false) {
const chunkGindex = this.getGindexAtChunkIndex(this.getChunkIndex(index));
// copy data from old chunk, use new memory to set a new chunk
const chunk = new Uint8Array(32);
chunk.set(target.getRoot(chunkGindex));
this.elementType.struct_serializeToBytes(value, chunk, this.getChunkOffset(index));
target.setRoot(chunkGindex, chunk, expand);
return true;
}
tree_getProperty(target, property) {
const length = this.tree_getLength(target);
if (property === "length") {
return length;
}
const index = Number(property);
if (Number.isNaN(index) || index >= length) {
return undefined;
}
return this.tree_getValueAtIndex(target, index);
}
tree_setProperty(target, property, value, expand = false) {
return this.tree_setValueAtIndex(target, property, value, expand);
}
tree_deleteProperty(target, property) {
return this.tree_setProperty(target, property, this.elementType.struct_defaultValue());
}
tree_getPropertyNames(target) {
return Array.from({ length: this.tree_getLength(target) }, (_, i) => String(i)).concat("length");
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
bytes_getVariableOffsets(target) {
return [];
}
tree_getLeafGindices(target, root = BigInt(1)) {
const chunkCount = this.tree_getChunkCount(target);
const startIndex = persistent_merkle_tree_1.concatGindices([root, persistent_merkle_tree_1.toGindex(this.getChunkDepth(), BigInt(0))]);
const gindices = [];
for (let i = 0, gindex = startIndex; i < chunkCount; i++, gindex++) {
gindices.push(gindex);
}
return gindices;
}
}
exports.BasicArrayType = BasicArrayType;
class CompositeArrayType extends _abstract.CompositeType {
constructor(options) {
super();
_defineProperty(this, "elementType", void 0);
this.elementType = options.elementType;
}
struct_getSerializedLength(value) {
if (this.elementType.hasVariableSerializedLength()) {
let s = 0;
for (let i = 0; i < this.struct_getLength(value); i++) {
s += this.elementType.struct_getSerializedLength(value[i]) + 4;
}
return s;
} else {
return this.elementType.struct_getSerializedLength(null) * this.struct_getLength(value);
class CompositeArrayType extends abstract_1.CompositeType {
elementType;
constructor(options) {
super();
this.elementType = options.elementType;
}
}
getMaxSerializedLength() {
if (this.elementType.hasVariableSerializedLength()) {
return this.getMaxLength() * 4 + this.getMaxLength() * this.elementType.getMaxSerializedLength();
} else {
return this.getMaxLength() * this.elementType.getMaxSerializedLength();
struct_getSerializedLength(value) {
const fixedLen = this.elementType.getFixedSerializedLength();
if (fixedLen === null) {
let s = 0;
for (let i = 0; i < this.struct_getLength(value); i++) {
s += this.elementType.struct_getSerializedLength(value[i]) + 4;
}
return s;
}
else {
return fixedLen * this.struct_getLength(value);
}
}
}
getMinSerializedLength() {
if (this.elementType.hasVariableSerializedLength()) {
return this.getMinLength() * 4 + this.getMinLength() * this.elementType.getMinSerializedLength();
} else {
return this.getMinLength() * this.elementType.getMinSerializedLength();
getMaxSerializedLength() {
const elementFixedLen = this.elementType.getFixedSerializedLength();
if (elementFixedLen === null) {
return this.getMaxLength() * 4 + this.getMaxLength() * this.elementType.getMaxSerializedLength();
}
else {
return this.getMaxLength() * elementFixedLen;
}
}
}
struct_assertValidValue(value) {
for (let i = 0; i < this.struct_getLength(value); i++) {
try {
this.elementType.struct_assertValidValue(value[i]);
} catch (e) {
throw new Error("Invalid element ".concat(i, ": ").concat(e.message));
}
getMinSerializedLength() {
const elementFixedLen = this.elementType.getFixedSerializedLength();
if (elementFixedLen === null) {
return this.getMinLength() * 4 + this.getMinLength() * this.elementType.getMinSerializedLength();
}
else {
return this.getMinLength() * elementFixedLen;
}
}
}
struct_equals(value1, value2) {
if (this.struct_getLength(value1) !== this.struct_getLength(value2)) {
return false;
struct_assertValidValue(value) {
for (let i = 0; i < this.struct_getLength(value); i++) {
try {
this.elementType.struct_assertValidValue(value[i]);
}
catch (e) {
throw new Error(`Invalid element ${i}: ${e.message}`);
}
}
}
for (let i = 0; i < this.struct_getLength(value1); i++) {
if (!this.elementType.struct_equals(value1[i], value2[i])) {
return false;
}
struct_equals(value1, value2) {
if (this.struct_getLength(value1) !== this.struct_getLength(value2)) {
return false;
}
for (let i = 0; i < this.struct_getLength(value1); i++) {
if (!this.elementType.struct_equals(value1[i], value2[i])) {
return false;
}
}
return true;
}
return true;
}
struct_clone(value) {
const newValue = this.struct_defaultValue();
for (let i = 0; i < this.struct_getLength(value); i++) {
newValue[i] = this.elementType.struct_clone(value[i]);
struct_clone(value) {
const newValue = this.struct_defaultValue();
for (let i = 0; i < this.struct_getLength(value); i++) {
newValue[i] = this.elementType.struct_clone(value[i]);
}
return newValue;
}
return newValue;
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
if (start === end) {
return [];
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
if (start === end) {
return [];
}
const fixedLen = this.elementType.getFixedSerializedLength();
if (fixedLen === null) {
const value = [];
// all elements variable-sized
// indices contain offsets
let currentIndex = start;
let nextIndex;
// data exists between offsets
const fixedSection = new DataView(data.buffer, data.byteOffset);
const firstOffset = start + fixedSection.getUint32(start, true);
let currentOffset = firstOffset;
let nextOffset;
while (currentIndex < firstOffset) {
if (currentOffset > end) {
throw new Error("Offset out of bounds");
}
nextIndex = currentIndex + 4;
nextOffset = nextIndex === firstOffset ? end : start + fixedSection.getUint32(nextIndex, true);
if (currentOffset > nextOffset) {
throw new Error("Offsets must be increasing");
}
try {
value.push(this.elementType.struct_deserializeFromBytes(data, currentOffset, nextOffset));
}
catch (e) {
throw new errorPath_1.SszErrorPath(e, value.length);
}
currentIndex = nextIndex;
currentOffset = nextOffset;
}
if (firstOffset !== currentIndex) {
throw new Error("First offset skips variable data");
}
return value;
}
else {
const elementSize = fixedLen;
return Array.from({ length: (end - start) / elementSize }, (_, i) => this.elementType.struct_deserializeFromBytes(data, start + i * elementSize, start + (i + 1) * elementSize));
}
}
if (this.elementType.hasVariableSerializedLength()) {
const value = []; // all elements variable-sized
// indices contain offsets
let currentIndex = start;
let nextIndex; // data exists between offsets
const fixedSection = new DataView(data.buffer, data.byteOffset);
const firstOffset = start + fixedSection.getUint32(start, true);
let currentOffset = firstOffset;
let nextOffset;
while (currentIndex < firstOffset) {
if (currentOffset > end) {
throw new Error("Offset out of bounds");
struct_serializeToBytes(value, output, offset) {
const length = this.struct_getLength(value);
if (this.elementType.hasVariableSerializedLength()) {
let variableIndex = offset + length * 4;
const fixedSection = new DataView(output.buffer, output.byteOffset + offset);
for (let i = 0; i < length; i++) {
// write offset
fixedSection.setUint32(i * 4, variableIndex - offset, true);
// write serialized element to variable section
variableIndex = this.elementType.struct_serializeToBytes(value[i], output, variableIndex);
}
return variableIndex;
}
nextIndex = currentIndex + 4;
nextOffset = nextIndex === firstOffset ? end : start + fixedSection.getUint32(nextIndex, true);
if (currentOffset > nextOffset) {
throw new Error("Offsets must be increasing");
else {
let index = offset;
for (let i = 0; i < length; i++) {
index = this.elementType.struct_serializeToBytes(value[i], output, index);
}
return index;
}
try {
value.push(this.elementType.struct_deserializeFromBytes(data, currentOffset, nextOffset));
} catch (e) {
throw new _errorPath.SszErrorPath(e, value.length);
}
struct_getRootAtChunkIndex(value, index) {
return this.elementType.hashTreeRoot(value[index]);
}
struct_getPropertyNames(value) {
const length = this.struct_getLength(value);
return Array.from({ length }, (_, i) => i).concat(["length"]);
}
struct_convertFromJson(data, options) {
return Array.from({ length: data.length }, (_, i) => this.elementType.struct_convertFromJson(data[i], options));
}
struct_convertToJson(value, options) {
return Array.from({ length: this.struct_getLength(value) }, (_, i) => this.elementType.struct_convertToJson(value[i], options));
}
struct_convertToTree(value) {
if (treeValue_1.isTreeBacked(value))
return value.tree.clone();
const contents = [];
for (const element of value) {
contents.push(this.elementType.struct_convertToTree(element).rootNode);
}
currentIndex = nextIndex;
currentOffset = nextOffset;
}
if (firstOffset !== currentIndex) {
throw new Error("First offset skips variable data");
}
return value;
} else {
const elementSize = this.elementType.struct_getSerializedLength(null);
return Array.from({
length: (end - start) / elementSize
}, (_, i) => this.elementType.struct_deserializeFromBytes(data, start + i * elementSize, start + (i + 1) * elementSize));
return new persistent_merkle_tree_1.Tree(persistent_merkle_tree_1.subtreeFillToContents(contents, this.getChunkDepth()));
}
}
struct_serializeToBytes(value, output, offset) {
const length = this.struct_getLength(value);
if (this.elementType.hasVariableSerializedLength()) {
let variableIndex = offset + length * 4;
const fixedSection = new DataView(output.buffer, output.byteOffset + offset);
for (let i = 0; i < length; i++) {
// write offset
fixedSection.setUint32(i * 4, variableIndex - offset, true); // write serialized element to variable section
variableIndex = this.elementType.struct_serializeToBytes(value[i], output, variableIndex);
}
return variableIndex;
} else {
let index = offset;
for (let i = 0; i < length; i++) {
index = this.elementType.struct_serializeToBytes(value[i], output, index);
}
return index;
tree_convertToStruct(target) {
const value = this.struct_defaultValue();
const length = this.tree_getLength(target);
for (let i = 0; i < length; i++) {
value[i] = this.elementType.tree_convertToStruct(this.tree_getSubtreeAtChunkIndex(target, i));
}
return value;
}
}
struct_getRootAtChunkIndex(value, index) {
return this.elementType.hashTreeRoot(value[index]);
}
struct_getPropertyNames(value) {
const length = this.struct_getLength(value);
return Array.from({
length
}, (_, i) => i).concat(["length"]);
}
struct_convertFromJson(data, options) {
return Array.from({
length: data.length
}, (_, i) => this.elementType.struct_convertFromJson(data[i], options));
}
struct_convertToJson(value, options) {
return Array.from({
length: this.struct_getLength(value)
}, (_, i) => this.elementType.struct_convertToJson(value[i], options));
}
struct_convertToTree(value) {
if ((0, _treeValue.isTreeBacked)(value)) return value.tree.clone();
const contents = [];
for (const element of value) {
contents.push(this.elementType.struct_convertToTree(element).rootNode);
tree_getSerializedLength(target) {
const fixedLen = this.elementType.getFixedSerializedLength();
if (fixedLen === null) {
let s = 0;
for (let i = 0; i < this.tree_getLength(target); i++) {
s += this.elementType.tree_getSerializedLength(this.tree_getSubtreeAtChunkIndex(target, i)) + 4;
}
return s;
}
else {
return fixedLen * this.tree_getLength(target);
}
}
return new _persistentMerkleTree.Tree((0, _persistentMerkleTree.subtreeFillToContents)(contents, this.getChunkDepth()));
}
tree_convertToStruct(target) {
const value = this.struct_defaultValue();
const length = this.tree_getLength(target);
for (let i = 0; i < length; i++) {
value[i] = this.elementType.tree_convertToStruct(this.tree_getSubtreeAtChunkIndex(target, i));
tree_serializeToBytes(target, output, offset) {
const length = this.tree_getLength(target);
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, length);
if (this.elementType.hasVariableSerializedLength()) {
let variableIndex = offset + length * 4;
const fixedSection = new DataView(output.buffer, output.byteOffset + offset, length * 4);
for (let i = 0; i < nodes.length; i++) {
// write offset
fixedSection.setUint32(i * 4, variableIndex - offset, true);
// write serialized element to variable section
variableIndex = this.elementType.tree_serializeToBytes(new persistent_merkle_tree_1.Tree(nodes[i]), output, variableIndex);
}
return variableIndex;
}
else {
let index = offset;
for (let i = 0; i < nodes.length; i++) {
index = this.elementType.tree_serializeToBytes(new persistent_merkle_tree_1.Tree(nodes[i]), output, index);
}
return index;
}
}
return value;
}
tree_getSerializedLength(target) {
if (this.elementType.hasVariableSerializedLength()) {
let s = 0;
for (let i = 0; i < this.tree_getLength(target); i++) {
s += this.elementType.tree_getSerializedLength(this.tree_getSubtreeAtChunkIndex(target, i)) + 4;
}
return s;
} else {
return this.elementType.tree_getSerializedLength(null) * this.tree_getLength(target);
getPropertyGindex(prop) {
return this.getGindexAtChunkIndex(prop);
}
}
tree_serializeToBytes(target, output, offset) {
const length = this.tree_getLength(target);
if (this.elementType.hasVariableSerializedLength()) {
let variableIndex = offset + length * 4;
const fixedSection = new DataView(output.buffer, output.byteOffset + offset, length * 4);
let i = 0;
for (const node of target.iterateNodesAtDepth(this.getChunkDepth(), i, length)) {
// write offset
fixedSection.setUint32(i * 4, variableIndex - offset, true); // write serialized element to variable section
variableIndex = this.elementType.tree_serializeToBytes(new _persistentMerkleTree.Tree(node), output, variableIndex);
i++;
}
return variableIndex;
} else {
let index = offset;
let i = 0;
for (const node of target.iterateNodesAtDepth(this.getChunkDepth(), i, length)) {
index = this.elementType.tree_serializeToBytes(new _persistentMerkleTree.Tree(node), output, index);
i++;
}
return index;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getPropertyType(prop) {
return this.elementType;
}
}
getPropertyGindex(prop) {
return this.getGindexAtChunkIndex(prop);
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
getPropertyType(prop) {
return this.elementType;
}
tree_getProperty(target, property) {
const length = this.tree_getLength(target);
if (property === "length") {
return length;
tree_getProperty(target, property) {
const length = this.tree_getLength(target);
if (property === "length") {
return length;
}
const index = Number(property);
if (Number.isNaN(index)) {
return undefined;
}
if (index >= length) {
return undefined;
}
return this.tree_getSubtreeAtChunkIndex(target, index);
}
const index = Number(property);
if (Number.isNaN(index)) {
return undefined;
tree_setProperty(target, property, value, expand = false) {
this.tree_setSubtreeAtChunkIndex(target, property, value, expand);
return true;
}
if (index >= length) {
return undefined;
tree_deleteProperty(target, property) {
return this.tree_setProperty(target, property, this.elementType.tree_defaultValue());
}
return this.tree_getSubtreeAtChunkIndex(target, index);
}
tree_setProperty(target, property, value, expand = false) {
this.tree_setSubtreeAtChunkIndex(target, property, value, expand);
return true;
}
tree_deleteProperty(target, property) {
return this.tree_setProperty(target, property, this.elementType.tree_defaultValue());
}
tree_getPropertyNames(target) {
return Array.from({
length: this.tree_getLength(target)
}, (_, i) => i).concat(["length"]);
}
*tree_iterateValues(target) {
for (const gindex of (0, _persistentMerkleTree.iterateAtDepth)(this.getChunkDepth(), BigInt(0), BigInt(this.tree_getLength(target)))) {
yield target.getSubtree(gindex);
tree_getPropertyNames(target) {
return Array.from({ length: this.tree_getLength(target) }, (_, i) => i).concat(["length"]);
}
}
*tree_readonlyIterateValues(target) {
for (const node of target.iterateNodesAtDepth(this.getChunkDepth(), 0, this.tree_getLength(target))) {
yield new _persistentMerkleTree.Tree(node);
*tree_iterateValues(target) {
for (const gindex of persistent_merkle_tree_1.iterateAtDepth(this.getChunkDepth(), BigInt(0), BigInt(this.tree_getLength(target)))) {
yield target.getSubtree(gindex);
}
}
}
bytes_getVariableOffsets(target) {
if (this.elementType.hasVariableSerializedLength()) {
if (target.length === 0) {
return [];
}
const offsets = []; // all elements are variable-sized
// indices contain offsets, which are indices deeper in the byte array
const fixedSection = new DataView(target.buffer, target.byteOffset);
const firstOffset = fixedSection.getUint32(0, true);
let currentOffset = firstOffset;
let nextOffset;
let currentIndex = 0;
let nextIndex = 0;
while (currentIndex < firstOffset) {
if (currentOffset > target.length) {
throw new Error("Offset out of bounds");
*tree_readonlyIterateValues(target) {
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, this.tree_getLength(target));
for (let i = 0; i < nodes.length; i++) {
yield new persistent_merkle_tree_1.Tree(nodes[i]);
}
nextIndex = currentIndex + 4;
nextOffset = nextIndex === firstOffset ? target.length : fixedSection.getUint32(nextIndex, true);
if (currentOffset > nextOffset) {
throw new Error("Offsets must be increasing");
}
bytes_getVariableOffsets(target) {
if (this.elementType.hasVariableSerializedLength()) {
if (target.length === 0) {
return [];
}
const offsets = [];
// all elements are variable-sized
// indices contain offsets, which are indices deeper in the byte array
const fixedSection = new DataView(target.buffer, target.byteOffset);
const firstOffset = fixedSection.getUint32(0, true);
let currentOffset = firstOffset;
let nextOffset;
let currentIndex = 0;
let nextIndex = 0;
while (currentIndex < firstOffset) {
if (currentOffset > target.length) {
throw new Error("Offset out of bounds");
}
nextIndex = currentIndex + 4;
nextOffset = nextIndex === firstOffset ? target.length : fixedSection.getUint32(nextIndex, true);
if (currentOffset > nextOffset) {
throw new Error("Offsets must be increasing");
}
offsets.push([currentOffset, nextOffset]);
currentIndex = nextIndex;
currentOffset = nextOffset;
}
if (firstOffset !== currentIndex) {
throw new Error("First offset skips variable data");
}
return offsets;
}
offsets.push([currentOffset, nextOffset]);
currentIndex = nextIndex;
currentOffset = nextOffset;
}
if (firstOffset !== currentIndex) {
throw new Error("First offset skips variable data");
}
return offsets;
} else {
return [];
else {
return [];
}
}
}
tree_getLeafGindices(target, root = BigInt(1)) {
// Underlying elements exist one per chunk
// Iterate through chunk gindices, recursively fetching leaf gindices from each chunk
const chunkCount = this.tree_getChunkCount(target);
const gindices = [];
const startIndex = (0, _persistentMerkleTree.toGindex)(this.getChunkDepth(), BigInt(0));
const extendedStartIndex = (0, _persistentMerkleTree.concatGindices)([root, startIndex]);
if (this.elementType.hasVariableSerializedLength()) {
if (!target) {
throw new Error("variable type requires tree argument to get leaves");
} // variable-length elements must pass the underlying subtrees to determine the length
for (let i = 0, gindex = startIndex, extendedGindex = extendedStartIndex; i < chunkCount; i++, gindex++, extendedGindex++) {
gindices.push(...this.elementType.tree_getLeafGindices(target.getSubtree(gindex), extendedGindex));
}
} else {
for (let i = 0, gindex = extendedStartIndex; i < chunkCount; i++, gindex++) {
gindices.push(...this.elementType.tree_getLeafGindices(undefined, gindex));
}
tree_getLeafGindices(target, root = BigInt(1)) {
// Underlying elements exist one per chunk
// Iterate through chunk gindices, recursively fetching leaf gindices from each chunk
const chunkCount = this.tree_getChunkCount(target);
const gindices = [];
const startIndex = persistent_merkle_tree_1.toGindex(this.getChunkDepth(), BigInt(0));
const extendedStartIndex = persistent_merkle_tree_1.concatGindices([root, startIndex]);
if (this.elementType.hasVariableSerializedLength()) {
if (!target) {
throw new Error("variable type requires tree argument to get leaves");
}
// variable-length elements must pass the underlying subtrees to determine the length
for (let i = 0, gindex = startIndex, extendedGindex = extendedStartIndex; i < chunkCount; i++, gindex++, extendedGindex++) {
gindices.push(...this.elementType.tree_getLeafGindices(target.getSubtree(gindex), extendedGindex));
}
}
else {
for (let i = 0, gindex = extendedStartIndex; i < chunkCount; i++, gindex++) {
gindices.push(...this.elementType.tree_getLeafGindices(undefined, gindex));
}
}
return gindices;
}
return gindices;
}
}
exports.CompositeArrayType = CompositeArrayType;
//# sourceMappingURL=array.js.map

@@ -37,1 +37,2 @@ import { BitList, Json } from "../../interface";

}
//# sourceMappingURL=bitList.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isBitListType = isBitListType;
exports.BitListType = exports.BITLIST_TYPE = void 0;
var _list = require("./list");
var _basic = require("../basic");
var _type = require("../type");
var _byteArray = require("../../util/byteArray");
var _constants = require("../../util/constants");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const BITLIST_TYPE = Symbol.for("ssz/BitListType");
exports.BITLIST_TYPE = BITLIST_TYPE;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitListType = exports.isBitListType = exports.BITLIST_TYPE = void 0;
const list_1 = require("./list");
const basic_1 = require("../basic");
const type_1 = require("../type");
const byteArray_1 = require("../../util/byteArray");
const constants_1 = require("../../util/constants");
exports.BITLIST_TYPE = Symbol.for("ssz/BitListType");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function isBitListType(type) {
return (0, _type.isTypeOf)(type, BITLIST_TYPE);
return type_1.isTypeOf(type, exports.BITLIST_TYPE);
}
class BitListType extends _list.BasicListType {
constructor(options) {
super(_objectSpread({
elementType: _basic.booleanType
}, options));
this._typeSymbols.add(BITLIST_TYPE);
}
struct_getByte(value, index) {
const firstBitIndex = index * 8;
const lastBitIndex = Math.min(firstBitIndex + 7, value.length - 1);
let bitstring = "0b";
for (let i = lastBitIndex; i >= firstBitIndex; i--) {
bitstring += value[i] ? "1" : "0";
exports.isBitListType = isBitListType;
class BitListType extends list_1.BasicListType {
constructor(options) {
super({ elementType: basic_1.booleanType, ...options });
this._typeSymbols.add(exports.BITLIST_TYPE);
}
return Number(bitstring);
}
struct_getLength(value) {
return value.length;
}
struct_getByteLength(value) {
return Math.ceil(value.length / 8);
}
struct_getSerializedLength(value) {
if (value.length % 8 === 0) {
return this.struct_getByteLength(value) + 1;
} else {
return this.struct_getByteLength(value);
struct_getByte(value, index) {
const firstBitIndex = index * 8;
const lastBitIndex = Math.min(firstBitIndex + 7, value.length - 1);
let bitstring = "0b";
for (let i = lastBitIndex; i >= firstBitIndex; i--) {
bitstring += value[i] ? "1" : "0";
}
return Number(bitstring);
}
}
getMaxSerializedLength() {
return Math.ceil(this.limit / 8) + 1;
}
getMinSerializedLength() {
return 1;
}
struct_getChunkCount(value) {
return Math.ceil(this.struct_getLength(value) / 256);
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const value = [];
const toBool = c => c === "1" ? true : false;
for (let i = start; i < end - 1; i++) {
let bitstring = data[i].toString(2);
bitstring = "0".repeat(8 - bitstring.length) + bitstring;
value.push(...Array.prototype.map.call(bitstring, toBool).reverse());
struct_getLength(value) {
return value.length;
}
const lastByte = data[end - 1];
if (lastByte === 0) {
throw new Error("Invalid deserialized bitlist, padding bit required");
struct_getByteLength(value) {
return Math.ceil(value.length / 8);
}
if (lastByte === 1) {
return value;
struct_getSerializedLength(value) {
if (value.length % 8 === 0) {
return this.struct_getByteLength(value) + 1;
}
else {
return this.struct_getByteLength(value);
}
}
const lastBits = Array.prototype.map.call(lastByte.toString(2), toBool).reverse();
const last1 = lastBits.lastIndexOf(true);
value.push(...lastBits.slice(0, last1));
if (value.length > this.limit) {
throw new Error("Invalid deserialized bitlist, length greater than limit");
getMaxSerializedLength() {
return Math.ceil(this.limit / 8) + 1;
}
return value;
}
struct_serializeToBytes(value, output, offset) {
const byteLength = this.struct_getByteLength(value);
for (let i = 0; i < byteLength; i++) {
output[offset + i] = this.struct_getByte(value, i);
getMinSerializedLength() {
return 1;
}
const newOffset = offset + byteLength;
if (value.length % 8 === 0) {
output[newOffset] = 1;
return newOffset + 1;
} else {
output[newOffset - 1] |= 1 << value.length % 8;
return newOffset;
struct_getChunkCount(value) {
return Math.ceil(this.struct_getLength(value) / 256);
}
}
struct_getRootAtChunkIndex(value, chunkIndex) {
const output = new Uint8Array(_constants.BYTES_PER_CHUNK);
const byteLength = Math.min(_constants.BYTES_PER_CHUNK, this.struct_getByteLength(value) - chunkIndex);
const byteOffset = chunkIndex * _constants.BYTES_PER_CHUNK;
for (let i = 0; i < byteLength; i++) {
output[i] = this.struct_getByte(value, i + byteOffset);
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const value = [];
const toBool = (c) => (c === "1" ? true : false);
for (let i = start; i < end - 1; i++) {
let bitstring = data[i].toString(2);
bitstring = "0".repeat(8 - bitstring.length) + bitstring;
value.push(...Array.prototype.map.call(bitstring, toBool).reverse());
}
const lastByte = data[end - 1];
if (lastByte === 0) {
throw new Error("Invalid deserialized bitlist, padding bit required");
}
if (lastByte === 1) {
return value;
}
const lastBits = Array.prototype.map.call(lastByte.toString(2), toBool).reverse();
const last1 = lastBits.lastIndexOf(true);
value.push(...lastBits.slice(0, last1));
if (value.length > this.limit) {
throw new Error("Invalid deserialized bitlist, length greater than limit");
}
return value;
}
return output;
}
struct_convertFromJson(data) {
const bytes = (0, _byteArray.fromHexString)(data);
return this.struct_deserializeFromBytes(bytes, 0, bytes.length);
}
struct_convertToJson(value) {
return (0, _byteArray.toHexString)(this.serialize(value));
}
tree_getByteLength(target) {
return Math.ceil(this.tree_getLength(target) / 8);
}
tree_getSerializedLength(target) {
const bitLength = this.tree_getLength(target);
if (bitLength % 8 === 0) {
return this.tree_getByteLength(target) + 1;
} else {
return this.tree_getByteLength(target);
struct_serializeToBytes(value, output, offset) {
const byteLength = this.struct_getByteLength(value);
for (let i = 0; i < byteLength; i++) {
output[offset + i] = this.struct_getByte(value, i);
}
const newOffset = offset + byteLength;
if (value.length % 8 === 0) {
output[newOffset] = 1;
return newOffset + 1;
}
else {
output[newOffset - 1] |= 1 << value.length % 8;
return newOffset;
}
}
}
tree_deserializeFromBytes(data, start, end) {
const lastByte = data[end - 1];
if (lastByte === 0) {
throw new Error("Invalid deserialized bitlist, padding bit required");
struct_getRootAtChunkIndex(value, chunkIndex) {
const output = new Uint8Array(constants_1.BYTES_PER_CHUNK);
const byteLength = Math.min(constants_1.BYTES_PER_CHUNK, this.struct_getByteLength(value) - chunkIndex);
const byteOffset = chunkIndex * constants_1.BYTES_PER_CHUNK;
for (let i = 0; i < byteLength; i++) {
output[i] = this.struct_getByte(value, i + byteOffset);
}
return output;
}
if (lastByte === 1) {
const target = super.tree_deserializeFromBytes(data, start, end - 1);
const length = (end - start - 1) * 8;
this.tree_setLength(target, length);
return target;
} // the last byte is > 1, so a padding bit will exist in the last byte and need to be removed
const target = super.tree_deserializeFromBytes(data, start, end);
const lastGindex = this.getGindexAtChunkIndex(Math.ceil((end - start) / 32) - 1); // copy chunk into new memory
const lastChunk = new Uint8Array(32);
lastChunk.set(target.getRoot(lastGindex));
const lastChunkByte = (end - start) % 32 - 1; // mask lastChunkByte
const lastByteBitLength = lastByte.toString(2).length - 1;
const length = (end - start - 1) * 8 + lastByteBitLength;
const mask = 0xff >> 8 - lastByteBitLength;
lastChunk[lastChunkByte] &= mask;
target.setRoot(lastGindex, lastChunk);
this.tree_setLength(target, length);
return target;
}
tree_serializeToBytes(target, output, offset) {
const sizeNoPadding = this.tree_getByteLength(target);
const fullChunkCount = Math.floor(sizeNoPadding / 32);
const remainder = sizeNoPadding % 32;
let i = 0;
if (fullChunkCount > 0) {
for (const node of target.iterateNodesAtDepth(this.getChunkDepth(), 0, fullChunkCount)) {
output.set(node.root, offset + i * 32);
i++;
}
struct_convertFromJson(data) {
const bytes = byteArray_1.fromHexString(data);
return this.struct_deserializeFromBytes(bytes, 0, bytes.length);
}
if (remainder) {
output.set(this.tree_getRootAtChunkIndex(target, fullChunkCount).slice(0, remainder), offset + i * 32);
struct_convertToJson(value) {
return byteArray_1.toHexString(this.serialize(value));
}
const bitLength = this.tree_getLength(target);
const size = this.tree_getSerializedLength(target);
const newOffset = offset + size; // set padding bit
output[newOffset - 1] |= 1 << bitLength % 8;
return newOffset;
}
getBitOffset(index) {
return index % 8;
}
getChunkOffset(index) {
return Math.floor(index % 256 / 8);
}
getChunkIndex(index) {
return Math.floor(index / 256);
}
tree_getChunkCount(target) {
return Math.ceil(this.tree_getLength(target) / 256);
}
*tree_iterateValues(target) {
const length = this.tree_getLength(target);
const chunkCount = this.tree_getChunkCount(target);
const nodeIterator = target.iterateNodesAtDepth(this.getChunkDepth(), 0, chunkCount);
let i = 0;
for (const node of nodeIterator) {
const chunk = node.root;
for (let j = 0; j < 256 && i < length; i++, j++) {
const byte = chunk[this.getChunkOffset(i)];
yield !!(byte & 1 << this.getBitOffset(i));
}
tree_getByteLength(target) {
return Math.ceil(this.tree_getLength(target) / 8);
}
}
tree_getValueAtIndex(target, index) {
const chunk = this.tree_getRootAtChunkIndex(target, this.getChunkIndex(index));
const byte = chunk[this.getChunkOffset(index)];
return !!(byte & 1 << this.getBitOffset(index));
}
tree_setValueAtIndex(target, property, value, expand = false) {
const chunkGindex = this.getGindexAtChunkIndex(this.getChunkIndex(property));
const chunk = new Uint8Array(32);
chunk.set(target.getRoot(chunkGindex));
const byteOffset = this.getChunkOffset(property);
if (value) {
chunk[byteOffset] |= 1 << this.getBitOffset(property);
} else {
chunk[byteOffset] &= 0xff ^ 1 << this.getBitOffset(property);
tree_getSerializedLength(target) {
const bitLength = this.tree_getLength(target);
if (bitLength % 8 === 0) {
return this.tree_getByteLength(target) + 1;
}
else {
return this.tree_getByteLength(target);
}
}
target.setRoot(chunkGindex, chunk, expand);
return true;
}
getMaxChunkCount() {
return Math.ceil(this.limit / 256);
}
tree_deserializeFromBytes(data, start, end) {
const lastByte = data[end - 1];
if (lastByte === 0) {
throw new Error("Invalid deserialized bitlist, padding bit required");
}
if (lastByte === 1) {
const target = super.tree_deserializeFromBytes(data, start, end - 1);
const length = (end - start - 1) * 8;
this.tree_setLength(target, length);
return target;
}
// the last byte is > 1, so a padding bit will exist in the last byte and need to be removed
const target = super.tree_deserializeFromBytes(data, start, end);
const lastGindex = this.getGindexAtChunkIndex(Math.ceil((end - start) / 32) - 1);
// copy chunk into new memory
const lastChunk = new Uint8Array(32);
lastChunk.set(target.getRoot(lastGindex));
const lastChunkByte = ((end - start) % 32) - 1;
// mask lastChunkByte
const lastByteBitLength = lastByte.toString(2).length - 1;
const length = (end - start - 1) * 8 + lastByteBitLength;
const mask = 0xff >> (8 - lastByteBitLength);
lastChunk[lastChunkByte] &= mask;
target.setRoot(lastGindex, lastChunk);
this.tree_setLength(target, length);
return target;
}
tree_serializeToBytes(target, output, offset) {
const sizeNoPadding = this.tree_getByteLength(target);
const fullChunkCount = Math.floor(sizeNoPadding / 32);
const remainder = sizeNoPadding % 32;
let i = 0;
if (fullChunkCount > 0) {
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, fullChunkCount);
for (; i < nodes.length; i++) {
output.set(nodes[i].root, offset + i * 32);
}
}
if (remainder) {
output.set(this.tree_getRootAtChunkIndex(target, fullChunkCount).slice(0, remainder), offset + i * 32);
}
const bitLength = this.tree_getLength(target);
const size = this.tree_getSerializedLength(target);
const newOffset = offset + size;
// set padding bit
output[newOffset - 1] |= 1 << bitLength % 8;
return newOffset;
}
getBitOffset(index) {
return index % 8;
}
getChunkOffset(index) {
return Math.floor((index % 256) / 8);
}
getChunkIndex(index) {
return Math.floor(index / 256);
}
tree_getChunkCount(target) {
return Math.ceil(this.tree_getLength(target) / 256);
}
*tree_iterateValues(target) {
const length = this.tree_getLength(target);
const chunkCount = this.tree_getChunkCount(target);
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, chunkCount);
let i = 0;
for (let nodeIx = 0; nodeIx < nodes.length; nodeIx++) {
const chunk = nodes[nodeIx].root;
for (let j = 0; j < 256 && i < length; i++, j++) {
const byte = chunk[this.getChunkOffset(i)];
yield !!(byte & (1 << this.getBitOffset(i)));
}
}
}
tree_getValueAtIndex(target, index) {
const chunk = this.tree_getRootAtChunkIndex(target, this.getChunkIndex(index));
const byte = chunk[this.getChunkOffset(index)];
return !!(byte & (1 << this.getBitOffset(index)));
}
tree_setValueAtIndex(target, property, value, expand = false) {
const chunkGindex = this.getGindexAtChunkIndex(this.getChunkIndex(property));
const chunk = new Uint8Array(32);
chunk.set(target.getRoot(chunkGindex));
const byteOffset = this.getChunkOffset(property);
if (value) {
chunk[byteOffset] |= 1 << this.getBitOffset(property);
}
else {
chunk[byteOffset] &= 0xff ^ (1 << this.getBitOffset(property));
}
target.setRoot(chunkGindex, chunk, expand);
return true;
}
getMaxChunkCount() {
return Math.ceil(this.limit / 256);
}
}
exports.BitListType = BitListType;
//# sourceMappingURL=bitList.js.map

@@ -15,2 +15,3 @@ import { BitVector, Json } from "../../interface";

struct_getSerializedLength(value: BitVector): number;
getFixedSerializedLength(): null | number;
getMaxSerializedLength(): number;

@@ -37,1 +38,2 @@ getMinSerializedLength(): number;

}
//# sourceMappingURL=bitVector.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isBitVectorType = isBitVectorType;
exports.BitVectorType = exports.BITVECTOR_TYPE = void 0;
var _vector = require("./vector");
var _basic = require("../basic");
var _type = require("../type");
var _byteArray = require("../../util/byteArray");
var _constants = require("../../util/constants");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const BITVECTOR_TYPE = Symbol.for("ssz/BitVectorType");
exports.BITVECTOR_TYPE = BITVECTOR_TYPE;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BitVectorType = exports.isBitVectorType = exports.BITVECTOR_TYPE = void 0;
const vector_1 = require("./vector");
const basic_1 = require("../basic");
const type_1 = require("../type");
const byteArray_1 = require("../../util/byteArray");
const constants_1 = require("../../util/constants");
exports.BITVECTOR_TYPE = Symbol.for("ssz/BitVectorType");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function isBitVectorType(type) {
return (0, _type.isTypeOf)(type, BITVECTOR_TYPE);
return type_1.isTypeOf(type, exports.BITVECTOR_TYPE);
}
class BitVectorType extends _vector.BasicVectorType {
constructor(options) {
super(_objectSpread({
elementType: _basic.booleanType
}, options));
this._typeSymbols.add(BITVECTOR_TYPE);
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getLength(value) {
return this.length;
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getByteLength(value) {
return Math.ceil(this.length / 8);
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getSerializedLength(value) {
return Math.ceil(this.length / 8);
}
getMaxSerializedLength() {
return this.struct_getSerializedLength(null);
}
getMinSerializedLength() {
return this.struct_getSerializedLength(null);
}
struct_getChunkCount(value) {
return Math.ceil(this.struct_getLength(value) / 256);
}
struct_getByte(value, index) {
const firstBitIndex = index * 8;
const lastBitIndex = Math.min(firstBitIndex + 7, value.length - 1);
let bitstring = "0b";
for (let i = lastBitIndex; i >= firstBitIndex; i--) {
bitstring += value[i] ? "1" : "0";
exports.isBitVectorType = isBitVectorType;
class BitVectorType extends vector_1.BasicVectorType {
constructor(options) {
super({ elementType: basic_1.booleanType, ...options });
this._typeSymbols.add(exports.BITVECTOR_TYPE);
}
return Number(bitstring);
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
if (end - start !== this.size(null)) {
throw new Error("Invalid bitvector: length not equal to vector length");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getLength(value) {
return this.length;
}
const value = [];
for (let i = start; i < end - 1; i++) {
value.push(...(0, _byteArray.getByteBits)(data, i));
// Override all length methods to understand .length as bits not bytes
// eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getByteLength(value) {
return Math.ceil(this.length / 8);
}
const lastBitLength = this.length % 8;
if (!lastBitLength) {
// vector takes up the whole byte, no need for checks
value.push(...(0, _byteArray.getByteBits)(data, end - 1));
} else {
const lastBits = (0, _byteArray.getByteBits)(data, end - 1);
if (lastBits.slice(lastBitLength).some(b => b)) {
throw new Error("Invalid bitvector: nonzero bits past length");
}
value.push(...lastBits.slice(0, lastBitLength));
// eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getSerializedLength(value) {
return Math.ceil(this.length / 8);
}
return value;
}
struct_serializeToBytes(value, output, offset) {
const byteLength = this.struct_getByteLength(value);
for (let i = 0; i < byteLength; i++) {
output[offset + i] = this.struct_getByte(value, i);
getFixedSerializedLength() {
return Math.ceil(this.length / 8);
}
return offset + byteLength;
}
struct_getRootAtChunkIndex(value, chunkIndex) {
const output = new Uint8Array(_constants.BYTES_PER_CHUNK);
const byteLength = Math.min(_constants.BYTES_PER_CHUNK, this.struct_getByteLength(value) - chunkIndex);
const byteOffset = chunkIndex * _constants.BYTES_PER_CHUNK;
for (let i = 0; i < byteLength; i++) {
output[i] = this.struct_getByte(value, i + byteOffset);
getMaxSerializedLength() {
return Math.ceil(this.length / 8);
}
return output;
}
struct_convertFromJson(data) {
const bytes = (0, _byteArray.fromHexString)(data);
return this.fromBytes(bytes, 0, bytes.length);
}
struct_convertToJson(value) {
return (0, _byteArray.toHexString)(this.serialize(value));
}
tree_getByteLength(target) {
return Math.ceil(this.tree_getLength(target) / 8);
}
tree_getSerializedLength(target) {
return this.tree_getByteLength(target);
}
tree_deserializeFromBytes(data, start, end) {
// mask last byte to ensure it doesn't go over length
const lastByte = data[end - 1]; // If the data len fits full bytes this check must be skipped.
// Otherwise we must ensure that the extra bits are set to zero.
const lastByteBitLen = this.length % 8;
if (lastByteBitLen > 0) {
const mask = 0xff << lastByteBitLen & 0xff;
if ((lastByte & mask) > 0) {
throw new Error("Invalid deserialized bitvector length");
}
getMinSerializedLength() {
return Math.ceil(this.length / 8);
}
return super.tree_deserializeFromBytes(data, start, end);
}
getBitOffset(index) {
return index % 8;
}
getChunkOffset(index) {
return Math.floor(index % 256 / 8);
}
getChunkIndex(index) {
return Math.floor(index / 256);
}
tree_getChunkCount(target) {
return Math.ceil(this.tree_getLength(target) / 256);
}
*tree_iterateValues(target) {
const length = this.tree_getLength(target);
const chunkCount = this.tree_getChunkCount(target);
const nodeIterator = target.iterateNodesAtDepth(this.getChunkDepth(), 0, chunkCount);
let i = 0;
for (const node of nodeIterator) {
const chunk = node.root;
for (let j = 0; j < 256 && i < length; i++, j++) {
const byte = chunk[this.getChunkOffset(i)];
yield !!(byte & 1 << this.getBitOffset(i));
}
struct_getChunkCount(value) {
return Math.ceil(this.struct_getLength(value) / 256);
}
}
tree_getValueAtIndex(target, index) {
const chunk = this.tree_getRootAtChunkIndex(target, this.getChunkIndex(index));
const byte = chunk[this.getChunkOffset(index)];
return !!(byte & 1 << this.getBitOffset(index));
}
tree_setProperty(target, property, value) {
const chunkGindex = this.getGindexAtChunkIndex(this.getChunkIndex(property));
const chunk = new Uint8Array(32);
chunk.set(target.getRoot(chunkGindex));
const byteOffset = this.getChunkOffset(property);
if (value) {
chunk[byteOffset] |= 1 << this.getBitOffset(property);
} else {
chunk[byteOffset] &= 0xff ^ 1 << this.getBitOffset(property);
struct_getByte(value, index) {
const firstBitIndex = index * 8;
const lastBitIndex = Math.min(firstBitIndex + 7, value.length - 1);
let bitstring = "0b";
for (let i = lastBitIndex; i >= firstBitIndex; i--) {
bitstring += value[i] ? "1" : "0";
}
return Number(bitstring);
}
target.setRoot(chunkGindex, chunk);
return true;
}
getMaxChunkCount() {
return Math.ceil(this.length / 256);
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (end - start !== this.size(null)) {
throw new Error("Invalid bitvector: length not equal to vector length");
}
const value = [];
for (let i = start; i < end - 1; i++) {
value.push(...byteArray_1.getByteBits(data, i));
}
const lastBitLength = this.length % 8;
if (!lastBitLength) {
// vector takes up the whole byte, no need for checks
value.push(...byteArray_1.getByteBits(data, end - 1));
}
else {
const lastBits = byteArray_1.getByteBits(data, end - 1);
if (lastBits.slice(lastBitLength).some((b) => b)) {
throw new Error("Invalid bitvector: nonzero bits past length");
}
value.push(...lastBits.slice(0, lastBitLength));
}
return value;
}
struct_serializeToBytes(value, output, offset) {
const byteLength = this.struct_getByteLength(value);
for (let i = 0; i < byteLength; i++) {
output[offset + i] = this.struct_getByte(value, i);
}
return offset + byteLength;
}
struct_getRootAtChunkIndex(value, chunkIndex) {
const output = new Uint8Array(constants_1.BYTES_PER_CHUNK);
const byteLength = Math.min(constants_1.BYTES_PER_CHUNK, this.struct_getByteLength(value) - chunkIndex);
const byteOffset = chunkIndex * constants_1.BYTES_PER_CHUNK;
for (let i = 0; i < byteLength; i++) {
output[i] = this.struct_getByte(value, i + byteOffset);
}
return output;
}
struct_convertFromJson(data) {
const bytes = byteArray_1.fromHexString(data);
return this.fromBytes(bytes, 0, bytes.length);
}
struct_convertToJson(value) {
return byteArray_1.toHexString(this.serialize(value));
}
tree_getByteLength(target) {
return Math.ceil(this.tree_getLength(target) / 8);
}
tree_getSerializedLength(target) {
return this.tree_getByteLength(target);
}
tree_deserializeFromBytes(data, start, end) {
// mask last byte to ensure it doesn't go over length
const lastByte = data[end - 1];
// If the data len fits full bytes this check must be skipped.
// Otherwise we must ensure that the extra bits are set to zero.
const lastByteBitLen = this.length % 8;
if (lastByteBitLen > 0) {
const mask = (0xff << lastByteBitLen) & 0xff;
if ((lastByte & mask) > 0) {
throw new Error("Invalid deserialized bitvector length");
}
}
return super.tree_deserializeFromBytes(data, start, end);
}
getBitOffset(index) {
return index % 8;
}
getChunkOffset(index) {
return Math.floor((index % 256) / 8);
}
getChunkIndex(index) {
return Math.floor(index / 256);
}
tree_getChunkCount(target) {
return Math.ceil(this.tree_getLength(target) / 256);
}
*tree_iterateValues(target) {
const length = this.tree_getLength(target);
const chunkCount = this.tree_getChunkCount(target);
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, chunkCount);
let i = 0;
for (let nodeIx = 0; nodeIx < nodes.length; nodeIx++) {
const chunk = nodes[nodeIx].root;
for (let j = 0; j < 256 && i < length; i++, j++) {
const byte = chunk[this.getChunkOffset(i)];
yield !!(byte & (1 << this.getBitOffset(i)));
}
}
}
tree_getValueAtIndex(target, index) {
const chunk = this.tree_getRootAtChunkIndex(target, this.getChunkIndex(index));
const byte = chunk[this.getChunkOffset(index)];
return !!(byte & (1 << this.getBitOffset(index)));
}
tree_setProperty(target, property, value) {
const chunkGindex = this.getGindexAtChunkIndex(this.getChunkIndex(property));
const chunk = new Uint8Array(32);
chunk.set(target.getRoot(chunkGindex));
const byteOffset = this.getChunkOffset(property);
if (value) {
chunk[byteOffset] |= 1 << this.getBitOffset(property);
}
else {
chunk[byteOffset] &= 0xff ^ (1 << this.getBitOffset(property));
}
target.setRoot(chunkGindex, chunk);
return true;
}
getMaxChunkCount() {
return Math.ceil(this.length / 256);
}
}
exports.BitVectorType = BitVectorType;
//# sourceMappingURL=bitVector.js.map

@@ -19,1 +19,2 @@ import { ByteVector, Json } from "../../interface";

}
//# sourceMappingURL=byteVector.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isByteVectorType = isByteVectorType;
exports.ByteVectorType = exports.BYTEVECTOR_TYPE = void 0;
var _vector = require("./vector");
var _basic = require("../basic");
var _type = require("../type");
var _byteArray = require("../../util/byteArray");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const BYTEVECTOR_TYPE = Symbol.for("ssz/ByteVectorType");
exports.BYTEVECTOR_TYPE = BYTEVECTOR_TYPE;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ByteVectorType = exports.isByteVectorType = exports.BYTEVECTOR_TYPE = void 0;
const vector_1 = require("./vector");
const basic_1 = require("../basic");
const type_1 = require("../type");
const byteArray_1 = require("../../util/byteArray");
exports.BYTEVECTOR_TYPE = Symbol.for("ssz/ByteVectorType");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function isByteVectorType(type) {
return (0, _type.isTypeOf)(type, BYTEVECTOR_TYPE);
return type_1.isTypeOf(type, exports.BYTEVECTOR_TYPE);
}
class ByteVectorType extends _vector.BasicVectorType {
constructor(options) {
super(_objectSpread({
elementType: _basic.byteType
}, options));
this._typeSymbols.add(BYTEVECTOR_TYPE);
}
struct_defaultValue() {
return new Uint8Array(this.length);
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const length = end - start;
if (length !== this.length) {
throw new Error("Invalid deserialized vector length: expected ".concat(this.length, ", actual: ").concat(length));
exports.isByteVectorType = isByteVectorType;
class ByteVectorType extends vector_1.BasicVectorType {
constructor(options) {
super({ elementType: basic_1.byteType, ...options });
this._typeSymbols.add(exports.BYTEVECTOR_TYPE);
}
const value = new Uint8Array(length);
value.set(data.slice(start, end));
return value;
}
struct_serializeToBytes(value, output, offset) {
output.set(value, offset);
return offset + this.length;
}
struct_convertFromJson(data) {
const value = (0, _byteArray.fromHexString)(data);
if (value.length !== this.length) {
throw new Error("Invalid JSON vector length: expected ".concat(this.length, ", actual: ").concat(value.length));
struct_defaultValue() {
return new Uint8Array(this.length);
}
return value;
}
struct_convertToJson(value) {
return (0, _byteArray.toHexString)(value);
}
tree_convertToStruct(target) {
const value = new Uint8Array(this.length);
const chunkIterator = target.iterateNodesAtDepth(this.getChunkDepth(), 0, this.getMaxChunkCount());
if (this.length % 32 === 0) {
for (let i = 0; i < this.length; i += 32) {
value.set(chunkIterator.next().value.root, i);
}
} else {
let i;
for (i = 0; i < this.length - 32; i += 32) {
value.set(chunkIterator.next().value.root, i);
}
value.set(chunkIterator.next().value.root.subarray(0, this.length - i), i);
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const length = end - start;
if (length !== this.length) {
throw new Error(`Invalid deserialized vector length: expected ${this.length}, actual: ${length}`);
}
const value = new Uint8Array(length);
value.set(data.slice(start, end));
return value;
}
return value;
}
struct_serializeToBytes(value, output, offset) {
output.set(value, offset);
return offset + this.length;
}
struct_convertFromJson(data) {
const value = byteArray_1.fromHexString(data);
if (value.length !== this.length) {
throw new Error(`Invalid JSON vector length: expected ${this.length}, actual: ${value.length}`);
}
return value;
}
struct_convertToJson(value) {
return byteArray_1.toHexString(value);
}
tree_convertToStruct(target) {
const value = new Uint8Array(this.length);
const chunks = target.getNodesAtDepth(this.getChunkDepth(), 0, this.getMaxChunkCount());
let chunkIx = 0;
if (this.length % 32 === 0) {
for (let i = 0; i < this.length; i += 32) {
value.set(chunks[chunkIx].root, i);
chunkIx++;
}
}
else {
let i;
for (i = 0; i < this.length - 32; i += 32) {
value.set(chunks[chunkIx].root, i);
chunkIx++;
}
value.set(chunks[chunkIx].root.subarray(0, this.length - i), i);
}
return value;
}
}
exports.ByteVectorType = ByteVectorType;
//# sourceMappingURL=byteVector.js.map

@@ -10,4 +10,14 @@ import { Json, ObjectLike } from "../../interface";

export declare function isContainerType<T extends ObjectLike = ObjectLike>(type: Type<unknown>): type is ContainerType<T>;
declare type FieldInfo = {
isBasic: boolean;
gindex: bigint;
};
export declare class ContainerType<T extends ObjectLike = ObjectLike> extends CompositeType<T> {
fields: Record<string, Type<any>>;
/**
* This caches FieldInfo by field name so that we don't have to query this same data in a lot of apis.
* This helps speed up 30% with a simple test of increasing state.slot from 0 to 1_000_000 as shown in the
* performance test of uint.test.ts.
**/
fieldInfos: Map<string, FieldInfo>;
constructor(options: IContainerOptions);

@@ -43,4 +53,7 @@ struct_defaultValue(): T;

hasVariableSerializedLength(): boolean;
getFixedSerializedLength(): null | number;
getMaxChunkCount(): number;
tree_getLeafGindices(target?: Tree, root?: Gindex): Gindex[];
}
export {};
//# sourceMappingURL=container.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isContainerType = isContainerType;
exports.ContainerType = exports.CONTAINER_TYPE = void 0;
var _abstract = require("./abstract");
var _type = require("../type");
var _persistentMerkleTree = require("@chainsafe/persistent-merkle-tree");
var _errorPath = require("../../util/errorPath");
var _json = require("../../util/json");
var _treeValue = require("../../backings/tree/treeValue");
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const CONTAINER_TYPE = Symbol.for("ssz/ContainerType");
exports.CONTAINER_TYPE = CONTAINER_TYPE;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContainerType = exports.isContainerType = exports.CONTAINER_TYPE = void 0;
const abstract_1 = require("./abstract");
const type_1 = require("../type");
const persistent_merkle_tree_1 = require("@chainsafe/persistent-merkle-tree");
const errorPath_1 = require("../../util/errorPath");
const json_1 = require("../../util/json");
const treeValue_1 = require("../../backings/tree/treeValue");
exports.CONTAINER_TYPE = Symbol.for("ssz/ContainerType");
function isContainerType(type) {
return (0, _type.isTypeOf)(type, CONTAINER_TYPE);
return type_1.isTypeOf(type, exports.CONTAINER_TYPE);
}
class ContainerType extends _abstract.CompositeType {
// ES6 ensures key order is chronological
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(options) {
super();
_defineProperty(this, "fields", void 0);
this.fields = _objectSpread({}, options.fields);
this._typeSymbols.add(CONTAINER_TYPE);
}
struct_defaultValue() {
const obj = {};
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
obj[fieldName] = fieldType.struct_defaultValue();
exports.isContainerType = isContainerType;
class ContainerType extends abstract_1.CompositeType {
// ES6 ensures key order is chronological
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fields;
/**
* This caches FieldInfo by field name so that we don't have to query this same data in a lot of apis.
* This helps speed up 30% with a simple test of increasing state.slot from 0 to 1_000_000 as shown in the
* performance test of uint.test.ts.
**/
fieldInfos;
constructor(options) {
super();
this.fields = { ...options.fields };
this._typeSymbols.add(exports.CONTAINER_TYPE);
this.fieldInfos = new Map();
let chunkIndex = 0;
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
this.fieldInfos.set(fieldName, {
isBasic: !abstract_1.isCompositeType(fieldType),
gindex: this.getGindexAtChunkIndex(chunkIndex),
});
chunkIndex++;
}
}
return obj;
}
struct_getSerializedLength(value) {
let s = 0;
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
if (fieldType.hasVariableSerializedLength()) {
s += fieldType.struct_getSerializedLength(value[fieldName]) + 4;
} else {
s += fieldType.struct_getSerializedLength(null);
}
struct_defaultValue() {
const obj = {};
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
obj[fieldName] = fieldType.struct_defaultValue();
}
return obj;
}
return s;
}
getMaxSerializedLength() {
const fixedSize = Object.values(this.fields).reduce((total, fieldType) => total + (fieldType.hasVariableSerializedLength() ? 4 : fieldType.getMaxSerializedLength()), 0);
const maxDynamicSize = Object.values(this.fields).reduce((total, fieldType) => total += fieldType.hasVariableSerializedLength() ? fieldType.getMaxSerializedLength() : 0, 0);
return fixedSize + maxDynamicSize;
}
getMinSerializedLength() {
const fixedSize = Object.values(this.fields).reduce((total, fieldType) => total + (fieldType.hasVariableSerializedLength() ? 4 : fieldType.getMinSerializedLength()), 0);
const minDynamicSize = Object.values(this.fields).reduce((total, fieldType) => total += fieldType.hasVariableSerializedLength() ? fieldType.getMinSerializedLength() : 0, 0);
return fixedSize + minDynamicSize;
}
struct_assertValidValue(value) {
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
try {
// @ts-ignore
fieldType.struct_assertValidValue(value[fieldName]);
} catch (e) {
throw new Error("Invalid field ".concat(fieldName, ": ").concat(e.message));
}
struct_getSerializedLength(value) {
let s = 0;
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
const fixedLen = fieldType.getFixedSerializedLength();
if (fixedLen === null) {
s += fieldType.struct_getSerializedLength(value[fieldName]) + 4;
}
else {
s += fixedLen;
}
}
return s;
}
}
struct_equals(value1, value2) {
this.struct_assertValidValue(value1);
this.struct_assertValidValue(value2);
return Object.entries(this.fields).every(([fieldName, fieldType]) => {
return fieldType.struct_equals(value1[fieldName], value2[fieldName]);
});
}
struct_clone(value) {
const newValue = {};
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
newValue[fieldName] = fieldType.struct_clone(value[fieldName]);
getMaxSerializedLength() {
let maxSize = 0;
for (const fieldType of Object.values(this.fields)) {
const fieldFixedLen = fieldType.getFixedSerializedLength();
if (fieldFixedLen === null) {
// +4 for the offset
maxSize += 4 + fieldType.getMaxSerializedLength();
}
else {
maxSize += fieldFixedLen;
}
}
return maxSize;
}
return newValue;
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
let currentIndex = start;
let nextIndex = currentIndex;
const value = {}; // Since variable-sized values can be interspersed with fixed-sized values, we precalculate
// the offset indices so we can more easily deserialize the fields in once pass
// first we get the fixed sizes
const fixedSizes = Object.values(this.fields).map(fieldType => !fieldType.hasVariableSerializedLength() && fieldType.struct_getSerializedLength(null)); // with the fixed sizes, we can read the offsets, and store for our single pass
const offsets = [];
const fixedSection = new DataView(data.buffer, data.byteOffset);
const fixedEnd = fixedSizes.reduce((index, size) => {
if (size === false) {
offsets.push(start + fixedSection.getUint32(index, true));
return index + 4;
} else {
return index + size;
}
}, start);
offsets.push(end);
if (fixedEnd !== offsets[0]) {
throw new Error("Not all variable bytes consumed");
getMinSerializedLength() {
let maxSize = 0;
for (const fieldType of Object.values(this.fields)) {
const fieldFixedLen = fieldType.getFixedSerializedLength();
if (fieldFixedLen === null) {
// +4 for the offset
maxSize += 4 + fieldType.getMinSerializedLength();
}
else {
maxSize += fieldFixedLen;
}
}
return maxSize;
}
let offsetIndex = 0;
for (const [i, [fieldName, fieldType]] of Object.entries(this.fields).entries()) {
try {
const fieldSize = fixedSizes[i];
if (fieldSize === false) {
// variable-sized field
if (offsets[offsetIndex] > end) {
throw new Error("Offset out of bounds");
}
if (offsets[offsetIndex] > offsets[offsetIndex + 1]) {
throw new Error("Offsets must be increasing");
}
value[fieldName] = fieldType.struct_deserializeFromBytes(data, offsets[offsetIndex], offsets[offsetIndex + 1]);
offsetIndex++;
currentIndex += 4;
} else {
// fixed-sized field
nextIndex = currentIndex + fieldSize;
value[fieldName] = fieldType.struct_deserializeFromBytes(data, currentIndex, nextIndex);
currentIndex = nextIndex;
struct_assertValidValue(value) {
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
try {
fieldType.struct_assertValidValue(value[fieldName]);
}
catch (e) {
throw new Error(`Invalid field ${fieldName}: ${e.message}`);
}
}
} catch (e) {
throw new _errorPath.SszErrorPath(e, fieldName);
}
}
if (offsets.length > 1) {
if (offsetIndex !== offsets.length - 1) {
throw new Error("Not all variable bytes consumed");
}
if (currentIndex !== offsets[0]) {
throw new Error("Not all fixed bytes consumed");
}
} else {
if (currentIndex !== end) {
throw new Error("Not all fixed bytes consumed");
}
struct_equals(value1, value2) {
this.struct_assertValidValue(value1);
this.struct_assertValidValue(value2);
return Object.entries(this.fields).every(([fieldName, fieldType]) => {
return fieldType.struct_equals(value1[fieldName], value2[fieldName]);
});
}
return value;
}
struct_serializeToBytes(value, output, offset) {
let variableIndex = offset + Object.values(this.fields).reduce((total, fieldType) => total + (fieldType.hasVariableSerializedLength() ? 4 : fieldType.struct_getSerializedLength(null)), 0);
const fixedSection = new DataView(output.buffer, output.byteOffset + offset);
let fixedIndex = offset;
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
if (fieldType.hasVariableSerializedLength()) {
// write offset
fixedSection.setUint32(fixedIndex - offset, variableIndex - offset, true);
fixedIndex += 4; // write serialized element to variable section
variableIndex = fieldType.toBytes(value[fieldName], output, variableIndex);
} else {
fixedIndex = fieldType.toBytes(value[fieldName], output, fixedIndex);
}
struct_clone(value) {
const newValue = {};
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
newValue[fieldName] = fieldType.struct_clone(value[fieldName]);
}
return newValue;
}
return variableIndex;
}
struct_getRootAtChunkIndex(value, index) {
const fieldName = Object.keys(this.fields)[index];
const fieldType = this.fields[fieldName];
return fieldType.struct_hashTreeRoot(value[fieldName]);
}
struct_convertFromJson(data, options) {
if (typeof data !== "object") {
throw new Error("Invalid JSON container: expected Object");
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
let currentIndex = start;
let nextIndex = currentIndex;
const value = {};
// Since variable-sized values can be interspersed with fixed-sized values, we precalculate
// the offset indices so we can more easily deserialize the fields in once pass first we get the fixed sizes
// Note: `fixedSizes[i] = null` if that field has variable length
const fixedSizes = Object.values(this.fields).map((fieldType) => fieldType.getFixedSerializedLength());
// with the fixed sizes, we can read the offsets, and store for our single pass
const offsets = [];
const fixedSection = new DataView(data.buffer, data.byteOffset);
const fixedEnd = fixedSizes.reduce((index, size) => {
if (size === null) {
offsets.push(start + fixedSection.getUint32(index, true));
return index + 4;
}
else {
return index + size;
}
}, start);
offsets.push(end);
if (fixedEnd !== offsets[0]) {
throw new Error("Not all variable bytes consumed");
}
let offsetIndex = 0;
for (const [i, [fieldName, fieldType]] of Object.entries(this.fields).entries()) {
try {
const fieldSize = fixedSizes[i];
if (fieldSize === null) {
// variable-sized field
if (offsets[offsetIndex] > end) {
throw new Error("Offset out of bounds");
}
if (offsets[offsetIndex] > offsets[offsetIndex + 1]) {
throw new Error("Offsets must be increasing");
}
value[fieldName] = fieldType.struct_deserializeFromBytes(data, offsets[offsetIndex], offsets[offsetIndex + 1]);
offsetIndex++;
currentIndex += 4;
}
else {
// fixed-sized field
nextIndex = currentIndex + fieldSize;
value[fieldName] = fieldType.struct_deserializeFromBytes(data, currentIndex, nextIndex);
currentIndex = nextIndex;
}
}
catch (e) {
throw new errorPath_1.SszErrorPath(e, fieldName);
}
}
if (offsets.length > 1) {
if (offsetIndex !== offsets.length - 1) {
throw new Error("Not all variable bytes consumed");
}
if (currentIndex !== offsets[0]) {
throw new Error("Not all fixed bytes consumed");
}
}
else {
if (currentIndex !== end) {
throw new Error("Not all fixed bytes consumed");
}
}
return value;
}
const value = {};
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
const expectedCase = options ? options.case : null;
const expectedFieldName = (0, _json.toExpectedCase)(fieldName, expectedCase);
if (data[expectedFieldName] === undefined) {
throw new Error("Invalid JSON container field: expected field ".concat(expectedFieldName, " is undefined"));
}
value[fieldName] = fieldType.fromJson(data[expectedFieldName], options);
struct_serializeToBytes(value, output, offset) {
let variableIndex = offset;
for (const fieldType of Object.values(this.fields)) {
const fixedLen = fieldType.getFixedSerializedLength();
variableIndex += fixedLen === null ? 4 : fixedLen;
}
const fixedSection = new DataView(output.buffer, output.byteOffset + offset);
let fixedIndex = offset;
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
if (fieldType.hasVariableSerializedLength()) {
// write offset
fixedSection.setUint32(fixedIndex - offset, variableIndex - offset, true);
fixedIndex += 4;
// write serialized element to variable section
variableIndex = fieldType.toBytes(value[fieldName], output, variableIndex);
}
else {
fixedIndex = fieldType.toBytes(value[fieldName], output, fixedIndex);
}
}
return variableIndex;
}
return value;
}
struct_convertToJson(value, options) {
const data = {};
const expectedCase = options ? options.case : null;
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
data[(0, _json.toExpectedCase)(fieldName, expectedCase)] = fieldType.toJson(value[fieldName], options);
struct_getRootAtChunkIndex(value, index) {
const fieldName = Object.keys(this.fields)[index];
const fieldType = this.fields[fieldName];
return fieldType.struct_hashTreeRoot(value[fieldName]);
}
return data;
}
struct_convertToTree(value) {
if ((0, _treeValue.isTreeBacked)(value)) return value.tree.clone();
return new _persistentMerkleTree.Tree((0, _persistentMerkleTree.subtreeFillToContents)(Object.entries(this.fields).map(([fieldName, fieldType]) => {
if (!(0, _abstract.isCompositeType)(fieldType)) {
const chunk = new Uint8Array(32);
fieldType.toBytes(value[fieldName], chunk, 0);
return new _persistentMerkleTree.LeafNode(chunk);
} else {
return fieldType.struct_convertToTree(value[fieldName]).rootNode;
}
}), this.getChunkDepth()));
}
struct_getPropertyNames() {
return Object.keys(this.fields);
}
bytes_getVariableOffsets(target) {
const types = Object.values(this.fields);
const offsets = []; // variable-sized values can be interspersed with fixed-sized values
// variable-sized value indices are serialized as offsets, indices deeper in the byte array
let currentIndex = 0;
let nextIndex = 0;
const fixedSection = new DataView(target.buffer, target.byteOffset);
const fixedOffsets = [];
const variableOffsets = [];
let variableIndex = 0;
for (const [i, fieldType] of types.entries()) {
if (fieldType.hasVariableSerializedLength()) {
const offset = fixedSection.getUint32(currentIndex, true);
if (offset > target.length) {
throw new Error("Offset out of bounds");
struct_convertFromJson(data, options) {
if (typeof data !== "object") {
throw new Error("Invalid JSON container: expected Object");
}
variableOffsets.push(offset);
currentIndex = nextIndex = currentIndex + 4;
variableIndex++;
} else {
nextIndex = currentIndex + fieldType.struct_getSerializedLength(null);
fixedOffsets[i] = [currentIndex, nextIndex];
currentIndex = nextIndex;
}
const value = {};
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
const expectedCase = options && options.case;
const expectedFieldName = json_1.toExpectedCase(fieldName, expectedCase);
if (data[expectedFieldName] === undefined) {
throw new Error(`Invalid JSON container field: expected field ${expectedFieldName} is undefined`);
}
value[fieldName] = fieldType.fromJson(data[expectedFieldName], options);
}
return value;
}
variableOffsets.push(target.length);
variableIndex = 0;
for (const [i, fieldType] of types.entries()) {
if (fieldType.hasVariableSerializedLength()) {
if (variableOffsets[variableIndex] > variableOffsets[variableIndex + 1]) {
throw new Error("Offsets must be increasing");
struct_convertToJson(value, options) {
const data = {};
const expectedCase = options && options.case;
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
data[json_1.toExpectedCase(fieldName, expectedCase)] = fieldType.toJson(value[fieldName], options);
}
offsets.push([variableOffsets[variableIndex], variableOffsets[variableIndex + 1]]);
variableIndex++;
} else {
offsets.push(fixedOffsets[i]);
}
return data;
}
return offsets;
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = (0, _persistentMerkleTree.subtreeFillToContents)(Object.values(this.fields).map(fieldType => {
if (!(0, _abstract.isCompositeType)(fieldType)) {
return (0, _persistentMerkleTree.zeroNode)(0);
} else {
return fieldType.tree_defaultNode();
struct_convertToTree(value) {
if (treeValue_1.isTreeBacked(value))
return value.tree.clone();
return new persistent_merkle_tree_1.Tree(persistent_merkle_tree_1.subtreeFillToContents(Object.entries(this.fields).map(([fieldName, fieldType]) => {
if (!abstract_1.isCompositeType(fieldType)) {
const chunk = new Uint8Array(32);
fieldType.toBytes(value[fieldName], chunk, 0);
return new persistent_merkle_tree_1.LeafNode(chunk);
}
else {
return fieldType.struct_convertToTree(value[fieldName]).rootNode;
}
}), this.getChunkDepth()));
}
struct_getPropertyNames() {
return Object.keys(this.fields);
}
bytes_getVariableOffsets(target) {
const types = Object.values(this.fields);
const offsets = [];
// variable-sized values can be interspersed with fixed-sized values
// variable-sized value indices are serialized as offsets, indices deeper in the byte array
let currentIndex = 0;
let nextIndex = 0;
const fixedSection = new DataView(target.buffer, target.byteOffset);
const fixedOffsets = [];
const variableOffsets = [];
let variableIndex = 0;
for (const [i, fieldType] of types.entries()) {
const fixedLen = fieldType.getFixedSerializedLength();
if (fixedLen === null) {
const offset = fixedSection.getUint32(currentIndex, true);
if (offset > target.length) {
throw new Error("Offset out of bounds");
}
variableOffsets.push(offset);
currentIndex = nextIndex = currentIndex + 4;
variableIndex++;
}
else {
nextIndex = currentIndex + fixedLen;
fixedOffsets[i] = [currentIndex, nextIndex];
currentIndex = nextIndex;
}
}
}), this.getChunkDepth());
variableOffsets.push(target.length);
variableIndex = 0;
for (const [i, fieldType] of types.entries()) {
if (fieldType.hasVariableSerializedLength()) {
if (variableOffsets[variableIndex] > variableOffsets[variableIndex + 1]) {
throw new Error("Offsets must be increasing");
}
offsets.push([variableOffsets[variableIndex], variableOffsets[variableIndex + 1]]);
variableIndex++;
}
else {
offsets.push(fixedOffsets[i]);
}
}
return offsets;
}
return this._defaultNode;
}
tree_convertToStruct(target) {
const value = {};
for (const [i, [fieldName, fieldType]] of Object.entries(this.fields).entries()) {
if (!(0, _abstract.isCompositeType)(fieldType)) {
const chunk = this.tree_getRootAtChunkIndex(target, i);
value[fieldName] = fieldType.struct_deserializeFromBytes(chunk, 0);
} else {
const subtree = this.tree_getSubtreeAtChunkIndex(target, i);
value[fieldName] = fieldType.tree_convertToStruct(subtree);
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = persistent_merkle_tree_1.subtreeFillToContents(Object.values(this.fields).map((fieldType) => {
if (!abstract_1.isCompositeType(fieldType)) {
return persistent_merkle_tree_1.zeroNode(0);
}
else {
return fieldType.tree_defaultNode();
}
}), this.getChunkDepth());
}
return this._defaultNode;
}
return value;
}
tree_getSerializedLength(target) {
let s = 0;
for (const [i, fieldType] of Object.values(this.fields).entries()) {
if (fieldType.hasVariableSerializedLength()) {
s += fieldType.tree_getSerializedLength(this.tree_getSubtreeAtChunkIndex(target, i)) + 4;
} else {
s += fieldType.struct_getSerializedLength(null);
}
tree_convertToStruct(target) {
const value = {};
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
const fieldInfo = this.fieldInfos.get(fieldName);
if (fieldInfo.isBasic) {
const chunk = target.getRoot(fieldInfo.gindex);
value[fieldName] = fieldType.struct_deserializeFromBytes(chunk, 0);
}
else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compositeType = fieldType;
const subtree = target.getSubtree(fieldInfo.gindex);
value[fieldName] = compositeType.tree_convertToStruct(subtree);
}
}
return value;
}
return s;
}
tree_deserializeFromBytes(data, start, end) {
const target = this.tree_defaultValue();
const offsets = this.bytes_getVariableOffsets(new Uint8Array(data.buffer, data.byteOffset + start, end - start));
for (const [i, fieldType] of Object.values(this.fields).entries()) {
const [currentOffset, nextOffset] = offsets[i];
if (!(0, _abstract.isCompositeType)(fieldType)) {
// view of the chunk, shared buffer from `data`
const dataChunk = new Uint8Array(data.buffer, data.byteOffset + start + currentOffset, nextOffset - currentOffset);
const chunk = new Uint8Array(32); // copy chunk into new memory
chunk.set(dataChunk);
this.tree_setRootAtChunkIndex(target, i, chunk);
} else {
this.tree_setSubtreeAtChunkIndex(target, i, fieldType.tree_deserializeFromBytes(data, start + currentOffset, start + nextOffset));
}
tree_getSerializedLength(target) {
let s = 0;
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
const fixedLen = fieldType.getFixedSerializedLength();
if (fixedLen === null) {
s +=
fieldType.tree_getSerializedLength(target.getSubtree(this.fieldInfos.get(fieldName).gindex)) + 4;
}
else {
s += fixedLen;
}
}
return s;
}
return target;
}
tree_serializeToBytes(target, output, offset) {
let variableIndex = offset + Object.values(this.fields).reduce((total, fieldType) => total + (fieldType.hasVariableSerializedLength() ? 4 : fieldType.struct_getSerializedLength(null)), 0);
const fixedSection = new DataView(output.buffer, output.byteOffset + offset);
let fixedIndex = offset;
let i = 0;
const fieldTypes = Object.values(this.fields);
for (const node of target.iterateNodesAtDepth(this.getChunkDepth(), i, fieldTypes.length)) {
const fieldType = fieldTypes[i];
if (!(0, _abstract.isCompositeType)(fieldType)) {
const s = fieldType.struct_getSerializedLength();
output.set(node.root.slice(0, s), fixedIndex);
fixedIndex += s;
} else if (fieldType.hasVariableSerializedLength()) {
// write offset
fixedSection.setUint32(fixedIndex - offset, variableIndex - offset, true);
fixedIndex += 4; // write serialized element to variable section
variableIndex = fieldType.tree_serializeToBytes(new _persistentMerkleTree.Tree(node), output, variableIndex);
} else {
fixedIndex = fieldType.tree_serializeToBytes(new _persistentMerkleTree.Tree(node), output, fixedIndex);
}
i++;
tree_deserializeFromBytes(data, start, end) {
const target = this.tree_defaultValue();
const offsets = this.bytes_getVariableOffsets(new Uint8Array(data.buffer, data.byteOffset + start, end - start));
for (const [i, [fieldName, fieldType]] of Object.entries(this.fields).entries()) {
const [currentOffset, nextOffset] = offsets[i];
const { isBasic, gindex } = this.fieldInfos.get(fieldName);
if (isBasic) {
// view of the chunk, shared buffer from `data`
const dataChunk = new Uint8Array(data.buffer, data.byteOffset + start + currentOffset, nextOffset - currentOffset);
const chunk = new Uint8Array(32);
// copy chunk into new memory
chunk.set(dataChunk);
target.setRoot(gindex, chunk);
}
else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compositeType = fieldType;
target.setSubtree(gindex, compositeType.tree_deserializeFromBytes(data, start + currentOffset, start + nextOffset));
}
}
return target;
}
return variableIndex;
}
getPropertyGindex(prop) {
const chunkIndex = Object.keys(this.fields).findIndex(fieldName => fieldName === prop);
if (chunkIndex === -1) {
throw new Error("Invalid container field name: ".concat(String(prop)));
tree_serializeToBytes(target, output, offset) {
let variableIndex = offset;
for (const fieldType of Object.values(this.fields)) {
const fixedLen = fieldType.getFixedSerializedLength();
variableIndex += fixedLen === null ? 4 : fixedLen;
}
const fixedSection = new DataView(output.buffer, output.byteOffset + offset);
let fixedIndex = offset;
const fieldTypes = Object.values(this.fields);
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, fieldTypes.length);
for (let i = 0; i < fieldTypes.length; i++) {
const fieldType = fieldTypes[i];
const node = nodes[i];
if (!abstract_1.isCompositeType(fieldType)) {
const s = fieldType.struct_getSerializedLength();
output.set(node.root.slice(0, s), fixedIndex);
fixedIndex += s;
}
else if (fieldType.hasVariableSerializedLength()) {
// write offset
fixedSection.setUint32(fixedIndex - offset, variableIndex - offset, true);
fixedIndex += 4;
// write serialized element to variable section
variableIndex = fieldType.tree_serializeToBytes(new persistent_merkle_tree_1.Tree(node), output, variableIndex);
}
else {
fixedIndex = fieldType.tree_serializeToBytes(new persistent_merkle_tree_1.Tree(node), output, fixedIndex);
}
}
return variableIndex;
}
return this.getGindexAtChunkIndex(chunkIndex);
}
getPropertyType(prop) {
const type = this.fields[prop];
if (!type) {
throw new Error("Invalid container field name: ".concat(String(prop)));
getPropertyGindex(prop) {
const fieldInfo = this.fieldInfos.get(prop);
if (!fieldInfo) {
throw new Error(`Invalid container field name: ${String(prop)}`);
}
return fieldInfo.gindex;
}
return type;
}
tree_getPropertyNames() {
return Object.keys(this.fields);
}
tree_getProperty(target, prop) {
const chunkIndex = Object.keys(this.fields).findIndex(fieldName => fieldName === prop);
if (chunkIndex === -1) {
return undefined;
getPropertyType(prop) {
const type = this.fields[prop];
if (!type) {
throw new Error(`Invalid container field name: ${String(prop)}`);
}
return type;
}
const fieldType = this.fields[prop];
if (!(0, _abstract.isCompositeType)(fieldType)) {
const chunk = this.tree_getRootAtChunkIndex(target, chunkIndex);
return fieldType.struct_deserializeFromBytes(chunk, 0);
} else {
return this.tree_getSubtreeAtChunkIndex(target, chunkIndex);
tree_getPropertyNames() {
return Object.keys(this.fields);
}
}
tree_setProperty(target, property, value) {
const chunkIndex = Object.keys(this.fields).findIndex(fieldName => fieldName === property);
if (chunkIndex === -1) {
throw new Error("Invalid container field name");
tree_getProperty(target, prop) {
const fieldType = this.fields[prop];
const fieldInfo = this.fieldInfos.get(prop);
if (!fieldInfo) {
return undefined;
}
const { isBasic, gindex } = fieldInfo;
if (isBasic) {
const chunk = target.getRoot(gindex);
return fieldType.struct_deserializeFromBytes(chunk, 0);
}
else {
return target.getSubtree(gindex);
}
}
const chunkGindex = this.getGindexAtChunkIndex(chunkIndex);
const fieldType = this.fields[property];
if (!(0, _abstract.isCompositeType)(fieldType)) {
const chunk = new Uint8Array(32);
fieldType.struct_serializeToBytes(value, chunk, 0);
target.setRoot(chunkGindex, chunk);
return true;
} else {
target.setSubtree(chunkGindex, value);
return true;
tree_setProperty(target, property, value) {
const fieldType = this.fields[property];
const fieldInfo = this.fieldInfos.get(property);
if (!fieldInfo) {
throw new Error("Invalid container field name");
}
const { isBasic, gindex } = fieldInfo;
if (isBasic) {
const chunk = new Uint8Array(32);
fieldType.struct_serializeToBytes(value, chunk, 0);
target.setRoot(gindex, chunk);
return true;
}
else {
target.setSubtree(gindex, value);
return true;
}
}
}
tree_deleteProperty(target, prop) {
const chunkIndex = Object.keys(this.fields).findIndex(fieldName => fieldName === prop);
if (chunkIndex === -1) {
throw new Error("Invalid container field name");
tree_deleteProperty(target, prop) {
const fieldInfo = this.fieldInfos.get(prop);
if (!fieldInfo) {
throw new Error("Invalid container field name");
}
const fieldType = this.fields[prop];
if (fieldInfo.isBasic) {
return this.tree_setProperty(target, prop, fieldType.struct_defaultValue());
}
else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compositeType = fieldType;
return this.tree_setProperty(target, prop, compositeType.tree_defaultValue());
}
}
const fieldType = this.fields[prop];
if (!(0, _abstract.isCompositeType)(fieldType)) {
return this.tree_setProperty(target, prop, fieldType.struct_defaultValue());
} else {
return this.tree_setProperty(target, prop, fieldType.tree_defaultValue());
*tree_iterateValues(target) {
const gindexIterator = persistent_merkle_tree_1.iterateAtDepth(this.getChunkDepth(), BigInt(0), BigInt(this.getMaxChunkCount()))[Symbol.iterator]();
for (const propType of Object.values(this.fields)) {
const { value, done } = gindexIterator.next();
if (done) {
return;
}
else {
if (!abstract_1.isCompositeType(propType)) {
yield propType.struct_deserializeFromBytes(value.root, 0);
}
else {
yield target.getSubtree(value);
}
}
}
}
}
*tree_iterateValues(target) {
const gindexIterator = (0, _persistentMerkleTree.iterateAtDepth)(this.getChunkDepth(), BigInt(0), BigInt(this.getMaxChunkCount()))[Symbol.iterator]();
for (const propType of Object.values(this.fields)) {
const {
value,
done
} = gindexIterator.next();
if (done) {
return;
} else {
if (!(0, _abstract.isCompositeType)(propType)) {
yield propType.struct_deserializeFromBytes(value.root, 0);
} else {
yield target.getSubtree(value);
*tree_readonlyIterateValues(target) {
const fieldTypes = Object.values(this.fields);
const nodes = target.getNodesAtDepth(this.getChunkDepth(), 0, fieldTypes.length);
for (let i = 0; i < fieldTypes.length; i++) {
const fieldType = fieldTypes[i];
const node = nodes[i];
if (!abstract_1.isCompositeType(fieldType)) {
yield fieldType.struct_deserializeFromBytes(node.root, 0);
}
else {
yield new persistent_merkle_tree_1.Tree(node);
}
}
}
}
}
*tree_readonlyIterateValues(target) {
const chunkIterator = target.iterateNodesAtDepth(this.getChunkDepth(), 0, this.getMaxChunkCount());
for (const propType of Object.values(this.fields)) {
const {
value,
done
} = chunkIterator.next();
if (done) {
return;
} else {
if (!(0, _abstract.isCompositeType)(propType)) {
yield propType.struct_deserializeFromBytes(value.root, 0);
} else {
yield new _persistentMerkleTree.Tree(value);
hasVariableSerializedLength() {
return Object.values(this.fields).some((fieldType) => fieldType.hasVariableSerializedLength());
}
getFixedSerializedLength() {
let fixedLen = 0;
for (const fieldType of Object.values(this.fields)) {
const fieldFixedLen = fieldType.getFixedSerializedLength();
if (fieldFixedLen === null) {
return null;
}
else {
fixedLen += fieldFixedLen;
}
}
}
return fixedLen;
}
}
hasVariableSerializedLength() {
return Object.values(this.fields).some(fieldType => fieldType.hasVariableSerializedLength());
}
getMaxChunkCount() {
return Object.keys(this.fields).length;
}
tree_getLeafGindices(target, root = BigInt(1)) {
const gindices = [];
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
const fieldGindex = this.getPropertyGindex(fieldName);
const extendedFieldGindex = (0, _persistentMerkleTree.concatGindices)([root, fieldGindex]);
if (!(0, _abstract.isCompositeType)(fieldType)) {
gindices.push(extendedFieldGindex);
} else {
if (fieldType.hasVariableSerializedLength()) {
if (!target) {
throw new Error("variable type requires tree argument to get leaves");
}
gindices.push(...fieldType.tree_getLeafGindices(target.getSubtree(fieldGindex), extendedFieldGindex));
} else {
gindices.push(...fieldType.tree_getLeafGindices(undefined, extendedFieldGindex));
getMaxChunkCount() {
return Object.keys(this.fields).length;
}
tree_getLeafGindices(target, root = BigInt(1)) {
const gindices = [];
for (const [fieldName, fieldType] of Object.entries(this.fields)) {
const { gindex: fieldGindex, isBasic } = this.fieldInfos.get(fieldName);
const extendedFieldGindex = persistent_merkle_tree_1.concatGindices([root, fieldGindex]);
if (isBasic) {
gindices.push(extendedFieldGindex);
}
else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const compositeType = fieldType;
if (fieldType.hasVariableSerializedLength()) {
if (!target) {
throw new Error("variable type requires tree argument to get leaves");
}
gindices.push(...compositeType.tree_getLeafGindices(target.getSubtree(fieldGindex), extendedFieldGindex));
}
else {
gindices.push(...compositeType.tree_getLeafGindices(undefined, extendedFieldGindex));
}
}
}
}
return gindices;
}
return gindices;
}
}
exports.ContainerType = ContainerType;
//# sourceMappingURL=container.js.map

@@ -10,1 +10,2 @@ export * from "./abstract";

export * from "./container";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _abstract = require("./abstract");
Object.keys(_abstract).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _abstract[key];
}
});
});
var _array = require("./array");
Object.keys(_array).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _array[key];
}
});
});
var _vector = require("./vector");
Object.keys(_vector).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _vector[key];
}
});
});
var _list = require("./list");
Object.keys(_list).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _list[key];
}
});
});
var _bitVector = require("./bitVector");
Object.keys(_bitVector).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _bitVector[key];
}
});
});
var _bitList = require("./bitList");
Object.keys(_bitList).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _bitList[key];
}
});
});
var _byteVector = require("./byteVector");
Object.keys(_byteVector).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _byteVector[key];
}
});
});
var _root = require("./root");
Object.keys(_root).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _root[key];
}
});
});
var _container = require("./container");
Object.keys(_container).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _container[key];
}
});
});
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./abstract"), exports);
__exportStar(require("./array"), exports);
__exportStar(require("./vector"), exports);
__exportStar(require("./list"), exports);
__exportStar(require("./bitVector"), exports);
__exportStar(require("./bitList"), exports);
__exportStar(require("./byteVector"), exports);
__exportStar(require("./root"), exports);
__exportStar(require("./container"), exports);
//# sourceMappingURL=index.js.map

@@ -51,9 +51,11 @@ import { Json, List } from "../../interface";

hasVariableSerializedLength(): boolean;
getFixedSerializedLength(): null | number;
getMaxChunkCount(): number;
tree_getLeafGindices(target?: Tree, root?: Gindex): Gindex[];
}
export declare class CompositeListType<T extends List<object> = List<object>> extends CompositeArrayType<T> {
export declare class CompositeListType<T extends List<unknown> = List<unknown>> extends CompositeArrayType<T> {
limit: number;
constructor(options: IListOptions);
hasVariableSerializedLength(): boolean;
getFixedSerializedLength(): null | number;
getMaxChunkCount(): number;

@@ -84,1 +86,2 @@ struct_defaultValue(): T;

export {};
//# sourceMappingURL=list.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isListType = isListType;
exports.CompositeListType = exports.BasicListType = exports.ListType = exports.LIST_TYPE = exports.LENGTH_GINDEX = void 0;
var _array = require("./array");
var _basic = require("../basic");
var _type = require("../type");
var _compat = require("../../util/compat");
var _persistentMerkleTree = require("@chainsafe/persistent-merkle-tree");
var _treeValue = require("../../backings/tree/treeValue");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeListType = exports.BasicListType = exports.ListType = exports.isListType = exports.LIST_TYPE = exports.LENGTH_GINDEX = void 0;
const array_1 = require("./array");
const basic_1 = require("../basic");
const type_1 = require("../type");
const compat_1 = require("../../util/compat");
const persistent_merkle_tree_1 = require("@chainsafe/persistent-merkle-tree");
const treeValue_1 = require("../../backings/tree/treeValue");
/**

@@ -32,425 +19,343 @@ * SSZ Lists (variable-length arrays) include the length of the list in the tree

*/
const LENGTH_GINDEX = BigInt(3);
exports.LENGTH_GINDEX = LENGTH_GINDEX;
const LIST_TYPE = Symbol.for("ssz/ListType"); // eslint-disable-next-line @typescript-eslint/no-explicit-any
exports.LIST_TYPE = LIST_TYPE;
exports.LENGTH_GINDEX = BigInt(3);
exports.LIST_TYPE = Symbol.for("ssz/ListType");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isListType(type) {
return (0, _type.isTypeOf)(type, LIST_TYPE);
} // Trick typescript into treating ListType as a constructor
const ListType = // eslint-disable-next-line @typescript-eslint/no-explicit-any
return type_1.isTypeOf(type, exports.LIST_TYPE);
}
exports.isListType = isListType;
// Trick typescript into treating ListType as a constructor
exports.ListType =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function ListType(options) {
if ((0, _basic.isBasicType)(options.elementType)) {
return new BasicListType(options);
} else {
return new CompositeListType(options);
}
if (basic_1.isBasicType(options.elementType)) {
return new BasicListType(options);
}
else {
return new CompositeListType(options);
}
};
exports.ListType = ListType;
class BasicListType extends _array.BasicArrayType {
constructor(options) {
super(options);
_defineProperty(this, "limit", void 0);
this.limit = options.limit;
this._typeSymbols.add(LIST_TYPE);
}
struct_defaultValue() {
return [];
}
struct_getLength(value) {
return value.length;
}
getMaxLength() {
return this.limit;
}
getMinLength() {
return 0;
}
bytes_validate(data, start, end) {
super.bytes_validate(data, start, end);
if (end - start > this.getMaxSerializedLength()) {
throw new Error("Deserialized list length greater than limit");
class BasicListType extends array_1.BasicArrayType {
limit;
constructor(options) {
super(options);
this.limit = options.limit;
this._typeSymbols.add(exports.LIST_TYPE);
}
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
return super.struct_deserializeFromBytes(data, start, end);
}
struct_getChunkCount(value) {
return Math.ceil(value.length * this.elementType.struct_getSerializedLength() / 32);
}
struct_hashTreeRoot(value) {
return (0, _compat.mixInLength)(super.struct_hashTreeRoot(value), value.length);
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_convertFromJson(data, options) {
if (!Array.isArray(data)) {
throw new Error("Invalid JSON list: expected an Array");
struct_defaultValue() {
return [];
}
const maxLength = this.limit;
if (data.length > maxLength) {
throw new Error("Invalid JSON list: length ".concat(data.length, " greater than limit ").concat(maxLength));
struct_getLength(value) {
return value.length;
}
return super.struct_convertFromJson(data);
}
struct_convertToTree(value) {
if ((0, _treeValue.isTreeBacked)(value)) return value.tree.clone();
const tree = super.struct_convertToTree(value);
this.tree_setLength(tree, value.length);
return tree;
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = new _persistentMerkleTree.BranchNode((0, _persistentMerkleTree.zeroNode)(super.getChunkDepth()), (0, _persistentMerkleTree.zeroNode)(0));
getMaxLength() {
return this.limit;
}
return this._defaultNode;
}
tree_defaultValue() {
return new _persistentMerkleTree.Tree(this.tree_defaultNode());
}
tree_getLength(target) {
return _basic.number32Type.struct_deserializeFromBytes(target.getRoot(LENGTH_GINDEX), 0);
}
tree_setLength(target, length) {
const chunk = new Uint8Array(32);
_basic.number32Type.toBytes(length, chunk, 0);
target.setRoot(LENGTH_GINDEX, chunk);
}
tree_deserializeFromBytes(data, start, end) {
const length = (end - start) / this.elementType.struct_getSerializedLength();
if (!Number.isSafeInteger(length)) {
throw new Error("Deserialized list byte length must be divisible by element size");
getMinLength() {
return 0;
}
if (length > this.limit) {
throw new Error("Deserialized list length greater than limit");
bytes_validate(data, start, end) {
super.bytes_validate(data, start, end);
if (end - start > this.getMaxSerializedLength()) {
throw new Error("Deserialized list length greater than limit");
}
}
const value = super.tree_deserializeFromBytes(data, start, end);
this.tree_setLength(value, length);
return value;
}
tree_getChunkCount(target) {
return Math.ceil(this.tree_getLength(target) * this.elementType.struct_getSerializedLength() / 32);
}
getChunkDepth() {
return super.getChunkDepth() + 1;
}
tree_setProperty(target, property, value) {
const length = this.tree_getLength(target);
if (property > length) {
throw new Error("Invalid length index");
} else if (property == length) {
this.tree_pushSingle(target, value);
return true;
} else {
return this.tree_setValueAtIndex(target, property, value);
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
return super.struct_deserializeFromBytes(data, start, end);
}
}
tree_deleteProperty(target, property) {
const length = this.tree_getLength(target);
if (property > length) {
throw new Error("Invalid length index");
} else if (property == length) {
this.tree_pop(target);
return true;
} else {
return super.tree_deleteProperty(target, property);
struct_getChunkCount(value) {
return Math.ceil((value.length * this.elementType.struct_getSerializedLength()) / 32);
}
}
tree_pushSingle(target, value) {
const length = this.tree_getLength(target);
const expand = this.getChunkIndex(length) != this.getChunkIndex(length + 1);
this.tree_setValueAtIndex(target, length, value, expand);
this.tree_setLength(target, length + 1);
return length + 1;
}
tree_push(target, ...values) {
let newLength;
for (const value of values) newLength = this.tree_pushSingle(target, value);
return newLength;
}
tree_pop(target) {
const length = this.tree_getLength(target);
const value = this.tree_getProperty(target, length - 1);
super.tree_deleteProperty(target, length - 1);
this.tree_setLength(target, length - 1);
return value;
}
hasVariableSerializedLength() {
return true;
}
getMaxChunkCount() {
return Math.ceil(this.limit * this.elementType.size() / 32);
}
tree_getLeafGindices(target, root = BigInt(1)) {
if (!target) {
throw new Error("variable type requires tree argument to get leaves");
struct_hashTreeRoot(value) {
return compat_1.mixInLength(super.struct_hashTreeRoot(value), value.length);
}
const gindices = super.tree_getLeafGindices(target, root); // include the length chunk
gindices.push((0, _persistentMerkleTree.concatGindices)([root, LENGTH_GINDEX]));
return gindices;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_convertFromJson(data, options) {
if (!Array.isArray(data)) {
throw new Error("Invalid JSON list: expected an Array");
}
const maxLength = this.limit;
if (data.length > maxLength) {
throw new Error(`Invalid JSON list: length ${data.length} greater than limit ${maxLength}`);
}
return super.struct_convertFromJson(data);
}
struct_convertToTree(value) {
if (treeValue_1.isTreeBacked(value))
return value.tree.clone();
const tree = super.struct_convertToTree(value);
this.tree_setLength(tree, value.length);
return tree;
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = new persistent_merkle_tree_1.BranchNode(persistent_merkle_tree_1.zeroNode(super.getChunkDepth()), persistent_merkle_tree_1.zeroNode(0));
}
return this._defaultNode;
}
tree_defaultValue() {
return new persistent_merkle_tree_1.Tree(this.tree_defaultNode());
}
tree_getLength(target) {
return basic_1.number32Type.struct_deserializeFromBytes(target.getRoot(exports.LENGTH_GINDEX), 0);
}
tree_setLength(target, length) {
const chunk = new Uint8Array(32);
basic_1.number32Type.toBytes(length, chunk, 0);
target.setRoot(exports.LENGTH_GINDEX, chunk);
}
tree_deserializeFromBytes(data, start, end) {
const length = (end - start) / this.elementType.struct_getSerializedLength();
if (!Number.isSafeInteger(length)) {
throw new Error("Deserialized list byte length must be divisible by element size");
}
if (length > this.limit) {
throw new Error("Deserialized list length greater than limit");
}
const value = super.tree_deserializeFromBytes(data, start, end);
this.tree_setLength(value, length);
return value;
}
tree_getChunkCount(target) {
return Math.ceil((this.tree_getLength(target) * this.elementType.struct_getSerializedLength()) / 32);
}
getChunkDepth() {
return super.getChunkDepth() + 1;
}
tree_setProperty(target, property, value) {
const length = this.tree_getLength(target);
if (property > length) {
throw new Error("Invalid length index");
}
else if (property == length) {
this.tree_pushSingle(target, value);
return true;
}
else {
return this.tree_setValueAtIndex(target, property, value);
}
}
tree_deleteProperty(target, property) {
const length = this.tree_getLength(target);
if (property > length) {
throw new Error("Invalid length index");
}
else if (property == length) {
this.tree_pop(target);
return true;
}
else {
return super.tree_deleteProperty(target, property);
}
}
tree_pushSingle(target, value) {
const length = this.tree_getLength(target);
const expand = this.getChunkIndex(length) != this.getChunkIndex(length + 1);
this.tree_setValueAtIndex(target, length, value, expand);
this.tree_setLength(target, length + 1);
return length + 1;
}
tree_push(target, ...values) {
let newLength;
for (const value of values)
newLength = this.tree_pushSingle(target, value);
return newLength || this.tree_getLength(target);
}
tree_pop(target) {
const length = this.tree_getLength(target);
const value = this.tree_getProperty(target, length - 1);
super.tree_deleteProperty(target, length - 1);
this.tree_setLength(target, length - 1);
return value;
}
hasVariableSerializedLength() {
return true;
}
getFixedSerializedLength() {
return null;
}
getMaxChunkCount() {
return Math.ceil((this.limit * this.elementType.size()) / 32);
}
tree_getLeafGindices(target, root = BigInt(1)) {
if (!target) {
throw new Error("variable type requires tree argument to get leaves");
}
const gindices = super.tree_getLeafGindices(target, root);
// include the length chunk
gindices.push(persistent_merkle_tree_1.concatGindices([root, exports.LENGTH_GINDEX]));
return gindices;
}
}
exports.BasicListType = BasicListType;
class CompositeListType extends _array.CompositeArrayType {
constructor(options) {
super(options);
_defineProperty(this, "limit", void 0);
this.limit = options.limit;
this._typeSymbols.add(LIST_TYPE);
}
hasVariableSerializedLength() {
return true;
}
getMaxChunkCount() {
return this.limit;
}
struct_defaultValue() {
return [];
}
struct_getLength(value) {
return value.length;
}
getMaxLength() {
return this.limit;
}
getMinLength() {
return 0;
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const value = super.struct_deserializeFromBytes(data, start, end);
if (value.length > this.limit) {
throw new Error("Deserialized list length greater than limit: ".concat(value.length, " ").concat(this.limit));
class CompositeListType extends array_1.CompositeArrayType {
limit;
constructor(options) {
super(options);
this.limit = options.limit;
this._typeSymbols.add(exports.LIST_TYPE);
}
return value;
}
struct_getChunkCount(value) {
return value.length;
}
struct_hashTreeRoot(value) {
return (0, _compat.mixInLength)(super.struct_hashTreeRoot(value), value.length);
}
struct_convertFromJson(data, options) {
if (!Array.isArray(data)) {
throw new Error("Invalid JSON list: expected an Array");
hasVariableSerializedLength() {
return true;
}
const maxLength = this.limit;
if (data.length > maxLength) {
throw new Error("Invalid JSON list: length ".concat(data.length, " greater than limit ").concat(maxLength));
getFixedSerializedLength() {
return null;
}
return super.struct_convertFromJson(data, options);
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = new _persistentMerkleTree.BranchNode((0, _persistentMerkleTree.zeroNode)(super.getChunkDepth()), (0, _persistentMerkleTree.zeroNode)(0));
getMaxChunkCount() {
return this.limit;
}
return this._defaultNode;
}
tree_defaultValue() {
return new _persistentMerkleTree.Tree(this.tree_defaultNode());
}
struct_convertToTree(value) {
if ((0, _treeValue.isTreeBacked)(value)) return value.tree.clone();
const tree = super.struct_convertToTree(value);
this.tree_setLength(tree, value.length);
return tree;
}
tree_getLength(target) {
return _basic.number32Type.struct_deserializeFromBytes(target.getRoot(LENGTH_GINDEX), 0);
}
tree_setLength(target, length) {
const chunk = new Uint8Array(32);
_basic.number32Type.struct_serializeToBytes(length, chunk, 0);
target.setRoot(LENGTH_GINDEX, chunk);
}
tree_deserializeFromBytes(data, start, end) {
const target = this.tree_defaultValue();
if (this.elementType.hasVariableSerializedLength()) {
const offsets = this.bytes_getVariableOffsets(new Uint8Array(data.buffer, data.byteOffset + start, end - start));
if (offsets.length > this.limit) {
throw new Error("Deserialized list length greater than limit");
}
for (let i = 0; i < offsets.length; i++) {
const [currentOffset, nextOffset] = offsets[i];
this.tree_setSubtreeAtChunkIndex(target, i, this.elementType.tree_deserializeFromBytes(data, start + currentOffset, start + nextOffset));
}
this.tree_setLength(target, offsets.length);
} else {
const elementSize = this.elementType.struct_getSerializedLength(null);
const length = (end - start) / elementSize;
if (!Number.isSafeInteger(length)) {
throw new Error("Deserialized list byte length must be divisible by element size");
}
if (length > this.limit) {
throw new Error("Deserialized list length greater than limit");
}
for (let i = 0; i < length; i++) {
this.tree_setSubtreeAtChunkIndex(target, i, this.elementType.tree_deserializeFromBytes(data, start + i * elementSize, start + (i + 1) * elementSize), true // expand tree as needed
);
}
this.tree_setLength(target, length);
struct_defaultValue() {
return [];
}
return target;
}
tree_getChunkCount(target) {
return this.tree_getLength(target);
}
getChunkDepth() {
return super.getChunkDepth() + 1;
}
tree_setProperty(target, property, value) {
const length = this.tree_getLength(target);
if (property > length) {
throw new Error("Invalid length index");
} else if (property == length) {
this.tree_pushSingle(target, value);
} else {
this.tree_setSubtreeAtChunkIndex(target, property, value);
struct_getLength(value) {
return value.length;
}
return true;
}
tree_deleteProperty(target, property) {
const length = this.tree_getLength(target);
if (property > length) {
throw new Error("Invalid length index");
} else if (property == length) {
this.tree_pop(target);
return true;
} else {
return super.tree_deleteProperty(target, property);
getMaxLength() {
return this.limit;
}
}
tree_pushSingle(target, value) {
const length = this.tree_getLength(target);
this.tree_setSubtreeAtChunkIndex(target, length, value, true);
this.tree_setLength(target, length + 1);
return length + 1;
}
tree_push(target, ...values) {
let newLength;
for (const value of values) newLength = this.tree_pushSingle(target, value);
return newLength;
}
tree_pop(target) {
const length = this.tree_getLength(target);
const value = this.tree_getProperty(target, length - 1);
this.tree_setSubtreeAtChunkIndex(target, length - 1, new _persistentMerkleTree.Tree((0, _persistentMerkleTree.zeroNode)(0)));
this.tree_setLength(target, length - 1);
return value;
}
tree_getLeafGindices(target, root = BigInt(1)) {
if (!target) {
throw new Error("variable type requires tree argument to get leaves");
getMinLength() {
return 0;
}
const gindices = super.tree_getLeafGindices(target, root); // include the length chunk
gindices.push((0, _persistentMerkleTree.concatGindices)([root, LENGTH_GINDEX]));
return gindices;
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const value = super.struct_deserializeFromBytes(data, start, end);
if (value.length > this.limit) {
throw new Error(`Deserialized list length greater than limit: ${value.length} ${this.limit}`);
}
return value;
}
struct_getChunkCount(value) {
return value.length;
}
struct_hashTreeRoot(value) {
return compat_1.mixInLength(super.struct_hashTreeRoot(value), value.length);
}
struct_convertFromJson(data, options) {
if (!Array.isArray(data)) {
throw new Error("Invalid JSON list: expected an Array");
}
const maxLength = this.limit;
if (data.length > maxLength) {
throw new Error(`Invalid JSON list: length ${data.length} greater than limit ${maxLength}`);
}
return super.struct_convertFromJson(data, options);
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = new persistent_merkle_tree_1.BranchNode(persistent_merkle_tree_1.zeroNode(super.getChunkDepth()), persistent_merkle_tree_1.zeroNode(0));
}
return this._defaultNode;
}
tree_defaultValue() {
return new persistent_merkle_tree_1.Tree(this.tree_defaultNode());
}
struct_convertToTree(value) {
if (treeValue_1.isTreeBacked(value))
return value.tree.clone();
const tree = super.struct_convertToTree(value);
this.tree_setLength(tree, value.length);
return tree;
}
tree_getLength(target) {
return basic_1.number32Type.struct_deserializeFromBytes(target.getRoot(exports.LENGTH_GINDEX), 0);
}
tree_setLength(target, length) {
const chunk = new Uint8Array(32);
basic_1.number32Type.struct_serializeToBytes(length, chunk, 0);
target.setRoot(exports.LENGTH_GINDEX, chunk);
}
tree_deserializeFromBytes(data, start, end) {
const target = this.tree_defaultValue();
const fixedLen = this.elementType.getFixedSerializedLength();
if (fixedLen === null) {
const offsets = this.bytes_getVariableOffsets(new Uint8Array(data.buffer, data.byteOffset + start, end - start));
if (offsets.length > this.limit) {
throw new Error("Deserialized list length greater than limit");
}
for (let i = 0; i < offsets.length; i++) {
const [currentOffset, nextOffset] = offsets[i];
this.tree_setSubtreeAtChunkIndex(target, i, this.elementType.tree_deserializeFromBytes(data, start + currentOffset, start + nextOffset));
}
this.tree_setLength(target, offsets.length);
}
else {
const elementSize = fixedLen;
const length = (end - start) / elementSize;
if (!Number.isSafeInteger(length)) {
throw new Error("Deserialized list byte length must be divisible by element size");
}
if (length > this.limit) {
throw new Error("Deserialized list length greater than limit");
}
for (let i = 0; i < length; i++) {
this.tree_setSubtreeAtChunkIndex(target, i, this.elementType.tree_deserializeFromBytes(data, start + i * elementSize, start + (i + 1) * elementSize), true // expand tree as needed
);
}
this.tree_setLength(target, length);
}
return target;
}
tree_getChunkCount(target) {
return this.tree_getLength(target);
}
getChunkDepth() {
return super.getChunkDepth() + 1;
}
tree_setProperty(target, property, value) {
const length = this.tree_getLength(target);
if (property > length) {
throw new Error("Invalid length index");
}
else if (property == length) {
this.tree_pushSingle(target, value);
}
else {
this.tree_setSubtreeAtChunkIndex(target, property, value);
}
return true;
}
tree_deleteProperty(target, property) {
const length = this.tree_getLength(target);
if (property > length) {
throw new Error("Invalid length index");
}
else if (property == length) {
this.tree_pop(target);
return true;
}
else {
return super.tree_deleteProperty(target, property);
}
}
tree_pushSingle(target, value) {
const length = this.tree_getLength(target);
this.tree_setSubtreeAtChunkIndex(target, length, value, true);
this.tree_setLength(target, length + 1);
return length + 1;
}
tree_push(target, ...values) {
let newLength;
for (const value of values)
newLength = this.tree_pushSingle(target, value);
return newLength || this.tree_getLength(target);
}
tree_pop(target) {
const length = this.tree_getLength(target);
const value = this.tree_getProperty(target, length - 1);
this.tree_setSubtreeAtChunkIndex(target, length - 1, new persistent_merkle_tree_1.Tree(persistent_merkle_tree_1.zeroNode(0)));
this.tree_setLength(target, length - 1);
return value;
}
tree_getLeafGindices(target, root = BigInt(1)) {
if (!target) {
throw new Error("variable type requires tree argument to get leaves");
}
const gindices = super.tree_getLeafGindices(target, root);
// include the length chunk
gindices.push(persistent_merkle_tree_1.concatGindices([root, exports.LENGTH_GINDEX]));
return gindices;
}
}
exports.CompositeListType = CompositeListType;
//# sourceMappingURL=list.js.map

@@ -21,1 +21,2 @@ import { ByteVectorType } from "./byteVector";

}
//# sourceMappingURL=root.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isRootType = isRootType;
exports.RootType = exports.ROOT_TYPE = void 0;
var _byteVector = require("./byteVector");
var _type = require("../type");
var _backings = require("../../backings");
var _byteArray = require("../../util/byteArray");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const ROOT_TYPE = Symbol.for("ssz/RootType");
exports.ROOT_TYPE = ROOT_TYPE;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RootType = exports.isRootType = exports.ROOT_TYPE = void 0;
const byteVector_1 = require("./byteVector");
const type_1 = require("../type");
const backings_1 = require("../../backings");
const byteArray_1 = require("../../util/byteArray");
exports.ROOT_TYPE = Symbol.for("ssz/RootType");
function isRootType(type) {
return (0, _type.isTypeOf)(type, ROOT_TYPE);
return type_1.isTypeOf(type, exports.ROOT_TYPE);
}
exports.isRootType = isRootType;
function convertRootToUint8Array(value) {
if (value instanceof Uint8Array) {
return value;
} else if ((0, _backings.isTreeBacked)(value)) {
return value.tree.root;
} else if (Array.isArray(value)) {
return new Uint8Array(value);
} else {
throw new Error("Unable to convert root to Uint8Array: not Uint8Array, tree-backed, or Array");
}
if (value instanceof Uint8Array) {
return value;
}
else if (backings_1.isTreeBacked(value)) {
return value.tree.root;
}
else if (Array.isArray(value)) {
return new Uint8Array(value);
}
else {
throw new Error("Unable to convert root to Uint8Array: not Uint8Array, tree-backed, or Array");
}
}
class RootType extends _byteVector.ByteVectorType {
constructor(options) {
super({
length: 32
});
_defineProperty(this, "_expandedType", void 0);
this._expandedType = options.expandedType;
this._typeSymbols.add(ROOT_TYPE);
}
get expandedType() {
if (typeof this._expandedType === "function") {
this._expandedType = this._expandedType();
class RootType extends byteVector_1.ByteVectorType {
_expandedType;
constructor(options) {
super({ length: 32 });
this._expandedType = options.expandedType;
this._typeSymbols.add(exports.ROOT_TYPE);
}
return this._expandedType;
}
struct_equals(value1, value2) {
return (0, _byteArray.byteArrayEquals)(convertRootToUint8Array(value1), convertRootToUint8Array(value2));
}
equals(value1, value2) {
return this.struct_equals(value1, value2);
}
get expandedType() {
if (typeof this._expandedType === "function") {
this._expandedType = this._expandedType();
}
return this._expandedType;
}
struct_equals(value1, value2) {
return byteArray_1.byteArrayEquals(convertRootToUint8Array(value1), convertRootToUint8Array(value2));
}
equals(value1, value2) {
return this.struct_equals(value1, value2);
}
}
exports.RootType = RootType;
//# sourceMappingURL=root.js.map

@@ -32,5 +32,6 @@ import { Json, Vector } from "../../interface";

hasVariableSerializedLength(): boolean;
getFixedSerializedLength(): null | number;
getMaxChunkCount(): number;
}
export declare class CompositeVectorType<T extends Vector<object> = Vector<object>> extends CompositeArrayType<T> {
export declare class CompositeVectorType<T extends Vector<unknown> = Vector<unknown>> extends CompositeArrayType<T> {
length: number;

@@ -51,4 +52,6 @@ constructor(options: IVectorOptions);

hasVariableSerializedLength(): boolean;
getFixedSerializedLength(): null | number;
getMaxChunkCount(): number;
}
export {};
//# sourceMappingURL=vector.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeVectorType = exports.BasicVectorType = exports.VectorType = exports.isVectorType = exports.VECTOR_TYPE = void 0;
const array_1 = require("./array");
const basic_1 = require("../basic");
const type_1 = require("../type");
const persistent_merkle_tree_1 = require("@chainsafe/persistent-merkle-tree");
exports.VECTOR_TYPE = Symbol.for("ssz/VectorType");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isVectorType(type) {
return type_1.isTypeOf(type, exports.VECTOR_TYPE);
}
exports.isVectorType = isVectorType;
exports.CompositeVectorType = exports.BasicVectorType = exports.VectorType = exports.VECTOR_TYPE = void 0;
var _array = require("./array");
var _basic = require("../basic");
var _type = require("../type");
var _persistentMerkleTree = require("@chainsafe/persistent-merkle-tree");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const VECTOR_TYPE = Symbol.for("ssz/VectorType"); // eslint-disable-next-line @typescript-eslint/no-explicit-any
exports.VECTOR_TYPE = VECTOR_TYPE;
function isVectorType(type) {
return (0, _type.isTypeOf)(type, VECTOR_TYPE);
} // eslint-disable-next-line @typescript-eslint/no-explicit-any
// Trick typescript into treating VectorType as a constructor
const VectorType = // eslint-disable-next-line @typescript-eslint/no-explicit-any
exports.VectorType =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function VectorType(options) {
if ((0, _basic.isBasicType)(options.elementType)) {
return new BasicVectorType(options);
} else {
return new CompositeVectorType(options);
}
if (basic_1.isBasicType(options.elementType)) {
return new BasicVectorType(options);
}
else {
return new CompositeVectorType(options);
}
};
exports.VectorType = VectorType;
class BasicVectorType extends _array.BasicArrayType {
constructor(options) {
super(options);
_defineProperty(this, "length", void 0);
this.length = options.length;
this._typeSymbols.add(VECTOR_TYPE);
}
struct_defaultValue() {
return Array.from({
length: this.length
}, () => {
return this.elementType.struct_defaultValue();
});
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getLength(value) {
return this.length;
}
getMaxLength() {
return this.length;
}
getMinLength() {
return this.length;
}
bytes_validate(data, start, end) {
super.bytes_validate(data, start, end);
if (end - start !== this.size(null)) {
throw new Error("Incorrect deserialized vector length");
class BasicVectorType extends array_1.BasicArrayType {
length;
constructor(options) {
super(options);
this.length = options.length;
this._typeSymbols.add(exports.VECTOR_TYPE);
}
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
return super.struct_deserializeFromBytes(data, start, end);
}
struct_assertValidValue(value) {
const actualLength = value.length;
const expectedLength = this.struct_getLength(value);
if (actualLength !== expectedLength) {
throw new Error("Invalid vector length: expected ".concat(expectedLength, ", actual ").concat(actualLength));
struct_defaultValue() {
return Array.from({ length: this.length }, () => {
return this.elementType.struct_defaultValue();
});
}
super.struct_assertValidValue(value);
}
struct_convertFromJson(data) {
if (!Array.isArray(data)) {
throw new Error("Invalid JSON vector: expected an Array");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getLength(value) {
return this.length;
}
const expectedLength = this.length;
if (data.length !== expectedLength) {
throw new Error("Invalid JSON vector length: expected ".concat(expectedLength, ", actual ").concat(data.length));
getMaxLength() {
return this.length;
}
return super.struct_convertFromJson(data);
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = (0, _persistentMerkleTree.subtreeFillToLength)((0, _persistentMerkleTree.zeroNode)(0), this.getChunkDepth(), this.getMaxChunkCount());
getMinLength() {
return this.length;
}
return this._defaultNode;
}
tree_defaultValue() {
return new _persistentMerkleTree.Tree(this.tree_defaultNode());
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
tree_getLength(target) {
return this.length;
}
tree_deserializeFromBytes(data, start, end) {
if (end - start !== this.struct_getSerializedLength(null)) {
throw new Error("Incorrect deserialized vector length");
bytes_validate(data, start, end) {
super.bytes_validate(data, start, end);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (end - start !== this.size(null)) {
throw new Error("Incorrect deserialized vector length");
}
}
return super.tree_deserializeFromBytes(data, start, end);
}
tree_setProperty(target, property, value) {
if (property >= this.tree_getLength(target)) {
throw new Error("Invalid array index");
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
return super.struct_deserializeFromBytes(data, start, end);
}
return super.tree_setProperty(target, property, value, false);
}
hasVariableSerializedLength() {
return false;
}
getMaxChunkCount() {
return Math.ceil(this.length * this.elementType.size() / 32);
}
struct_assertValidValue(value) {
const actualLength = value.length;
const expectedLength = this.struct_getLength(value);
if (actualLength !== expectedLength) {
throw new Error(`Invalid vector length: expected ${expectedLength}, actual ${actualLength}`);
}
super.struct_assertValidValue(value);
}
struct_convertFromJson(data) {
if (!Array.isArray(data)) {
throw new Error("Invalid JSON vector: expected an Array");
}
const expectedLength = this.length;
if (data.length !== expectedLength) {
throw new Error(`Invalid JSON vector length: expected ${expectedLength}, actual ${data.length}`);
}
return super.struct_convertFromJson(data);
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = persistent_merkle_tree_1.subtreeFillToLength(persistent_merkle_tree_1.zeroNode(0), this.getChunkDepth(), this.getMaxChunkCount());
}
return this._defaultNode;
}
tree_defaultValue() {
return new persistent_merkle_tree_1.Tree(this.tree_defaultNode());
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
tree_getLength(target) {
return this.length;
}
tree_deserializeFromBytes(data, start, end) {
const fixedLen = this.getFixedSerializedLength();
if (end - start !== fixedLen) {
throw new Error("Incorrect deserialized vector length");
}
return super.tree_deserializeFromBytes(data, start, end);
}
tree_setProperty(target, property, value) {
if (property >= this.tree_getLength(target)) {
throw new Error("Invalid array index");
}
return super.tree_setProperty(target, property, value, false);
}
hasVariableSerializedLength() {
return false;
}
getFixedSerializedLength() {
return this.length * this.elementType.size();
}
getMaxChunkCount() {
return Math.ceil((this.length * this.elementType.size()) / 32);
}
}
exports.BasicVectorType = BasicVectorType;
class CompositeVectorType extends _array.CompositeArrayType {
constructor(options) {
super(options);
_defineProperty(this, "length", void 0);
this.length = options.length;
this._typeSymbols.add(VECTOR_TYPE);
}
struct_defaultValue() {
return Array.from({
length: this.length
}, () => {
return this.elementType.struct_defaultValue();
});
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getLength(value) {
return this.length;
}
getMaxLength() {
return this.length;
}
getMinLength() {
return this.length;
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const value = super.struct_deserializeFromBytes(data, start, end);
if (value.length !== this.length) {
throw new Error("Incorrect deserialized vector length");
class CompositeVectorType extends array_1.CompositeArrayType {
length;
constructor(options) {
super(options);
this.length = options.length;
this._typeSymbols.add(exports.VECTOR_TYPE);
}
return value;
}
struct_assertValidValue(value) {
const actualLength = value.length;
const expectedLength = this.struct_getLength(value);
if (actualLength !== expectedLength) {
throw new Error("Invalid vector length: expected ".concat(expectedLength, ", actual ").concat(actualLength));
struct_defaultValue() {
return Array.from({ length: this.length }, () => {
return this.elementType.struct_defaultValue();
});
}
super.struct_assertValidValue(value);
}
struct_convertFromJson(data) {
if (!Array.isArray(data)) {
throw new Error("Invalid JSON vector: expected an Array");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
struct_getLength(value) {
return this.length;
}
const expectedLength = this.length;
if (data.length !== expectedLength) {
throw new Error("Invalid JSON vector length: expected ".concat(expectedLength, ", actual ").concat(data.length));
getMaxLength() {
return this.length;
}
return super.struct_convertFromJson(data);
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = (0, _persistentMerkleTree.subtreeFillToLength)(this.elementType.tree_defaultNode(), this.getChunkDepth(), this.length);
getMinLength() {
return this.length;
}
return this._defaultNode;
}
tree_defaultValue() {
return new _persistentMerkleTree.Tree(this.tree_defaultNode());
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
tree_getLength(target) {
return this.length;
}
tree_deserializeFromBytes(data, start, end) {
const target = this.tree_defaultValue();
if (this.elementType.hasVariableSerializedLength()) {
const offsets = this.bytes_getVariableOffsets(new Uint8Array(data.buffer, data.byteOffset + start, end - start));
if (offsets.length !== this.length) {
throw new Error("Incorrect deserialized vector length");
}
for (let i = 0; i < offsets.length; i++) {
const [currentOffset, nextOffset] = offsets[i];
this.tree_setSubtreeAtChunkIndex(target, i, this.elementType.tree_deserializeFromBytes(data, start + currentOffset, start + nextOffset));
}
} else {
const elementSize = this.elementType.struct_getSerializedLength(null);
const length = (end - start) / elementSize;
if (length !== this.length) {
throw new Error("Incorrect deserialized vector length");
}
for (let i = 0; i < length; i++) {
this.tree_setSubtreeAtChunkIndex(target, i, this.elementType.tree_deserializeFromBytes(data, start + i * elementSize, start + (i + 1) * elementSize));
}
struct_deserializeFromBytes(data, start, end) {
this.bytes_validate(data, start, end);
const value = super.struct_deserializeFromBytes(data, start, end);
if (value.length !== this.length) {
throw new Error("Incorrect deserialized vector length");
}
return value;
}
return target;
}
setProperty(target, property, value) {
if (property >= this.tree_getLength(target)) {
throw new Error("Invalid array index");
struct_assertValidValue(value) {
const actualLength = value.length;
const expectedLength = this.struct_getLength(value);
if (actualLength !== expectedLength) {
throw new Error(`Invalid vector length: expected ${expectedLength}, actual ${actualLength}`);
}
super.struct_assertValidValue(value);
}
return super.tree_setProperty(target, property, value, false);
}
hasVariableSerializedLength() {
return this.elementType.hasVariableSerializedLength();
}
getMaxChunkCount() {
return this.length;
}
struct_convertFromJson(data) {
if (!Array.isArray(data)) {
throw new Error("Invalid JSON vector: expected an Array");
}
const expectedLength = this.length;
if (data.length !== expectedLength) {
throw new Error(`Invalid JSON vector length: expected ${expectedLength}, actual ${data.length}`);
}
return super.struct_convertFromJson(data);
}
tree_defaultNode() {
if (!this._defaultNode) {
this._defaultNode = persistent_merkle_tree_1.subtreeFillToLength(this.elementType.tree_defaultNode(), this.getChunkDepth(), this.length);
}
return this._defaultNode;
}
tree_defaultValue() {
return new persistent_merkle_tree_1.Tree(this.tree_defaultNode());
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
tree_getLength(target) {
return this.length;
}
tree_deserializeFromBytes(data, start, end) {
const target = this.tree_defaultValue();
const fixedLen = this.elementType.getFixedSerializedLength();
if (fixedLen === null) {
const offsets = this.bytes_getVariableOffsets(new Uint8Array(data.buffer, data.byteOffset + start, end - start));
if (offsets.length !== this.length) {
throw new Error("Incorrect deserialized vector length");
}
for (let i = 0; i < offsets.length; i++) {
const [currentOffset, nextOffset] = offsets[i];
this.tree_setSubtreeAtChunkIndex(target, i, this.elementType.tree_deserializeFromBytes(data, start + currentOffset, start + nextOffset));
}
}
else {
const elementSize = fixedLen;
const length = (end - start) / elementSize;
if (length !== this.length) {
throw new Error("Incorrect deserialized vector length");
}
for (let i = 0; i < length; i++) {
this.tree_setSubtreeAtChunkIndex(target, i, this.elementType.tree_deserializeFromBytes(data, start + i * elementSize, start + (i + 1) * elementSize));
}
}
return target;
}
setProperty(target, property, value) {
if (property >= this.tree_getLength(target)) {
throw new Error("Invalid array index");
}
return super.tree_setProperty(target, property, value, false);
}
hasVariableSerializedLength() {
return this.elementType.hasVariableSerializedLength();
}
getFixedSerializedLength() {
const elementFixedLen = this.elementType.getFixedSerializedLength();
if (elementFixedLen === null) {
return null;
}
else {
return this.length * elementFixedLen;
}
}
getMaxChunkCount() {
return this.length;
}
}
exports.CompositeVectorType = CompositeVectorType;
//# sourceMappingURL=vector.js.map

@@ -46,1 +46,2 @@ /**

export * from "./type";
//# sourceMappingURL=index.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _basic = require("./basic");
Object.keys(_basic).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _basic[key];
}
});
});
var _composite = require("./composite");
Object.keys(_composite).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _composite[key];
}
});
});
var _type = require("./type");
Object.keys(_type).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _type[key];
}
});
});
/**
* SSZ is a type system that defines:
* - efficient serialization / deserialization
* - stable merkleization
* - default constructor
*
* Along with these standardized operations, we provide:
* - equality
* - valid value assertion
* - copy / clone
* - serialized byte length (for serialization)
* - chunk count (for merkleization)
*
* This library operates on values of several kinds of 'backings', or underlying representations of data.
* Each backing has runtime tradeoffs for the above operations that arise from the nature of the underlying
* representation. Effort has been made to minimize the differences between backings for the core API, which
* includes the above operations, property getter/setters, and iteration (value iteration for vectors/lists
* and enumerable key iteration for containers).
*
* We support the following backings, which correspond to the core operations of serialization and merkleization:
*
* - Structural - This backing has a native javascript type representation.
* Containers are constructed as js Objects, vectors and lists as Arrays (or TypedArrays)
* Within operations, property access is performed using js getter notation, with gets
* corresponding to the structure of the value's type. Because structural non-constructor operations do not
* assume the underlying representation of values, all backings can be operated on in this context.
*
* - Tree - This backing has an immutable merkle tree representation.
* The data is always represented as a tree, and within operations, the tree
* structure is harnessed as much as possible. Property getters return subtrees except for basic types,
* when the native value corresponding th that type is returned.
* Values backed by a tree are wrapped in an ES6 Proxy object to provide a convenient, 'structural' interface
* for property getters/setters.
*
* - ByteArray - This backing has a byte array representation.
* The data is always represented as a Uint8Array, and within operations,
* the serialized structure is harnessed as much as possible.
* Property getters return sub-arrays except for basic types, when the native value
* corresponding to that type is returned.
* Values backed by an array are wrapped in an ES6 Proxy object to provide a convenient, 'structural' interface
* for property getters/setters.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./basic"), exports);
__exportStar(require("./composite"), exports);
__exportStar(require("./type"), exports);
//# sourceMappingURL=index.js.map

@@ -55,2 +55,6 @@ import { Json } from "../interface";

/**
* if hasVariableSerializedLength() === true, returns null. Otherwise returns a length value
*/
abstract getFixedSerializedLength(): null | number;
/**
* Maximal serialized byte length

@@ -98,1 +102,2 @@ */

}
//# sourceMappingURL=type.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isTypeOf = isTypeOf;
exports.Type = void 0;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/* eslint-disable @typescript-eslint/member-ordering */
/* eslint-disable @typescript-eslint/camelcase */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Type = exports.isTypeOf = void 0;
/**

@@ -21,129 +11,96 @@ * Check if `type` is an instance of `typeSymbol` type

function isTypeOf(type, typeSymbol) {
return type._typeSymbols.has(typeSymbol);
return type._typeSymbols.has(typeSymbol);
}
exports.isTypeOf = isTypeOf;
/**
* A Type is either a BasicType of a CompositeType
*/
class Type {
/**
* Symbols used to track the identity of a type
*
* Used by various isFooType functions
*/
constructor() {
_defineProperty(this, "_typeSymbols", void 0);
this._typeSymbols = new Set();
}
/**
* Valid value assertion
*/
assertValidValue(value) {
return this.struct_assertValidValue(value);
}
/**
* Default constructor
*/
defaultValue() {
return this.struct_defaultValue();
}
/**
* Clone / copy
*/
clone(value) {
return this.struct_clone(value);
}
/**
* Equality
*/
equals(value1, value2) {
return this.struct_equals(value1, value2);
} // Serialization / Deserialization
/**
* Check if type has a variable number of elements (or subelements)
*
* For basic types, this is always false
*/
/**
* Serialized byte length
*/
size(value) {
return this.struct_getSerializedLength(value);
}
/**
* Low-level deserialization
*/
fromBytes(data, start, end) {
return this.struct_deserializeFromBytes(data, start, end);
}
/**
* Deserialization
*/
deserialize(data) {
return this.fromBytes(data, 0, data.length);
}
/**
* Low-level serialization
*
* Serializes to a pre-allocated Uint8Array
*/
toBytes(value, output, offset) {
return this.struct_serializeToBytes(value, output, offset);
}
/**
* Serialization
*/
serialize(value) {
const output = new Uint8Array(this.size(value));
this.toBytes(value, output, 0);
return output;
}
/**
* Merkleization
*/
hashTreeRoot(value) {
return this.struct_hashTreeRoot(value);
}
/**
* Convert from JSON-serializable object
*/
fromJson(data, options) {
return this.struct_convertFromJson(data, options);
}
/**
* Convert to JSON-serializable object
*/
toJson(value, options) {
return this.struct_convertToJson(value, options);
}
/**
* Symbols used to track the identity of a type
*
* Used by various isFooType functions
*/
_typeSymbols;
constructor() {
this._typeSymbols = new Set();
}
/**
* Valid value assertion
*/
assertValidValue(value) {
return this.struct_assertValidValue(value);
}
/**
* Default constructor
*/
defaultValue() {
return this.struct_defaultValue();
}
/**
* Clone / copy
*/
clone(value) {
return this.struct_clone(value);
}
/**
* Equality
*/
equals(value1, value2) {
return this.struct_equals(value1, value2);
}
/**
* Serialized byte length
*/
size(value) {
return this.struct_getSerializedLength(value);
}
/**
* Low-level deserialization
*/
fromBytes(data, start, end) {
return this.struct_deserializeFromBytes(data, start, end);
}
/**
* Deserialization
*/
deserialize(data) {
return this.fromBytes(data, 0, data.length);
}
/**
* Low-level serialization
*
* Serializes to a pre-allocated Uint8Array
*/
toBytes(value, output, offset) {
return this.struct_serializeToBytes(value, output, offset);
}
/**
* Serialization
*/
serialize(value) {
const output = new Uint8Array(this.size(value));
this.toBytes(value, output, 0);
return output;
}
/**
* Merkleization
*/
hashTreeRoot(value) {
return this.struct_hashTreeRoot(value);
}
/**
* Convert from JSON-serializable object
*/
fromJson(data, options) {
return this.struct_convertFromJson(data, options);
}
/**
* Convert to JSON-serializable object
*/
toJson(value, options) {
return this.struct_convertToJson(value, options);
}
}
exports.Type = Type;
//# sourceMappingURL=type.js.map
export declare function bigIntPow(base: bigint, exponent: bigint): bigint;
//# sourceMappingURL=bigInt.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.bigIntPow = bigIntPow;
Object.defineProperty(exports, "__esModule", { value: true });
exports.bigIntPow = void 0;
function bigIntPow(base, exponent) {
if (exponent < 0) {
throw new RangeError("Exponent must be positive");
}
let out = BigInt(1);
for (; exponent > 0; exponent--) {
out *= base;
}
return out;
if (exponent < 0) {
throw new RangeError("Exponent must be positive");
}
let out = BigInt(1);
for (; exponent > 0; exponent--) {
out *= base;
}
return out;
}
exports.bigIntPow = bigIntPow;
//# sourceMappingURL=bigInt.js.map

@@ -6,1 +6,2 @@ import { ByteVector } from "../interface";

export declare function getByteBits(target: Uint8Array, offset: number): boolean[];
//# sourceMappingURL=byteArray.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toHexString = toHexString;
exports.fromHexString = fromHexString;
exports.byteArrayEquals = byteArrayEquals;
exports.getByteBits = getByteBits;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getByteBits = exports.byteArrayEquals = exports.fromHexString = exports.toHexString = void 0;
// Caching this info costs about ~1000 bytes and speeds up toHexString() by x6
const hexByByte = [];
function toHexString(bytes) {
let hex = "0x";
for (const byte of bytes) {
if (!hexByByte[byte]) {
hexByByte[byte] = byte < 16 ? "0" + byte.toString(16) : byte.toString(16);
let hex = "0x";
for (const byte of bytes) {
if (!hexByByte[byte]) {
hexByByte[byte] = byte < 16 ? "0" + byte.toString(16) : byte.toString(16);
}
hex += hexByByte[byte];
}
hex += hexByByte[byte];
}
return hex;
return hex;
}
exports.toHexString = toHexString;
function fromHexString(hex) {
if (typeof hex !== "string") {
throw new Error("Expected hex string to be a string");
}
if (hex.startsWith("0x")) {
hex = hex.slice(2);
}
if (hex.length % 2 !== 0) {
throw new Error("Expected an even number of characters");
}
const bytes = [];
for (let i = 0, len = hex.length; i < len; i += 2) {
const byte = parseInt(hex.slice(i, i + 2), 16);
bytes.push(byte);
}
return new Uint8Array(bytes);
if (typeof hex !== "string") {
throw new Error("Expected hex string to be a string");
}
if (hex.startsWith("0x")) {
hex = hex.slice(2);
}
if (hex.length % 2 !== 0) {
throw new Error("Expected an even number of characters");
}
const bytes = [];
for (let i = 0, len = hex.length; i < len; i += 2) {
const byte = parseInt(hex.slice(i, i + 2), 16);
bytes.push(byte);
}
return new Uint8Array(bytes);
}
exports.fromHexString = fromHexString;
function byteArrayEquals(a, b) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i])
return false;
}
return true;
}
exports.byteArrayEquals = byteArrayEquals;
function getByteBits(target, offset) {
const byte = target[offset];
if (!byte) {
return [false, false, false, false, false, false, false, false];
}
const bits = Array.prototype.map.call(byte.toString(2).padStart(8, "0"), c => c === "1" ? true : false).reverse();
return bits;
const byte = target[offset];
if (!byte) {
return [false, false, false, false, false, false, false, false];
}
const bits = Array.prototype.map
.call(byte.toString(2).padStart(8, "0"), (c) => (c === "1" ? true : false))
.reverse();
return bits;
}
exports.getByteBits = getByteBits;
//# sourceMappingURL=byteArray.js.map
export declare function hash(...inputs: Uint8Array[]): Uint8Array;
export declare function merkleize(chunks: Iterable<Uint8Array>, padTo: number): Uint8Array;
export declare function mixInLength(root: Uint8Array, length: number): Uint8Array;
//# sourceMappingURL=compat.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hash = hash;
exports.merkleize = merkleize;
exports.mixInLength = mixInLength;
var _hash2 = require("./hash");
var _merkleize2 = require("./merkleize");
Object.defineProperty(exports, "__esModule", { value: true });
exports.mixInLength = exports.merkleize = exports.hash = void 0;
const hash_1 = require("./hash");
const merkleize_1 = require("./merkleize");
function hash(...inputs) {
return Uint8Array.from((0, _hash2.hash)(...inputs.map(Buffer.from)));
return Uint8Array.from(hash_1.hash(...inputs.map(Buffer.from)));
}
exports.hash = hash;
function merkleize(chunks, padTo) {
return (0, _merkleize2.merkleize)(Array.from(chunks).map(Buffer.from), padTo);
return merkleize_1.merkleize(Array.from(chunks).map(Buffer.from), padTo);
}
exports.merkleize = merkleize;
function mixInLength(root, length) {
const lengthBuf = Buffer.alloc(32);
lengthBuf.writeUIntLE(length, 0, 6);
return hash(root, lengthBuf);
const lengthBuf = Buffer.alloc(32);
lengthBuf.writeUIntLE(length, 0, 6);
return hash(root, lengthBuf);
}
exports.mixInLength = mixInLength;
//# sourceMappingURL=compat.js.map

@@ -6,1 +6,2 @@ /** @module ssz */

export declare const BYTES_PER_LENGTH_PREFIX = 4;
//# sourceMappingURL=constants.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
/** @module ssz */
Object.defineProperty(exports, "__esModule", { value: true });
exports.BYTES_PER_LENGTH_PREFIX = exports.BYTES_PER_CHUNK = void 0;
/** @module ssz */
// Number of bytes per chunk.
/** @ignore */
const BYTES_PER_CHUNK = 32; // Number of bytes per serialized length prefix.
exports.BYTES_PER_CHUNK = 32;
// Number of bytes per serialized length prefix.
/** @ignore */
exports.BYTES_PER_CHUNK = BYTES_PER_CHUNK;
const BYTES_PER_LENGTH_PREFIX = 4;
exports.BYTES_PER_LENGTH_PREFIX = BYTES_PER_LENGTH_PREFIX;
exports.BYTES_PER_LENGTH_PREFIX = 4;
//# sourceMappingURL=constants.js.map

@@ -17,1 +17,2 @@ declare type JsonPathItem = string | number;

export {};
//# sourceMappingURL=errorPath.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.renderJsonPath = renderJsonPath;
exports.SszErrorPath = void 0;
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderJsonPath = exports.SszErrorPath = void 0;
/**

@@ -15,17 +8,14 @@ * Tracks the JSON path location of nested errors

class SszErrorPath extends Error {
constructor(e, keyOrIndex) {
const prevJsonPath = e instanceof SszErrorPath ? e.jsonPath : [];
const jsonPath = [keyOrIndex, ...prevJsonPath];
const rawMessage = e instanceof SszErrorPath ? e.rawMessage : e.message;
super("".concat(renderJsonPath(jsonPath), ": ").concat(rawMessage));
_defineProperty(this, "jsonPath", void 0);
_defineProperty(this, "rawMessage", void 0);
this.jsonPath = jsonPath;
this.rawMessage = rawMessage;
}
jsonPath;
rawMessage;
constructor(e, keyOrIndex) {
const prevJsonPath = e instanceof SszErrorPath ? e.jsonPath : [];
const jsonPath = [keyOrIndex, ...prevJsonPath];
const rawMessage = e instanceof SszErrorPath ? e.rawMessage : e.message;
super(`${renderJsonPath(jsonPath)}: ${rawMessage}`);
this.jsonPath = jsonPath;
this.rawMessage = rawMessage;
}
}
exports.SszErrorPath = SszErrorPath;
/**

@@ -36,24 +26,18 @@ * Render an array of JSON path items

*/
exports.SszErrorPath = SszErrorPath;
function renderJsonPath(jsonPath) {
let path = "";
for (const item of jsonPath) {
switch (typeof item) {
case "number":
path += "[".concat(item, "]");
break;
case "string":
default:
path += path.length > 0 ? ".".concat(item) : item;
break;
let path = "";
for (const item of jsonPath) {
switch (typeof item) {
case "number":
path += `[${item}]`;
break;
case "string":
default:
path += path.length > 0 ? `.${item}` : item;
break;
}
}
}
return path;
return path;
}
exports.renderJsonPath = renderJsonPath;
//# sourceMappingURL=errorPath.js.map

@@ -5,1 +5,2 @@ /**

export declare function hash(...inputs: Uint8Array[]): Uint8Array;
//# sourceMappingURL=hash.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hash = hash;
var _asSha = _interopRequireDefault(require("@chainsafe/as-sha256"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hash = void 0;
/** @module ssz */
const as_sha256_1 = __importDefault(require("@chainsafe/as-sha256"));
/**

@@ -18,4 +13,5 @@ * Hash used for hashTreeRoot

function hash(...inputs) {
return _asSha.default.digest(Buffer.concat(inputs));
return as_sha256_1.default.digest(Buffer.concat(inputs));
}
exports.hash = hash;
//# sourceMappingURL=hash.js.map
import { IJsonOptions } from "../types";
export declare function toExpectedCase(value: string, expectedCase?: IJsonOptions["case"]): string;
//# sourceMappingURL=json.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toExpectedCase = toExpectedCase;
var _case = _interopRequireDefault(require("case"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toExpectedCase = void 0;
const case_1 = __importDefault(require("case"));
function toExpectedCase(value, expectedCase = "camel") {
switch (expectedCase) {
case "camel":
return _case.default.camel(value);
case "snake":
return _case.default.snake(value);
default:
return value;
}
switch (expectedCase) {
case "camel":
return case_1.default.camel(value);
case "snake":
return case_1.default.snake(value);
default:
return value;
}
}
exports.toExpectedCase = toExpectedCase;
//# sourceMappingURL=json.js.map

@@ -7,1 +7,2 @@ /** @ignore */

export declare function previousPowerOf2(n: number): number;
//# sourceMappingURL=math.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.bitLength = bitLength;
exports.nextPowerOf2 = nextPowerOf2;
exports.previousPowerOf2 = previousPowerOf2;
Object.defineProperty(exports, "__esModule", { value: true });
exports.previousPowerOf2 = exports.nextPowerOf2 = exports.bitLength = void 0;
/** @ignore */
function bitLength(n) {
const bitstring = n.toString(2);
if (bitstring === "0") {
return 0;
}
return bitstring.length;
const bitstring = n.toString(2);
if (bitstring === "0") {
return 0;
}
return bitstring.length;
}
exports.bitLength = bitLength;
/** @ignore */
function nextPowerOf2(n) {
return n <= 0 ? 1 : Math.pow(2, bitLength(n - 1));
return n <= 0 ? 1 : Math.pow(2, bitLength(n - 1));
}
exports.nextPowerOf2 = nextPowerOf2;
/** @ignore */
function previousPowerOf2(n) {
return n === 0 ? 1 : Math.pow(2, bitLength(n) - 1);
return n === 0 ? 1 : Math.pow(2, bitLength(n) - 1);
}
exports.previousPowerOf2 = previousPowerOf2;
//# sourceMappingURL=math.js.map

@@ -6,1 +6,2 @@ /// <reference types="node" />

export declare function mixInLength(root: Buffer, length: number): Buffer;
//# sourceMappingURL=merkleize.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.merkleize = merkleize;
exports.mixInLength = mixInLength;
var _hash = require("./hash");
var _math = require("./math");
var _zeros = require("./zeros");
Object.defineProperty(exports, "__esModule", { value: true });
exports.mixInLength = exports.merkleize = void 0;
/** @module ssz */
const hash_1 = require("./hash");
const math_1 = require("./math");
const zeros_1 = require("./zeros");
/** @ignore */
function merkleize(chunks, padFor = 0) {
const layerCount = (0, _math.bitLength)((0, _math.nextPowerOf2)(padFor || chunks.length) - 1);
if (chunks.length == 0) {
return _zeros.zeroHashes[layerCount];
} // Instead of pushing on all padding zero chunks at the leaf level
// we push on zero hash chunks at the highest possible level to avoid over-hashing
let layer = 0;
while (layer < layerCount) {
// if the chunks.length is odd
// we need to push on the zero-hash of that level to merkleize that level
if (chunks.length % 2 == 1) {
chunks.push(_zeros.zeroHashes[layer]);
const layerCount = math_1.bitLength(math_1.nextPowerOf2(padFor || chunks.length) - 1);
if (chunks.length == 0) {
return zeros_1.zeroHashes[layerCount];
}
for (let i = 0; i < chunks.length; i += 2) {
const h = (0, _hash.hash)(chunks[i], chunks[i + 1]);
chunks[i / 2] = Buffer.from(h.buffer, h.byteOffset, h.byteLength);
// Instead of pushing on all padding zero chunks at the leaf level
// we push on zero hash chunks at the highest possible level to avoid over-hashing
let layer = 0;
while (layer < layerCount) {
// if the chunks.length is odd
// we need to push on the zero-hash of that level to merkleize that level
if (chunks.length % 2 == 1) {
chunks.push(zeros_1.zeroHashes[layer]);
}
for (let i = 0; i < chunks.length; i += 2) {
const h = hash_1.hash(chunks[i], chunks[i + 1]);
chunks[i / 2] = Buffer.from(h.buffer, h.byteOffset, h.byteLength);
}
chunks.splice(chunks.length / 2, chunks.length / 2);
layer++;
}
chunks.splice(chunks.length / 2, chunks.length / 2);
layer++;
}
return chunks[0];
return chunks[0];
}
exports.merkleize = merkleize;
/** @ignore */
function mixInLength(root, length) {
const lengthBuf = Buffer.alloc(32);
lengthBuf.writeUIntLE(length, 0, 6);
const h = (0, _hash.hash)(root, lengthBuf);
return Buffer.from(h.buffer, h.byteOffset, h.byteLength);
const lengthBuf = Buffer.alloc(32);
lengthBuf.writeUIntLE(length, 0, 6);
const h = hash_1.hash(root, lengthBuf);
return Buffer.from(h.buffer, h.byteOffset, h.byteLength);
}
exports.mixInLength = mixInLength;
//# sourceMappingURL=merkleize.js.map
import { Tree } from "@chainsafe/persistent-merkle-tree";
export declare function isTree(value: unknown): value is Tree;
//# sourceMappingURL=tree.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isTree = isTree;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isTree = void 0;
function isTree(value) {
return Boolean(value.rootNode && value.rootNode.isLeaf);
return Boolean(value.rootNode && value.rootNode.isLeaf);
}
exports.isTree = isTree;
//# sourceMappingURL=tree.js.map
/// <reference types="node" />
export declare const zeroHashes: Buffer[];
//# sourceMappingURL=zeros.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "__esModule", { value: true });
exports.zeroHashes = void 0;
var _constants = require("./constants");
var _hash = require("./hash");
/** @module ssz */
const constants_1 = require("./constants");
const hash_1 = require("./hash");
// create array of "zero hashes", successively hashed zero chunks
const zeroHashes = [Buffer.alloc(_constants.BYTES_PER_CHUNK)];
exports.zeroHashes = zeroHashes;
exports.zeroHashes = [Buffer.alloc(constants_1.BYTES_PER_CHUNK)];
for (let i = 0; i < 52; i++) {
const h = (0, _hash.hash)(zeroHashes[i], zeroHashes[i]);
zeroHashes.push(Buffer.from(h.buffer, h.byteOffset, h.byteLength));
const h = hash_1.hash(exports.zeroHashes[i], exports.zeroHashes[i]);
exports.zeroHashes.push(Buffer.from(h.buffer, h.byteOffset, h.byteLength));
}
//# sourceMappingURL=zeros.js.map
export {};
//# sourceMappingURL=web.d.ts.map
"use strict";
var ssz = _interopRequireWildcard(require("./index"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/** @module ssz */
const ssz = __importStar(require("./index"));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(function (window) {
window.ssz = ssz;
window.ssz = ssz;
})(window);
//# sourceMappingURL=web.js.map

@@ -7,3 +7,3 @@ {

"homepage": "https://github.com/chainsafe/ssz",
"version": "0.8.13",
"version": "0.8.15",
"main": "lib/index.js",

@@ -16,5 +16,3 @@ "files": [

"scripts": {
"build": "yarn build-lib && yarn build-types",
"build-lib": "babel src -x .ts -d lib --source-maps",
"build-types": "tsc --incremental --declaration --outDir lib --emitDeclarationOnly",
"build": "tsc -p tsconfig.build.json",
"build-web": "webpack --mode production --entry ./lib/web.js --output ./dist/ssz.min.js",

@@ -25,39 +23,38 @@ "build:docs": "typedoc --exclude src/index.ts,src/web.ts --out docs src",

"clean": "rm -rf lib && rm -rf dist && rm -f tsconfig.tsbuildinfo",
"lint": "eslint --color --ext .ts src/",
"lint:fix": "eslint --color --fix --ext .ts src/",
"lint": "eslint --color --ext .ts src/ test/",
"lint:fix": "yarn run lint --fix",
"prepublishOnly": "yarn build",
"pretest": "yarn check-types",
"test": "yarn test:unit",
"test:perf": "mocha --colors -r ts-node/register \"test/perf/**/*.test.ts\"",
"test:unit": "nyc -e .ts mocha --colors -r ts-node/register \"test/unit/**/*.test.ts\""
"benchmark": "node --max-old-space-size=4096 --expose-gc -r ts-node/register ./node_modules/.bin/benchmark 'test/perf/*.test.ts'",
"benchmark:local": "yarn benchmark --local",
"test:perf": "mocha \"test/perf/**/*.test.ts\"",
"test:unit": "nyc -e .ts mocha \"test/unit/**/*.test.ts\"",
"test:spec": "mocha \"test/spec/**/*.test.ts\"",
"download-spec-tests": "node -r ts-node/register test/spec/downloadTests.ts"
},
"types": "lib/index.d.ts",
"dependencies": {
"@chainsafe/as-sha256": "^0.2.2",
"@chainsafe/persistent-merkle-tree": "^0.3.3",
"@chainsafe/as-sha256": "^0.2.4",
"@chainsafe/persistent-merkle-tree": "^0.3.5",
"case": "^1.6.3"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.4",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
"@babel/plugin-syntax-bigint": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"@babel/preset-typescript": "^7.8.3",
"@babel/register": "^7.8.3",
"@chainsafe/lodestar-params": "^0.28.1",
"@chainsafe/lodestar-spec-test-util": "^0.28.1",
"@dapplion/benchmark": "^0.1.6",
"@types/chai": "^4.2.9",
"@types/mocha": "^7.0.1",
"@types/node": "^13.7.4",
"@typescript-eslint/eslint-plugin": "^2.20.0",
"@typescript-eslint/parser": "^2.20.0",
"@types/mocha": "^9.0.0",
"@types/node": "^14.14.17",
"@typescript-eslint/eslint-plugin": "4.9.0",
"@typescript-eslint/parser": "4.9.0",
"chai": "^4.2.0",
"eslint": "^6.8.0",
"eslint": "^7.14.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.4",
"mocha": "^7.0.1",
"mocha": "^8.3.0",
"nyc": "^15.0.0",
"prettier": "^2.0.5",
"ts-node": "^8.6.2",
"typescript": "^3.8.2"
"ts-node": "^9.1.1",
"typescript": "^4.2.0"
},

@@ -64,0 +61,0 @@ "keywords": [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc