decode static method
Implementation
static (int, BuBytes) decode(String? hrp, String addr) {
final decoded = Bech32DecoderBase.decodeBech32(
addr,
SegwitBech32Const.separator,
SegwitBech32Const.checksumStrLen,
_verifyChecksum,
);
final hrpGot = decoded.$1;
final data = decoded.$2;
if (hrp != null && hrp != hrpGot) {
throw BuError(
DefaultErrorType.badBech32,
'Incorrect bech32 hrp.',
details: {'expected': hrp, 'hrp': hrpGot},
);
}
if (data.isEmpty) {
throw const BuError(
DefaultErrorType.badBech32,
'Invalid bech32 format.',
details: {'reason': 'empty data payload'},
);
}
final convData = Bech32BaseUtils.convertFromBase32(data.sublist(1));
if (convData.length < SegwitBech32Const.witnessProgMinByteLen ||
convData.length > SegwitBech32Const.witnessProgMaxByteLen) {
throw BuError(
DefaultErrorType.invalidLength,
'Invalid bech32 format.',
details: {
'reason': 'witness program length ${convData.length} out of range',
},
);
}
final witVer = data[0];
if (witVer > SegwitBech32Const.witnessVerMaxVal) {
throw BuError(
DefaultErrorType.invalidLength,
'Invalid bech32 format.',
details: {
'reason':
'witness version $witVer exceeds the maximum of ${SegwitBech32Const.witnessVerMaxVal}',
},
);
}
if (witVer == 0 &&
!SegwitBech32Const.witnessVerZeroDataByteLen.contains(
convData.length,
)) {
throw BuError(
DefaultErrorType.invalidLength,
'Invalid bech32 format.',
details: {
'reason':
'witness version 0 requires a 20 or 32-byte program, got ${convData.length}',
},
);
}
return (witVer, BuBytes(convData));
}