Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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");
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");
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 _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:
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 _defaultValue(type: FullSSZType): any {
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))),
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:
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);
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);
}
}
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 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");
}
}
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));
}
}