mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-02-03 03:36:39 +00:00
- Fix extracting Ethereum Private key from seeds
- Integrate signing/sending transaction with the send view model
This commit is contained in:
parent
c6a26cac16
commit
532488960b
5 changed files with 122 additions and 52 deletions
|
@ -1,10 +1,10 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:cw_ethereum/pending_ethereum_transaction.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:web3dart/web3dart.dart';
|
||||
import 'package:cw_core/node.dart';
|
||||
import 'package:cw_ethereum/ethereum_transaction_priority.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:web3dart/crypto.dart';
|
||||
import 'package:web3dart/web3dart.dart';
|
||||
|
||||
class EthereumClient {
|
||||
Web3Client? _client;
|
||||
|
@ -19,9 +19,8 @@ class EthereumClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<EtherAmount> getBalance(EthereumAddress address) async {
|
||||
return await _client!.getBalance(address);
|
||||
}
|
||||
Future<EtherAmount> getBalance(EthereumAddress address) async =>
|
||||
await _client!.getBalance(address);
|
||||
|
||||
Future<int> getGasUnitPrice() async {
|
||||
final gasPrice = await _client!.getGasPrice();
|
||||
|
@ -29,48 +28,109 @@ class EthereumClient {
|
|||
}
|
||||
|
||||
Future<List<int>> getEstimatedGasForPriorities() async {
|
||||
// TODO: there is no difference, find out why
|
||||
// [53000, 53000, 53000]
|
||||
final result = await Future.wait(EthereumTransactionPriority.all.map(
|
||||
(priority) => _client!.estimateGas(
|
||||
// maxPriorityFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, priority.tip),
|
||||
// maxFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, 100),
|
||||
),
|
||||
maxPriorityFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, priority.tip),
|
||||
maxFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, priority.tip),
|
||||
),
|
||||
));
|
||||
|
||||
return result.map((e) => e.toInt()).toList();
|
||||
}
|
||||
|
||||
Future<TransactionInformation> signTransaction(
|
||||
Future<PendingEthereumTransaction> signTransaction(
|
||||
EthPrivateKey privateKey,
|
||||
String toAddress,
|
||||
String amount,
|
||||
int fee,
|
||||
int gas,
|
||||
EthereumTransactionPriority priority,
|
||||
) async {
|
||||
final estimatedGas = await _client!.estimateGas(
|
||||
maxPriorityFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, priority.tip),
|
||||
maxFeePerGas: EtherAmount.fromUnitAndValue(EtherUnit.gwei, 100),
|
||||
value: EtherAmount.inWei(BigInt.parse(amount)),
|
||||
sender: privateKey.address,
|
||||
to: EthereumAddress.fromHex(toAddress),
|
||||
);
|
||||
|
||||
final price = await _client!.getGasPrice();
|
||||
|
||||
final transaction = Transaction(
|
||||
from: privateKey.address,
|
||||
to: EthereumAddress.fromHex(toAddress),
|
||||
value: EtherAmount.fromUnitAndValue(EtherUnit.ether, amount),
|
||||
// maxPriorityFeePerGas: EtherAmount.inWei(BigInt.from(fee)),
|
||||
maxGas: gas,
|
||||
gasPrice: price,
|
||||
value: EtherAmount.inWei(BigInt.parse(amount)),
|
||||
);
|
||||
|
||||
final Uint8List signedTransactionRaw = await _client!.signTransaction(privateKey, transaction);
|
||||
final signedTransaction = await _client!.signTransaction(privateKey, transaction);
|
||||
|
||||
final transactionHash = bytesToHex(signedTransactionRaw);
|
||||
|
||||
final signedTransaction = await _client!.getTransactionByHash(transactionHash);
|
||||
|
||||
return signedTransaction;
|
||||
}
|
||||
|
||||
Future<String> sendTransaction(
|
||||
EthPrivateKey privateKey, TransactionInformation transactionInformation) async {
|
||||
final transaction = Transaction(
|
||||
from: transactionInformation.from,
|
||||
to: transactionInformation.to,
|
||||
value: transactionInformation.value,
|
||||
gasPrice: transactionInformation.gasPrice,
|
||||
data: transactionInformation.input,
|
||||
return PendingEthereumTransaction(
|
||||
signedTransaction: signedTransaction,
|
||||
amount: amount,
|
||||
fee: estimatedGas * price.getInWei,
|
||||
sendTransaction: () => sendTransaction(signedTransaction),
|
||||
);
|
||||
|
||||
return await _client!.sendTransaction(privateKey, transaction);
|
||||
}
|
||||
|
||||
Future<String> sendTransaction(Uint8List signedTransaction) async =>
|
||||
await _client!.sendRawTransaction(signedTransaction);
|
||||
|
||||
Future getTransactionDetails(String transactionHash) async {
|
||||
// Wait for the transaction receipt to become available
|
||||
TransactionReceipt? receipt;
|
||||
while (receipt == null) {
|
||||
print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
|
||||
receipt = await _client!.getTransactionReceipt(transactionHash);
|
||||
await Future.delayed(Duration(seconds: 1));
|
||||
}
|
||||
|
||||
// Print the receipt information
|
||||
print('Transaction Hash: ${receipt.transactionHash}');
|
||||
print('Block Hash: ${receipt.blockHash}');
|
||||
print('Block Number: ${receipt.blockNumber}');
|
||||
print('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) {
|
||||
print("********************************");
|
||||
transactionInformation = await _client!.getTransactionByHash(transactionHash);
|
||||
await Future.delayed(Duration(seconds: 1));
|
||||
}
|
||||
// Print the receipt information
|
||||
print('Transaction Hash: ${transactionInformation.hash}');
|
||||
print('Block Hash: ${transactionInformation.blockHash}');
|
||||
print('Block Number: ${transactionInformation.blockNumber}');
|
||||
print('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<List<Transaction>> fetchTransactions(String address) async {
|
||||
// get(Uri.https(
|
||||
// "https://api.etherscan.io",
|
||||
// "api/",
|
||||
// {
|
||||
// "module": "account",
|
||||
// "action": "txlist",
|
||||
// "address": address,
|
||||
// "apikey": secrets.,
|
||||
// },
|
||||
// ));
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -8,11 +8,11 @@ class EthereumTransactionPriority extends TransactionPriority {
|
|||
|
||||
static const List<EthereumTransactionPriority> all = [fast, medium, slow];
|
||||
static const EthereumTransactionPriority slow =
|
||||
EthereumTransactionPriority(title: 'Slow', raw: 0, tip: 2);
|
||||
EthereumTransactionPriority(title: 'Slow', raw: 0, tip: 50);
|
||||
static const EthereumTransactionPriority medium =
|
||||
EthereumTransactionPriority(title: 'Medium', raw: 1, tip: 5);
|
||||
EthereumTransactionPriority(title: 'Medium', raw: 1, tip: 75);
|
||||
static const EthereumTransactionPriority fast =
|
||||
EthereumTransactionPriority(title: 'Fast', raw: 2, tip: 10);
|
||||
EthereumTransactionPriority(title: 'Fast', raw: 2, tip: 100);
|
||||
|
||||
static EthereumTransactionPriority deserialize({required int raw}) {
|
||||
switch (raw) {
|
||||
|
|
|
@ -19,10 +19,11 @@ 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:hex/hex.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:web3dart/web3dart.dart';
|
||||
import 'package:bip39/bip39.dart' as bip39;
|
||||
import 'package:bip32/bip32.dart' as bip32;
|
||||
|
||||
part 'ethereum_wallet.g.dart';
|
||||
|
||||
|
@ -147,17 +148,15 @@ abstract class EthereumWalletBase
|
|||
}
|
||||
}
|
||||
|
||||
final transactionInfo = await _client.signTransaction(
|
||||
final pendingEthereumTransaction = await _client.signTransaction(
|
||||
_privateKey,
|
||||
_credentials.outputs.first.address,
|
||||
totalAmount.toString(),
|
||||
_priorityFees[_credentials.priority!.raw],
|
||||
_credentials.priority!,
|
||||
);
|
||||
|
||||
return PendingEthereumTransaction(
|
||||
sendTransaction: () => _client.sendTransaction(_privateKey, transactionInfo),
|
||||
transactionInformation: transactionInfo,
|
||||
);
|
||||
return pendingEthereumTransaction;
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -256,7 +255,14 @@ abstract class EthereumWalletBase
|
|||
}
|
||||
|
||||
Future<EthPrivateKey> getPrivateKey(String mnemonic, String password) async {
|
||||
final seed = bip39.mnemonicToSeedHex(mnemonic);
|
||||
return EthPrivateKey.fromHex(seed);
|
||||
final seed = bip39.mnemonicToSeed(mnemonic);
|
||||
|
||||
final root = bip32.BIP32.fromSeed(seed);
|
||||
|
||||
const _hdPathEthereum = "m/44'/60'/0'/0";
|
||||
const index = 0;
|
||||
final addressAtIndex = root.derivePath("$_hdPathEthereum/$index");
|
||||
|
||||
return EthPrivateKey.fromHex(HEX.encode(addressAtIndex.privateKey as List<int>));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,32 +1,35 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:cw_core/pending_transaction.dart';
|
||||
import 'package:web3dart/crypto.dart';
|
||||
import 'package:web3dart/web3dart.dart';
|
||||
|
||||
class PendingEthereumTransaction with PendingTransaction {
|
||||
final Function sendTransaction;
|
||||
final TransactionInformation transactionInformation;
|
||||
final Uint8List signedTransaction;
|
||||
final BigInt fee;
|
||||
final String amount;
|
||||
|
||||
PendingEthereumTransaction({
|
||||
required this.sendTransaction,
|
||||
required this.transactionInformation,
|
||||
required this.signedTransaction,
|
||||
required this.fee,
|
||||
required this.amount,
|
||||
});
|
||||
|
||||
@override
|
||||
String get amountFormatted =>
|
||||
transactionInformation.value.getValueInUnit(EtherUnit.ether).toString();
|
||||
EtherAmount.inWei(BigInt.parse(amount)).getValueInUnit(EtherUnit.ether).toString();
|
||||
|
||||
@override
|
||||
Future<void> commit() async => sendTransaction();
|
||||
|
||||
@override
|
||||
String get feeFormatted {
|
||||
final fee = transactionInformation.gasPrice.getInWei * BigInt.from(transactionInformation.gas);
|
||||
|
||||
return EtherAmount.inWei(fee).getValueInUnit(EtherUnit.ether).toString();
|
||||
}
|
||||
String get feeFormatted => EtherAmount.inWei(fee).getValueInUnit(EtherUnit.ether).toString();
|
||||
|
||||
@override
|
||||
String get hex => transactionInformation.hash;
|
||||
String get hex => bytesToHex(signedTransaction, include0x: true);
|
||||
|
||||
@override
|
||||
String get id => transactionInformation.hashCode.toString();
|
||||
String get id => '';
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ dependencies:
|
|||
web3dart: 2.3.5
|
||||
mobx: ^2.0.7+4
|
||||
bip39: ^1.0.6
|
||||
bip32: ^2.0.0
|
||||
ed25519_hd_key: ^2.2.0
|
||||
hex: ^0.2.0
|
||||
http: ^0.13.4
|
||||
|
|
Loading…
Reference in a new issue