2023-01-04 14:51:23 +00:00
|
|
|
import 'dart:async';
|
2023-01-03 20:19:02 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
|
|
|
import 'package:cw_core/node.dart';
|
|
|
|
import 'package:cw_core/pathForWallet.dart';
|
|
|
|
import 'package:cw_core/pending_transaction.dart';
|
|
|
|
import 'package:cw_core/sync_status.dart';
|
|
|
|
import 'package:cw_core/transaction_priority.dart';
|
|
|
|
import 'package:cw_core/wallet_addresses.dart';
|
2022-12-28 15:02:04 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
2023-01-03 20:19:02 +00:00
|
|
|
import 'package:cw_core/wallet_info.dart';
|
2022-12-28 15:02:04 +00:00
|
|
|
import 'package:cw_ethereum/ethereum_balance.dart';
|
2023-01-04 14:51:23 +00:00
|
|
|
import 'package:cw_ethereum/ethereum_client.dart';
|
2022-12-28 15:02:04 +00:00
|
|
|
import 'package:cw_ethereum/ethereum_transaction_history.dart';
|
|
|
|
import 'package:cw_ethereum/ethereum_transaction_info.dart';
|
2023-01-03 20:19:02 +00:00
|
|
|
import 'package:cw_ethereum/ethereum_wallet_addresses.dart';
|
|
|
|
import 'package:cw_ethereum/file.dart';
|
2022-12-28 15:02:04 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
2023-01-04 14:51:23 +00:00
|
|
|
import 'package:web3dart/web3dart.dart';
|
2022-12-28 15:02:04 +00:00
|
|
|
|
|
|
|
part 'ethereum_wallet.g.dart';
|
|
|
|
|
|
|
|
class EthereumWallet = EthereumWalletBase with _$EthereumWallet;
|
|
|
|
|
|
|
|
abstract class EthereumWalletBase
|
|
|
|
extends WalletBase<EthereumBalance, EthereumTransactionHistory, EthereumTransactionInfo>
|
|
|
|
with Store {
|
2023-01-03 20:19:02 +00:00
|
|
|
EthereumWalletBase({
|
|
|
|
required WalletInfo walletInfo,
|
|
|
|
required this.mnemonic,
|
|
|
|
required this.privateKey,
|
|
|
|
required String password,
|
2023-01-04 14:51:23 +00:00
|
|
|
EthereumBalance? initialBalance,
|
2023-01-03 20:19:02 +00:00
|
|
|
}) : syncStatus = NotConnectedSyncStatus(),
|
|
|
|
_password = password,
|
|
|
|
walletAddresses = EthereumWalletAddresses(walletInfo),
|
2023-01-04 14:51:23 +00:00
|
|
|
balance = ObservableMap<CryptoCurrency, EthereumBalance>.of(
|
|
|
|
{CryptoCurrency.eth: initialBalance ?? EthereumBalance(0, 0)}),
|
2023-01-03 20:19:02 +00:00
|
|
|
super(walletInfo) {
|
|
|
|
this.walletInfo = walletInfo;
|
|
|
|
transactionHistory = EthereumTransactionHistory();
|
2023-01-05 18:03:55 +00:00
|
|
|
walletAddresses.address = EthPrivateKey.fromHex(privateKey).address.toString();
|
2023-01-03 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final String mnemonic;
|
|
|
|
final String privateKey;
|
|
|
|
final String _password;
|
|
|
|
|
2023-01-04 14:51:23 +00:00
|
|
|
late EthereumClient _client;
|
|
|
|
|
|
|
|
EtherAmount? _gasPrice;
|
|
|
|
|
|
|
|
@override
|
|
|
|
WalletAddresses walletAddresses;
|
|
|
|
|
2023-01-03 20:19:02 +00:00
|
|
|
@override
|
|
|
|
SyncStatus syncStatus;
|
|
|
|
|
|
|
|
@override
|
2023-01-04 14:51:23 +00:00
|
|
|
@observable
|
|
|
|
late ObservableMap<CryptoCurrency, EthereumBalance> balance;
|
2023-01-03 20:19:02 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
int calculateEstimatedFee(TransactionPriority priority, int? amount) {
|
2023-01-04 14:51:23 +00:00
|
|
|
throw UnimplementedError("calculateEstimatedFee");
|
2023-01-03 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> changePassword(String password) {
|
2023-01-04 14:51:23 +00:00
|
|
|
throw UnimplementedError("changePassword");
|
2023-01-03 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void close() {}
|
|
|
|
|
|
|
|
@override
|
2023-01-04 14:51:23 +00:00
|
|
|
Future<void> connectToNode({required Node node}) async {
|
|
|
|
try {
|
|
|
|
syncStatus = ConnectingSyncStatus();
|
|
|
|
|
|
|
|
final isConnected = await _client.connect(node);
|
|
|
|
|
|
|
|
if (!isConnected) {
|
|
|
|
throw Exception("Ethereum Node connection failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateBalance();
|
|
|
|
|
|
|
|
syncStatus = ConnectedSyncStatus();
|
|
|
|
} catch (e) {
|
|
|
|
syncStatus = FailedSyncStatus();
|
|
|
|
}
|
2023-01-03 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<PendingTransaction> createTransaction(Object credentials) {
|
2023-01-04 14:51:23 +00:00
|
|
|
throw UnimplementedError("createTransaction");
|
2023-01-03 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<Map<String, EthereumTransactionInfo>> fetchTransactions() {
|
2023-01-04 14:51:23 +00:00
|
|
|
throw UnimplementedError("fetchTransactions");
|
2023-01-03 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-01-04 14:51:23 +00:00
|
|
|
Object get keys => throw UnimplementedError("keys");
|
2023-01-03 20:19:02 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> rescan({required int height}) {
|
2023-01-04 14:51:23 +00:00
|
|
|
throw UnimplementedError("rescan");
|
2023-01-03 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> save() async {
|
|
|
|
final path = await makePath();
|
|
|
|
await write(path: path, password: _password, data: toJSON());
|
|
|
|
await transactionHistory.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get seed => mnemonic;
|
|
|
|
|
|
|
|
@override
|
2023-01-04 14:51:23 +00:00
|
|
|
Future<void> startSync() async {
|
|
|
|
try {
|
|
|
|
syncStatus = AttemptingSyncStatus();
|
|
|
|
await _updateBalance();
|
|
|
|
_gasPrice = await _client.getGasPrice();
|
|
|
|
|
|
|
|
Timer.periodic(
|
|
|
|
const Duration(minutes: 1), (timer) async => _gasPrice = await _client.getGasPrice());
|
|
|
|
|
|
|
|
syncStatus = SyncedSyncStatus();
|
|
|
|
} catch (e, stacktrace) {
|
|
|
|
print(stacktrace);
|
|
|
|
print(e.toString());
|
|
|
|
syncStatus = FailedSyncStatus();
|
|
|
|
}
|
2023-01-03 20:19:02 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 14:51:23 +00:00
|
|
|
int feeRate() {
|
|
|
|
if (_gasPrice != null) {
|
|
|
|
return _gasPrice!.getInEther.toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-01-03 20:19:02 +00:00
|
|
|
|
|
|
|
Future<String> makePath() async => pathForWallet(name: walletInfo.name, type: walletInfo.type);
|
|
|
|
|
|
|
|
String toJSON() => json.encode({
|
|
|
|
'mnemonic': mnemonic,
|
2023-01-04 14:51:23 +00:00
|
|
|
'balance': balance[currency]!.toJSON(),
|
2023-01-03 20:19:02 +00:00
|
|
|
// TODO: save other attributes
|
|
|
|
});
|
2023-01-04 14:51:23 +00:00
|
|
|
|
|
|
|
Future<void> _updateBalance() async {
|
|
|
|
balance[currency] = await _fetchBalances();
|
|
|
|
await save();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<EthereumBalance> _fetchBalances() async {
|
|
|
|
final balance = await _client.getBalance(privateKey);
|
|
|
|
|
|
|
|
return EthereumBalance(balance.getInEther.toInt(), balance.getInEther.toInt());
|
|
|
|
}
|
2022-12-28 15:02:04 +00:00
|
|
|
}
|