decode static method

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

  return (SecretBytes.take(privKeyBytes), pubKeyMode);
}