getBitcoinWalletDetails function Null safety

BitcoinWalletDetails getBitcoinWalletDetails(
  1. String? value
)

Detailed wallet check. The returned object contains all the necessary info like address type, network, wallet type and address. Before using the returned object, use isValid getter to check if the result is valid

Implementation

BitcoinWalletDetails getBitcoinWalletDetails(String? value) {
  if (value == null || value.length < 34) {
    return BitcoinWalletDetails.invalid();
  }
  final isSegwitTest = value.startsWith('tb');
  final isSegwit = value.startsWith('bc');
  if (isSegwit || isSegwitTest) {
    return _getSegWitDetails(
      value,
      isSegwitTest,
    );
  }

  final checkCodec = Base58CheckCodec.bitcoin();
  Base58CheckPayload decoded;
  try {
    decoded = checkCodec.decode(value);
  } catch (e) {
    return BitcoinWalletDetails.invalid();
  }
  if (decoded.payload.length != 20) {
    return BitcoinWalletDetails.invalid();
  }
  final version = decoded.version;
  BitcoinAddressType? type = _typeByVersion[version];
  BitcoinAddressNetwork? network = _networkByVersion[version];
  if (type == null) {
    return BitcoinWalletDetails.invalid();
  }
  return BitcoinWalletDetails(
    address: value,
    addressNetwork: network!,
    addressType: type,
    walletType: BitcoinWalletType.Regular,
  );
}