cake_wallet/cw_nano/lib/nano_wallet.dart

281 lines
7.9 KiB
Dart
Raw Normal View History

2023-07-25 15:21:49 +00:00
import 'dart:convert';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/node.dart';
2023-07-24 16:56:20 +00:00
import 'package:cw_core/pathForWallet.dart';
2023-07-25 15:21:49 +00:00
import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/sync_status.dart';
2023-07-27 17:02:10 +00:00
import 'package:cw_core/transaction_direction.dart';
2023-07-24 16:56:20 +00:00
import 'package:cw_core/transaction_priority.dart';
2023-07-25 15:21:49 +00:00
import 'package:cw_core/wallet_addresses.dart';
import 'package:cw_core/wallet_info.dart';
2023-07-26 17:15:22 +00:00
import 'package:cw_nano/file.dart';
2023-07-24 20:23:09 +00:00
import 'package:cw_nano/nano_balance.dart';
2023-07-27 17:02:10 +00:00
import 'package:cw_nano/nano_client.dart';
2023-07-25 15:21:49 +00:00
import 'package:cw_nano/nano_transaction_history.dart';
import 'package:cw_nano/nano_transaction_info.dart';
2023-07-26 17:15:22 +00:00
import 'package:cw_nano/nano_util.dart';
2023-07-28 14:36:50 +00:00
import 'package:cw_nano/nano_wallet_info.dart';
2023-07-31 13:10:33 +00:00
import 'package:cw_nano/pending_nano_transaction.dart';
2023-07-24 16:56:20 +00:00
import 'package:mobx/mobx.dart';
2023-07-25 15:21:49 +00:00
import 'dart:async';
import 'package:cw_nano/nano_wallet_addresses.dart';
2023-07-24 16:56:20 +00:00
import 'package:cw_core/wallet_base.dart';
2023-07-25 15:21:49 +00:00
import 'package:web3dart/web3dart.dart';
2023-07-26 17:15:22 +00:00
import 'package:bip39/bip39.dart' as bip39;
import 'package:bip32/bip32.dart' as bip32;
2023-07-24 16:56:20 +00:00
part 'nano_wallet.g.dart';
class NanoWallet = NanoWalletBase with _$NanoWallet;
2023-07-24 20:23:09 +00:00
abstract class NanoWalletBase
extends WalletBase<NanoBalance, NanoTransactionHistory, NanoTransactionInfo> with Store {
2023-07-25 15:21:49 +00:00
NanoWalletBase({
2023-07-28 14:36:50 +00:00
required NanoWalletInfo walletInfo,
2023-07-25 15:21:49 +00:00
required String mnemonic,
required String password,
NanoBalance? initialBalance,
}) : syncStatus = NotConnectedSyncStatus(),
_password = password,
_mnemonic = mnemonic,
2023-07-28 14:36:50 +00:00
_derivationType = walletInfo.derivationType,
2023-07-24 16:56:20 +00:00
_isTransactionUpdating = false,
2023-07-27 17:02:10 +00:00
_client = NanoClient(),
2023-07-24 16:56:20 +00:00
walletAddresses = NanoWalletAddresses(walletInfo),
2023-07-25 15:21:49 +00:00
balance = ObservableMap<CryptoCurrency, NanoBalance>.of({
2023-07-25 17:36:24 +00:00
CryptoCurrency.nano: initialBalance ??
2023-07-25 15:21:49 +00:00
NanoBalance(currentBalance: BigInt.zero, receivableBalance: BigInt.zero)
}),
2023-07-24 16:56:20 +00:00
super(walletInfo) {
2023-07-25 15:21:49 +00:00
this.walletInfo = walletInfo;
2023-07-27 17:02:10 +00:00
transactionHistory = NanoTransactionHistory(walletInfo: walletInfo, password: password);
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
final String _mnemonic;
final String _password;
2023-07-26 17:15:22 +00:00
final DerivationType _derivationType;
late final String _privateKey;
late final String _publicAddress;
2023-07-27 14:30:07 +00:00
late final String _seedKey;
2023-07-25 15:21:49 +00:00
2023-07-27 17:02:10 +00:00
late NanoClient _client;
2023-07-25 15:21:49 +00:00
bool _isTransactionUpdating;
2023-07-24 16:56:20 +00:00
@override
2023-07-25 15:21:49 +00:00
WalletAddresses walletAddresses;
2023-07-24 16:56:20 +00:00
@override
@observable
SyncStatus syncStatus;
@override
@observable
2023-07-25 15:21:49 +00:00
late ObservableMap<CryptoCurrency, NanoBalance> balance;
2023-07-24 16:56:20 +00:00
2023-07-26 17:15:22 +00:00
// initialize the different forms of private / public key we'll need:
Future<void> init() async {
final String type = (_derivationType == DerivationType.nano) ? "standard" : "hd";
2023-07-27 14:30:07 +00:00
_seedKey = bip39.mnemonicToEntropy(_mnemonic).toUpperCase();
_privateKey = await NanoUtil.uniSeedToPrivate(_seedKey, 0, type);
_publicAddress = await NanoUtil.uniSeedToAddress(_seedKey, 0, type);
this.walletInfo.address = _publicAddress;
2023-07-26 17:15:22 +00:00
await walletAddresses.init();
2023-07-27 17:02:10 +00:00
await transactionHistory.init();
2023-07-26 17:15:22 +00:00
await save();
}
2023-07-24 20:23:09 +00:00
2023-07-24 16:56:20 +00:00
@override
2023-07-25 15:21:49 +00:00
int calculateEstimatedFee(TransactionPriority priority, int? amount) {
2023-07-26 17:15:22 +00:00
return 0; // always 0 :)
2023-07-25 15:21:49 +00:00
}
2023-07-24 16:56:20 +00:00
@override
2023-07-25 15:21:49 +00:00
Future<void> changePassword(String password) {
2023-07-25 17:36:24 +00:00
print("e");
2023-07-25 15:21:49 +00:00
throw UnimplementedError("changePassword");
2023-07-24 16:56:20 +00:00
}
@override
2023-07-25 15:21:49 +00:00
void close() {
2023-07-27 17:02:10 +00:00
_client.stop();
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
@action
2023-07-24 16:56:20 +00:00
@override
2023-07-25 15:21:49 +00:00
Future<void> connectToNode({required Node node}) async {
2023-07-27 17:02:10 +00:00
try {
syncStatus = ConnectingSyncStatus();
final isConnected = _client.connect(node);
if (!isConnected) {
throw Exception("Ethereum Node connection failed");
}
// _client.setListeners(_privateKey.address, _onNewTransaction);
_updateBalance();
syncStatus = ConnectedSyncStatus();
} catch (e) {
syncStatus = FailedSyncStatus();
}
2023-07-24 16:56:20 +00:00
}
@override
Future<PendingTransaction> createTransaction(Object credentials) async {
2023-07-25 17:36:24 +00:00
print("g");
2023-07-28 14:36:50 +00:00
print(credentials);
// throw UnimplementedError();
2023-07-31 13:10:33 +00:00
return PendingNanoTransaction(
amount: BigInt.one,
fee: 0,
id: "test",
nanoClient: _client,
);
// return PendingNanoTransaction(txb.build(), type,
// electrumClient: electrumClient, amount: amount, fee: fee)
// ..addListener((transaction) async {
// transactionHistory.addOne(transaction);
// await updateBalance();
// });
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
Future<void> updateTransactions() async {
2023-07-27 17:02:10 +00:00
try {
if (_isTransactionUpdating) {
return;
}
_isTransactionUpdating = true;
final transactions = await fetchTransactions();
transactionHistory.addMany(transactions);
await transactionHistory.save();
_isTransactionUpdating = false;
} catch (_) {
_isTransactionUpdating = false;
}
2023-07-24 16:56:20 +00:00
}
@override
2023-07-25 15:21:49 +00:00
Future<Map<String, NanoTransactionInfo>> fetchTransactions() async {
2023-07-27 17:02:10 +00:00
String address = _publicAddress;
final transactions = await _client.fetchTransactions(address);
final Map<String, NanoTransactionInfo> result = {};
for (var transactionModel in transactions) {
result[transactionModel.hash] = NanoTransactionInfo(
id: transactionModel.hash,
amountRaw: transactionModel.amount,
height: transactionModel.height,
direction: transactionModel.account == address
? TransactionDirection.outgoing
: TransactionDirection.incoming,
confirmed: transactionModel.confirmed,
date: transactionModel.date ?? DateTime.now(),
confirmations: transactionModel.confirmed ? 1 : 0,
);
}
return result;
2023-07-24 16:56:20 +00:00
}
@override
2023-07-25 17:36:24 +00:00
Object get keys {
print("j");
throw UnimplementedError("keys");
}
2023-07-24 16:56:20 +00:00
@override
2023-07-28 14:36:50 +00:00
Future<void> rescan({required int height}) async {
fetchTransactions();
_updateBalance();
return;
2023-07-24 16:56:20 +00:00
}
@override
2023-07-25 15:21:49 +00:00
Future<void> save() async {
2023-07-26 17:15:22 +00:00
await walletAddresses.updateAddressesInBox();
final path = await makePath();
await write(path: path, password: _password, data: toJSON());
await transactionHistory.save();
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
@override
String get seed => _mnemonic;
2023-07-24 16:56:20 +00:00
2023-07-25 15:21:49 +00:00
@action
2023-07-24 16:56:20 +00:00
@override
2023-07-25 15:21:49 +00:00
Future<void> startSync() async {
2023-07-27 17:02:10 +00:00
try {
syncStatus = AttemptingSyncStatus();
await _updateBalance();
await updateTransactions();
syncStatus = SyncedSyncStatus();
} catch (e) {
syncStatus = FailedSyncStatus();
}
2023-07-24 16:56:20 +00:00
}
2023-07-26 17:15:22 +00:00
Future<String> makePath() async => pathForWallet(name: walletInfo.name, type: walletInfo.type);
String toJSON() => json.encode({
2023-07-27 14:30:07 +00:00
'seedKey': _seedKey,
2023-07-26 17:15:22 +00:00
'mnemonic': _mnemonic,
// 'balance': balance[currency]!.toJSON(),
2023-07-27 14:30:07 +00:00
'derivationType': _derivationType.toString()
2023-07-26 17:15:22 +00:00
});
2023-07-25 15:21:49 +00:00
static Future<NanoWallet> open({
required String name,
required String password,
required WalletInfo walletInfo,
}) async {
2023-07-27 14:30:07 +00:00
final path = await pathForWallet(name: name, type: walletInfo.type);
final jsonSource = await read(path: path, password: password);
final data = json.decode(jsonSource) as Map;
final mnemonic = data['mnemonic'] as String;
2023-07-27 17:02:10 +00:00
final balance = NanoBalance.fromString(
formattedCurrentBalance: data['balance'] as String? ?? "0",
formattedReceivableBalance: "0");
2023-07-27 14:30:07 +00:00
DerivationType derivationType = DerivationType.bip39;
2023-07-27 17:02:10 +00:00
if (data['derivationType'] == "DerivationType.nano") {
2023-07-27 14:30:07 +00:00
derivationType = DerivationType.nano;
}
2023-07-28 14:36:50 +00:00
final nanoWalletInfo = NanoWalletInfo(
2023-07-27 14:30:07 +00:00
walletInfo: walletInfo,
2023-07-28 14:36:50 +00:00
derivationType: derivationType,
);
return NanoWallet(
walletInfo: nanoWalletInfo,
2023-07-27 14:30:07 +00:00
password: password,
mnemonic: mnemonic,
initialBalance: balance,
);
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
Future<void> _updateBalance() async {
2023-07-28 13:30:11 +00:00
balance[currency] = await _client.getBalance(_publicAddress);
2023-07-25 15:21:49 +00:00
await save();
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
Future<void>? updateBalance() async => await _updateBalance();
2023-07-24 16:56:20 +00:00
2023-07-25 15:21:49 +00:00
void _onNewTransaction(FilterEvent event) {
throw UnimplementedError();
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
@override
Future<void> renameWalletFiles(String newWalletName) async {
print("rename");
throw UnimplementedError();
2023-07-24 16:56:20 +00:00
}
}