Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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]) =>
obj[fieldName] = defaultValue(fieldType));
return obj;
}
}
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:
assert(Object(value) === value);
// eslint-disable-next-line no-case-declarations
const obj: Record = {};
type.fields.forEach(([fieldName, fieldType]) => {
obj[fieldName] = _createValue(fieldType, value[fieldName]);
});
return obj;
}
}
export function fixedSize(type: FullSSZType): number {
switch (type.type) {
case Type.uint:
return type.byteLength;
case Type.bool:
return 1;
case Type.bitVector:
return Math.ceil(type.length / 8);
case Type.byteVector:
return type.length;
case Type.vector:
return fixedSize(type.elementType) * type.length;
case Type.container:
return type.fields
.map(([, fieldType]) => fixedSize(fieldType))
.reduce((a, b) => a + b, 0);
default:
throw new Error("fixedSize: invalid type");
}
}
);
} else {
return mixInLength(
merkleize(value.map((v) => _hashTreeRoot(v, elementType)), type.maxLength),
value.length
);
}
case Type.vector:
value = value as SerializableArray;
elementType = type.elementType;
if (isBasicType(elementType)) {
return merkleize(pack(value, elementType));
} else {
return merkleize(value.map((v) => _hashTreeRoot(v, elementType)));
}
case Type.container:
type = type as ContainerType;
return merkleize(
type.fields.map(([fieldName, fieldType]) =>
_hashTreeRoot((value as SerializableObject)[fieldName], fieldType)));
}
}
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;
}
}
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));
}
}
throw new Error(`Invalid list, element ${i}: ${e.message}`);
}
});
break;
case Type.vector:
assert(Array.isArray(value), "Invalid vector: not an Array");
assert(value.length === type.length, "Invalid vector: incorrect length");
value.forEach((element: any, i: number) => {
try {
_assertValidValue(element, type.elementType);
} catch (e) {
throw new Error(`Invalid vector, element ${i}: ${e.message}`);
}
});
break;
case Type.container:
assert(value === Object(value), "Invalid container: not an Object");
type.fields.forEach(([fieldName, fieldType]) => {
try {
assert(value[fieldName] !== undefined, "field does not exist");
_assertValidValue(value[fieldName], fieldType);
} catch (e) {
throw new Error(`Invalid container, field ${fieldName}: ${e.message}`);
}
});
break;
}
}
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");
}
}
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);
}
}