WIF.fromString constructor

WIF.fromString(
  1. String wif, {
  2. int? version,
})

Decodes a wif string into the private key and version. WifVersionMismatch is thrown if the specified version does not match the WIF. InvalidWif is thrown if the base58 is valid but the data doesn't meet the correct format. If no version is specified, any version will be accepted. May throw InvalidBase58 or InvalidBase58Checksum when decoding the WIF.

Implementation

factory WIF.fromString(String wif, { int? version }) {

  final data = base58Decode(wif);

  // Determine if the data meets the compressed or uncompressed formats
  final compressed = data.length == 34;
  if (!compressed && data.length != 33) throw InvalidWif();
  if (compressed && data.last != 1) throw InvalidWif();

  final decodedVersion = data.first;
  if (version != null && version != decodedVersion) {
    throw WifVersionMismatch();
  }

  final wifObj = WIF(
    privkey: ECPrivateKey(data.sublist(1, 33), compressed: compressed),
    version: decodedVersion,
  );
  wifObj._wifCache = wif;
  return wifObj;

}