createWalletByImportedMnemonic method

Future<Wallet> createWalletByImportedMnemonic({
  1. required String mnemonic,
  2. required String walletName,
  3. String passphrase = '',
})

Implementation

Future<Wallet> createWalletByImportedMnemonic({
  required String mnemonic,
  required String walletName,
  String passphrase = '',
}) async {
  final isAlreadyExist = (await walletRepository.readAll())
          .firstWhereOrNull((element) => element.name == walletName) !=
      null;

  if (isAlreadyExist) {
    throw Exception("This wallet Name is already exist");
  }

  final blockchainsData =
      await blockchainService.createBlockchainsDataFromTheMnemonic(
          mnemonic: mnemonic, passphrase: passphrase);

  final id = int.tryParse(
          (await walletRepository.readAll()).lastOrNull?.id ?? '-1')! +
      1;

  final wallet = Wallet(
    id: id.toString(),
    name: walletName,
    mnemonic: mnemonic,
    passphrase: passphrase,
    blockchainsData: blockchainsData,
  );

  walletsStream.value.add(wallet);
  walletsStream.add(walletsStream.value);
  walletRepository.saveAll(walletsStream.value);
  return wallet;
}