restoreIdentityFromDerivationPath static method

Future<KeyStoreModel> restoreIdentityFromDerivationPath(
  1. String derivationPath,
  2. String mnemonic, {
  3. String? pkh,
  4. bool validate = true,
  5. String passphrase = "",
  6. SignerCurve signerCurve = SignerCurve.ED25519,
})

Implementation

static Future<KeyStoreModel> restoreIdentityFromDerivationPath(
    String derivationPath, String mnemonic,
    {String? pkh,
    bool validate = true,
    String passphrase = "",
    SignerCurve signerCurve = SignerCurve.ED25519}) async {
  // if the signer curve is secp256r1 then throw an error as it is not supported
  if (signerCurve == SignerCurve.SECP256R1) {
    throw new Exception("SECP256R1 is not supported");
  }
  if (validate) {
    if (![12, 15, 18, 21, 24].contains(mnemonic.split(' ').length)) {
      throw new Exception("Invalid mnemonic length.");
    }
    if (!bip39.validateMnemonic(mnemonic)) {
      throw new Exception("The given mnemonic could not be validated.");
    }
  }

  Uint8List seed = bip39.mnemonicToSeed(mnemonic);

  if (derivationPath.length > 0) {
    if (signerCurve == SignerCurve.ED25519) {
      KeyData keysource =
          await ED25519_HD_KEY.derivePath(derivationPath, seed);
      var combinedKey =
          Uint8List.fromList(keysource.key + keysource.chainCode);
      return WalletUtils().getKeysFromPrivateKey(
          GenerateKeys.readKeysWithHint(
              combinedKey, GenerateKeys.keyPrefixes[PrefixEnum.edsk]!),
          signerCurve);
    } else {
      // Generate a BIP32 hierarchical deterministic key from the seed
      final masterKey = bip32.BIP32.fromSeed(seed);
      final key = masterKey.derivePath(derivationPath);

      return WalletUtils().getKeysFromPrivateKey(
          GenerateKeys.readKeysWithHint(key.privateKey!.sublist(0, 32),
              GenerateKeys.keyPrefixes[PrefixEnum.spsk]!),
          signerCurve);
    }
  } else {
    return await _unlockKeys(mnemonic: mnemonic, password: passphrase);
  }
}