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';
|
|
|
|
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_client.dart';
|
2023-08-04 14:54:20 +00:00
|
|
|
import 'package:cw_nano/nano_mnemonic.dart' as nm;
|
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-09-06 14:12:36 +00:00
|
|
|
import 'package:cw_nano/nano_wallet_creation_credentials.dart';
|
2023-07-24 20:23:09 +00:00
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:bip39/bip39.dart' as bip39;
|
2023-08-04 14:54:20 +00:00
|
|
|
import 'package:nanodart/nanodart.dart';
|
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-08-04 14:54:20 +00:00
|
|
|
// nano standard:
|
|
|
|
DerivationType derivationType = DerivationType.nano;
|
|
|
|
String seedKey = NanoSeeds.generateSeed();
|
|
|
|
String mnemonic = NanoUtil.seedToMnemonic(seedKey);
|
|
|
|
|
|
|
|
// bip39:
|
|
|
|
// derivationType derivationType = DerivationType.bip39;
|
|
|
|
// String mnemonic = bip39.generateMnemonic();
|
|
|
|
|
2023-08-14 22:02:12 +00:00
|
|
|
credentials.walletInfo!.derivationType = derivationType;
|
2023-07-28 14:36:50 +00:00
|
|
|
|
|
|
|
final wallet = NanoWallet(
|
2023-08-14 22:02:12 +00:00
|
|
|
walletInfo: credentials.walletInfo!,
|
2023-07-24 20:23:09 +00:00
|
|
|
mnemonic: mnemonic,
|
|
|
|
password: credentials.password!,
|
|
|
|
);
|
2023-08-04 14:54:20 +00:00
|
|
|
wallet.init();
|
2023-07-24 20:23:09 +00:00
|
|
|
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 {
|
2023-08-08 13:49:13 +00:00
|
|
|
final currentWalletInfo = walletInfoSource.values
|
|
|
|
.firstWhere((info) => info.id == WalletBase.idFor(currentName, getType()));
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-08-15 15:59:31 +00:00
|
|
|
currentWalletInfo.derivationType = DerivationType.nano; // doesn't matter for the rename action
|
2023-08-08 13:49:13 +00:00
|
|
|
|
|
|
|
String randomWords =
|
|
|
|
(List<String>.from(nm.NanoMnemomics.WORDLIST)..shuffle()).take(24).join(' ');
|
|
|
|
final currentWallet =
|
2023-08-14 22:02:12 +00:00
|
|
|
NanoWallet(walletInfo: currentWalletInfo, password: password, mnemonic: randomWords);
|
2023-08-08 13:49:13 +00:00
|
|
|
|
|
|
|
await currentWallet.renameWalletFiles(newName);
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-08-08 13:49:13 +00:00
|
|
|
final newWalletInfo = currentWalletInfo;
|
|
|
|
newWalletInfo.id = WalletBase.idFor(newName, getType());
|
|
|
|
newWalletInfo.name = newName;
|
2023-07-24 20:23:09 +00:00
|
|
|
|
2023-08-08 13:49:13 +00:00
|
|
|
await walletInfoSource.put(currentWalletInfo.key, newWalletInfo);
|
2023-07-24 20:23:09 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 16:22:25 +00:00
|
|
|
static Future<dynamic> getInfoFromSeedOrMnemonic(
|
|
|
|
DerivationType derivationType, {
|
|
|
|
String? seedKey,
|
|
|
|
String? mnemonic,
|
|
|
|
required Node node,
|
|
|
|
}) async {
|
2023-08-15 15:59:31 +00:00
|
|
|
NanoClient nanoClient = NanoClient();
|
2023-08-16 16:22:25 +00:00
|
|
|
nanoClient.connect(node);
|
2023-08-15 15:59:31 +00:00
|
|
|
late String publicAddress;
|
|
|
|
|
|
|
|
if (seedKey != null) {
|
|
|
|
if (derivationType == DerivationType.bip39) {
|
|
|
|
publicAddress = await NanoUtil.hdSeedToAddress(seedKey, 0);
|
|
|
|
} else if (derivationType == DerivationType.nano) {
|
|
|
|
publicAddress = await NanoUtil.seedToAddress(seedKey, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (derivationType == DerivationType.bip39) {
|
|
|
|
if (mnemonic != null) {
|
|
|
|
seedKey = await NanoUtil.hdMnemonicListToSeed(mnemonic.split(' '));
|
|
|
|
publicAddress = await NanoUtil.hdSeedToAddress(seedKey, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (derivationType == DerivationType.nano) {
|
|
|
|
if (mnemonic != null) {
|
|
|
|
seedKey = await NanoUtil.mnemonicToSeed(mnemonic);
|
|
|
|
publicAddress = await NanoUtil.seedToAddress(seedKey, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountInfo = await nanoClient.getAccountInfo(publicAddress);
|
|
|
|
accountInfo["address"] = publicAddress;
|
|
|
|
return accountInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<List<DerivationType>> compareDerivationMethods(
|
2023-08-16 16:22:25 +00:00
|
|
|
{String? mnemonic, String? seedKey, required Node node}) async {
|
2023-08-14 22:02:12 +00:00
|
|
|
if (mnemonic?.split(' ').length == 12) {
|
2023-08-15 15:59:31 +00:00
|
|
|
return [DerivationType.bip39];
|
2023-07-28 14:36:50 +00:00
|
|
|
}
|
2023-08-14 22:02:12 +00:00
|
|
|
if (seedKey?.length == 128) {
|
2023-08-15 15:59:31 +00:00
|
|
|
return [DerivationType.bip39];
|
2023-08-14 22:02:12 +00:00
|
|
|
} else if (seedKey?.length == 64) {
|
2023-08-15 15:59:31 +00:00
|
|
|
return [DerivationType.nano];
|
2023-07-28 14:36:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 22:02:12 +00:00
|
|
|
late String publicAddressStandard;
|
|
|
|
late String publicAddressBip39;
|
|
|
|
|
2023-07-28 14:36:50 +00:00
|
|
|
try {
|
|
|
|
NanoClient nanoClient = NanoClient();
|
2023-08-16 16:22:25 +00:00
|
|
|
nanoClient.connect(node);
|
2023-07-28 14:36:50 +00:00
|
|
|
|
2023-08-14 22:02:12 +00:00
|
|
|
if (mnemonic != null) {
|
|
|
|
seedKey = await NanoUtil.hdMnemonicListToSeed(mnemonic.split(' '));
|
|
|
|
publicAddressBip39 = await NanoUtil.hdSeedToAddress(seedKey, 0);
|
|
|
|
|
|
|
|
seedKey = await NanoUtil.mnemonicToSeed(mnemonic);
|
|
|
|
publicAddressStandard = await NanoUtil.seedToAddress(seedKey, 0);
|
|
|
|
} else if (seedKey != null) {
|
|
|
|
try {
|
|
|
|
publicAddressBip39 = await NanoUtil.hdSeedToAddress(seedKey, 0);
|
|
|
|
} catch (e) {
|
2023-08-15 15:59:31 +00:00
|
|
|
return [DerivationType.nano];
|
2023-08-14 22:02:12 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
publicAddressStandard = await NanoUtil.seedToAddress(seedKey, 0);
|
|
|
|
} catch (e) {
|
2023-08-15 15:59:31 +00:00
|
|
|
return [DerivationType.bip39];
|
2023-08-14 22:02:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if account has a history:
|
|
|
|
var bip39Info;
|
|
|
|
var standardInfo;
|
|
|
|
|
|
|
|
try {
|
|
|
|
bip39Info = await nanoClient.getAccountInfo(publicAddressBip39);
|
|
|
|
} catch (e) {
|
|
|
|
bip39Info = null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
standardInfo = await nanoClient.getAccountInfo(publicAddressStandard);
|
|
|
|
} catch (e) {
|
|
|
|
standardInfo = null;
|
2023-07-28 14:36:50 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 22:02:12 +00:00
|
|
|
// one of these is *probably* null:
|
2023-08-15 15:59:31 +00:00
|
|
|
if ((bip39Info == null || bip39Info["error"] != null) &&
|
|
|
|
(standardInfo != null && standardInfo["error"] == null)) {
|
|
|
|
return [DerivationType.nano];
|
|
|
|
} else if ((standardInfo == null || standardInfo["error"] != null) &&
|
|
|
|
(bip39Info != null && bip39Info["error"] == null)) {
|
|
|
|
return [DerivationType.bip39];
|
2023-08-14 22:02:12 +00:00
|
|
|
}
|
2023-07-28 14:36:50 +00:00
|
|
|
|
2023-08-15 15:59:31 +00:00
|
|
|
// we don't know for sure:
|
|
|
|
return [DerivationType.nano, DerivationType.bip39];
|
2023-07-28 14:36:50 +00:00
|
|
|
} catch (e) {
|
2023-08-15 15:59:31 +00:00
|
|
|
return [DerivationType.unknown];
|
2023-07-28 14:36:50 +00:00
|
|
|
}
|
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-08-16 14:24:20 +00:00
|
|
|
if (credentials.seedKey.contains(' ')) {
|
|
|
|
throw Exception("Invalid key!");
|
|
|
|
} else {
|
|
|
|
if (credentials.seedKey.length != 64 && credentials.seedKey.length != 128) {
|
|
|
|
throw Exception("Invalid key length!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DerivationType derivationType = credentials.derivationType ?? DerivationType.nano;
|
|
|
|
credentials.walletInfo!.derivationType = derivationType;
|
|
|
|
|
|
|
|
final wallet = await NanoWallet(
|
|
|
|
password: credentials.password!,
|
2023-08-16 16:22:25 +00:00
|
|
|
mnemonic: credentials.seedKey, // we can't derive the mnemonic from the key in all cases
|
2023-08-16 14:24:20 +00:00
|
|
|
walletInfo: credentials.walletInfo!,
|
|
|
|
);
|
|
|
|
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-08-15 22:50:00 +00:00
|
|
|
if (credentials.mnemonic.contains(' ')) {
|
|
|
|
if (!bip39.validateMnemonic(credentials.mnemonic)) {
|
|
|
|
throw nm.NanoMnemonicIsIncorrectException();
|
|
|
|
}
|
2023-07-25 15:21:49 +00:00
|
|
|
|
2023-08-15 22:50:00 +00:00
|
|
|
if (!NanoMnemomics.validateMnemonic(credentials.mnemonic.split(' '))) {
|
|
|
|
throw nm.NanoMnemonicIsIncorrectException();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (credentials.mnemonic.length != 64 && credentials.mnemonic.length != 128) {
|
|
|
|
throw Exception("Invalid seed length");
|
|
|
|
}
|
2023-07-26 17:15:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 15:59:31 +00:00
|
|
|
DerivationType derivationType = credentials.derivationType ?? DerivationType.nano;
|
2023-07-26 17:15:22 +00:00
|
|
|
|
2023-08-14 22:02:12 +00:00
|
|
|
credentials.walletInfo!.derivationType = derivationType;
|
2023-07-26 17:15:22 +00:00
|
|
|
|
|
|
|
final wallet = await NanoWallet(
|
|
|
|
password: credentials.password!,
|
|
|
|
mnemonic: credentials.mnemonic,
|
2023-08-14 22:02:12 +00:00
|
|
|
walletInfo: credentials.walletInfo!,
|
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
|
|
|
}
|
|
|
|
}
|