cake_wallet/cw_evm/lib/evm_chain_client.dart
Konstantin Ullrich 5eabdcdca1
Cw 604 integrate bitcoin ledger (#1407)
* CW-503 Prepare Ledger integration

* CW-503 Revert Tor ignore

* CW-503 Add Connect Device Page

* CW-503 Add createWalletFromDevice for monero

* CW-503 Add Connect Device Page

* CW-503 Add Connect Device Page

* CW-503 Add Debug Options

* CW-503 Add proper hardware wallet selection screen

* CW-503 Minor design changes on connect_device_page

* CW-503 Add Create Wallet from Ledger

* CW-503 Spent Ledger ETH Funds

* CW-503 Minor fixes

* CW-503 Fix Merge conflicts

* CW-503 Fix Merge conflicts

* CW-503 Fix Merge conflicts

* CW-503 Fix minor conflicts

* CW-503 Improve Ledger BLE Communication

* CW-503 Improve Ledger BLE Communication

* CW-503 Rollback Monero Test code

* CW-503 Fix Execution failed for task :app:checkReleaseDuplicateClasses

* CW-503 Better Error-Exceptions

* CW-503 Add SetPinScreen before restore from hardware-wallet

* CW-503 override web3dart to use cake's git hosted version

* CW-503 Implement ledger sign messages

* CW-503 Implement ledger sign messages and send erc20 tokens

* CW-503 Fix merge conflicts

* CW-503 Fix merge conflicts

* CW-503 Use dep override for ledger_flutter

* CW-503 Ledger ERC20 finalisation

* CW-503 More graceful error handling

* CW-503 Even more graceful error handling & remove debug code

* CW-503 Minor Changes for Vik

* CW-503 Fix USB connection

* CW-503 Maybe this overrides web3dart finally

* Indicate Loading on the first 5 Wallet Accounts

* Prepare Ledger Bitcoin

* Fix conflicts with main

* Add Bluetooth permission to iOS [skip ci]

* add privacyinfo for ios [skip ci]

* update PrivacyInfo.xcprivacy [scip ci]

* ios shit [skip ci]

* Improve bitcoin xpub

* Resolve open Todos regarding iOS Support

* Minor debug

* Remove erc20 dependency to have more granular control over the tx UX

* Create Bitcoin Wallets using xpub

* Create Bitcoin Wallets using xpub

* Better error handling

* Improve Ledger account handling

* Add Bitcoin Support for Ledger

* Add Bitcoin Support for Ledger

* Add Bitcoin Support for Ledger

* Implement requested Changes

* Implement requested Changes

* Minor fix

* Implement requested Changes

* Implement requested Changes

* Add comment to remind me

* Enable RBF for Bitcoin Ledger

* Fix merge conflicts

* Update wallet_info.dart

* Cw 503 ledger support (#1310)

* CW-503 Prepare Ledger integration

* CW-503 Revert Tor ignore

* CW-503 Add Connect Device Page

* CW-503 Add createWalletFromDevice for monero

* CW-503 Add Connect Device Page

* CW-503 Add Connect Device Page

* CW-503 Add Debug Options

* CW-503 Add proper hardware wallet selection screen

* CW-503 Minor design changes on connect_device_page

* CW-503 Add Create Wallet from Ledger

* CW-503 Spent Ledger ETH Funds

* CW-503 Minor fixes

* CW-503 Fix Merge conflicts

* CW-503 Fix Merge conflicts

* CW-503 Fix Merge conflicts

* CW-503 Fix minor conflicts

* CW-503 Improve Ledger BLE Communication

* CW-503 Improve Ledger BLE Communication

* CW-503 Rollback Monero Test code

* CW-503 Fix Execution failed for task :app:checkReleaseDuplicateClasses

* CW-503 Better Error-Exceptions

* CW-503 Add SetPinScreen before restore from hardware-wallet

* CW-503 override web3dart to use cake's git hosted version

* CW-503 Implement ledger sign messages

* CW-503 Implement ledger sign messages and send erc20 tokens

* CW-503 Fix merge conflicts

* CW-503 Fix merge conflicts

* CW-503 Use dep override for ledger_flutter

* CW-503 Ledger ERC20 finalisation

* CW-503 More graceful error handling

* CW-503 Even more graceful error handling & remove debug code

* CW-503 Minor Changes for Vik

* CW-503 Fix USB connection

* CW-503 Maybe this overrides web3dart finally

* Indicate Loading on the first 5 Wallet Accounts

* Fix conflicts with main

* Add Bluetooth permission to iOS [skip ci]

* add privacyinfo for ios [skip ci]

* update PrivacyInfo.xcprivacy [scip ci]

* ios shit [skip ci]

* Resolve open Todos regarding iOS Support

* Remove erc20 dependency to have more granular control over the tx UX

* Better error handling

* Improve Ledger account handling

* Implement requested Changes

* Implement requested Changes

* Implement requested Changes

* Implement requested Changes

* Fix merge conflicts

* Update wallet_info.dart

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>

* Fix merge conflicts

* Fix merge conflicts

* Minor Fix to derivations

* Update cw_bitcoin/lib/bitcoin_wallet.dart [skip ci]

* Update cw_bitcoin/lib/bitcoin_wallet.dart [skip ci]

* Update cw_bitcoin/lib/electrum_wallet.dart [skip ci]

* Fix backward compatibility issues

* Merge Tron

* Fix Tron with HW changes

* fix ble on iOS
fix tron address validation

---------

Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
2024-05-05 04:44:50 +03:00

301 lines
9.2 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/erc20_token.dart';
import 'package:cw_core/node.dart';
import 'package:cw_evm/evm_chain_transaction_model.dart';
import 'package:cw_evm/evm_chain_transaction_priority.dart';
import 'package:cw_evm/evm_erc20_balance.dart';
import 'package:cw_evm/pending_evm_chain_transaction.dart';
import 'package:cw_evm/.secrets.g.dart' as secrets;
import 'package:flutter/services.dart';
import 'package:hex/hex.dart' as hex;
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
import 'contract/erc20.dart';
abstract class EVMChainClient {
final httpClient = Client();
Web3Client? _client;
//! To be overridden by all child classes
int get chainId;
Future<List<EVMChainTransactionModel>> fetchTransactions(String address,
{String? contractAddress});
Future<List<EVMChainTransactionModel>> fetchInternalTransactions(String address);
Uint8List prepareSignedTransactionForSending(Uint8List signedTransaction);
//! Common methods across all child classes
bool connect(Node node) {
try {
_client = Web3Client(node.uri.toString(), httpClient);
return true;
} catch (e) {
return false;
}
}
void setListeners(EthereumAddress userAddress, Function() onNewTransaction) async {
// _client?.pendingTransactions().listen((transactionHash) async {
// final transaction = await _client!.getTransactionByHash(transactionHash);
//
// if (transaction.from.hex == userAddress || transaction.to?.hex == userAddress) {
// onNewTransaction();
// }
// });
}
Future<EtherAmount> getBalance(EthereumAddress address) async {
try {
return await _client!.getBalance(address);
} catch (_) {
return EtherAmount.zero();
}
}
Future<int> getGasUnitPrice() async {
try {
final gasPrice = await _client!.getGasPrice();
return gasPrice.getInWei.toInt();
} catch (_) {
return 0;
}
}
Future<int> getEstimatedGas() async {
try {
final estimatedGas = await _client!.estimateGas();
return estimatedGas.toInt();
} catch (_) {
return 0;
}
}
Future<PendingEVMChainTransaction> signTransaction({
required Credentials privateKey,
required String toAddress,
required BigInt amount,
required int gas,
required EVMChainTransactionPriority priority,
required CryptoCurrency currency,
required int exponent,
String? contractAddress,
String? data,
}) async {
assert(currency == CryptoCurrency.eth ||
currency == CryptoCurrency.maticpoly ||
contractAddress != null);
bool isNativeToken = currency == CryptoCurrency.eth || currency == CryptoCurrency.maticpoly;
final price = _client!.getGasPrice();
final Transaction transaction = createTransaction(
from: privateKey.address,
to: EthereumAddress.fromHex(toAddress),
maxPriorityFeePerGas: EtherAmount.fromInt(EtherUnit.gwei, priority.tip),
amount: isNativeToken ? EtherAmount.inWei(amount) : EtherAmount.zero(),
data: data != null ? hexToBytes(data) : null,
);
Uint8List signedTransaction;
final Function _sendTransaction;
if (isNativeToken) {
signedTransaction = await _client!.signTransaction(privateKey, transaction, chainId: chainId);
} else {
final erc20 = ERC20(
client: _client!,
address: EthereumAddress.fromHex(contractAddress!),
chainId: chainId,
);
signedTransaction = await erc20.transfer(
EthereumAddress.fromHex(toAddress),
amount,
credentials: privateKey,
transaction: transaction,
);
}
_sendTransaction = () async => await sendTransaction(signedTransaction);
return PendingEVMChainTransaction(
signedTransaction: signedTransaction,
amount: amount.toString(),
fee: BigInt.from(gas) * (await price).getInWei,
sendTransaction: _sendTransaction,
exponent: exponent,
);
}
Transaction createTransaction({
required EthereumAddress from,
required EthereumAddress to,
required EtherAmount amount,
EtherAmount? maxPriorityFeePerGas,
Uint8List? data,
}) {
return Transaction(
from: from,
to: to,
maxPriorityFeePerGas: maxPriorityFeePerGas,
value: amount,
data: data,
);
}
Future<String> sendTransaction(Uint8List signedTransaction) async {
return await _client!.sendRawTransaction(prepareSignedTransactionForSending(signedTransaction));
}
Future getTransactionDetails(String transactionHash) async {
// Wait for the transaction receipt to become available
TransactionReceipt? receipt;
while (receipt == null) {
receipt = await _client!.getTransactionReceipt(transactionHash);
await Future.delayed(const Duration(seconds: 1));
}
// Print the receipt information
log('Transaction Hash: ${receipt.transactionHash}');
log('Block Hash: ${receipt.blockHash}');
log('Block Number: ${receipt.blockNumber}');
log('Gas Used: ${receipt.gasUsed}');
/*
Transaction Hash: [112, 244, 4, 238, 89, 199, 171, 191, 210, 236, 110, 42, 185, 202, 220, 21, 27, 132, 123, 221, 137, 90, 77, 13, 23, 43, 12, 230, 93, 63, 221, 116]
I/flutter ( 4474): Block Hash: [149, 44, 250, 119, 111, 104, 82, 98, 17, 89, 30, 190, 25, 44, 218, 118, 127, 189, 241, 35, 213, 106, 25, 95, 195, 37, 55, 131, 185, 180, 246, 200]
I/flutter ( 4474): Block Number: 17120242
I/flutter ( 4474): Gas Used: 21000
*/
// Wait for the transaction receipt to become available
TransactionInformation? transactionInformation;
while (transactionInformation == null) {
log("********************************");
transactionInformation = await _client!.getTransactionByHash(transactionHash);
await Future.delayed(const Duration(seconds: 1));
}
// Print the receipt information
log('Transaction Hash: ${transactionInformation.hash}');
log('Block Hash: ${transactionInformation.blockHash}');
log('Block Number: ${transactionInformation.blockNumber}');
log('Gas Used: ${transactionInformation.gas}');
/*
Transaction Hash: 0x70f404ee59c7abbfd2ec6e2ab9cadc151b847bdd895a4d0d172b0ce65d3fdd74
I/flutter ( 4474): Block Hash: 0x952cfa776f68526211591ebe192cda767fbdf123d56a195fc3253783b9b4f6c8
I/flutter ( 4474): Block Number: 17120242
I/flutter ( 4474): Gas Used: 53000
*/
}
Future<EVMChainERC20Balance> fetchERC20Balances(
EthereumAddress userAddress, String contractAddress) async {
final erc20 = ERC20(address: EthereumAddress.fromHex(contractAddress), client: _client!);
final balance = await erc20.balanceOf(userAddress);
int exponent = (await erc20.decimals()).toInt();
return EVMChainERC20Balance(balance, exponent: exponent);
}
Future<Erc20Token?> getErc20Token(String contractAddress, String chainName) async {
try {
final uri = Uri.https(
'deep-index.moralis.io',
'/api/v2.2/erc20/metadata',
{
"chain": chainName,
"addresses": contractAddress,
},
);
final response = await httpClient.get(
uri,
headers: {
"Accept": "application/json",
"X-API-Key": secrets.moralisApiKey,
},
);
final decodedResponse = jsonDecode(response.body)[0] as Map<String, dynamic>;
final symbol = (decodedResponse['symbol'] ?? '') as String;
String filteredSymbol = symbol.replaceFirst(RegExp('^\\\$'), '');
final name = decodedResponse['name'] ?? '';
final decimal = decodedResponse['decimals'] ?? '0';
final iconPath = decodedResponse['logo'] ?? '';
return Erc20Token(
name: name,
symbol: filteredSymbol,
contractAddress: contractAddress,
decimal: int.tryParse(decimal) ?? 0,
iconPath: iconPath,
);
} catch (e) {
try {
final erc20 = ERC20(address: EthereumAddress.fromHex(contractAddress), client: _client!);
final name = await erc20.name();
final symbol = await erc20.symbol();
final decimal = await erc20.decimals();
return Erc20Token(
name: name,
symbol: symbol,
contractAddress: contractAddress,
decimal: decimal.toInt(),
);
} catch (_) {}
return null;
}
}
Uint8List hexToBytes(String hexString) {
return Uint8List.fromList(
hex.HEX.decode(hexString.startsWith('0x') ? hexString.substring(2) : hexString));
}
void stop() {
_client?.dispose();
}
Web3Client? getWeb3Client() {
return _client;
}
// Future<int> _getDecimalPlacesForContract(DeployedContract contract) async {
// final String abi = await rootBundle.loadString("assets/abi_json/erc20_abi.json");
// final contractAbi = ContractAbi.fromJson(abi, "ERC20");
//
// final contract = DeployedContract(
// contractAbi,
// EthereumAddress.fromHex(_erc20Currencies[erc20Currency]!),
// );
// final decimalsFunction = contract.function('decimals');
// final decimals = await _client!.call(
// contract: contract,
// function: decimalsFunction,
// params: [],
// );
//
// int exponent = int.parse(decimals.first.toString());
// return exponent;
// }
}