decodeRLP function
@param {Uint8List} data @param {int} offset
This function is the main RLP decoding logic. It recursively decodes RLP-encoded data.
It checks the first byte of the data to determine the type of encoding (long item, list, long string, short string).
Depending on the type, it calls _decodeChildren to handle the decoding recursively.
The decoded result is stored in a List
Implementation
DecodedRLP decodeRLP(Uint8List data, int offset) {
DecodedRLP _decodeChildren(
Uint8List data, int offset, int childOffset, int length) {
List<dynamic> result = [];
while (childOffset < offset + 1 + length) {
DecodedRLP decoded = decodeRLP(data, childOffset);
result.add(decoded.result);
childOffset += decoded.consumed;
}
return DecodedRLP(consumed: (1 + length), result: result);
}
if (data[offset] >= 0xf8) {
final lengthLength = data[offset] - 0xf7;
final length = unarrayifyInteger(data, offset + 1, lengthLength);
return _decodeChildren(
data, offset, offset + 1 + lengthLength, lengthLength + length);
} else if (data[offset] >= 0xc0) {
final length = data[offset] - 0xc0;
return _decodeChildren(data, offset, offset + 1, length);
} else if (data[offset] >= 0xb8) {
final lengthLength = data[offset] - 0xb7;
final length = unarrayifyInteger(data, offset + 1, lengthLength);
final result = hex.encode(data.sublist(
offset + 1 + lengthLength, offset + 1 + lengthLength + length));
return DecodedRLP(consumed: (1 + lengthLength + length), result: result);
} else if (data[offset] >= 0x80) {
final length = data[offset] - 0x80;
final result = hex.encode(data.sublist(offset + 1, offset + 1 + length));
return DecodedRLP(consumed: (1 + length), result: result);
}
return DecodedRLP(consumed: 1, result: hexlifyByte(data[offset]));
}