encode static method
Encodes dataBytes (the raw account/public-key bytes) as an SS58
address under ss58Format (the network/address-type identifier -
e.g. 0 for Polkadot, 2 for Kusama, 42 for the generic
Substrate format).
ss58Format must be in 0..16383 and not one of the two reserved
values (46, 47). Values 0..63 ("simple" formats) are packed into a
single format byte; anything from 64 up uses the spec's two-byte
encoding instead - both cases are handled automatically, callers
just pass the plain format number either way.
Throws SS58FormatError if ss58Format is out of range or reserved.
Implementation
static String encode(List<int> dataBytes, int ss58Format) {
if (ss58Format < 0 ||
ss58Format > _Ss58Const.formatMaxVal ||
_Ss58Const.reservedFormats.contains(ss58Format)) {
throw BuError(
DefaultErrorType.invalidLength,
'Invalid SS58 format.',
details: {
'reason': 'ss58Format $ss58Format is out of range or reserved',
},
);
}
List<int> ss58FormatBytes;
if (ss58Format <= _Ss58Const.simpleAccountFormatMaxVal) {
// Simple formats (0..63) are just the format value as a single
// byte - toLeBytes() on a value this small always produces exactly
// one byte.
ss58FormatBytes = ss58Format.toLeBytes();
} else {
// Full (14-bit) formats spread across two bytes with the top two
// bits of the first byte set to `01` as a marker distinguishing
// this from the single-byte form (whose values are always < 64,
// i.e. always have those two bits clear) - see SS58Decoder.decode
// for the exact inverse of this bit layout.
ss58FormatBytes = [
((ss58Format & 0x00FC) >> 2) | 0x0040,
(ss58Format >> 8) | ((ss58Format & 0x0003) << 6),
];
}
final payload = [...ss58FormatBytes, ...dataBytes];
final checksum = _Ss58Utils.computeChecksum(payload);
return Base58Encoder.encode([...payload, ...checksum]);
}