cake_wallet/lib/monero/monero_wallet_service.dart

230 lines
7 KiB
Dart
Raw Normal View History

2020-09-10 14:51:59 +00:00
import 'dart:io';
2020-09-15 20:35:49 +00:00
import 'package:cake_wallet/core/wallet_base.dart';
2021-07-06 10:51:54 +00:00
import 'package:cake_wallet/monero/monero_wallet_utils.dart';
2020-09-15 20:35:49 +00:00
import 'package:hive/hive.dart';
import 'package:cw_monero/wallet_manager.dart' as monero_wallet_manager;
import 'package:cw_monero/wallet.dart' as monero_wallet;
import 'package:cw_monero/exceptions/wallet_opening_exception.dart';
2020-06-20 07:10:00 +00:00
import 'package:cake_wallet/monero/monero_wallet.dart';
2020-06-01 18:13:56 +00:00
import 'package:cake_wallet/core/wallet_credentials.dart';
2020-06-20 07:10:00 +00:00
import 'package:cake_wallet/core/wallet_service.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/pathForWallet.dart';
import 'package:cake_wallet/entities/wallet_info.dart';
import 'package:cake_wallet/entities/wallet_type.dart';
2020-06-01 18:13:56 +00:00
class MoneroNewWalletCredentials extends WalletCredentials {
2020-06-20 07:10:00 +00:00
MoneroNewWalletCredentials({String name, String password, this.language})
2020-06-01 18:13:56 +00:00
: super(name: name, password: password);
final String language;
}
class MoneroRestoreWalletFromSeedCredentials extends WalletCredentials {
2020-06-20 07:10:00 +00:00
MoneroRestoreWalletFromSeedCredentials(
2020-09-15 20:35:49 +00:00
{String name, String password, int height, this.mnemonic})
: super(name: name, password: password, height: height);
2020-06-01 18:13:56 +00:00
final String mnemonic;
}
2020-10-29 10:49:07 +00:00
class MoneroWalletLoadingException implements Exception {
@override
String toString() => 'Failure to load the wallet.';
2020-10-29 10:49:07 +00:00
}
2020-06-01 18:13:56 +00:00
class MoneroRestoreWalletFromKeysCredentials extends WalletCredentials {
2020-06-20 07:10:00 +00:00
MoneroRestoreWalletFromKeysCredentials(
2020-06-01 18:13:56 +00:00
{String name,
String password,
this.language,
this.address,
this.viewKey,
this.spendKey,
2020-09-15 20:35:49 +00:00
int height})
: super(name: name, password: password, height: height);
2020-06-01 18:13:56 +00:00
final String language;
final String address;
final String viewKey;
final String spendKey;
}
2020-06-20 07:10:00 +00:00
class MoneroWalletService extends WalletService<
2020-06-01 18:13:56 +00:00
MoneroNewWalletCredentials,
MoneroRestoreWalletFromSeedCredentials,
MoneroRestoreWalletFromKeysCredentials> {
2020-09-15 20:35:49 +00:00
MoneroWalletService(this.walletInfoSource);
final Box<WalletInfo> walletInfoSource;
2021-07-06 10:51:54 +00:00
static bool walletFilesExist(String path) =>
!File(path).existsSync() && !File('$path.keys').existsSync();
@override
WalletType getType() => WalletType.monero;
2020-06-01 18:13:56 +00:00
@override
2020-06-20 07:10:00 +00:00
Future<MoneroWallet> create(MoneroNewWalletCredentials credentials) async {
2020-06-01 18:13:56 +00:00
try {
final path = await pathForWallet(name: credentials.name, type: getType());
2020-06-01 18:13:56 +00:00
await monero_wallet_manager.createWallet(
path: path,
password: credentials.password,
language: credentials.language);
final wallet = MoneroWallet(walletInfo: credentials.walletInfo);
2020-06-20 07:10:00 +00:00
await wallet.init();
return wallet;
2020-06-01 18:13:56 +00:00
} catch (e) {
2020-09-10 14:51:59 +00:00
// TODO: Implement Exception for wallet list service.
2020-10-09 18:34:21 +00:00
print('MoneroWalletsManager Error: ${e.toString()}');
2020-06-01 18:13:56 +00:00
rethrow;
}
}
@override
Future<bool> isWalletExit(String name) async {
try {
final path = await pathForWallet(name: name, type: getType());
2020-06-01 18:13:56 +00:00
return monero_wallet_manager.isWalletExist(path: path);
} catch (e) {
2020-09-10 14:51:59 +00:00
// TODO: Implement Exception for wallet list service.
2020-06-01 18:13:56 +00:00
print('MoneroWalletsManager Error: $e');
rethrow;
}
}
@override
2020-06-20 07:10:00 +00:00
Future<MoneroWallet> openWallet(String name, String password) async {
2020-06-01 18:13:56 +00:00
try {
final path = await pathForWallet(name: name, type: getType());
if (walletFilesExist(path)) {
await repairOldAndroidWallet(name);
}
2020-10-29 10:49:07 +00:00
await monero_wallet_manager
.openWalletAsync({'path': path, 'password': password});
2020-09-15 20:35:49 +00:00
final walletInfo = walletInfoSource.values.firstWhere(
(info) => info.id == WalletBase.idFor(name, getType()),
2020-10-29 10:49:07 +00:00
orElse: () => null);
final wallet = MoneroWallet(walletInfo: walletInfo);
final isValid = wallet.walletAddresses.validate();
2020-10-29 10:49:07 +00:00
if (!isValid) {
2021-07-06 10:51:54 +00:00
await restoreOrResetWalletFiles(name);
wallet.close();
return openWallet(name, password);
2020-10-29 10:49:07 +00:00
}
2020-06-20 07:10:00 +00:00
await wallet.init();
return wallet;
2020-06-01 18:13:56 +00:00
} catch (e) {
2020-09-10 14:51:59 +00:00
// TODO: Implement Exception for wallet list service.
2020-12-23 12:48:04 +00:00
if ((e.toString().contains('bad_alloc') ||
(e is WalletOpeningException &&
(e.message == 'std::bad_alloc' ||
e.message.contains('bad_alloc')))) ||
(e.toString().contains('does not correspond') ||
(e is WalletOpeningException &&
e.message.contains('does not correspond')))) {
2021-07-06 10:51:54 +00:00
await restoreOrResetWalletFiles(name);
2020-12-23 12:48:04 +00:00
return openWallet(name, password);
}
2020-06-01 18:13:56 +00:00
rethrow;
}
}
2020-09-10 14:51:59 +00:00
@override
2020-10-29 10:49:07 +00:00
Future<void> remove(String wallet) async {
final path = await pathForWalletDir(name: wallet, type: getType());
2020-10-29 10:49:07 +00:00
final file = Directory(path);
final isExist = file.existsSync();
if (isExist) {
await file.delete(recursive: true);
}
}
2020-06-01 18:13:56 +00:00
@override
2020-06-20 07:10:00 +00:00
Future<MoneroWallet> restoreFromKeys(
2020-06-01 18:13:56 +00:00
MoneroRestoreWalletFromKeysCredentials credentials) async {
try {
final path = await pathForWallet(name: credentials.name, type: getType());
2020-06-01 18:13:56 +00:00
await monero_wallet_manager.restoreFromKeys(
path: path,
password: credentials.password,
language: credentials.language,
restoreHeight: credentials.height,
address: credentials.address,
viewKey: credentials.viewKey,
spendKey: credentials.spendKey);
final wallet = MoneroWallet(walletInfo: credentials.walletInfo);
2020-06-20 07:10:00 +00:00
await wallet.init();
return wallet;
2020-06-01 18:13:56 +00:00
} catch (e) {
2020-09-10 14:51:59 +00:00
// TODO: Implement Exception for wallet list service.
2020-06-01 18:13:56 +00:00
print('MoneroWalletsManager Error: $e');
rethrow;
}
}
@override
2020-06-20 07:10:00 +00:00
Future<MoneroWallet> restoreFromSeed(
2020-06-01 18:13:56 +00:00
MoneroRestoreWalletFromSeedCredentials credentials) async {
try {
final path = await pathForWallet(name: credentials.name, type: getType());
2020-06-01 18:13:56 +00:00
await monero_wallet_manager.restoreFromSeed(
path: path,
password: credentials.password,
seed: credentials.mnemonic,
restoreHeight: credentials.height);
final wallet = MoneroWallet(walletInfo: credentials.walletInfo);
2020-06-20 07:10:00 +00:00
await wallet.init();
return wallet;
2020-06-01 18:13:56 +00:00
} catch (e) {
2020-09-10 14:51:59 +00:00
// TODO: Implement Exception for wallet list service.
2020-06-01 18:13:56 +00:00
print('MoneroWalletsManager Error: $e');
rethrow;
}
}
Future<void> repairOldAndroidWallet(String name) async {
try {
if (!Platform.isAndroid) {
return;
}
final oldAndroidWalletDirPath =
await outdatedAndroidPathForWalletDir(name: name);
final dir = Directory(oldAndroidWalletDirPath);
if (!dir.existsSync()) {
return;
}
final newWalletDirPath =
await pathForWalletDir(name: name, type: getType());
dir.listSync().forEach((f) {
final file = File(f.path);
final name = f.path.split('/').last;
final newPath = newWalletDirPath + '/$name';
final newFile = File(newPath);
if (!newFile.existsSync()) {
newFile.createSync();
}
newFile.writeAsBytesSync(file.readAsBytesSync());
});
} catch (e) {
print(e.toString());
}
}
2020-06-01 18:13:56 +00:00
}