Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function _equals(value1: any, value2: any, type: FullSSZType): boolean {
switch (type.type) {
case Type.uint:
if(value1 === Infinity || value2 === Infinity) {
return value1 === value2;
}
return (new BN(value1)).eq(new BN(value2));
case Type.bool:
return value1 === value2;
case Type.bitList:
case Type.bitVector:
case Type.byteList:
case Type.byteVector:
return value1.equals(value2);
case Type.list:
return value1.length === value2.length &&
value1.every((element1: any, i: number) => equals(element1, value2[i], type.elementType));
case Type.vector:
return value1.every((element1: any, i: number) => equals(element1, value2[i], type.elementType));
function _expandYamlValue(value: any, type: FullSSZType): any {
switch(type.type) {
case Type.uint:
if (
type.byteLength > 6
&& type.useNumber
&& value.toArrayLike(Buffer, type.byteLength).equals(Buffer.alloc(type.byteLength, 255))
) {
return Infinity;
}
return type.useNumber ? value.toNumber() : new BN(value);
case Type.bool:
return value;
case Type.bitList:
return BitList.deserialize(Buffer.from(value.slice(2), "hex"));
case Type.bitVector:
return BitVector.fromBitfield(Buffer.from(value.slice(2), "hex"), type.length);
case Type.byteList:
case Type.byteVector:
export function chunkCount(type: FullSSZType): number {
switch (type.type) {
case Type.uint:
case Type.bool:
return 1;
case Type.bitList:
return Math.floor((type.maxLength + 255) / 256);
case Type.bitVector:
return Math.floor((type.length + 255) / 256);
case Type.byteList:
return Math.floor((type.maxLength * itemLength(byte) + 31) / 32);
case Type.byteVector:
return Math.floor((type.length * itemLength(byte) + 31) / 32);
case Type.list:
return Math.floor((type.maxLength * itemLength(type.elementType) + 31) / 32);
case Type.vector:
return Math.floor((type.length * itemLength(type.elementType) + 31) / 32);
case Type.container:
return type.fields.length;
function _clone(value: any, type: FullSSZType): any {
const obj: any = {};
switch (type.type) {
case Type.uint:
if (BN.isBN(value)) {
return value.clone();
} else {
return value;
}
case Type.bool:
return value;
case Type.bitList:
case Type.bitVector:
return value.clone();
case Type.byteList:
case Type.byteVector:
return (value as Buffer).slice();
case Type.list:
case Type.vector:
return value.map((element: any) => clone(element, type.elementType));
export function _serialize(value: SerializableValue, type: FullSSZType, output: Buffer, start: number): number {
switch(type.type) {
case Type.bool:
return _serializeBool(value as Bool, output, start);
case Type.uint:
return _serializeUint(value as Uint, type, output, start);
case Type.bitList:
return _serializeBitList(value as BitList, type, output, start);
case Type.bitVector:
return _serializeBitVector(value as BitVector, type, output, start);
case Type.byteList:
case Type.byteVector:
return _serializeByteArray(value as Bytes, type, output, start);
case Type.list:
case Type.vector:
return _serializeArray(value as SerializableArray, type, output, start);
case Type.container:
return _serializeObject(value as SerializableObject, type, output, start);
}
}
function expandJsonValue(value: any, type: FullSSZType): any {
switch (type.type) {
case Type.uint:
if (type.byteLength > 6 && type.useNumber)
return Infinity;
return type.useNumber ? new BN(value).toNumber() : new BN(value);
case Type.bool:
return value;
case Type.bitList:
return BitList.deserialize(Buffer.from(value.slice(2), "hex"));
case Type.bitVector:
return BitVector.fromBitfield(Buffer.from(value.slice(2), "hex"), type.length);
case Type.byteList:
case Type.byteVector:
return Buffer.from(value.slice(2), "hex");
case Type.list:
case Type.vector:
return value.map((element: any) => expandJsonValue(element, type.elementType));
case Type.container:
export function _hashTreeRoot(value: SerializableValue, type: FullSSZType): Buffer {
let elementType: FullSSZType;
switch (type.type) {
case Type.uint:
case Type.bool:
case Type.byteVector:
return merkleize(pack([value], type));
case Type.bitVector:
value = value as BitVector;
return merkleize(chunkify(Buffer.from(value.toBitfield())), chunkCount(type));
case Type.bitList:
value = value as BitList;
return mixInLength(
merkleize(chunkify(Buffer.from(value.toBitfield())), chunkCount(type)),
value.bitLength
);
case Type.byteList:
value = value as Bytes;
return mixInLength(
merkleize(pack([value], type), chunkCount(type)),
export function _deserialize(data: Buffer, type: FullSSZType, start: number, end: number): SerializableValue {
switch (type.type) {
case Type.uint:
return _deserializeUint(data, type, start);
case Type.bool:
return _deserializeBool(data, start);
case Type.bitList:
return _deserializeBitList(data, type, start, end);
case Type.bitVector:
return _deserializeBitVector(data, type, start, end);
case Type.byteList:
case Type.byteVector:
return _deserializeByteArray(data, type, start, end);
case Type.list:
case Type.vector:
return _deserializeArray(data, type, start, end);
case Type.container:
return _deserializeObject(data, type, start, end);
}
export function _assertValidValue(value: any, type: FullSSZType): void {
switch (type.type) {
case Type.uint:
assert(BN.isBN(value) || value === Number(value), "Invalid uint: not a uint");
if (value === Infinity) {
break;
}
assert((new BN(value)).gten(0), "Invalid uint: value < 0");
assert((new BN(value)).lt((new BN(2)).pow(new BN(type.byteLength * 8))), "Invalid uint: not in range");
break;
case Type.bool:
assert(value === true || value === false, "Invalid boolean: not a boolean");
break;
case Type.bitList:
assert(BitList.isBitList(value), "Invalid BitList: not a BitList");
assert(value.bitLength <= type.maxLength, "Invalid BitList: longer than max length");
break;
case Type.bitVector:
assert(BitVector.isBitVector(value), "Invalid BitVector: not a BitVector");
function _createValue(type: FullSSZType, value: any = null): any {
if (value === null || value === undefined) {
value = defaultValue(type);
}
switch(type.type) {
case Type.uint:
case Type.bool:
case Type.bitList:
case Type.bitVector:
case Type.byteList:
case Type.byteVector:
assertValidValue(type, value);
return value;
case Type.list:
assert(Array.isArray(value));
return value.map((v: any) =>
_createValue(type.elementType, v));
case Type.vector:
assert(Array.isArray(value));
return Array.from({length: type.length}, (_, i) =>
_createValue(type.elementType, value[i]));
case Type.container: