decodeWithChecksum static method
Decodes data and verifies its trailing 4-byte checksum, returning
only the data bytes (checksum stripped) once verified.
Throws Base58ChecksumError if the checksum doesn't match, or
Base58FormatError if data contains an invalid character.
Implementation
static BuBytes decodeWithChecksum(
String data, [
Base58Alphabets alphabet = Base58Alphabets.bitcoin,
]) {
final decoded = decode(data, alphabet);
if (decoded.length < Base58Const.checksumByteLen) {
throw BuError(
DefaultErrorType.invalidChecksum,
"Invalid checksum.",
details: {
'reason':
'Decoded data (${decoded.length} bytes) is shorter than the '
'${Base58Const.checksumByteLen}-byte checksum.',
},
);
}
final dataBytes = decoded.sublist(
0,
decoded.length - Base58Const.checksumByteLen,
);
final checksumBytes = decoded.sublist(
decoded.length - Base58Const.checksumByteLen,
);
final expectedChecksum = Base58Utils.computeChecksum(dataBytes);
if (!BytesUtils.bytesEqual(checksumBytes, expectedChecksum)) {
throw const BuError(DefaultErrorType.badBase58, "Invalid Base58 string.");
}
return BuBytes(dataBytes);
}