decodeScale static method

LayoutDecodeResult<BigInt> decodeScale(
  1. List<int> bytes, {
  2. int offset = 0,
})

Decodes a SCALE compact integer at offset.

Implementation

static LayoutDecodeResult<BigInt> decodeScale(
  List<int> bytes, {
  int offset = 0,
}) {
  _requireAvailable(bytes, offset, 1);
  final byte = bytes[offset];

  switch (byte & 0x03) {
    case 0x00:
      return LayoutDecodeResult(consumed: 1, value: BigInt.from(byte) >> 2);
    case 0x01:
      _requireAvailable(bytes, offset, 2);
      final val = BigintUtils.fromBytes(
        bytes,
        offset: offset,
        length: 2,
        sign: false,
        byteOrder: Endian.little,
      );
      return LayoutDecodeResult(consumed: 2, value: val >> 2);
    case 0x02:
      _requireAvailable(bytes, offset, 4);
      final val = BigintUtils.fromBytes(
        bytes,
        offset: offset,
        length: 4,
        byteOrder: Endian.little,
      );
      return LayoutDecodeResult(consumed: 4, value: val >> 2);
    default:
      final int dataLen = (byte >> 2) + 4;
      _requireAvailable(bytes, offset + 1, dataLen);
      final val = BigintUtils.fromBytes(
        bytes,
        offset: offset + 1,
        length: dataLen,
        byteOrder: Endian.little,
      );
      return LayoutDecodeResult(consumed: dataLen + 1, value: val);
  }
}