How to use the @chainsafe/ssz-type-schema.Type.byteVector function in @chainsafe/ssz-type-schema

To help you get started, weโ€™ve selected a few @chainsafe/ssz-type-schema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ChainSafe / lodestar / packages / ssz / src / core / createValue.ts View on Github external
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]);
github ChainSafe / lodestar / packages / ssz / src / core / size.ts View on Github external
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");
  }
}
github ChainSafe / lodestar / packages / ssz / src / core / equals.ts View on Github external
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));
  }
}
github ChainSafe / lodestar / packages / ssz / src / proof / util / typeToIndex.ts View on Github external
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:
github ChainSafe / lodestar / packages / ssz / src / core / hashTreeRoot.ts View on Github external
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
      );
github ChainSafe / lodestar / packages / ssz / src / core / serialize.ts View on Github external
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);
  }
}
github ChainSafe / lodestar / packages / eth2.0-utils / src / converters / yaml.ts View on Github external
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;
  }
}
github ChainSafe / lodestar / packages / ssz / src / core / deserialize.ts View on Github external
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);
  }
}
github ChainSafe / lodestar / packages / ssz / src / core / clone.ts View on Github external
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;
  }
}