Address.fromString constructor

Address.fromString(
  1. String encoded,
  2. Network network
)

Decodes an address string (encoded) and returns the sub-class object for the type of address. Throws InvalidAddress, InvalidAddressNetwork, InvalidBech32Checksum or InvalidBase58Checksum if there is an error with the address. The address must match the network provided.

Implementation

factory Address.fromString(String encoded, Network network) {
  // Try base58
  try {
    return Base58Address.fromString(encoded, network);
  } on Exception catch(e) {
    // If not base58, then try bech32
    try {
      return Bech32Address.fromString(encoded, network);
    } on InvalidBech32 {
      // Not valid bech32
      // If it is not a valid base58 string either, throw InvalidAddress.
      // Otherwise rethrow error from base58 which could have been due to the
      // wrong network or checksum
      if (e is InvalidBase58) {
        throw InvalidAddress();
      }
      throw e;
    }
  }
}