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';
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;
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;
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: WalletType.monero);
await monero_wallet_manager.createWallet(
path: path,
password: credentials.password,
language: credentials.language);
2020-09-15 20:35:49 +00:00
final wallet = MoneroWallet(
filename: monero_wallet.getFilename(),
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: WalletType.monero);
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: WalletType.monero);
if (!File(path).existsSync()) {
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(
2020-10-29 10:49:07 +00:00
(info) => info.id == WalletBase.idFor(name, WalletType.monero),
orElse: () => null);
2020-09-15 20:35:49 +00:00
final wallet = MoneroWallet(
filename: monero_wallet.getFilename(), walletInfo: walletInfo);
final isValid = wallet.validate();
2020-10-29 10:49:07 +00:00
if (!isValid) {
2020-11-12 20:02:37 +00:00
// if (wallet.seed?.isNotEmpty ?? false) {
// let restore from seed in this case;
// final seed = wallet.seed;
// final credentials = MoneroRestoreWalletFromSeedCredentials(
// name: name, password: password, mnemonic: seed, height: 2000000)
// ..walletInfo = walletInfo;
// await remove(name);
// return restoreFromSeed(credentials);
2020-11-12 20:02:37 +00:00
// }
2020-10-29 10:49:07 +00:00
throw MoneroWalletLoadingException();
}
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;
}
}
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: WalletType.monero);
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: WalletType.monero);
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);
2020-09-15 20:35:49 +00:00
final wallet = MoneroWallet(
filename: monero_wallet.getFilename(),
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: WalletType.monero);
await monero_wallet_manager.restoreFromSeed(
path: path,
password: credentials.password,
seed: credentials.mnemonic,
restoreHeight: credentials.height);
2020-09-15 20:35:49 +00:00
final wallet = MoneroWallet(
filename: monero_wallet.getFilename(),
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()) {
throw MoneroWalletLoadingException();
}
final newWalletDirPath =
await pathForWalletDir(name: name, type: WalletType.monero);
dir.listSync().forEach((f) {
final file = File(f.path);
final name = f.path.split('/').last;
final newPath = newWalletDirPath + '/$name';
final newFile = File(newPath);
print(file.path);
if (!newFile.existsSync()) {
newFile.createSync();
}
newFile.writeAsBytesSync(file.readAsBytesSync());
});
} catch (e) {
print(e.toString());
throw MoneroWalletLoadingException();
}
}
2020-06-01 18:13:56 +00:00
}