decode static method

(List<int>, PubKeyModes) decode(
  1. String wif, {
  2. List<int> netVer = const [],
})

Decodes a WIF-encoded private key

The wif is the WIF-encoded private key to be decoded, and netVer is an optional list of network version bytes. By default, it's an empty list.

Implementation

static (List<int>, PubKeyModes) decode(
  String wif, {
  List<int> netVer = const [],
}) {
  List<int> privKeyBytes = Base58Decoder.checkDecode(wif);
  if (netVer.isEmpty || privKeyBytes[0] != netVer[0]) {
    throw ArgumentException.invalidOperationArguments(
      "decode",
      name: "netVer",
      reason: "Invalid net version.",
    );
  }
  privKeyBytes = privKeyBytes.sublist(1);
  PubKeyModes pubKeyMode;
  if (Secp256k1PrivateKey.isValidBytes(
    privKeyBytes.sublist(0, privKeyBytes.length - 1),
  )) {
    // Check the compressed public key suffix
    if (privKeyBytes[privKeyBytes.length - 1] != WifConst.comprPubKeySuffix) {
      throw ArgumentException.invalidOperationArguments(
        "decode",
        name: "wif",
        reason: "Invalid compressed public key suffix.",
      );
    }
    privKeyBytes = privKeyBytes.sublist(0, privKeyBytes.length - 1);
    pubKeyMode = PubKeyModes.compressed;
  } else {
    if (!Secp256k1PrivateKey.isValidBytes(privKeyBytes)) {
      throw ArgumentException.invalidOperationArguments(
        "decode",
        name: "wif",
        reason: "Invalid key.",
      );
    }
    pubKeyMode = PubKeyModes.uncompressed;
  }

  return (privKeyBytes, pubKeyMode);
}