cake_wallet/cw_ethereum/lib/ethereum_wallet.dart
2023-03-16 19:24:21 +02:00

304 lines
9.2 KiB
Dart

import 'dart:async';
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';
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:cw_ethereum/ethereum_balance.dart';
import 'package:cw_ethereum/ethereum_client.dart';
import 'package:cw_ethereum/ethereum_exceptions.dart';
import 'package:cw_ethereum/ethereum_transaction_credentials.dart';
import 'package:cw_ethereum/ethereum_transaction_history.dart';
import 'package:cw_ethereum/ethereum_transaction_info.dart';
import 'package:cw_ethereum/ethereum_transaction_priority.dart';
import 'package:cw_ethereum/ethereum_wallet_addresses.dart';
import 'package:cw_ethereum/file.dart';
import 'package:cw_ethereum/pending_ethereum_transaction.dart';
import 'package:mobx/mobx.dart';
import 'package:web3dart/web3dart.dart';
import 'package:ed25519_hd_key/ed25519_hd_key.dart';
import 'package:bip39/bip39.dart' as bip39;
import 'package:hex/hex.dart';
part 'ethereum_wallet.g.dart';
class EthereumWallet = EthereumWalletBase with _$EthereumWallet;
abstract class EthereumWalletBase
extends WalletBase<EthereumBalance, EthereumTransactionHistory, EthereumTransactionInfo>
with Store {
EthereumWalletBase({
required WalletInfo walletInfo,
required String mnemonic,
required String password,
EthereumBalance? initialBalance,
}) : syncStatus = NotConnectedSyncStatus(),
_password = password,
_mnemonic = mnemonic,
_priorityFees = [],
_client = EthereumClient(),
walletAddresses = EthereumWalletAddresses(walletInfo),
balance = ObservableMap<CryptoCurrency, EthereumBalance>.of(
{CryptoCurrency.eth: initialBalance ?? EthereumBalance(available: 0, additional: 0)}),
super(walletInfo) {
this.walletInfo = walletInfo;
}
final String _mnemonic;
final String _password;
late final String _privateKey;
late EthereumClient _client;
List<int> _priorityFees;
int? _gasPrice;
@override
WalletAddresses walletAddresses;
@override
@observable
SyncStatus syncStatus;
@override
@observable
late ObservableMap<CryptoCurrency, EthereumBalance> balance;
Future<void> init() async {
_privateKey = await getPrivateKey(_mnemonic, _password);
transactionHistory = EthereumTransactionHistory();
walletAddresses.address = EthPrivateKey.fromHex(_privateKey).address.toString();
}
@override
int calculateEstimatedFee(TransactionPriority priority, int? amount) {
try {
if (priority is EthereumTransactionPriority) {
return _gasPrice! * _priorityFees[priority.raw];
}
return 0;
} catch (e) {
return 0;
}
}
@override
Future<void> changePassword(String password) {
throw UnimplementedError("changePassword");
}
@override
void close() {}
@action
@override
Future<void> connectToNode({required Node node}) async {
try {
syncStatus = ConnectingSyncStatus();
final isConnected = _client.connect(node);
if (!isConnected) {
throw Exception("Ethereum Node connection failed");
}
_updateBalance();
syncStatus = ConnectedSyncStatus();
} catch (e) {
syncStatus = FailedSyncStatus();
}
}
@override
Future<PendingTransaction> createTransaction(Object credentials) async {
final _credentials = credentials as EthereumTransactionCredentials;
final outputs = _credentials.outputs;
final hasMultiDestination = outputs.length > 1;
final balance = await _client.getBalance(_privateKey);
if (hasMultiDestination) {
outputs.any((element) => element.sendAll);
}
// if (hasMultiDestination) {
// if (outputs.any((item) => item.sendAll
// || (item.formattedCryptoAmount ?? 0) <= 0)) {
// throw EthereumTransactionCreationException();
// }
//
// final BigInt totalAmount = outputs.fold(0, (acc, value) =>
// acc + (value.formattedCryptoAmount ?? 0));
//
// if (balance.getInWei < EtherAmount.inWei(totalAmount)) {
// throw MoneroTransactionCreationException('Wrong balance. Not enough XMR on your balance.');
// }
//
// final moneroOutputs = outputs.map((output) {
// final outputAddress = output.isParsedAddress
// ? output.extractedAddress
// : output.address;
//
// return MoneroOutput(
// address: outputAddress!,
// amount: output.cryptoAmount!.replaceAll(',', '.'));
// }).toList();
//
// pendingTransactionDescription =
// await transaction_history.createTransactionMultDest(
// outputs: moneroOutputs,
// priorityRaw: _credentials.priority.serialize(),
// accountIndex: walletAddresses.account!.id);
// } else {
// final output = outputs.first;
// final address = output.isParsedAddress
// ? output.extractedAddress
// : output.address;
// final amount = output.sendAll
// ? null
// : output.cryptoAmount!.replaceAll(',', '.');
// final formattedAmount = output.sendAll
// ? null
// : output.formattedCryptoAmount;
//
// if ((formattedAmount != null && unlockedBalance < formattedAmount) ||
// (formattedAmount == null && unlockedBalance <= 0)) {
// final formattedBalance = moneroAmountToString(amount: unlockedBalance);
//
// throw MoneroTransactionCreationException(
// 'Incorrect unlocked balance. Unlocked: $formattedBalance. Transaction amount: ${output.cryptoAmount}.');
// }
//
// pendingTransactionDescription =
// await transaction_history.createTransaction(
// address: address!,
// amount: amount,
// priorityRaw: _credentials.priority.serialize(),
// accountIndex: walletAddresses.account!.id);
// }
return PendingEthereumTransaction(
client: _client,
credentials: _credentials,
privateKey: _privateKey,
);
}
@override
Future<Map<String, EthereumTransactionInfo>> fetchTransactions() {
throw UnimplementedError("fetchTransactions");
}
@override
Object get keys => throw UnimplementedError("keys");
@override
Future<void> rescan({required int height}) {
throw UnimplementedError("rescan");
}
@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;
@action
@override
Future<void> startSync() async {
try {
syncStatus = AttemptingSyncStatus();
await _updateBalance();
_gasPrice = await _client.getGasUnitPrice();
_priorityFees = await _client.getEstimatedGasForPriorities();
Timer.periodic(
const Duration(minutes: 1), (timer) async => _gasPrice = await _client.getGasUnitPrice());
Timer.periodic(const Duration(minutes: 1),
(timer) async => _priorityFees = await _client.getEstimatedGasForPriorities());
syncStatus = SyncedSyncStatus();
} catch (e, stacktrace) {
print(stacktrace);
print(e.toString());
syncStatus = FailedSyncStatus();
}
}
int feeRate(TransactionPriority priority) {
try {
if (priority is EthereumTransactionPriority) {
return _priorityFees[priority.raw];
}
return 0;
} catch (e) {
return 0;
}
}
Future<String> makePath() async => pathForWallet(name: walletInfo.name, type: walletInfo.type);
String toJSON() => json.encode({
'mnemonic': _mnemonic,
'balance': balance[currency]!.toJSON(),
// TODO: save other attributes
});
static Future<EthereumWallet> open({
required String name,
required String password,
required WalletInfo walletInfo,
}) async {
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;
final balance = EthereumBalance.fromJSON(data['balance'] as String) ??
EthereumBalance(available: 0, additional: 0);
return EthereumWallet(
walletInfo: walletInfo,
password: password,
mnemonic: mnemonic,
initialBalance: balance,
);
}
Future<void> _updateBalance() async {
balance[currency] = await _fetchBalances();
await save();
}
Future<EthereumBalance> _fetchBalances() async {
final balance = await _client.getBalance(_privateKey);
return EthereumBalance(
available: balance.getInEther.toInt(),
additional: balance.getInEther.toInt(),
);
}
Future<String> getPrivateKey(String mnemonic, String password) async {
final seed = bip39.mnemonicToSeedHex(mnemonic);
final master = await ED25519_HD_KEY.getMasterKeyFromSeed(
HEX.decode(seed),
masterSecret: password,
);
final privateKey = HEX.encode(master.key);
return privateKey;
}
}