decodeBech32 function
Function to decode a Bech32-encoded string into its version and data.
Implementation
Bech32Result? decodeBech32(String address, {bool versifyVersion = true}) {
final decodeBech = _bech32Decode(address);
if (decodeBech == null) return null;
final data = decodeBech.$2;
final bits = convertBits(data.sublist(1), 5, 8, pad: false);
if (bits == null || bits.length < 2 || bits.length > 40) {
return null;
}
if (data[0] > 16) {
return null;
}
if (data[0] == 0 && bits.length != 20 && bits.length != 32) {
return null;
}
if (versifyVersion) {
final spec = decodeBech.$3;
if (data[0] == 0 && spec != Bech32Type.bech32 ||
data[0] != 0 && spec != Bech32Type.bech32M) {
return null;
}
}
return Bech32Result(
data: Uint8List.fromList(bits), hrp: decodeBech.$1, version: data[0]);
}