Skip to content

Commit

Permalink
Merge pull request #12604 from collins-okafor/master
Browse files Browse the repository at this point in the history
transaction search with destination address
  • Loading branch information
soosr committed Mar 11, 2024
2 parents ac8dce7 + 77bcc9c commit 0dc050c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
using System.Reactive.Linq;
using System.Threading.Tasks;
using DynamicData;
using NBitcoin;
using ReactiveUI;
using WalletWasabi.Fluent.Extensions;
using WalletWasabi.Fluent.ViewModels.SearchBar.Patterns;
using WalletWasabi.Fluent.ViewModels.SearchBar.SearchItems;
using WalletWasabi.Fluent.ViewModels.Wallets;
using WalletWasabi.Fluent.ViewModels.Wallets.Home.History.HistoryItems;
using WalletWasabi.Helpers;
using WalletWasabi.Wallets;

namespace WalletWasabi.Fluent.ViewModels.SearchBar.Sources;
Expand Down Expand Up @@ -117,6 +119,14 @@ private static IEnumerable<ISearchItem> Search(string query)
private static IEnumerable<(WalletViewModel, HistoryItemViewModelBase)> Filter(string queryStr)
{
return Flatten(GetTransactionsByWallet())
.Where(tuple => ContainsId(tuple.Item2, queryStr));
.Where(tuple => NBitcoinHelpers.TryParseBitcoinAddress(tuple.Item1.WalletModel.Network, queryStr, out var address) ?
ContainsDestinationAddress(tuple.Item1, tuple.Item2, address) :
ContainsId(tuple.Item2, queryStr));
}

private static bool ContainsDestinationAddress(WalletViewModel walletViewModel, HistoryItemViewModelBase historyItem, BitcoinAddress address)
{
var txid = historyItem.Transaction.Id;
return walletViewModel.WalletModel.Transactions.GetDestinationAddresses(txid).Contains(address);
}
}
15 changes: 15 additions & 0 deletions WalletWasabi/Helpers/NBitcoinHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NBitcoin.Crypto;
using NBitcoin.Protocol;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -133,4 +134,18 @@ public static async Task<AddressManager> LoadAddressManagerFromPeerFileAsync(str
addrman.ReadWrite(stream);
return addrman;
}

public static bool TryParseBitcoinAddress(Network network, string queryStr, [NotNullWhen(true)] out BitcoinAddress? address)
{
address = null;
try
{
address = BitcoinAddress.Create(queryStr, network);
return true;
}
catch (FormatException)
{
return false;
}
}
}

0 comments on commit 0dc050c

Please sign in to comment.