fix(app): validate contact addresses when saving

Adding a contact now checks the address against the wallet's network and
rejects duplicates; whitespace in a pasted address is removed. Before, any
string was stored and the error only surfaced at send time.
This commit is contained in:
davide committed 2026-11-03 14:15:03 +01:00
1 parent 06d1f59c2b
commit eca4d4ac0c
5 files changed
+117 -5

No files matched your search

@@ -2,6 +2,9 @@ using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using NBitcoin;
using PalladiumWallet.App.Localization;
using PalladiumWallet.Core.Chain;
using PalladiumWallet.Core.Storage;
namespace PalladiumWallet.App.ViewModels;
@@ -33,8 +36,23 @@ public partial class MainWindowViewModel
private void AddContact()
{
var name = NewContactName.Trim();
var addr = NewContactAddress.Trim();
// Pasted addresses often carry spaces or line breaks anywhere: drop all whitespace.
var addr = string.Concat(NewContactAddress.Where(c => !char.IsWhiteSpace(c)));
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(addr)) return;
try
{
BitcoinAddress.Create(addr, PalladiumNetworks.For(Net));
}
catch (System.FormatException)
{
StatusMessage = Loc.Tr("msg.contact.address.invalid");
return;
}
if (Contacts.Any(c => string.Equals(c.Address, addr, System.StringComparison.OrdinalIgnoreCase)))
{
StatusMessage = Loc.Tr("msg.contact.duplicate");
return;
}
Contacts.Add(new ContactEntry(name, addr));
NewContactName = NewContactAddress = "";
PersistContacts();