From c6d5ad598cd586b2fc29261f6c34d725474f58e1 Mon Sep 17 00:00:00 2001 From: likho Date: Wed, 14 Dec 2022 18:09:24 +0200 Subject: [PATCH] WIP:ADd Ethereum --- .../coins/ethereum/ethereum_wallet.dart | 117 ++++++++++++++---- 1 file changed, 93 insertions(+), 24 deletions(-) diff --git a/lib/services/coins/ethereum/ethereum_wallet.dart b/lib/services/coins/ethereum/ethereum_wallet.dart index ce1855f68..513effe8d 100644 --- a/lib/services/coins/ethereum/ethereum_wallet.dart +++ b/lib/services/coins/ethereum/ethereum_wallet.dart @@ -11,6 +11,7 @@ import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/prefs.dart'; import 'package:string_to_hex/string_to_hex.dart'; import 'package:web3dart/web3dart.dart'; +import 'package:web3dart/web3dart.dart' as Transaction; // import 'package:string_to_hex/string_to_hex.dart'; // import 'package:web3dart/credentials.dart'; // import 'package:web3dart/web3dart.dart'; @@ -57,6 +58,8 @@ class EthereumWallet extends CoinServiceAPI { "https://mainnet.infura.io/v3/22677300bf774e49a458b73313ee56ba", Client()); + late EthPrivateKey _credentials; + EthereumWallet( {required String walletId, required String walletName, @@ -82,25 +85,48 @@ class EthereumWallet extends CoinServiceAPI { @override // TODO: implement allOwnAddresses - Future> get allOwnAddresses => throw UnimplementedError(); + Future> get allOwnAddresses => + _allOwnAddresses ??= _fetchAllOwnAddresses(); + Future>? _allOwnAddresses; + + Future> _fetchAllOwnAddresses() async { + List addresses = []; + final ownAddress = _credentials.address; + addresses.add(ownAddress.toString()); + return addresses; + } @override - // TODO: implement availableBalance - Future get availableBalance => throw UnimplementedError(); + Future get availableBalance async { + EtherAmount ethBalance = await _client.getBalance(_credentials.address); + return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString()); + } @override // TODO: implement balanceMinusMaxFee Future get balanceMinusMaxFee => throw UnimplementedError(); @override - Future confirmSend({required Map txData}) { - // TODO: implement confirmSend - throw UnimplementedError(); + Future confirmSend({required Map txData}) async { + final transaction = await _client.sendTransaction( + _credentials, + Transaction( + to: EthereumAddress.fromHex( + '0xC914Bb2ba888e3367bcecEb5C2d99DF7C7423706'), + gasPrice: EtherAmount.inWei(BigInt.one), + maxGas: 100000, + value: EtherAmount.fromUnitAndValue(EtherUnit.ether, 1), + ), + ); + + return transaction; } @override - // TODO: implement currentReceivingAddress - Future get currentReceivingAddress => throw UnimplementedError(); + Future get currentReceivingAddress async { + final _currentReceivingAddress = _credentials.address; + return _currentReceivingAddress.toString(); + } @override Future estimateFeeFor(int satoshiAmount, int feeRate) { @@ -115,8 +141,19 @@ class EthereumWallet extends CoinServiceAPI { } @override - // TODO: implement fees - Future get fees => throw UnimplementedError(); + Future get fees => _feeObject ??= _getFees(); + Future? _feeObject; + + Future _getFees() async { + // TODO: implement _getFees + return FeeObject( + numberOfBlocksFast: 10, + numberOfBlocksAverage: 10, + numberOfBlocksSlow: 10, + fast: 1, + medium: 1, + slow: 1); + } @override Future fullRescan( @@ -157,12 +194,12 @@ class EthereumWallet extends CoinServiceAPI { Future initializeNew() async { await _prefs.init(); final String mnemonic = bip39.generateMnemonic(strength: 256); - final credentials = - EthPrivateKey.fromHex(StringToHex.toHexString(mnemonic)); + print("Mnemonic is $mnemonic"); + _credentials = EthPrivateKey.fromHex(StringToHex.toHexString(mnemonic)); final String password = generatePassword(); var rng = Random.secure(); - Wallet wallet = Wallet.createNew(credentials, password, rng); + Wallet wallet = Wallet.createNew(_credentials, password, rng); await _secureStore.write(key: '${_walletId}_mnemonic', value: mnemonic); @@ -188,13 +225,14 @@ class EthereumWallet extends CoinServiceAPI { .put(boxName: walletId, key: "isFavorite", value: false); } - @override - // TODO: implement isConnected - bool get isConnected => throw UnimplementedError(); + bool _isConnected = false; @override - // TODO: implement isRefreshing - bool get isRefreshing => throw UnimplementedError(); + bool get isConnected => _isConnected; + + bool refreshMutex = false; + @override + bool get isRefreshing => refreshMutex; @override // TODO: implement maxFee @@ -231,9 +269,37 @@ class EthereumWallet extends CoinServiceAPI { {required String mnemonic, required int maxUnusedAddressGap, required int maxNumberOfIndexesToCheck, - required int height}) { - // TODO: implement recoverFromMnemonic - throw UnimplementedError(); + required int height}) async { + await _prefs.init(); + print("Mnemonic is $mnemonic"); + _credentials = EthPrivateKey.fromHex(StringToHex.toHexString(mnemonic)); + + final String password = generatePassword(); + var rng = Random.secure(); + Wallet wallet = Wallet.createNew(_credentials, password, rng); + + await _secureStore.write(key: '${_walletId}_mnemonic', value: mnemonic); + + await DB.instance + .put(boxName: walletId, key: "id", value: _walletId); + await DB.instance.put( + boxName: walletId, key: 'receivingAddresses', value: ["0"]); + await DB.instance + .put(boxName: walletId, key: "receivingIndex", value: 0); + await DB.instance + .put(boxName: walletId, key: "changeIndex", value: 0); + await DB.instance.put( + boxName: walletId, + key: 'blocked_tx_hashes', + value: ["0xdefault"], + ); // A list of transaction hashes to represent frozen utxos in wallet + // initialize address book entries + await DB.instance.put( + boxName: walletId, + key: 'addressBookEntries', + value: {}); + await DB.instance + .put(boxName: walletId, key: "isFavorite", value: false); } @override @@ -258,8 +324,11 @@ class EthereumWallet extends CoinServiceAPI { } @override - // TODO: implement totalBalance - Future get totalBalance => throw UnimplementedError(); + // TODO: Check difference between total and available balance for eth + Future get totalBalance async { + EtherAmount ethBalance = await _client.getBalance(_credentials.address); + return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString()); + } @override Future get transactionData => @@ -287,7 +356,7 @@ class EthereumWallet extends CoinServiceAPI { @override bool validateAddress(String address) { // TODO: implement validateAddress - throw UnimplementedError(); + return true; } Future _fetchTransactionData() async {