How to use the @chainsafe/ssz-type-schema.Type.vector 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 / defaultValue.ts View on Github external
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;
  }
}
github ChainSafe / lodestar / packages / ssz / src / core / createValue.ts View on Github external
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;
  }
}
github ChainSafe / lodestar / packages / ssz / src / core / deserialize.ts View on Github external
}
    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;
}
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 / ssz / src / util / chunk.ts View on Github external
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");
  }
}
github ChainSafe / lodestar / packages / eth2.0-utils / src / converters / yaml.ts View on Github external
&& 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 / hashTreeRoot.ts View on Github external
);
    case Type.list:
      value = value as SerializableArray;
      elementType = type.elementType;
      if (isBasicType(elementType)) {
        return mixInLength(
          merkleize(pack(value, elementType), chunkCount(type)),
          value.length
        );
      } 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)));
  }
}
github ChainSafe / lodestar / packages / ssz / src / core / clone.ts View on Github external
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;
  }
}
github ChainSafe / lodestar / packages / ssz / src / core / size.ts View on Github external
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))
        .reduce((a, b) => a + b, 0);
    default:
      throw new Error("variableSize: invalid type");
  }
}
github ChainSafe / lodestar / packages / ssz / src / proof / util / typeToIndex.ts View on Github external
function maxLength(type: BitsType | BytesType | ArrayType): number {
  switch (type.type) {
    case Type.bitList:
    case Type.byteList:
    case Type.list:
      return type.maxLength;
    case Type.bitVector:
    case Type.byteVector:
    case Type.vector:
      return type.length;
    default:
      throw new Error("");
  }
}