cake_wallet/cw_nano/lib/nano_wallet.dart

202 lines
5.2 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-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-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-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-26 17:15:22 +00:00
enum DerivationType { bip39, nano }
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({
required WalletInfo walletInfo,
required String mnemonic,
required String password,
2023-07-26 17:15:22 +00:00
required DerivationType derivationType,
2023-07-25 15:21:49 +00:00
NanoBalance? initialBalance,
}) : syncStatus = NotConnectedSyncStatus(),
_password = password,
_mnemonic = mnemonic,
2023-07-26 17:15:22 +00:00
_derivationType = derivationType,
2023-07-24 16:56:20 +00:00
_isTransactionUpdating = false,
2023-07-25 15:21:49 +00:00
_priorityFees = [],
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;
transactionHistory = NanoTransactionHistory();
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;
late final String _seed;
2023-07-25 15:21:49 +00:00
List<int> _priorityFees;
int? _gasPrice;
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";
_seed = bip39.mnemonicToEntropy(_mnemonic).toUpperCase();
_privateKey = await NanoUtil.uniSeedToPrivate(_mnemonic, 0, type);
_publicAddress = await NanoUtil.uniSeedToAddress(_mnemonic, 0, type);
await walletAddresses.init();
// await transactionHistory.init();
// walletAddresses.address = _privateKey.address.toString();
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() {
// _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-25 17:36:24 +00:00
print("f");
2023-07-25 15:21:49 +00:00
throw UnimplementedError();
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-24 20:23:09 +00:00
throw UnimplementedError();
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
Future<void> updateTransactions() async {
2023-07-25 17:36:24 +00:00
print("h");
2023-07-25 15:21:49 +00:00
throw UnimplementedError();
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-25 17:36:24 +00:00
print("i");
2023-07-25 15:21:49 +00:00
throw UnimplementedError();
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-25 15:21:49 +00:00
Future<void> rescan({required int height}) {
2023-07-25 17:36:24 +00:00
print("k");
2023-07-25 15:21:49 +00:00
throw UnimplementedError("rescan");
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 {
throw UnimplementedError();
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
int feeRate(TransactionPriority priority) {
throw UnimplementedError();
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({
'mnemonic': _mnemonic,
// 'balance': balance[currency]!.toJSON(),
});
2023-07-25 15:21:49 +00:00
static Future<NanoWallet> open({
required String name,
required String password,
required WalletInfo walletInfo,
}) async {
throw UnimplementedError();
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
Future<void> _updateBalance() async {
await save();
2023-07-24 16:56:20 +00:00
}
2023-07-25 15:21:49 +00:00
Future<EthPrivateKey> getPrivateKey(String mnemonic, String password) async {
2023-07-25 17:36:24 +00:00
print("o");
2023-07-25 15:21:49 +00:00
throw UnimplementedError();
}
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
}
}