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

+16
View File
@@ -477,6 +477,22 @@ public sealed class Loc
"服务器 {0} 不在 Palladium 链上(创世块或检查点不同)。请选择其他服务器。"],
// Contacts
["msg.contact.address.invalid"] = [
"Indirizzo non valido per questa rete: il contatto non è stato salvato.",
"Invalid address for this network: the contact was not saved.",
"Dirección no válida para esta red: el contacto no se guardó.",
"Adresse invalide pour ce réseau : le contact n'a pas été enregistré.",
"Endereço inválido para esta rede: o contato não foi salvo.",
"Ungültige Adresse für dieses Netzwerk: Der Kontakt wurde nicht gespeichert.",
"该网络的地址无效:联系人未保存。"],
["msg.contact.duplicate"] = [
"Esiste già un contatto con questo indirizzo.",
"A contact with this address already exists.",
"Ya existe un contacto con esta dirección.",
"Un contact avec cette adresse existe déjà.",
"Já existe um contato com este endereço.",
"Ein Kontakt mit dieser Adresse existiert bereits.",
"已存在相同地址的联系人。"],
["contacts.name"] = ["Nome", "Name", "Nombre", "Nom", "Nome", "Name", "姓名"],
["contacts.address"] = ["Indirizzo", "Address", "Dirección", "Adresse", "Endereço", "Adresse", "地址"],
["contacts.name.ph"] = ["Nome contatto", "Contact name", "Nombre del contacto", "Nom du contact", "Nome do contato", "Kontaktname", "联系人姓名"],
@@ -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();