// Haveno App extends the features of Haveno, supporting mobile devices and more.
// Copyright (C) 2024 Kewbit (https://kewbit.org)
// Source Code: https://git.haveno.com/haveno/haveno-app.git
//
// Author: Kewbit
// Website: https://kewbit.org
// Contact Email: me@kewbit.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
import 'package:flutter/material.dart';
import 'package:haveno/grpc_models.dart';
import 'package:haveno/haveno_client.dart';
import 'package:haveno/haveno_service.dart';
import 'package:haveno_app/models/schema.dart';
class WalletsProvider with ChangeNotifier, CooldownMixin {
final HavenoChannel _havenoChannel;
final WalletsService _walletsService = WalletsService();
BalancesInfo? _balances;
List _xmrTxs = [];
String? _xmrPrimaryAddress;
List _xmrIncomingTransfers = [];
List _xmrOutgoingTransfers = [];
WalletsProvider(this._havenoChannel) {
setCooldownDurations({
'getBalances': const Duration(seconds: 15),
});
}
BalancesInfo? get balances => _balances;
String? get xmrPrimaryAddress => _xmrPrimaryAddress;
List? get xmrTxs => _xmrTxs;
List get xmrIncomingTransfers => _xmrIncomingTransfers;
List get xmrOutgoingTransfers => _xmrOutgoingTransfers;
Future getBalances() async {
await _havenoChannel.onConnected;
if (!await isCooldownValid('getBalances')) {
try {
_balances = await _walletsService.getBalances();
updateCooldown('getBalances');
print("Loaded balances...");
notifyListeners();
return;
} catch (e) {
print("Failed to get balances: $e");
}
} else {
print("Tried to get getBalances when cooldown is active");
return;
}
}
Future getXmrPrimaryAddress() async {
await _havenoChannel.onConnected;
try {
final getXmrPrimaryAddressReply = await _havenoChannel.walletsClient!
.getXmrPrimaryAddress(GetXmrPrimaryAddressRequest());
_xmrPrimaryAddress = getXmrPrimaryAddressReply.primaryAddress;
print("Primary Address: $_xmrPrimaryAddress");
notifyListeners();
} catch (e) {
print("Failed to get primary address: $e");
}
}
Future getXmrTxs() async {
await _havenoChannel.onConnected;
try {
final getXmrTxsReply =
await _havenoChannel.walletsClient!.getXmrTxs(GetXmrTxsRequest());
_xmrTxs = getXmrTxsReply.txs;
_xmrIncomingTransfers = [];
_xmrOutgoingTransfers = [];
for (var xmrTx in _xmrTxs) {
_xmrIncomingTransfers.addAll(xmrTx.incomingTransfers);
if (xmrTx.hasOutgoingTransfer()) {
_xmrOutgoingTransfers.add(xmrTx.outgoingTransfer);
}
}
notifyListeners();
} catch (e) {
print("Failed to get XMR transactions: $e");
}
}
Future createXmrTx(Iterable destinations) async {
await _havenoChannel.onConnected;
try {
final createXmrTxsReply = await _havenoChannel.walletsClient!
.createXmrTx(CreateXmrTxRequest(destinations: destinations));
var tx = createXmrTxsReply.tx;
// Check if tx with the same hash already exists
bool exists = _xmrTxs.any((existingTx) => existingTx.hash == tx.hash);
if (!exists) {
_xmrTxs.add(tx);
notifyListeners();
}
} catch (e) {
print("Failed to create an XMR transaction: $e");
}
}
Future relayXmrTx(String metadata) async {
await _havenoChannel.onConnected;
try {
await _havenoChannel.walletsClient!
.relayXmrTx(RelayXmrTxRequest(metadata: metadata));
} catch (e) {
print("Error relaying transaciton: $e");
}
}
Future getXmrSeed() async {
await _havenoChannel.onConnected;
try {
final getXmrSeedReply =
await _havenoChannel.walletsClient!.getXmrSeed(GetXmrSeedRequest());
return getXmrSeedReply.seed;
} catch (e) {
print("Error getting seed phrase: $e");
return null;
}
}
Future setWalletPassword(String newPassword, String? password) async {
await _havenoChannel.onConnected;
try {
await _havenoChannel.walletsClient!.setWalletPassword(
SetWalletPasswordRequest(
password: password, newPassword: newPassword));
} catch (e) {
print("Error setting wallet password: $e");
}
}
Future lockWallet(String password) async {
await _havenoChannel.onConnected;
try {
await _havenoChannel.walletsClient!.lockWallet(LockWalletRequest());
} catch (e) {
print("Error setting wallet password: $e");
}
}
Future unlockWallet(String password) async {
await _havenoChannel.onConnected;
try {
await _havenoChannel.walletsClient!.unlockWallet(UnlockWalletRequest());
} catch (e) {
print("Error setting wallet password: $e");
}
}
Future removeWalletPassword(String password) async {
await _havenoChannel.onConnected;
try {
await _havenoChannel.walletsClient!.removeWalletPassword(
RemoveWalletPasswordRequest(password: password));
} catch (e) {
print("Error removing wallet password: $e");
}
}
}