decode static method

Tuple<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.

Returns a tuple containing the decoded private key as a List<int> and the associated WifPubKeyModes representing the public key mode, where WifPubKeyModes.compressed indicates the compressed mode.

Implementation

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

  return Tuple(privKeyBytes, pubKeyMode);
}