cake_wallet/cw_ethereum/lib/ethereum_wallet_service.dart
OmarHatem 7751a46c4d Merge branch 'main' of https://github.com/cake-tech/cake_wallet into linux/password-direct-input
 Conflicts:
	.gitignore
	cw_bitcoin/pubspec.lock
	cw_core/lib/hive_type_ids.dart
	cw_core/lib/wallet_info.dart
	cw_core/pubspec.lock
	cw_ethereum/lib/ethereum_wallet_service.dart
	cw_monero/example/pubspec.lock
	cw_monero/lib/monero_wallet.dart
	cw_monero/lib/monero_wallet_service.dart
	cw_monero/pubspec.lock
	lib/di.dart
	lib/entities/default_settings_migration.dart
	lib/main.dart
	lib/src/screens/contact/contact_list_page.dart
	lib/src/screens/dashboard/desktop_widgets/desktop_action_button.dart
	lib/src/screens/dashboard/desktop_widgets/desktop_wallet_selection_dropdown.dart
	lib/src/screens/dashboard/widgets/address_page.dart
	lib/src/screens/dashboard/widgets/balance_page.dart
	lib/src/screens/exchange/exchange_page.dart
	lib/src/screens/receive/anonpay_invoice_page.dart
	lib/src/screens/restore/restore_wallet_from_seed_details.dart
	lib/src/screens/restore/wallet_restore_from_keys_form.dart
	lib/src/screens/restore/wallet_restore_page.dart
	lib/src/screens/restore/widgets/restore_button.dart
	lib/src/screens/settings/display_settings_page.dart
	lib/src/screens/settings/manage_nodes_page.dart
	lib/src/screens/settings/widgets/settings_cell_with_arrow.dart
	lib/src/screens/wallet_list/wallet_list_page.dart
	lib/src/widgets/search_bar_widget.dart
	lib/src/widgets/seed_widget.dart
	lib/store/settings_store.dart
	lib/themes/extensions/picker_theme.dart
	lib/themes/high_contrast_theme.dart
	lib/themes/theme_base.dart
	pubspec_base.yaml
	res/values/strings_ar.arb
	res/values/strings_bg.arb
	res/values/strings_cs.arb
	res/values/strings_de.arb
	res/values/strings_en.arb
	res/values/strings_es.arb
	res/values/strings_fr.arb
	res/values/strings_ha.arb
	res/values/strings_hi.arb
	res/values/strings_hr.arb
	res/values/strings_id.arb
	res/values/strings_it.arb
	res/values/strings_ja.arb
	res/values/strings_ko.arb
	res/values/strings_my.arb
	res/values/strings_nl.arb
	res/values/strings_pl.arb
	res/values/strings_pt.arb
	res/values/strings_ru.arb
	res/values/strings_th.arb
	res/values/strings_tr.arb
	res/values/strings_uk.arb
	res/values/strings_ur.arb
	res/values/strings_yo.arb
	res/values/strings_zh.arb
	tool/append_translation.dart
	tool/configure.dart
2023-09-07 06:24:40 +03:00

128 lines
4 KiB
Dart

import 'dart:io';
import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_service.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_ethereum/encryption_file_utils.dart';
import 'package:cw_ethereum/ethereum_mnemonics.dart';
import 'package:cw_ethereum/ethereum_wallet.dart';
import 'package:cw_ethereum/ethereum_wallet_creation_credentials.dart';
import 'package:hive/hive.dart';
import 'package:bip39/bip39.dart' as bip39;
import 'package:collection/collection.dart';
class EthereumWalletService extends WalletService<EthereumNewWalletCredentials,
EthereumRestoreWalletFromSeedCredentials, EthereumRestoreWalletFromPrivateKey> {
EthereumWalletService(this.walletInfoSource, this.isDirect);
final Box<WalletInfo> walletInfoSource;
final bool isDirect;
@override
Future<EthereumWallet> create(EthereumNewWalletCredentials credentials) async {
final mnemonic = bip39.generateMnemonic();
final wallet = EthereumWallet(
walletInfo: credentials.walletInfo!,
mnemonic: mnemonic,
password: credentials.password!,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
await wallet.init();
wallet.addInitialTokens();
await wallet.save();
return wallet;
}
@override
WalletType getType() => WalletType.ethereum;
@override
Future<bool> isWalletExit(String name) async =>
File(await pathForWallet(name: name, type: getType())).existsSync();
@override
Future<EthereumWallet> openWallet(String name, String password) async {
final walletInfo =
walletInfoSource.values.firstWhere((info) => info.id == WalletBase.idFor(name, getType()));
final wallet = await EthereumWalletBase.open(
name: name,
password: password,
walletInfo: walletInfo,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
await wallet.init();
await wallet.save();
return wallet;
}
@override
Future<void> remove(String wallet) async {
File(await pathForWalletDir(name: wallet, type: getType())).delete(recursive: true);
final walletInfo = walletInfoSource.values
.firstWhereOrNull((info) => info.id == WalletBase.idFor(wallet, getType()))!;
await walletInfoSource.delete(walletInfo.key);
}
@override
Future<EthereumWallet> restoreFromKeys(EthereumRestoreWalletFromPrivateKey credentials) async {
final wallet = EthereumWallet(
password: credentials.password!,
privateKey: credentials.privateKey,
walletInfo: credentials.walletInfo!,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
await wallet.init();
wallet.addInitialTokens();
await wallet.save();
return wallet;
}
@override
Future<EthereumWallet> restoreFromSeed(
EthereumRestoreWalletFromSeedCredentials credentials) async {
if (!bip39.validateMnemonic(credentials.mnemonic)) {
throw EthereumMnemonicIsIncorrectException();
}
final wallet = EthereumWallet(
password: credentials.password!,
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo!,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
await wallet.init();
wallet.addInitialTokens();
await wallet.save();
return wallet;
}
@override
Future<void> rename(String currentName, String password, String newName) async {
final currentWalletInfo = walletInfoSource.values
.firstWhere((info) => info.id == WalletBase.idFor(currentName, getType()));
final currentWallet = await EthereumWalletBase.open(
password: password,
name: currentName,
walletInfo: currentWalletInfo,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
await currentWallet.renameWalletFiles(newName);
final newWalletInfo = currentWalletInfo;
newWalletInfo.id = WalletBase.idFor(newName, getType());
newWalletInfo.name = newName;
await walletInfoSource.put(currentWalletInfo.key, newWalletInfo);
}
}