Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
switch (type.type) {
case Type.bool:
return false;
case Type.uint:
switch (type.use) {
case UintImpl.bn:
return new BN(0);
case UintImpl.bigint:
return BigInt(0);
case UintImpl.number:
return 0;
}
break;
case Type.bitList:
return BitList.fromBitfield(Buffer.alloc(0), 0);
case Type.bitVector:
return BitVector.fromBitfield(
Buffer.alloc(Math.max(1, Math.ceil(type.length / 8))),
type.length);
case Type.byteList:
return Buffer.alloc(0);
case Type.byteVector:
return Buffer.alloc(type.length);
case Type.list:
return [];
case Type.vector:
return Array.from({length: type.length}, () =>
defaultValue(type.elementType));
case Type.container:
// eslint-disable-next-line no-case-declarations
const obj = {} as any;
type.fields.forEach(([fieldName, fieldType]) =>
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;
default:
throw new Error("unsupported type");
}
}
export function getElementType(type: FullSSZType, indexOrFieldName: PathElement): FullSSZType {
switch (type.type) {
case Type.bitList:
case Type.bitVector:
return bit;
case Type.byteList:
case Type.byteVector:
return byte;
case Type.list:
case Type.vector:
return type.elementType;
case Type.container:
return type.fields.find(([fieldName]) => indexOrFieldName === fieldName)[1];
default:
throw new Error("unsupported 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");
assert(value.bitLength === type.length, "Invalid BitVector: incorrect length");
break;
case Type.byteList:
assert(value instanceof Uint8Array || value instanceof Buffer, "Invalid byte array: not a Uint8Array/Buffer");
assert(value.length <= type.maxLength, "Invalid byte array: longer than max length");
break;
case Type.byteVector:
assert(value instanceof Uint8Array || value instanceof Buffer, "Invalid byte array: not a Uint8Array/Buffer");
assert(value.length === type.length, "Invalid byte array: incorrect length");
break;
case Type.list:
assert(Array.isArray(value), "Invalid list: not an Array");
assert(value.length <= type.maxLength, "Invalid list: longer than max length");
value.forEach((element: any, i: number) => {
try {
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));
case Type.container:
return type.fields.every(([fieldName, fieldType]) => equals(value1[fieldName], value2[fieldName], fieldType));
}
}
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:
type.fields.forEach(([fieldName, fieldType]) => {
value[fieldName] = expandJsonValue(value[fieldName], fieldType);
});
return value;
}
}
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));
case Type.container:
type.fields.forEach(([fieldName, fieldType]) => {
obj[fieldName] = clone(value[fieldName], fieldType);
});
return obj;
}
}
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)),
value.length
);
case Type.list:
value = value as SerializableArray;
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);
}
}