From 6370e927a22eead22830d950bb93988fe37d50c4 Mon Sep 17 00:00:00 2001 From: likho Date: Wed, 14 Dec 2022 12:15:22 +0200 Subject: [PATCH] WIP: Add Ethereum --- .../add_edit_node_view.dart | 10 +- .../coins/ethereum/ethereum_wallet.dart | 91 ++++++++++++++++--- lib/services/price.dart | 2 +- lib/utilities/default_nodes.dart | 2 +- pubspec.yaml | 3 +- 5 files changed, 85 insertions(+), 23 deletions(-) diff --git a/lib/pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart b/lib/pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart index 4f29fc5ab..8aed2ac67 100644 --- a/lib/pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart +++ b/lib/pages/settings_views/global_settings_view/manage_nodes_views/add_edit_node_view.dart @@ -31,7 +31,7 @@ import 'package:stackwallet/widgets/stack_dialog.dart'; import 'package:stackwallet/widgets/stack_text_field.dart'; import 'package:stackwallet/widgets/textfield_icon_button.dart'; import 'package:uuid/uuid.dart'; -import 'package:web3dart/web3dart.dart'; +// import 'package:web3dart/web3dart.dart'; enum AddEditNodeViewType { add, edit } @@ -166,11 +166,11 @@ class _AddEditNodeViewState extends ConsumerState { break; case Coin.ethereum: - final client = Web3Client( - "https://mainnet.infura.io/v3/22677300bf774e49a458b73313ee56ba", - Client()); + // final client = Web3Client( + // "https://mainnet.infura.io/v3/22677300bf774e49a458b73313ee56ba", + // Client()); try { - await client.getSyncStatus(); + // await client.getSyncStatus(); } catch (_) {} } diff --git a/lib/services/coins/ethereum/ethereum_wallet.dart b/lib/services/coins/ethereum/ethereum_wallet.dart index 3919611ae..ce1855f68 100644 --- a/lib/services/coins/ethereum/ethereum_wallet.dart +++ b/lib/services/coins/ethereum/ethereum_wallet.dart @@ -1,15 +1,19 @@ import 'dart:math'; import 'package:bip39/bip39.dart' as bip39; import 'package:decimal/decimal.dart'; +import 'package:stack_wallet_backup/generate_password.dart'; import 'package:stackwallet/models/paymint/fee_object_model.dart'; import 'package:stackwallet/models/paymint/transactions_model.dart'; import 'package:stackwallet/models/paymint/utxo_model.dart'; import 'package:stackwallet/services/price.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; 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/credentials.dart'; import 'package:web3dart/web3dart.dart'; +// import 'package:string_to_hex/string_to_hex.dart'; +// import 'package:web3dart/credentials.dart'; +// import 'package:web3dart/web3dart.dart'; import 'package:http/http.dart'; import 'package:stackwallet/hive/db.dart'; @@ -20,9 +24,7 @@ const int MINIMUM_CONFIRMATIONS = 1; const int DUST_LIMIT = 294; const String GENESIS_HASH_MAINNET = - "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"; -const String GENESIS_HASH_TESTNET = - "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"; + "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"; class EthereumWallet extends CoinServiceAPI { @override @@ -50,6 +52,10 @@ class EthereumWallet extends CoinServiceAPI { late SecureStorageInterface _secureStore; late PriceAPI _priceAPI; + final _prefs = Prefs.instance; + final _client = Web3Client( + "https://mainnet.infura.io/v3/22677300bf774e49a458b73313ee56ba", + Client()); EthereumWallet( {required String walletId, @@ -130,15 +136,56 @@ class EthereumWallet extends CoinServiceAPI { bool get hasCalledExit => throw UnimplementedError(); @override - Future initializeExisting() { - // TODO: implement initializeExisting - throw UnimplementedError(); + Future initializeExisting() async { + Logging.instance.log("Opening existing ${coin.prettyName} wallet.", + level: LogLevel.Info); + + if ((DB.instance.get(boxName: walletId, key: "id")) == null) { + throw Exception( + "Attempted to initialize an existing wallet using an unknown wallet ID!"); + } + await _prefs.init(); + final data = + DB.instance.get(boxName: walletId, key: "latest_tx_model") + as TransactionData?; + if (data != null) { + _transactionData = Future(() => data); + } } @override - Future initializeNew() { - // TODO: implement initializeNew - throw UnimplementedError(); + Future initializeNew() async { + await _prefs.init(); + final String mnemonic = bip39.generateMnemonic(strength: 256); + final 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 @@ -154,8 +201,17 @@ class EthereumWallet extends CoinServiceAPI { Future get maxFee => throw UnimplementedError(); @override - // TODO: implement mnemonic - Future> get mnemonic => throw UnimplementedError(); + Future> get mnemonic => _getMnemonicList(); + + Future> _getMnemonicList() async { + final mnemonicString = + await _secureStore.read(key: '${_walletId}_mnemonic'); + if (mnemonicString == null) { + return []; + } + final List data = mnemonicString.split(' '); + return data; + } @override // TODO: implement pendingBalance @@ -206,8 +262,11 @@ class EthereumWallet extends CoinServiceAPI { Future get totalBalance => throw UnimplementedError(); @override - // TODO: implement transactionData - Future get transactionData => throw UnimplementedError(); + Future get transactionData => + _transactionData ??= _fetchTransactionData(); + Future? _transactionData; + + TransactionData? cachedTxData; @override // TODO: implement unspentOutputs @@ -231,6 +290,10 @@ class EthereumWallet extends CoinServiceAPI { throw UnimplementedError(); } + Future _fetchTransactionData() async { + throw UnimplementedError(); + } + @override String get walletId => _walletId; late String _walletId; diff --git a/lib/services/price.dart b/lib/services/price.dart index 2514cc12a..2531cb76c 100644 --- a/lib/services/price.dart +++ b/lib/services/price.dart @@ -87,7 +87,7 @@ class PriceAPI { Map> result = {}; try { final uri = Uri.parse( - "https://api.coingecko.com/api/v3/coins/markets?vs_currency=${baseCurrency.toLowerCase()}&ids=monero,bitcoin,litecoin,epic-cash,zcoin,dogecoin,bitcoin-cash,namecoin,wownero&order=market_cap_desc&per_page=10&page=1&sparkline=false"); + "https://api.coingecko.com/api/v3/coins/markets?vs_currency=${baseCurrency.toLowerCase()}&ids=monero,bitcoin,litecoin,epic-cash,zcoin,dogecoin,bitcoin-cash,namecoin,wownero,ethereum&order=market_cap_desc&per_page=10&page=1&sparkline=false"); // final uri = Uri.parse( // "https://api.coingecko.com/api/v3/coins/markets?vs_currency=${baseCurrency.toLowerCase()}&ids=monero%2Cbitcoin%2Cepic-cash%2Czcoin%2Cdogecoin&order=market_cap_desc&per_page=10&page=1&sparkline=false"); diff --git a/lib/utilities/default_nodes.dart b/lib/utilities/default_nodes.dart index c09ebba4f..2055b9f17 100644 --- a/lib/utilities/default_nodes.dart +++ b/lib/utilities/default_nodes.dart @@ -2,7 +2,7 @@ import 'dart:convert'; import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; -import 'package:web3dart/browser.dart'; +// import 'package:web3dart/browser.dart'; abstract class DefaultNodes { static const String defaultNodeIdPrefix = "default_"; diff --git a/pubspec.yaml b/pubspec.yaml index 232ab10ce..25119a355 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -89,8 +89,7 @@ dependencies: bs58check: ^1.0.2 # Eth Plugins - web3dart: - 2.3.5 + web3dart: 2.3.5 string_to_hex: 0.2.2 # Storage plugins