mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-18 10:11:03 +00:00
373 lines
11 KiB
Dart
373 lines
11 KiB
Dart
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/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';
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:stackwallet/hive/db.dart';
|
|
import 'package:stackwallet/utilities/logger.dart';
|
|
import 'package:stackwallet/services/coins/coin_service.dart';
|
|
|
|
const int MINIMUM_CONFIRMATIONS = 1;
|
|
const int DUST_LIMIT = 294;
|
|
|
|
const String GENESIS_HASH_MAINNET =
|
|
"0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa";
|
|
|
|
class EthereumWallet extends CoinServiceAPI {
|
|
@override
|
|
set isFavorite(bool markFavorite) {
|
|
DB.instance.put<dynamic>(
|
|
boxName: walletId, key: "isFavorite", value: markFavorite);
|
|
}
|
|
|
|
@override
|
|
bool get isFavorite {
|
|
try {
|
|
return DB.instance.get<dynamic>(boxName: walletId, key: "isFavorite")
|
|
as bool;
|
|
} catch (e, s) {
|
|
Logging.instance.log(
|
|
"isFavorite fetch failed (returning false by default): $e\n$s",
|
|
level: LogLevel.Error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Coin get coin => _coin;
|
|
|
|
late SecureStorageInterface _secureStore;
|
|
|
|
late PriceAPI _priceAPI;
|
|
final _prefs = Prefs.instance;
|
|
final _client = Web3Client(
|
|
"https://mainnet.infura.io/v3/22677300bf774e49a458b73313ee56ba",
|
|
Client());
|
|
|
|
late EthPrivateKey _credentials;
|
|
|
|
EthereumWallet(
|
|
{required String walletId,
|
|
required String walletName,
|
|
required Coin coin,
|
|
PriceAPI? priceAPI,
|
|
required SecureStorageInterface secureStore}) {
|
|
_walletId = walletId;
|
|
_walletName = walletName;
|
|
_coin = coin;
|
|
|
|
_priceAPI = priceAPI ?? PriceAPI(Client());
|
|
_secureStore = secureStore;
|
|
}
|
|
|
|
@override
|
|
bool shouldAutoSync = false;
|
|
|
|
@override
|
|
String get walletName => _walletName;
|
|
late String _walletName;
|
|
|
|
late Coin _coin;
|
|
|
|
@override
|
|
// TODO: implement allOwnAddresses
|
|
Future<List<String>> get allOwnAddresses =>
|
|
_allOwnAddresses ??= _fetchAllOwnAddresses();
|
|
Future<List<String>>? _allOwnAddresses;
|
|
|
|
Future<List<String>> _fetchAllOwnAddresses() async {
|
|
List<String> addresses = [];
|
|
final ownAddress = _credentials.address;
|
|
addresses.add(ownAddress.toString());
|
|
return addresses;
|
|
}
|
|
|
|
@override
|
|
Future<Decimal> get availableBalance async {
|
|
EtherAmount ethBalance = await _client.getBalance(_credentials.address);
|
|
return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString());
|
|
}
|
|
|
|
@override
|
|
// TODO: implement balanceMinusMaxFee
|
|
Future<Decimal> get balanceMinusMaxFee => throw UnimplementedError();
|
|
|
|
@override
|
|
Future<String> confirmSend({required Map<String, dynamic> 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
|
|
Future<String> get currentReceivingAddress async {
|
|
final _currentReceivingAddress = _credentials.address;
|
|
return _currentReceivingAddress.toString();
|
|
}
|
|
|
|
@override
|
|
Future<int> estimateFeeFor(int satoshiAmount, int feeRate) {
|
|
// TODO: implement estimateFeeFor
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<void> exit() {
|
|
// TODO: implement exit
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<FeeObject> get fees => _feeObject ??= _getFees();
|
|
Future<FeeObject>? _feeObject;
|
|
|
|
Future<FeeObject> _getFees() async {
|
|
// TODO: implement _getFees
|
|
return FeeObject(
|
|
numberOfBlocksFast: 10,
|
|
numberOfBlocksAverage: 10,
|
|
numberOfBlocksSlow: 10,
|
|
fast: 1,
|
|
medium: 1,
|
|
slow: 1);
|
|
}
|
|
|
|
@override
|
|
Future<void> fullRescan(
|
|
int maxUnusedAddressGap, int maxNumberOfIndexesToCheck) {
|
|
// TODO: implement fullRescan
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<bool> generateNewAddress() {
|
|
// TODO: implement generateNewAddress
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
// TODO: implement hasCalledExit
|
|
bool get hasCalledExit => throw UnimplementedError();
|
|
|
|
@override
|
|
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() async {
|
|
await _prefs.init();
|
|
final String mnemonic = bip39.generateMnemonic(strength: 256);
|
|
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<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);
|
|
}
|
|
|
|
bool _isConnected = false;
|
|
|
|
@override
|
|
bool get isConnected => _isConnected;
|
|
|
|
bool refreshMutex = false;
|
|
@override
|
|
bool get isRefreshing => refreshMutex;
|
|
|
|
@override
|
|
// TODO: implement maxFee
|
|
Future<int> get maxFee => throw UnimplementedError();
|
|
|
|
@override
|
|
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
|
|
Future<Decimal> get pendingBalance => throw UnimplementedError();
|
|
|
|
@override
|
|
Future<Map<String, dynamic>> prepareSend(
|
|
{required String address,
|
|
required int satoshiAmount,
|
|
Map<String, dynamic>? args}) {
|
|
// TODO: implement prepareSend
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<void> recoverFromMnemonic(
|
|
{required String mnemonic,
|
|
required int maxUnusedAddressGap,
|
|
required int maxNumberOfIndexesToCheck,
|
|
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<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
|
|
Future<void> refresh() {
|
|
// TODO: implement refresh
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<String> send(
|
|
{required String toAddress,
|
|
required int amount,
|
|
Map<String, String> args = const {}}) {
|
|
// TODO: implement send
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<bool> testNetworkConnection() {
|
|
// TODO: implement testNetworkConnection
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
// TODO: Check difference between total and available balance for eth
|
|
Future<Decimal> get totalBalance async {
|
|
EtherAmount ethBalance = await _client.getBalance(_credentials.address);
|
|
return Decimal.parse(ethBalance.getValueInUnit(EtherUnit.ether).toString());
|
|
}
|
|
|
|
@override
|
|
Future<TransactionData> get transactionData =>
|
|
_transactionData ??= _fetchTransactionData();
|
|
Future<TransactionData>? _transactionData;
|
|
|
|
TransactionData? cachedTxData;
|
|
|
|
@override
|
|
// TODO: implement unspentOutputs
|
|
Future<List<UtxoObject>> get unspentOutputs => throw UnimplementedError();
|
|
|
|
@override
|
|
Future<void> updateNode(bool shouldRefresh) {
|
|
// TODO: implement updateNode
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<void> updateSentCachedTxData(Map<String, dynamic> txData) {
|
|
// TODO: implement updateSentCachedTxData
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
bool validateAddress(String address) {
|
|
// TODO: implement validateAddress
|
|
return true;
|
|
}
|
|
|
|
Future<TransactionData> _fetchTransactionData() async {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
String get walletId => _walletId;
|
|
late String _walletId;
|
|
|
|
@override
|
|
@override
|
|
set walletName(String newName) => _walletName = newName;
|
|
}
|