cake_wallet/cw_zano/lib/zano_wallet_service.dart

123 lines
4.3 KiB
Dart
Raw Normal View History

2023-10-02 14:17:35 +00:00
import 'dart:io';
2023-10-02 14:17:35 +00:00
import 'package:collection/collection.dart';
import 'package:cw_core/pathForWallet.dart';
2023-10-02 14:17:35 +00:00
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_credentials.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_core/wallet_service.dart';
2023-10-02 14:17:35 +00:00
import 'package:cw_core/wallet_type.dart';
2024-03-06 06:48:59 +00:00
import 'package:cw_zano/api/api_calls.dart';
import 'package:cw_zano/zano_wallet.dart';
import 'package:hive/hive.dart';
2023-10-02 14:17:35 +00:00
class ZanoNewWalletCredentials extends WalletCredentials {
ZanoNewWalletCredentials({required String name, String? password}) : super(name: name, password: password);
2023-10-02 14:17:35 +00:00
}
class ZanoRestoreWalletFromSeedCredentials extends WalletCredentials {
ZanoRestoreWalletFromSeedCredentials({required String name, required String password, required int height, required this.mnemonic})
2023-10-02 14:17:35 +00:00
: super(name: name, password: password, height: height);
final String mnemonic;
}
class ZanoRestoreWalletFromKeysCredentials extends WalletCredentials {
ZanoRestoreWalletFromKeysCredentials(
{required String name,
required String password,
required this.language,
required this.address,
required this.viewKey,
required this.spendKey,
required int height})
2023-10-02 14:17:35 +00:00
: super(name: name, password: password, height: height);
final String language;
final String address;
final String viewKey;
final String spendKey;
}
2024-08-03 12:18:19 +00:00
class ZanoWalletService extends WalletService<ZanoNewWalletCredentials,
ZanoRestoreWalletFromSeedCredentials, ZanoRestoreWalletFromKeysCredentials, ZanoNewWalletCredentials> {
2023-10-02 14:17:35 +00:00
ZanoWalletService(this.walletInfoSource);
final Box<WalletInfo> walletInfoSource;
static bool walletFilesExist(String path) => !File(path).existsSync() && !File('$path.keys').existsSync();
2023-10-02 14:17:35 +00:00
2023-11-17 17:40:23 +00:00
int hWallet = 0;
2023-10-02 14:17:35 +00:00
@override
WalletType getType() => WalletType.zano;
@override
Future<ZanoWallet> create(WalletCredentials credentials, {bool? isTestnet}) async {
2024-04-09 12:17:25 +00:00
print('zanowallet service create isTestnet $isTestnet');
return await ZanoWalletBase.create(credentials: credentials);
2023-10-02 14:17:35 +00:00
}
@override
Future<bool> isWalletExit(String name) async {
final path = await pathForWallet(name: name, type: getType());
return ApiCalls.isWalletExist(path: path);
2023-10-02 14:17:35 +00:00
}
@override
Future<ZanoWallet> openWallet(String name, String password) async {
final walletInfo = walletInfoSource.values.firstWhereOrNull((info) => info.id == WalletBase.idFor(name, getType()))!;
2023-10-02 14:17:35 +00:00
try {
final wallet = await ZanoWalletBase.open(name: name, password: password, walletInfo: walletInfo);
saveBackup(name);
2023-10-02 14:17:35 +00:00
return wallet;
} catch (e) {
await restoreWalletFilesFromBackup(name);
return await ZanoWalletBase.open(name: name, password: password, walletInfo: walletInfo);
2023-12-14 04:51:16 +00:00
}
}
2023-10-02 14:17:35 +00:00
@override
Future<void> remove(String wallet) async {
final path = await pathForWalletDir(name: wallet, type: getType());
final file = Directory(path);
final isExist = file.existsSync();
if (isExist) {
await file.delete(recursive: true);
}
final walletInfo = walletInfoSource.values.firstWhere((info) => info.id == WalletBase.idFor(wallet, getType()));
2023-10-02 14:17:35 +00:00
await walletInfoSource.delete(walletInfo.key);
}
@override
2023-12-14 04:51:16 +00:00
Future<void> rename(String currentName, String password, String newName) async {
final currentWalletInfo = walletInfoSource.values.firstWhere((info) => info.id == WalletBase.idFor(currentName, getType()));
2023-12-14 04:51:16 +00:00
final currentWallet = ZanoWallet(currentWalletInfo);
2023-10-02 14:17:35 +00:00
await currentWallet.renameWalletFiles(newName);
final newWalletInfo = currentWalletInfo;
newWalletInfo.id = WalletBase.idFor(newName, getType());
newWalletInfo.name = newName;
await walletInfoSource.put(currentWalletInfo.key, newWalletInfo);
}
@override
Future<ZanoWallet> restoreFromKeys(ZanoRestoreWalletFromKeysCredentials credentials, {bool? isTestnet}) async {
throw UnimplementedError();
2023-10-02 14:17:35 +00:00
}
@override
Future<ZanoWallet> restoreFromSeed(ZanoRestoreWalletFromSeedCredentials credentials, {bool? isTestnet}) async {
return ZanoWalletBase.restore(credentials: credentials);
2023-10-02 14:17:35 +00:00
}
2024-08-03 12:18:19 +00:00
@override
Future<ZanoWallet> restoreFromHardwareWallet(ZanoNewWalletCredentials credentials) {
throw UnimplementedError("Restoring a Zano wallet from a hardware wallet is not yet supported!");
}
2023-10-02 14:17:35 +00:00
}