decode static method
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");
}
}