encode static method
Encodes data using RLP encoding.
Supported types:
- Uint8List - encoded as bytes
- String - encoded as UTF-8 bytes
- int - encoded as big-endian bytes
- BigInt - encoded as big-endian bytes
- List - encoded as RLP list
null- encoded as empty bytes
Example:
RLP.encode('dog'); // [0x83, 0x64, 0x6f, 0x67]
RLP.encode(['cat', 'dog']); // [0xc8, 0x83, 0x63, 0x61, 0x74, 0x83, 0x64, 0x6f, 0x67]
Implementation
static Uint8List encode(dynamic data) {
if (data == null) {
return _encodeBytes(Uint8List(0));
}
if (data is Uint8List) {
return _encodeBytes(data);
}
if (data is List) {
return _encodeList(data);
}
if (data is String) {
return _encodeBytes(Uint8List.fromList(data.codeUnits));
}
if (data is int) {
if (data == 0) {
return _encodeBytes(Uint8List(0));
}
return _encodeBytes(BytesUtils.intToBytes(data));
}
if (data is BigInt) {
if (data == BigInt.zero) {
return _encodeBytes(Uint8List(0));
}
return _encodeBytes(BytesUtils.bigIntToBytes(data));
}
throw RlpException(
'Unsupported type for RLP encoding: ${data.runtimeType}');
}