validateUTXOAddress function
Implementation
(AddressError?, UTXONetworkType?) validateUTXOAddress({
required String address,
required CoinEntity? token,
}) {
if (address.trim().length != address.length) {
return (AddressError.WHITESPACE, null);
}
try {
// this is the main-check: see if an output-script can be generated
P2Hash(address).publicKeyScript;
} catch (e) {
if (address.startsWith("0x")) {
return (AddressError.WRONG_CHAIN, null);
} else if (e.toString().contains("checksum")) {
return (AddressError.INVALID_CHECKSUM, null);
} else {
return (AddressError.INVALID, null);
}
}
if (token?.isUTXO ?? false) if (!address.startsWithAny(
UTXO_Network_List.singleWhereOrNull((net) => net.coin == token)!
.addressPrefixes
.values
.toList())) {
return (AddressError.WRONG_CHAIN, null);
}
final network = UTXO_Network_List.singleWhereOrNull(
(net) => address.startsWithAny(net.addressPrefixes.values.toList()),
);
return (null, network); // successful validation
}