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 getGeneralizedIndex(type: FullSSZType, path: Path): GeneralizedIndex {
let root = 1n;
for (const p of path) {
assert(!isBasicType(type));
if (p === "__len__") {
assert(isListType(type));
return root * 2n + 1n;
} else {
if (isListType(type)) {
root *= 2n; // bit for length mix in
}
const [pos] = getItemPosition(type, p);
root = root * nextPowerOf2(BigInt(chunkCount(type))) + BigInt(pos);
type = getElementType(type, p);
}
}
return root;
}
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 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]) =>
obj[fieldName] = defaultValue(fieldType));
return obj;
}
}
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;
}
}
}
assert(firstOffset === currentIndex, "First offset skips variable data");
} else {
// all elements fixed-sized
let index = start;
const elementSize = fixedSize(type.elementType);
let nextIndex;
for (; index < end;) {
nextIndex = index + elementSize;
value.push(
_deserialize(data, type.elementType, index, nextIndex)
);
index = nextIndex;
}
}
if (type.type === Type.vector) {
assert(type.length === value.length, "Incorrect deserialized vector length");
}
if (type.type === Type.list) {
assert(type.maxLength >= value.length, "List length greater than max length");
}
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))),
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
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]) =>
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]) =>
obj[fieldName] = defaultValue(fieldType));
return obj;
}
}