decode static method

Map<String, dynamic> decode({
  1. required List<int> bytes,
  2. required StructLayout layout,
  3. Map<String, dynamic> validator = const {},
})

Decodes Borsh serialized bytes.

  • bytes : The bytes to decode.
  • layout : The layout representing the structure of the object.
  • validator (optional): A map used for validation.

Implementation

static Map<String, dynamic> decode(
    {required List<int> bytes,
    required StructLayout layout,
    Map<String, dynamic> validator = const {}}) {
  try {
    final decode = layout.deserialize(bytes).value;

    for (final i in validator.entries) {
      if (i.value is List) {
        if (!CompareUtils.iterableIsEqual(i.value, decode[i.key])) {
          throw MessageException("cannot validate borsh bytes",
              details: {"excepted": validator, "instruction": decode});
        }
      } else {
        if (i.value != decode[i.key]) {
          throw MessageException("cannot validate borsh bytes",
              details: {"excepted": validator, "instruction": decode});
        }
      }
    }
    return decode;
  } catch (e) {
    throw const MessageException("cannot validate borsh bytes");
  }
}