cake_wallet/lib/monero/monero_wallet_service.dart

158 lines
4.8 KiB
Dart
Raw Normal View History

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-06-01 18:13:56 +00:00
import 'package:cake_wallet/src/domain/common/pathForWallet.dart';
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
import 'package:cw_monero/wallet_manager.dart' as monero_wallet_manager;
import 'package:cw_monero/wallet.dart' as monero_wallet;
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-06-01 18:13:56 +00:00
{String name, String password, this.mnemonic, this.height})
: super(name: name, password: password);
final String mnemonic;
final int height;
}
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,
this.height})
: super(name: name, password: password);
final String language;
final String address;
final String viewKey;
final String spendKey;
final int height;
}
2020-06-20 07:10:00 +00:00
class MoneroWalletService extends WalletService<
2020-06-01 18:13:56 +00:00
MoneroNewWalletCredentials,
MoneroRestoreWalletFromSeedCredentials,
MoneroRestoreWalletFromKeysCredentials> {
@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-06-20 07:10:00 +00:00
final wallet = MoneroWallet(filename: monero_wallet.getFilename());
await wallet.init();
return wallet;
2020-06-01 18:13:56 +00:00
} catch (e) {
// TODO: Implement Exception fop wallet list service.
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) {
// TODO: Implement Exception fop wallet list service.
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);
// final id = walletTypeToString(WalletType.monero).toLowerCase() + '_' + name;
// final walletInfo = walletInfoSource.values
// .firstWhere((info) => info.id == id, orElse: () => null);
2020-06-20 07:10:00 +00:00
final wallet = MoneroWallet(filename: monero_wallet.getFilename());
await wallet.init();
return wallet;
2020-06-01 18:13:56 +00:00
} catch (e) {
// TODO: Implement Exception fop wallet list service.
print('MoneroWalletsManager Error: $e');
rethrow;
}
}
Future<void> remove(String wallet) async {
// TODO: implement remove
throw UnimplementedError();
}
@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-06-20 07:10:00 +00:00
final wallet = MoneroWallet(filename: monero_wallet.getFilename());
await wallet.init();
return wallet;
2020-06-01 18:13:56 +00:00
} catch (e) {
// TODO: Implement Exception fop wallet list service.
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-06-20 07:10:00 +00:00
final wallet = MoneroWallet(filename: monero_wallet.getFilename());
await wallet.init();
return wallet;
2020-06-01 18:13:56 +00:00
} catch (e) {
// TODO: Implement Exception fop wallet list service.
print('MoneroWalletsManager Error: $e');
rethrow;
}
}
}