Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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:
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;
}
&& 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:
return Buffer.from(value.slice(2), "hex");
case Type.list:
case Type.vector:
return value.map((element: any) => _expandYamlValue(element, type.elementType));
case Type.container:
type.fields.forEach(([fieldName, fieldType]) => {
value[fieldName] = _expandYamlValue(value[fieldName], fieldType);
});
return value;
}
}
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 getItemPosition(type: FullSSZType, indexOrFieldName: PathElement): [number, number, number] {
let start: number,
elementItemLength: number;
switch (type.type) {
case Type.bitList:
case Type.bitVector:
case Type.byteList:
case Type.byteVector:
case Type.list:
case Type.vector:
indexOrFieldName = indexOrFieldName as number;
assert(Number.isSafeInteger(indexOrFieldName));
elementItemLength = itemLength(getElementType(type, indexOrFieldName));
start = indexOrFieldName * elementItemLength;
return [Math.floor(start / 32), start % 32, start % 32 + elementItemLength];
case Type.container:
indexOrFieldName = indexOrFieldName as string;
assert(typeof indexOrFieldName === "string");
return [
type.fields.map(([fieldName]) => fieldName).indexOf(indexOrFieldName),
0,
itemLength(getElementType(type, indexOrFieldName)),
];
default:
throw new Error("unsupported type");
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 _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;
}
}
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 {
_assertValidValue(element, type.elementType);
} catch (e) {
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);
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 variableSize(type: FullSSZType, value: SerializableValue): number {
switch (type.type) {
case Type.bitList:
return Math.ceil(((value as BitList).bitLength + 1) / 8);
case Type.byteList:
return (value as Bytes).length;
case Type.list:
return (value as SerializableArray)
.map((v) =>
size((type as ListType).elementType, v) +
(isVariableSizeType(type.elementType) ? BYTES_PER_LENGTH_PREFIX : 0))
.reduce((a, b) => a + b, 0);
case Type.vector:
return (value as SerializableArray)
.map((v) =>
size((type as VectorType).elementType, v) +
(isVariableSizeType(type.elementType) ? BYTES_PER_LENGTH_PREFIX : 0))
.reduce((a, b) => a + b, 0);
case Type.container:
return type.fields
.map(([fieldName, fieldType]) =>
size(fieldType, (value as SerializableObject)[fieldName]) +
(isVariableSizeType(fieldType) ? BYTES_PER_LENGTH_PREFIX : 0))