mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-08 03:49:43 +00:00
949dade4b2
Conflicts: cw_bitcoin/lib/bitcoin_wallet_service.dart cw_bitcoin/lib/litecoin_wallet_service.dart cw_bitcoin_cash/lib/src/bitcoin_cash_wallet_service.dart cw_ethereum/lib/ethereum_wallet_service.dart cw_monero/lib/monero_wallet_service.dart cw_nano/lib/nano_wallet_service.dart cw_polygon/lib/polygon_wallet_service.dart lib/utils/exception_handler.dart 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_tl.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
131 lines
3.8 KiB
Dart
131 lines
3.8 KiB
Dart
import 'package:cw_core/encryption_file_utils.dart';
|
|
import 'package:cw_core/wallet_base.dart';
|
|
import 'package:cw_core/wallet_type.dart';
|
|
import 'package:cw_ethereum/ethereum_client.dart';
|
|
import 'package:cw_ethereum/ethereum_mnemonics_exception.dart';
|
|
import 'package:cw_ethereum/ethereum_wallet.dart';
|
|
import 'package:cw_evm/evm_chain_wallet_creation_credentials.dart';
|
|
import 'package:cw_evm/evm_chain_wallet_service.dart';
|
|
import 'package:bip39/bip39.dart' as bip39;
|
|
|
|
class EthereumWalletService extends EVMChainWalletService<EthereumWallet> {
|
|
EthereumWalletService(super.walletInfoSource, super.isDirect, {required this.client});
|
|
|
|
late EthereumClient client;
|
|
|
|
@override
|
|
WalletType getType() => WalletType.ethereum;
|
|
|
|
@override
|
|
Future<EthereumWallet> create(EVMChainNewWalletCredentials credentials) async {
|
|
final strength = credentials.seedPhraseLength == 24 ? 256 : 128;
|
|
|
|
final mnemonic = bip39.generateMnemonic(strength: strength);
|
|
|
|
final wallet = EthereumWallet(
|
|
walletInfo: credentials.walletInfo!,
|
|
mnemonic: mnemonic,
|
|
password: credentials.password!,
|
|
client: client,
|
|
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
|
|
);
|
|
|
|
await wallet.init();
|
|
wallet.addInitialTokens();
|
|
await wallet.save();
|
|
|
|
return wallet;
|
|
}
|
|
|
|
@override
|
|
Future<EthereumWallet> openWallet(String name, String password) async {
|
|
final walletInfo =
|
|
walletInfoSource.values.firstWhere((info) => info.id == WalletBase.idFor(name, getType()));
|
|
|
|
try {
|
|
final wallet = await EthereumWallet.open(
|
|
name: name,
|
|
password: password,
|
|
walletInfo: walletInfo,
|
|
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
|
|
);
|
|
|
|
await wallet.init();
|
|
await wallet.save();
|
|
saveBackup(name);
|
|
return wallet;
|
|
} catch (_) {
|
|
|
|
await restoreWalletFilesFromBackup(name);
|
|
|
|
final wallet = await EthereumWallet.open(
|
|
name: name,
|
|
password: password,
|
|
walletInfo: walletInfo,
|
|
);
|
|
await wallet.init();
|
|
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 EthereumWallet.open(
|
|
password: password,
|
|
name: currentName,
|
|
walletInfo: currentWalletInfo,
|
|
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
|
|
);
|
|
|
|
await currentWallet.renameWalletFiles(newName);
|
|
await saveBackup(newName);
|
|
|
|
final newWalletInfo = currentWalletInfo;
|
|
newWalletInfo.id = WalletBase.idFor(newName, getType());
|
|
newWalletInfo.name = newName;
|
|
|
|
await walletInfoSource.put(currentWalletInfo.key, newWalletInfo);
|
|
}
|
|
|
|
@override
|
|
Future<EthereumWallet> restoreFromKeys(EVMChainRestoreWalletFromPrivateKey credentials) async {
|
|
final wallet = EthereumWallet(
|
|
password: credentials.password!,
|
|
privateKey: credentials.privateKey,
|
|
walletInfo: credentials.walletInfo!,
|
|
client: client,
|
|
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
|
|
);
|
|
|
|
await wallet.init();
|
|
wallet.addInitialTokens();
|
|
await wallet.save();
|
|
|
|
return wallet;
|
|
}
|
|
|
|
@override
|
|
Future<EthereumWallet> restoreFromSeed(
|
|
EVMChainRestoreWalletFromSeedCredentials credentials) async {
|
|
if (!bip39.validateMnemonic(credentials.mnemonic)) {
|
|
throw EthereumMnemonicIsIncorrectException();
|
|
}
|
|
|
|
final wallet = EthereumWallet(
|
|
password: credentials.password!,
|
|
mnemonic: credentials.mnemonic,
|
|
walletInfo: credentials.walletInfo!,
|
|
client: client,
|
|
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
|
|
);
|
|
|
|
await wallet.init();
|
|
wallet.addInitialTokens();
|
|
await wallet.save();
|
|
|
|
return wallet;
|
|
}
|
|
}
|