WIP: Add Ethereum

This commit is contained in:
likho 2022-12-14 12:15:22 +02:00
parent eca8ca21bc
commit 6370e927a2
5 changed files with 85 additions and 23 deletions

View file

@ -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<AddEditNodeView> {
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 (_) {}
}

View file

@ -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<void> initializeExisting() {
// TODO: implement initializeExisting
throw UnimplementedError();
Future<void> initializeExisting() async {
Logging.instance.log("Opening existing ${coin.prettyName} wallet.",
level: LogLevel.Info);
if ((DB.instance.get<dynamic>(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<dynamic>(boxName: walletId, key: "latest_tx_model")
as TransactionData?;
if (data != null) {
_transactionData = Future(() => data);
}
}
@override
Future<void> initializeNew() {
// TODO: implement initializeNew
throw UnimplementedError();
Future<void> 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<dynamic>(boxName: walletId, key: "id", value: _walletId);
await DB.instance.put<dynamic>(
boxName: walletId, key: 'receivingAddresses', value: ["0"]);
await DB.instance
.put<dynamic>(boxName: walletId, key: "receivingIndex", value: 0);
await DB.instance
.put<dynamic>(boxName: walletId, key: "changeIndex", value: 0);
await DB.instance.put<dynamic>(
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<dynamic>(
boxName: walletId,
key: 'addressBookEntries',
value: <String, String>{});
await DB.instance
.put<dynamic>(boxName: walletId, key: "isFavorite", value: false);
}
@override
@ -154,8 +201,17 @@ class EthereumWallet extends CoinServiceAPI {
Future<int> get maxFee => throw UnimplementedError();
@override
// TODO: implement mnemonic
Future<List<String>> get mnemonic => throw UnimplementedError();
Future<List<String>> get mnemonic => _getMnemonicList();
Future<List<String>> _getMnemonicList() async {
final mnemonicString =
await _secureStore.read(key: '${_walletId}_mnemonic');
if (mnemonicString == null) {
return [];
}
final List<String> data = mnemonicString.split(' ');
return data;
}
@override
// TODO: implement pendingBalance
@ -206,8 +262,11 @@ class EthereumWallet extends CoinServiceAPI {
Future<Decimal> get totalBalance => throw UnimplementedError();
@override
// TODO: implement transactionData
Future<TransactionData> get transactionData => throw UnimplementedError();
Future<TransactionData> get transactionData =>
_transactionData ??= _fetchTransactionData();
Future<TransactionData>? _transactionData;
TransactionData? cachedTxData;
@override
// TODO: implement unspentOutputs
@ -231,6 +290,10 @@ class EthereumWallet extends CoinServiceAPI {
throw UnimplementedError();
}
Future<TransactionData> _fetchTransactionData() async {
throw UnimplementedError();
}
@override
String get walletId => _walletId;
late String _walletId;

View file

@ -87,7 +87,7 @@ class PriceAPI {
Map<Coin, Tuple2<Decimal, double>> 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");

View file

@ -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_";

View file

@ -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