From abf9f02f8ea43814218c4f3ecf9e6de042d1af26 Mon Sep 17 00:00:00 2001 From: likho Date: Wed, 25 Jan 2023 14:09:07 +0200 Subject: [PATCH] ADdress fix --- .../coins/ethereum/ethereum_wallet.dart | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/services/coins/ethereum/ethereum_wallet.dart b/lib/services/coins/ethereum/ethereum_wallet.dart index ce75fea22..1a9e54a66 100644 --- a/lib/services/coins/ethereum/ethereum_wallet.dart +++ b/lib/services/coins/ethereum/ethereum_wallet.dart @@ -1,7 +1,11 @@ import 'dart:async'; import 'dart:convert'; import 'dart:math'; +import 'package:bip32/bip32.dart' as bip32; import 'package:bip39/bip39.dart' as bip39; + +import "package:hex/hex.dart"; +import 'package:bitcoindart/bitcoindart.dart'; import 'package:decimal/decimal.dart'; import 'package:devicelocale/devicelocale.dart'; import 'package:ethereum_addresses/ethereum_addresses.dart'; @@ -87,6 +91,7 @@ class EthereumWallet extends CoinServiceAPI { // final _blockExplorer = "https://blockscout.com/eth/mainnet/api?"; final _blockExplorer = "https://api.etherscan.io/api?"; final _gasTrackerUrl = "https://beaconcha.in/api/v1/execution/gasnow"; + final _hdPath = "m/44'/60'/0'/0"; @override String get walletId => _walletId; @@ -309,9 +314,8 @@ class EthereumWallet extends CoinServiceAPI { //First get mnemonic so we can initialize credentials final mnemonicString = await _secureStore.read(key: '${_walletId}_mnemonic'); - - _credentials = - EthPrivateKey.fromHex(StringToHex.toHexString(mnemonicString)); + String privateKey = getPrivateKey(mnemonicString!); + _credentials = EthPrivateKey.fromHex(privateKey); Logging.instance.log("Opening existing ${coin.prettyName} wallet.", level: LogLevel.Info); @@ -329,13 +333,27 @@ class EthereumWallet extends CoinServiceAPI { } } + String getPrivateKey(String mnemonic) { + final isValidMnemonic = bip39.validateMnemonic(mnemonic); + if (!isValidMnemonic) { + throw 'Invalid mnemonic'; + } + + final seed = bip39.mnemonicToSeed(mnemonic); + final root = bip32.BIP32.fromSeed(seed); + const index = 0; + final addressAtIndex = root.derivePath("$_hdPath/$index"); + + return HEX.encode(addressAtIndex.privateKey as List); + } + @override Future initializeNew() async { await _prefs.init(); final String mnemonic = bip39.generateMnemonic(strength: 256); - _credentials = EthPrivateKey.fromHex(StringToHex.toHexString(mnemonic)); await _secureStore.write(key: '${_walletId}_mnemonic', value: mnemonic); - + String privateKey = getPrivateKey(mnemonic); + _credentials = EthPrivateKey.fromHex(privateKey); //Store credentials in secure store await _secureStore.write( key: '${_walletId}_credentials', value: _credentials.toString()); @@ -484,9 +502,12 @@ class EthereumWallet extends CoinServiceAPI { await _secureStore.write( key: '${_walletId}_mnemonic', value: mnemonic.trim()); - _credentials = EthPrivateKey.fromHex(StringToHex.toHexString(mnemonic)); + String privateKey = getPrivateKey(mnemonic); + _credentials = EthPrivateKey.fromHex(privateKey); - print(_credentials.address); + // _credentials = EthPrivateKey.fromHex(StringToHex.toHexString(mnemonic)); + + // print(_credentials.address); //Get ERC-20 transactions for wallet (So we can get the and save wallet's ERC-20 TOKENS AddressTransaction tokenTransactions = await fetchAddressTransactions( _credentials.address.toString(), "tokentx");