stack_wallet/lib/wallets/wallet/impl/stellar_wallet.dart

663 lines
20 KiB
Dart
Raw Normal View History

2024-01-11 23:18:58 +00:00
import 'dart:async';
import 'dart:convert';
import 'dart:io';
2024-01-11 23:18:58 +00:00
import 'package:isar/isar.dart';
2024-05-04 00:04:57 +00:00
import 'package:mutex/mutex.dart';
import 'package:socks5_proxy/socks.dart';
2024-05-27 23:56:22 +00:00
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart' as stellar;
import '../../../models/balance.dart';
import '../../../models/isar/models/blockchain_data/address.dart';
import '../../../models/isar/models/blockchain_data/transaction.dart';
import '../../../models/isar/models/blockchain_data/v2/input_v2.dart';
import '../../../models/isar/models/blockchain_data/v2/output_v2.dart';
import '../../../models/isar/models/blockchain_data/v2/transaction_v2.dart';
import '../../../models/paymint/fee_object_model.dart';
import '../../../services/event_bus/events/global/tor_connection_status_changed_event.dart';
import '../../../services/event_bus/events/global/tor_status_changed_event.dart';
import '../../../services/event_bus/global_event_bus.dart';
import '../../../services/tor_service.dart';
import '../../../utilities/amount/amount.dart';
import '../../../utilities/enums/fee_rate_type_enum.dart';
import '../../../utilities/logger.dart';
import '../../../utilities/test_stellar_node_connection.dart';
import '../../crypto_currency/crypto_currency.dart';
import '../../models/tx_data.dart';
import '../intermediate/bip39_wallet.dart';
2024-01-11 23:18:58 +00:00
class StellarWallet extends Bip39Wallet<Stellar> {
2024-05-04 00:04:57 +00:00
StellarWallet(CryptoCurrencyNetwork network) : super(Stellar(network)) {
final bus = GlobalEventBus.instance;
// Listen for tor status changes.
_torStatusListener = bus.on<TorConnectionStatusChangedEvent>().listen(
(event) async {
switch (event.newStatus) {
case TorConnectionStatus.connecting:
if (!_torConnectingLock.isLocked) {
await _torConnectingLock.acquire();
}
_requireMutex = true;
break;
2024-01-11 23:18:58 +00:00
2024-05-04 00:04:57 +00:00
case TorConnectionStatus.connected:
case TorConnectionStatus.disconnected:
if (_torConnectingLock.isLocked) {
_torConnectingLock.release();
}
_requireMutex = false;
break;
}
},
);
// Listen for tor preference changes.
_torPreferenceListener = bus.on<TorPreferenceChangedEvent>().listen(
(event) async {
_stellarSdk?.httpClient.close();
_stellarSdk = null;
},
);
}
Future<stellar.StellarSDK> get stellarSdk async {
if (_requireMutex) {
await _torConnectingLock.protect(() async {
_stellarSdk ??= _getFreshSdk();
});
} else {
_stellarSdk ??= _getFreshSdk();
2024-01-11 23:18:58 +00:00
}
return _stellarSdk!;
}
stellar.Network get stellarNetwork {
switch (cryptoCurrency.network) {
case CryptoCurrencyNetwork.main:
return stellar.Network.PUBLIC;
case CryptoCurrencyNetwork.test:
return stellar.Network.TESTNET;
default:
throw Exception("Unsupported network");
}
}
// ============== Private ====================================================
2024-05-04 00:04:57 +00:00
// add finalizer to cancel stream subscription when all references to an
// instance of this becomes inaccessible
final _ = Finalizer<StellarWallet>(
(p0) {
p0._torPreferenceListener?.cancel();
p0._torStatusListener?.cancel();
},
);
StreamSubscription<TorConnectionStatusChangedEvent>? _torStatusListener;
StreamSubscription<TorPreferenceChangedEvent>? _torPreferenceListener;
final Mutex _torConnectingLock = Mutex();
bool _requireMutex = false;
2024-01-11 23:18:58 +00:00
stellar.StellarSDK? _stellarSdk;
Future<int> _getBaseFee() async {
2024-05-04 00:04:57 +00:00
final fees = await (await stellarSdk).feeStats.execute();
2024-01-11 23:18:58 +00:00
return int.parse(fees.lastLedgerBaseFee);
}
2024-05-04 00:04:57 +00:00
stellar.StellarSDK _getFreshSdk() {
2024-01-11 23:18:58 +00:00
final currentNode = getCurrentNode();
HttpClient? _httpClient;
if (prefs.useTor) {
final ({InternetAddress host, int port}) proxyInfo =
TorService.sharedInstance.getProxyInfo();
_httpClient = HttpClient();
SocksTCPClient.assignToHttpClient(
_httpClient,
[
ProxySettings(
proxyInfo.host,
proxyInfo.port,
),
],
);
}
2024-05-04 00:04:57 +00:00
return stellar.StellarSDK(
"${currentNode.host}:${currentNode.port}",
httpClient: _httpClient,
);
2024-01-11 23:18:58 +00:00
}
Future<bool> _accountExists(String accountId) async {
bool exists = false;
try {
2024-05-04 00:04:57 +00:00
final receiverAccount =
await (await stellarSdk).accounts.account(accountId);
2024-01-11 23:18:58 +00:00
if (receiverAccount.accountId != "") {
exists = true;
}
} catch (e, s) {
Logging.instance.log(
2024-05-27 23:56:22 +00:00
"Error getting account ${e.toString()} - ${s.toString()}",
level: LogLevel.Error,
);
2024-01-11 23:18:58 +00:00
}
return exists;
}
Future<stellar.Wallet> _getStellarWallet() async {
return await stellar.Wallet.from(
await getMnemonic(),
passphrase: await getMnemonicPassphrase(),
);
}
Future<stellar.KeyPair> _getSenderKeyPair({required int index}) async {
final wallet = await _getStellarWallet();
return await wallet.getKeyPair(index: index);
}
Future<Address> _fetchStellarAddress({required int index}) async {
final stellar.KeyPair keyPair = await _getSenderKeyPair(index: index);
final String address = keyPair.accountId;
return Address(
walletId: walletId,
value: address,
publicKey: keyPair.publicKey,
derivationIndex: index,
derivationPath: null,
type: AddressType.stellar,
subType: AddressSubType.receiving,
);
}
// ============== Overrides ==================================================
@override
int get isarTransactionVersion => 2;
@override
FilterOperation? get changeAddressFilterOperation =>
FilterGroup.and(standardChangeAddressFilters);
@override
FilterOperation? get receivingAddressFilterOperation =>
FilterGroup.and(standardReceivingAddressFilters);
@override
Future<void> checkSaveInitialReceivingAddress() async {
2024-01-11 23:18:58 +00:00
try {
final address = await getCurrentReceivingAddress();
if (address == null) {
await mainDB
.updateOrPutAddresses([await _fetchStellarAddress(index: 0)]);
}
2024-01-12 19:56:09 +00:00
} catch (e, s) {
2024-01-11 23:18:58 +00:00
// do nothing, still allow user into wallet
2024-01-12 19:56:09 +00:00
Logging.instance.log(
"$runtimeType checkSaveInitialReceivingAddress() failed: $e\n$s",
2024-01-12 19:56:09 +00:00
level: LogLevel.Error,
);
2024-01-11 23:18:58 +00:00
}
}
@override
Future<TxData> prepareSend({required TxData txData}) async {
try {
if (txData.recipients?.length != 1) {
throw Exception("Missing recipient");
}
final feeRate = txData.feeRateType;
var fee = 1000;
if (feeRate is FeeRateType) {
final theFees = await fees;
switch (feeRate) {
case FeeRateType.fast:
fee = theFees.fast;
case FeeRateType.slow:
fee = theFees.slow;
case FeeRateType.average:
default:
fee = theFees.medium;
}
}
return txData.copyWith(
fee: Amount(
rawValue: BigInt.from(fee),
fractionDigits: cryptoCurrency.fractionDigits,
),
);
} catch (e, s) {
2024-05-27 23:56:22 +00:00
Logging.instance.log(
"$runtimeType prepareSend() failed: $e\n$s",
level: LogLevel.Error,
);
2024-01-11 23:18:58 +00:00
rethrow;
}
}
@override
Future<TxData> confirmSend({required TxData txData}) async {
final senderKeyPair = await _getSenderKeyPair(index: 0);
2024-05-04 00:04:57 +00:00
final sender =
await (await stellarSdk).accounts.account(senderKeyPair.accountId);
2024-01-11 23:18:58 +00:00
final address = txData.recipients!.first.address;
final amountToSend = txData.recipients!.first.amount;
final memo = txData.memo;
//First check if account exists, can be skipped, but if the account does not exist,
// the transaction fee will be charged when the transaction fails.
final validAccount = await _accountExists(address);
final stellar.TransactionBuilder transactionBuilder;
if (!validAccount) {
//Fund the account, user must ensure account is correct
final createAccBuilder = stellar.CreateAccountOperationBuilder(
address,
amountToSend.decimal.toString(),
);
transactionBuilder = stellar.TransactionBuilder(sender).addOperation(
createAccBuilder.build(),
);
} else {
transactionBuilder = stellar.TransactionBuilder(sender).addOperation(
stellar.PaymentOperationBuilder(
address,
stellar.Asset.NATIVE,
amountToSend.decimal.toString(),
).build(),
);
}
if (memo != null) {
transactionBuilder.addMemo(stellar.MemoText(memo));
}
final transaction = transactionBuilder.build();
transaction.sign(senderKeyPair, stellarNetwork);
try {
2024-05-04 00:04:57 +00:00
final response = await (await stellarSdk).submitTransaction(transaction);
2024-01-11 23:18:58 +00:00
if (!response.success) {
throw Exception("${response.extras?.resultCodes?.transactionResultCode}"
" ::: ${response.extras?.resultCodes?.operationsResultCodes}");
}
return txData.copyWith(
txHash: response.hash!,
txid: response.hash!,
);
} catch (e, s) {
Logging.instance.log("Error sending TX $e - $s", level: LogLevel.Error);
rethrow;
}
}
@override
Future<Amount> estimateFeeFor(Amount amount, int feeRate) async {
final baseFee = await _getBaseFee();
return Amount(
rawValue: BigInt.from(baseFee),
fractionDigits: cryptoCurrency.fractionDigits,
);
}
@override
Future<FeeObject> get fees async {
2024-05-04 00:04:57 +00:00
final int fee = await _getBaseFee();
2024-01-11 23:18:58 +00:00
return FeeObject(
numberOfBlocksFast: 1,
numberOfBlocksAverage: 1,
numberOfBlocksSlow: 1,
fast: fee,
medium: fee,
slow: fee,
);
}
@override
Future<bool> pingCheck() async {
final currentNode = getCurrentNode();
return await testStellarNodeConnection(currentNode.host, currentNode.port);
}
@override
Future<void> recover({required bool isRescan}) async {
await refreshMutex.protect(() async {
if (isRescan) {
await mainDB.deleteWalletBlockchainData(walletId);
}
await mainDB.updateOrPutAddresses([await _fetchStellarAddress(index: 0)]);
});
if (isRescan) {
unawaited(refresh());
}
}
@override
Future<void> updateBalance() async {
try {
stellar.AccountResponse accountResponse;
try {
2024-05-04 00:04:57 +00:00
accountResponse = await (await stellarSdk)
.accounts
2024-01-11 23:18:58 +00:00
.account((await getCurrentReceivingAddress())!.value)
.onError((error, stackTrace) => throw error!);
} catch (e) {
if (e is stellar.ErrorResponse &&
e.body.contains("The resource at the url requested was not found. "
"This usually occurs for one of two reasons: "
"The url requested is not valid, or no data in our database "
"could be found with the parameters provided.")) {
// probably just doesn't have any history yet or whatever stellar needs
return;
} else {
Logging.instance.log(
"$runtimeType ${info.name} $walletId "
"failed to fetch account to updateBalance",
level: LogLevel.Warning,
);
rethrow;
}
}
2024-05-04 00:04:57 +00:00
for (final stellar.Balance balance in accountResponse.balances) {
2024-01-11 23:18:58 +00:00
switch (balance.assetType) {
case stellar.Asset.TYPE_NATIVE:
final swBalance = Balance(
total: Amount(
rawValue: BigInt.from(float.parse(balance.balance) * 10000000),
fractionDigits: cryptoCurrency.fractionDigits,
),
spendable: Amount(
rawValue: BigInt.from(float.parse(balance.balance) * 10000000),
fractionDigits: cryptoCurrency.fractionDigits,
),
blockedTotal: Amount(
rawValue: BigInt.from(0),
fractionDigits: cryptoCurrency.fractionDigits,
),
pendingSpendable: Amount(
rawValue: BigInt.from(0),
fractionDigits: cryptoCurrency.fractionDigits,
),
);
await info.updateBalance(newBalance: swBalance, isar: mainDB.isar);
}
}
} catch (e, s) {
Logging.instance.log(
"$runtimeType ${info.name} $walletId "
"updateBalance() failed: $e\n$s",
level: LogLevel.Warning,
);
rethrow;
}
}
@override
Future<void> updateChainHeight() async {
try {
2024-05-04 00:04:57 +00:00
final height = await (await stellarSdk)
.ledgers
2024-01-11 23:18:58 +00:00
.order(stellar.RequestBuilderOrder.DESC)
.limit(1)
.execute()
.then((value) => value.records!.first.sequence);
await info.updateCachedChainHeight(newHeight: height, isar: mainDB.isar);
} catch (e, s) {
Logging.instance.log(
"$runtimeType updateChainHeight() failed: $e\n$s",
level: LogLevel.Error,
);
rethrow;
}
}
@override
Future<void> updateNode() async {
2024-05-04 00:04:57 +00:00
_stellarSdk?.httpClient.close();
_stellarSdk = _getFreshSdk();
2024-01-11 23:18:58 +00:00
}
@override
Future<void> updateTransactions() async {
try {
final myAddress = (await getCurrentReceivingAddress())!;
2024-05-04 00:04:57 +00:00
final List<TransactionV2> transactionList = [];
2024-01-11 23:18:58 +00:00
stellar.Page<stellar.OperationResponse> payments;
try {
2024-05-04 00:04:57 +00:00
payments = await (await stellarSdk)
.payments
2024-01-11 23:18:58 +00:00
.forAccount(myAddress.value)
.order(stellar.RequestBuilderOrder.DESC)
.execute();
} catch (e) {
if (e is stellar.ErrorResponse &&
e.body.contains("The resource at the url requested was not found. "
"This usually occurs for one of two reasons: "
"The url requested is not valid, or no data in our database "
"could be found with the parameters provided.")) {
// probably just doesn't have any history yet or whatever stellar needs
return;
} else {
Logging.instance.log(
"Stellar ${info.name} $walletId failed to fetch transactions",
level: LogLevel.Warning,
);
rethrow;
}
}
2024-05-04 00:04:57 +00:00
for (final stellar.OperationResponse response in payments.records!) {
2024-01-11 23:18:58 +00:00
// PaymentOperationResponse por;
if (response is stellar.PaymentOperationResponse) {
final por = response;
final addressTo = por.to!.accountId;
final addressFrom = por.from!.accountId;
final TransactionType type;
if (addressFrom == myAddress.value) {
if (addressTo == myAddress.value) {
type = TransactionType.sentToSelf;
} else {
type = TransactionType.outgoing;
}
} else {
type = TransactionType.incoming;
}
final amount = Amount(
2024-05-27 23:56:22 +00:00
rawValue: BigInt.parse(
float
.parse(por.amount!)
.toStringAsFixed(cryptoCurrency.fractionDigits)
.replaceAll(".", ""),
),
2024-01-11 23:18:58 +00:00
fractionDigits: cryptoCurrency.fractionDigits,
);
// hack eth tx data into inputs and outputs
final List<OutputV2> outputs = [];
final List<InputV2> inputs = [];
2024-05-04 00:04:57 +00:00
final OutputV2 output =
OutputV2.isarCantDoRequiredInDefaultConstructor(
2024-01-11 23:18:58 +00:00
scriptPubKeyHex: "00",
valueStringSats: amount.raw.toString(),
addresses: [
addressTo,
],
walletOwns: addressTo == myAddress.value,
);
2024-05-04 00:04:57 +00:00
final InputV2 input = InputV2.isarCantDoRequiredInDefaultConstructor(
2024-01-11 23:18:58 +00:00
scriptSigHex: null,
scriptSigAsm: null,
2024-01-11 23:18:58 +00:00
sequence: null,
outpoint: null,
addresses: [addressFrom],
valueStringSats: amount.raw.toString(),
witness: null,
innerRedeemScriptAsm: null,
coinbase: null,
walletOwns: addressFrom == myAddress.value,
);
outputs.add(output);
inputs.add(input);
int fee = 0;
int height = 0;
//Query the transaction linked to the payment,
// por.transaction returns a null sometimes
2024-05-04 00:04:57 +00:00
final stellar.TransactionResponse tx = await (await stellarSdk)
.transactions
.transaction(por.transactionHash!);
2024-01-11 23:18:58 +00:00
if (tx.hash.isNotEmpty) {
fee = tx.feeCharged!;
height = tx.ledger;
}
final otherData = {
"overrideFee": Amount(
rawValue: BigInt.from(fee),
fractionDigits: cryptoCurrency.fractionDigits,
).toJsonString(),
};
final theTransaction = TransactionV2(
walletId: walletId,
blockHash: "",
hash: por.transactionHash!,
txid: por.transactionHash!,
timestamp:
DateTime.parse(por.createdAt!).millisecondsSinceEpoch ~/ 1000,
height: height,
inputs: inputs,
outputs: outputs,
version: -1,
type: type,
subType: TransactionSubType.none,
otherData: jsonEncode(otherData),
);
transactionList.add(theTransaction);
} else if (response is stellar.CreateAccountOperationResponse) {
final caor = response;
final TransactionType type;
if (caor.sourceAccount == myAddress.value) {
type = TransactionType.outgoing;
} else {
type = TransactionType.incoming;
}
final amount = Amount(
2024-05-27 23:56:22 +00:00
rawValue: BigInt.parse(
float
.parse(caor.startingBalance!)
.toStringAsFixed(cryptoCurrency.fractionDigits)
.replaceAll(".", ""),
),
2024-01-11 23:18:58 +00:00
fractionDigits: cryptoCurrency.fractionDigits,
);
// hack eth tx data into inputs and outputs
final List<OutputV2> outputs = [];
final List<InputV2> inputs = [];
2024-05-04 00:04:57 +00:00
final OutputV2 output =
OutputV2.isarCantDoRequiredInDefaultConstructor(
2024-01-11 23:18:58 +00:00
scriptPubKeyHex: "00",
valueStringSats: amount.raw.toString(),
addresses: [
// this is what the previous code was doing and I don't think its correct
caor.sourceAccount!,
],
walletOwns: caor.sourceAccount! == myAddress.value,
);
2024-05-04 00:04:57 +00:00
final InputV2 input = InputV2.isarCantDoRequiredInDefaultConstructor(
2024-01-11 23:18:58 +00:00
scriptSigHex: null,
scriptSigAsm: null,
2024-01-11 23:18:58 +00:00
sequence: null,
outpoint: null,
addresses: [
// this is what the previous code was doing and I don't think its correct
caor.sourceAccount!,
],
valueStringSats: amount.raw.toString(),
witness: null,
innerRedeemScriptAsm: null,
coinbase: null,
walletOwns: caor.sourceAccount! == myAddress.value,
);
outputs.add(output);
inputs.add(input);
int fee = 0;
int height = 0;
2024-05-04 00:04:57 +00:00
final tx = await (await stellarSdk)
.transactions
.transaction(caor.transactionHash!);
2024-01-11 23:18:58 +00:00
if (tx.hash.isNotEmpty) {
fee = tx.feeCharged!;
height = tx.ledger;
}
final otherData = {
"overrideFee": Amount(
rawValue: BigInt.from(fee),
fractionDigits: cryptoCurrency.fractionDigits,
).toJsonString(),
};
final theTransaction = TransactionV2(
walletId: walletId,
blockHash: "",
hash: caor.transactionHash!,
txid: caor.transactionHash!,
timestamp:
DateTime.parse(caor.createdAt!).millisecondsSinceEpoch ~/ 1000,
height: height,
inputs: inputs,
outputs: outputs,
version: -1,
type: type,
subType: TransactionSubType.none,
otherData: jsonEncode(otherData),
);
transactionList.add(theTransaction);
}
}
await mainDB.updateOrPutTransactionV2s(transactionList);
} catch (e, s) {
Logging.instance.log(
2024-05-27 23:56:22 +00:00
"Exception rethrown from updateTransactions(): $e\n$s",
level: LogLevel.Error,
);
2024-01-11 23:18:58 +00:00
rethrow;
}
}
@override
Future<bool> updateUTXOs() async {
// do nothing for stellar
return false;
}
}