2023-07-24 20:23:09 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-07-28 14:36:50 +00:00
|
|
|
import 'package:cw_core/node.dart';
|
2023-07-24 20:23:09 +00:00
|
|
|
import 'package:cw_core/pathForWallet.dart';
|
|
|
|
import 'package:cw_core/wallet_base.dart';
|
2023-07-25 15:21:49 +00:00
|
|
|
import 'package:cw_core/wallet_credentials.dart';
|
2023-07-24 20:23:09 +00:00
|
|
|
import 'package:cw_core/wallet_info.dart';
|
|
|
|
import 'package:cw_core/wallet_service.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2023-07-28 14:36:50 +00:00
|
|
|
import 'package:cw_nano/nano_balance.dart';
|
|
|
|
import 'package:cw_nano/nano_client.dart';
|
2023-07-26 17:15:22 +00:00
|
|
|
import 'package:cw_nano/nano_mnemonic.dart';
|
2023-07-28 14:36:50 +00:00
|
|
|
import 'package:cw_nano/nano_util.dart';
|
2023-07-24 20:23:09 +00:00
|
|
|
import 'package:cw_nano/nano_wallet.dart';
|
2023-07-28 14:36:50 +00:00
|
|
|
import 'package:cw_nano/nano_wallet_info.dart';
|
2023-07-24 20:23:09 +00:00
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:bip39/bip39.dart' as bip39;
|
|
|
|
|
2023-07-25 15:21:49 +00:00
|
|
|
class NanoNewWalletCredentials extends WalletCredentials {
|
|
|
|
NanoNewWalletCredentials({required String name, String? password})
|
|
|
|
: super(name: name, password: password);
|
|
|
|
}
|
|
|
|
|
|
|
|
class NanoRestoreWalletFromSeedCredentials extends WalletCredentials {
|
|
|
|
NanoRestoreWalletFromSeedCredentials(
|
2023-07-26 17:15:22 +00:00
|
|
|
{required String name,
|
|
|
|
required this.mnemonic,
|
2023-07-31 13:10:33 +00:00
|
|
|
this.derivationType,
|
2023-07-26 17:15:22 +00:00
|
|
|
int height = 0,
|
|
|
|
String? password})
|
2023-07-25 15:21:49 +00:00
|
|
|
: super(name: name, password: password, height: height);
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-07-25 15:21:49 +00:00
|
|
|
final String mnemonic;
|
2023-07-31 13:10:33 +00:00
|
|
|
final DerivationType? derivationType;
|
2023-07-25 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class NanoWalletLoadingException implements Exception {
|
|
|
|
@override
|
|
|
|
String toString() => 'Failure to load the wallet.';
|
|
|
|
}
|
|
|
|
|
|
|
|
class NanoRestoreWalletFromKeysCredentials extends WalletCredentials {
|
2023-07-27 14:30:07 +00:00
|
|
|
NanoRestoreWalletFromKeysCredentials({
|
|
|
|
required String name,
|
|
|
|
required String password,
|
|
|
|
required this.seedKey,
|
2023-07-31 13:10:33 +00:00
|
|
|
this.derivationType,
|
2023-07-27 14:30:07 +00:00
|
|
|
}) : super(name: name, password: password);
|
2023-07-25 15:21:49 +00:00
|
|
|
|
2023-07-27 14:30:07 +00:00
|
|
|
final String seedKey;
|
2023-07-31 13:10:33 +00:00
|
|
|
final DerivationType? derivationType;
|
2023-07-25 15:21:49 +00:00
|
|
|
}
|
2023-07-24 20:23:09 +00:00
|
|
|
|
|
|
|
class NanoWalletService extends WalletService<NanoNewWalletCredentials,
|
2023-07-25 15:21:49 +00:00
|
|
|
NanoRestoreWalletFromSeedCredentials, NanoRestoreWalletFromKeysCredentials> {
|
2023-07-24 20:23:09 +00:00
|
|
|
NanoWalletService(this.walletInfoSource);
|
|
|
|
|
|
|
|
final Box<WalletInfo> walletInfoSource;
|
|
|
|
|
2023-07-25 15:21:49 +00:00
|
|
|
static bool walletFilesExist(String path) =>
|
|
|
|
!File(path).existsSync() && !File('$path.keys').existsSync();
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
@override
|
2023-07-25 15:21:49 +00:00
|
|
|
WalletType getType() => WalletType.nano;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<WalletBase> create(NanoNewWalletCredentials credentials) async {
|
2023-07-24 20:23:09 +00:00
|
|
|
final mnemonic = bip39.generateMnemonic();
|
2023-07-28 14:36:50 +00:00
|
|
|
final nanoWalletInfo = NanoWalletInfo(
|
2023-07-24 20:23:09 +00:00
|
|
|
walletInfo: credentials.walletInfo!,
|
2023-07-28 14:36:50 +00:00
|
|
|
derivationType: DerivationType.nano,
|
|
|
|
);
|
|
|
|
|
|
|
|
final wallet = NanoWallet(
|
|
|
|
walletInfo: nanoWalletInfo,
|
2023-07-24 20:23:09 +00:00
|
|
|
mnemonic: mnemonic,
|
|
|
|
password: credentials.password!,
|
|
|
|
);
|
|
|
|
return wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-07-25 15:21:49 +00:00
|
|
|
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()));
|
|
|
|
await walletInfoSource.delete(walletInfo.key);
|
|
|
|
}
|
2023-07-24 20:23:09 +00:00
|
|
|
|
|
|
|
@override
|
2023-07-25 15:21:49 +00:00
|
|
|
Future<void> rename(String currentName, String password, String newName) async {
|
|
|
|
// final currentWalletInfo = walletInfoSource.values
|
|
|
|
// .firstWhere((info) => info.id == WalletBase.idFor(currentName, getType()));
|
|
|
|
// final currentWallet = NanoWallet(walletInfo: currentWalletInfo);
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-07-25 15:21:49 +00:00
|
|
|
// await currentWallet.renameWalletFiles(newName);
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-07-25 15:21:49 +00:00
|
|
|
// final newWalletInfo = currentWalletInfo;
|
|
|
|
// newWalletInfo.id = WalletBase.idFor(newName, getType());
|
|
|
|
// newWalletInfo.name = newName;
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-07-25 15:21:49 +00:00
|
|
|
// await walletInfoSource.put(currentWalletInfo.key, newWalletInfo);
|
2023-07-24 20:23:09 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 14:30:07 +00:00
|
|
|
Future<DerivationType> compareDerivationMethods({String? mnemonic, String? seedKey}) async {
|
2023-07-28 14:36:50 +00:00
|
|
|
if (seedKey?.length == 128) {
|
|
|
|
return DerivationType.bip39;
|
|
|
|
}
|
|
|
|
if (mnemonic?.split(' ').length == 12) {
|
|
|
|
return DerivationType.bip39;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
NanoClient nanoClient = NanoClient();
|
|
|
|
// TODO: figure out how to load the current node uri in this context:
|
|
|
|
nanoClient.connect(Node(
|
|
|
|
uri: NanoClient.BACKUP_NODE_URI,
|
|
|
|
type: WalletType.nano,
|
|
|
|
));
|
|
|
|
|
|
|
|
late String publicAddressStandard;
|
|
|
|
late String publicAddressBip39;
|
|
|
|
|
|
|
|
if (seedKey == null) {
|
|
|
|
seedKey = bip39.mnemonicToEntropy(mnemonic).toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
publicAddressBip39 = await NanoUtil.hdSeedToAddress(seedKey, 0);
|
|
|
|
publicAddressStandard = await NanoUtil.seedToAddress(seedKey, 0);
|
|
|
|
|
|
|
|
// check if either has a balance:
|
|
|
|
|
|
|
|
NanoBalance bip39Balance = await nanoClient.getBalance(publicAddressBip39);
|
|
|
|
NanoBalance standardBalance = await nanoClient.getBalance(publicAddressStandard);
|
|
|
|
|
2023-07-31 13:10:33 +00:00
|
|
|
// TODO: this is a super basic implementation, and if both addresses have balances
|
2023-07-28 14:36:50 +00:00
|
|
|
// it might not be the one that the user wants, though it is unlikely
|
|
|
|
if (bip39Balance.currentBalance > standardBalance.currentBalance) {
|
|
|
|
return DerivationType.bip39;
|
|
|
|
} else {
|
|
|
|
return DerivationType.nano;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return DerivationType.nano;
|
|
|
|
}
|
2023-07-27 14:30:07 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
@override
|
2023-07-25 15:21:49 +00:00
|
|
|
Future<NanoWallet> restoreFromKeys(NanoRestoreWalletFromKeysCredentials credentials) async {
|
2023-07-27 14:30:07 +00:00
|
|
|
throw UnimplementedError("restoreFromKeys");
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-07-31 13:10:33 +00:00
|
|
|
// TODO: mnemonic can't be derived from the seedKey in the nano standard derivation
|
2023-07-28 14:36:50 +00:00
|
|
|
// which complicates things
|
|
|
|
|
2023-07-31 13:10:33 +00:00
|
|
|
// DerivationType derivationType = credentials.derivationType ?? await compareDerivationMethods(seedKey: credentials.seedKey);
|
2023-07-28 14:36:50 +00:00
|
|
|
// String? mnemonic;
|
|
|
|
// final nanoWalletInfo = NanoWalletInfo(
|
|
|
|
// walletInfo: credentials.walletInfo!,
|
|
|
|
// derivationType: derivationType,
|
|
|
|
// );
|
|
|
|
// final wallet = await NanoWallet(
|
|
|
|
// password: credentials.password!,
|
|
|
|
// mnemonic: mnemonic ?? "", // we can't derive the mnemonic from the key in all cases
|
|
|
|
// walletInfo: nanoWalletInfo,
|
|
|
|
// );
|
|
|
|
// await wallet.init();
|
|
|
|
// await wallet.save();
|
|
|
|
// return wallet;
|
2023-07-26 17:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 20:23:09 +00:00
|
|
|
@override
|
2023-07-25 15:21:49 +00:00
|
|
|
Future<NanoWallet> restoreFromSeed(NanoRestoreWalletFromSeedCredentials credentials) async {
|
2023-07-26 17:15:22 +00:00
|
|
|
if (!bip39.validateMnemonic(credentials.mnemonic)) {
|
|
|
|
throw NanoMnemonicIsIncorrectException();
|
|
|
|
}
|
2023-07-25 15:21:49 +00:00
|
|
|
|
2023-07-26 17:15:22 +00:00
|
|
|
if (!NanoMnemomics.validateMnemonic(credentials.mnemonic.split(' '))) {
|
|
|
|
throw NanoMnemonicIsIncorrectException();
|
|
|
|
}
|
|
|
|
|
2023-07-31 13:10:33 +00:00
|
|
|
DerivationType derivationType = credentials.derivationType ??
|
|
|
|
await compareDerivationMethods(mnemonic: credentials.mnemonic);
|
2023-07-26 17:15:22 +00:00
|
|
|
|
2023-07-28 14:36:50 +00:00
|
|
|
final nanoWalletInfo = NanoWalletInfo(
|
|
|
|
walletInfo: credentials.walletInfo!,
|
|
|
|
derivationType: derivationType,
|
|
|
|
);
|
2023-07-26 17:15:22 +00:00
|
|
|
|
|
|
|
final wallet = await NanoWallet(
|
|
|
|
password: credentials.password!,
|
|
|
|
mnemonic: credentials.mnemonic,
|
2023-07-28 14:36:50 +00:00
|
|
|
walletInfo: nanoWalletInfo,
|
2023-07-26 17:15:22 +00:00
|
|
|
);
|
|
|
|
|
2023-07-27 14:30:07 +00:00
|
|
|
await wallet.init();
|
2023-07-26 17:15:22 +00:00
|
|
|
await wallet.save();
|
|
|
|
return wallet;
|
2023-07-24 20:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-07-26 17:15:22 +00:00
|
|
|
Future<bool> isWalletExit(String name) async =>
|
|
|
|
File(await pathForWallet(name: name, type: getType())).existsSync();
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-07-25 15:21:49 +00:00
|
|
|
@override
|
2023-07-26 17:15:22 +00:00
|
|
|
Future<NanoWallet> openWallet(String name, String password) async {
|
|
|
|
final walletInfo =
|
|
|
|
walletInfoSource.values.firstWhere((info) => info.id == WalletBase.idFor(name, getType()));
|
|
|
|
final wallet = await NanoWalletBase.open(
|
|
|
|
name: name,
|
|
|
|
password: password,
|
|
|
|
walletInfo: walletInfo,
|
|
|
|
);
|
|
|
|
|
|
|
|
await wallet.init();
|
|
|
|
await wallet.save();
|
|
|
|
return wallet;
|
2023-07-24 20:23:09 +00:00
|
|
|
}
|
|
|
|
}
|