importAccount method

Future<Map<String, dynamic>> importAccount({
  1. required EVMKeyType keyType,
  2. required String key,
  3. required String name,
  4. required String password,
  5. String derivePath = '',
})

Import account from mnemonic/rawSeed/keystore and we get a JSON object. param cryptoType can be sr25519(default) or ed25519. throw error if import failed. return null if keystore password check failed.

Implementation

Future<Map<String, dynamic>> importAccount({
  required EVMKeyType keyType,
  required String key,
  required String name,
  required String password,
  String derivePath = '',
}) async {
  Wallet web3Wallet;
  switch (keyType) {
    case EVMKeyType.mnemonic:
      final wallet = ethers.Wallet.fromMnemonic(key);
      final credential = EthPrivateKey.fromHex(wallet.privateKey!);
      web3Wallet = Wallet.createNew(credential, password, Random.secure(),
          scryptN: crypt_n);
      break;
    case EVMKeyType.privateKey:
      final credential = EthPrivateKey.fromHex(key);
      web3Wallet = Wallet.createNew(credential, password, Random.secure(),
          scryptN: crypt_n);
      break;
    case EVMKeyType.keystore:
      web3Wallet = Wallet.fromJson(key, password);
  }
  final walletJson = jsonDecode(web3Wallet.toJson());
  final type = keyType.toString().split('.')[1];
  return {
    ...walletJson,
    'address': web3Wallet.privateKey.address.hexEip55,
    'name': name,
    type: key,
  };
}