How to use @chainsafe/ssz-type-schema - 10 common examples

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
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
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 / proof / util / typeToIndex.ts View on Github external
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;
}
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 / 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 / defaultValue.ts View on Github external
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
github ChainSafe / lodestar / packages / ssz / src / core / defaultValue.ts View on Github external
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]) =>
github ChainSafe / lodestar / packages / ssz / src / core / defaultValue.ts View on Github external
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;
  }
}