loadSecretKey static method

Future<UserSecretKey> loadSecretKey(
  1. String filePath,
  2. String password, {
  3. int? addressIndex,
})

Loads secret key from encrypted keystore file.

Parameters

  • filePath - Path to JSON keystore file
  • password - Decryption password
  • addressIndex - Account index (required for mnemonic wallets)

Returns

Future<UserSecretKey> - Decrypted secret key

Throws

  • ArgumentError - If addressIndex provided for secretKey wallet or missing for mnemonic
  • Exception - If password is incorrect or file is corrupted

Example

// Load from secretKey wallet
final key = await UserWallet.loadSecretKey(
  'wallet.json',
  'password',
);

// Load from mnemonic wallet (account 0)
final key0 = await UserWallet.loadSecretKey(
  'mnemonic-wallet.json',
  'password',
  addressIndex: 0,
);

// Use in Account
final account = Account.fromSecretKey(key);

Implementation

static Future<UserSecretKey> loadSecretKey(
  String filePath,
  String password, {
  int? addressIndex,
}) async {
  final String resolvedPath = path.isAbsolute(filePath)
      ? filePath
      : path.join(Directory.current.path, filePath);

  final String keyFileJson = File(resolvedPath).readAsStringSync();
  final Map<String, dynamic> keyFileObject = requireAs<Map<String, dynamic>>(
    jsonDecode(keyFileJson),
    'keyFileJson',
  );

  return decrypt(keyFileObject, password, addressIndex: addressIndex);
}