cake_wallet/lib/monero/monero_wallet_service.dart

162 lines
5.2 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;
}
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-06-01 18:13:56 +00:00
print('MoneroWalletsManager Error: $e');
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);
monero_wallet_manager.openWallet(path: path, password: password);
2020-09-15 20:35:49 +00:00
final walletInfo = walletInfoSource.values.firstWhere(
2020-09-23 18:26:10 +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);
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
Future<void> remove(String wallet) async =>
File(await pathForWalletDir(name: wallet, type: WalletType.bitcoin))
.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;
}
}
}