encode method

  1. @override
List<int> encode(
  1. String value
)
override

Encode the provided value as a Compact Uint (CUint) in Substrate SCALE format.

This method takes an unsigned integer value value, encodes it as a Compact Uint (CUint) in Substrate SCALE format, and returns the encoded value as a List

Implementation

@override
List<int> encode(String value) {
  final v = BigInt.parse(value);
  if (v <= SubstrateScaleCUintEncoderConst.singleByteModeMaxVal) {
    return BigintUtils.toBytes(v << 2, length: 1, order: Endian.little);
  }
  if (v <= SubstrateScaleCUintEncoderConst.twoByteModeMaxVal) {
    return BigintUtils.toBytes((v << 2) | BigInt.from(0x01),
        length: 2, order: Endian.little);
  }
  if (v <= SubstrateScaleCUintEncoderConst.fourByteModeMaxVal) {
    return BigintUtils.toBytes((v << 2) | BigInt.from(0x02),
        length: 4, order: Endian.little);
  }
  if (v <= SubstrateScaleCUintEncoderConst.bigIntegerModeMaxVal) {
    final List<int> valueBytes = BigintUtils.toBytes(v,
        order: Endian.little, length: BigintUtils.orderLen(v));
    final List<int> lenBytes = IntUtils.toBytes(
        (valueBytes.length - 4 << 2) | 0x03,
        length: 1,
        byteOrder: Endian.little);
    return List<int>.from([...lenBytes, ...valueBytes]);
  }

  throw ArgumentException("Out of range integer value ($value)");
}