keep stored and followed paynym contacts over directory entries and their names, search the listed contacts, and show the payment code when linking
What changed, and why it matters
This commit fixes a user-interface bug in Sparrow Wallet's PayNym contact handling. Previously, the wallet could mix up contacts that had the same display name but different payment codes, and it could overwrite a user's own stored contact names with names from the public PayNym directory. The change makes the wallet prefer contacts you have already saved or followed, lets you search the contact list, and shows the actual payment code when you link to someone so you can confirm it is the right person. It is a usability and correctness improvement rather than a fix for a remote exploit.
Treat as a normal bug-fix / UX-hardening commit. Reviewers may want to confirm that `followedPayNyms` is cleared appropriately when the wallet is closed or switched, and that the notification-address equality logic handles testnet/mainnet address prefixes correctly. No urgent security deployment is indicated by the diff alone.
Security signals we found
BIP47 payment-code / notification-address confusion is reduced by indexing contacts on notification address
Name-squatting / collision risk in the PayNym directory is mitigated by preferring stored and followed contacts over directory names
Link dialog now shows abbreviated payment code, improving user verification before creating a notification transaction
No input validation, cryptography, network, or privilege changes observed
Evidence from the diff
The patch changes PayNym contact resolution to key contacts by their BIP47 notification address instead of only by payment code string or name. It introduces a local followedPayNyms cache, a getChildWalletNymName helper that falls back to an abbreviated payment code when a name is already used by another stored contact, and a findFollowing search that matches by notification address or name. updateFollowing now merges three sources in priority order: stored wallet contacts, contacts followed in the current session, and directory entries, removing directory entries whose names collide with stored/followed contacts. The link dialog now displays the payment code alongside the nym name. A unit test verifies the name-collision fallback.
Changed components
PayNym contact list UIPayNymController contact resolution and follow logicSendController child-wallet labelingPayNymCell follow buttonPayNym unit testsInspect captured patch +95 / −23
### src/main/java/com/sparrowwallet/sparrow/control/PayNymCell.java
@@ -68,7 +68,7 @@ protected void updateItem(PayNym payNym, boolean empty) {
pane.setRight(hBox);
button.setOnAction(event -> {
button.setDisable(true);
- payNymController.followPayNym(payNym.paymentCode());
+ payNymController.followPayNym(payNym);
});
} else if(contact && payNymController != null) {
HBox hBox = new HBox();
### src/main/java/com/sparrowwallet/sparrow/paynym/PayNym.java
@@ -109,6 +109,18 @@ public static PayNym fromWallet(Wallet bip47Wallet) {
return new PayNym(externalPaymentCode, null, nymName, segwit, Collections.emptyList(), Collections.emptyList());
}
+ public static String getChildWalletNymName(Wallet masterWallet, PaymentCode externalPaymentCode, PayNym payNym) {
+ if(payNym == null || payNym.nymName() == null) {
+ return externalPaymentCode.toAbbreviatedString();
+ }
+
+ //A name already given to a stored contact with another payment code would show two contacts under it
+ Address notificationAddress = externalPaymentCode.getNotificationAddress();
+ boolean nameInUse = masterWallet.getChildWallets().stream().filter(Wallet::isBip47).anyMatch(childWallet -> payNym.nymName().equals(getNymName(childWallet))
+ && !childWallet.getKeystores().get(0).getExternalPaymentCode().getNotificationAddress().equals(notificationAddress));
+ return nameInUse ? externalPaymentCode.toAbbreviatedString() : payNym.nymName();
+ }
+
public static String getNymName(Wallet bip47Wallet) {
if(bip47Wallet.getLabel() != null) {
String suffix = " " + bip47Wallet.getScriptType().getName();
### src/main/java/com/sparrowwallet/sparrow/paynym/PayNymController.java
@@ -2,6 +2,8 @@
import com.google.common.eventbus.Subscribe;
import com.sparrowwallet.drongo.SecureString;
+import com.sparrowwallet.drongo.address.Address;
+import com.sparrowwallet.drongo.bip47.InvalidPaymentCodeException;
import com.sparrowwallet.drongo.bip47.PaymentCode;
import com.sparrowwallet.drongo.bip47.SecretPoint;
import com.sparrowwallet.drongo.crypto.ECKey;
@@ -31,7 +33,6 @@
import org.slf4j.LoggerFactory;
import java.util.*;
-import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -42,12 +43,12 @@
public class PayNymController {
private static final Logger log = LoggerFactory.getLogger(PayNymController.class);
- public static final Pattern SAMOURAI_PAYNYM_REGEX = Pattern.compile("\\+[a-z]+[0-9][0-9a-fA-F][0-9a-fA-F]");
public static final Pattern ASHIGARU_PAYNYM_REGEX = Pattern.compile("\\+[a-z]+[0-9][0-9]");
public static final String INVALID_PAYMENT_CODE_LABEL = "Invalid Payment Code";
private String walletId;
private PayNym walletPayNym;
+ private final List<PayNym> followedPayNyms = new ArrayList<>();
private boolean requestingPassword;
@FXML
@@ -243,7 +244,7 @@ private void resetFollowing() {
}
private void searchFollowing(String nymIdentifier) {
- Optional<PayNym> optExisting = walletPayNym.following().stream().filter(payNym -> payNym.nymName().equals(nymIdentifier) || payNym.paymentCode().toString().equals(nymIdentifier)).findFirst();
+ Optional<PayNym> optExisting = findFollowing(nymIdentifier);
if(optExisting.isPresent()) {
followingList.setUserData(Boolean.FALSE);
List<PayNym> existingPayNym = new ArrayList<>();
@@ -307,12 +308,15 @@ public void retrievePayNym(ActionEvent event) {
});
}
- public void followPayNym(PaymentCode contact) {
+ public void followPayNym(PayNym contact) {
Wallet masterWallet = getMasterWallet();
retrievePayNymProgress.setVisible(true);
PayNymService.getAuthToken(masterWallet, new HashMap<>()).subscribe(authToken -> {
String signature = PayNymService.getSignature(masterWallet, authToken);
- PayNymService.followPaymentCode(contact, authToken, signature).subscribe(followMap -> {
+ PayNymService.followPaymentCode(contact.paymentCode(), authToken, signature).subscribe(followMap -> {
+ Address notificationAddress = contact.notificationAddress();
+ followedPayNyms.removeIf(followed -> followed.notificationAddress().equals(notificationAddress));
+ followedPayNyms.add(contact);
refresh();
}, error -> {
retrievePayNymProgress.setVisible(false);
@@ -340,7 +344,8 @@ public void followPayNym(PaymentCode contact) {
public Boolean isFollowing(PayNym payNym) {
if(followingList.getItems() != null) {
- return followingList.getItems().stream().anyMatch(following -> payNym.paymentCode().equals(following.paymentCode()));
+ Address notificationAddress = payNym.notificationAddress();
+ return followingList.getItems().stream().anyMatch(following -> notificationAddress.equals(following.notificationAddress()));
}
return null;
@@ -354,36 +359,65 @@ public boolean isLinked(PayNym payNym) {
}
public void updateFollowing() {
- List<PayNym> followingPayNyms = new ArrayList<>();
+ followingList.setItems(FXCollections.observableList(getFollowing()));
+ }
+
+ //The directory names contacts, but the payment codes followed in this session and the contacts stored in the wallet are not replaced by what it lists
+ private List<PayNym> getFollowing() {
+ Map<Address, PayNym> payNymMap = new LinkedHashMap<>();
if(walletPayNym != null) {
- followingPayNyms.addAll(walletPayNym.following());
+ for(PayNym payNym : walletPayNym.following()) {
+ payNymMap.putIfAbsent(payNym.notificationAddress(), payNym);
+ }
}
- Map<PaymentCode, PayNym> payNymMap = followingPayNyms.stream().collect(Collectors.toMap(PayNym::paymentCode, Function.identity(), (u, v) -> u, LinkedHashMap::new));
- followingList.setItems(FXCollections.observableList(getExistingWalletPayNyms(payNymMap)));
- }
+ for(PayNym followed : followedPayNyms) {
+ Address notificationAddress = followed.notificationAddress();
+ if(followed.nymName() != null) {
+ payNymMap.entrySet().removeIf(entry -> !entry.getKey().equals(notificationAddress) && followed.nymName().equals(entry.getValue().nymName()));
+ }
+ payNymMap.put(notificationAddress, followed);
+ }
- private List<PayNym> getExistingWalletPayNyms(Map<PaymentCode, PayNym> payNymMap) {
+ Set<Address> storedAddresses = new HashSet<>();
+ Set<String> storedNames = new HashSet<>();
List<Wallet> childWallets = new ArrayList<>(getMasterWallet().getChildWallets());
childWallets.sort(Comparator.comparingInt(o -> -o.getScriptType().ordinal()));
for(Wallet childWallet : childWallets) {
if(childWallet.isBip47()) {
PaymentCode externalPaymentCode = childWallet.getKeystores().get(0).getExternalPaymentCode();
- String walletNymName = PayNym.getNymName(childWallet);
- if(payNymMap.get(externalPaymentCode) == null || (walletNymName != null && !walletNymName.equals(payNymMap.get(externalPaymentCode).nymName()))) {
- if(walletNymName != null && payNymMap.get(externalPaymentCode) != null && SAMOURAI_PAYNYM_REGEX.matcher(walletNymName).matches()) {
- childWallet.setLabel(payNymMap.get(externalPaymentCode).nymName() + " " + childWallet.getScriptType().getName());
- EventManager.get().post(new WalletLabelChangedEvent(childWallet));
- } else {
- payNymMap.put(externalPaymentCode, PayNym.fromWallet(childWallet));
+ Address notificationAddress = externalPaymentCode.getNotificationAddress();
+ if(storedAddresses.add(notificationAddress)) {
+ PayNym listed = payNymMap.get(notificationAddress);
+ String walletNymName = PayNym.getNymName(childWallet);
+ boolean listedAsStored = listed != null && listed.paymentCode().toString().equals(externalPaymentCode.toString())
+ && (walletNymName == null || walletNymName.equals(listed.nymName()));
+ if(!listedAsStored) {
+ payNymMap.put(notificationAddress, PayNym.fromWallet(childWallet));
+ }
+ if(payNymMap.get(notificationAddress).nymName() != null) {
+ storedNames.add(payNymMap.get(notificationAddress).nymName());
}
}
}
}
+ Set<Address> followedAddresses = followedPayNyms.stream().map(PayNym::notificationAddress).collect(Collectors.toSet());
+ payNymMap.entrySet().removeIf(entry -> !storedAddresses.contains(entry.getKey()) && !followedAddresses.contains(entry.getKey())
+ && storedNames.contains(entry.getValue().nymName()));
+
return new ArrayList<>(payNymMap.values());
}
+ private Optional<PayNym> findFollowing(String nymIdentifier) {
+ try {
+ Address notificationAddress = new PaymentCode(nymIdentifier).getNotificationAddress();
+ return getFollowing().stream().filter(payNym -> payNym.notificationAddress().equals(notificationAddress)).findFirst();
+ } catch(InvalidPaymentCodeException | IllegalArgumentException e) {
+ return getFollowing().stream().filter(payNym -> nymIdentifier.equals(payNym.nymName())).findFirst();
+ }
+ }
+
private void addWalletIfNotificationTransactionPresent(List<PayNym> following) {
Unlinked unlinked = getUnlinkedPayNyms(following);
@@ -477,8 +511,9 @@ public List<Wallet> addChildWallets(PayNym payNym, PaymentCode externalPaymentCo
Wallet masterWallet = getMasterWallet();
Storage storage = AppServices.get().getOpenWallets().get(masterWallet);
List<ScriptType> scriptTypes = masterWallet.getScriptType() != ScriptType.P2PKH ? PayNym.getSegwitScriptTypes() : payNym.getScriptTypes();
+ String nymName = PayNym.getChildWalletNymName(masterWallet, externalPaymentCode, payNym);
for(ScriptType childScriptType : scriptTypes) {
- String label = payNym.nymName() + " " + childScriptType.getName();
+ String label = nymName + " " + childScriptType.getName();
Wallet addedWallet = masterWallet.addChildWallet(externalPaymentCode, childScriptType, label);
if(!storage.isPersisted(addedWallet)) {
try {
@@ -498,8 +533,11 @@ public List<Wallet> addChildWallets(PayNym payNym, PaymentCode externalPaymentCo
public void linkPayNym(PayNym payNym) {
ButtonType previewType = new ButtonType("Preview", ButtonBar.ButtonData.LEFT);
ButtonType sendType = new ButtonType("Send", ButtonBar.ButtonData.YES);
+ String abbreviatedPaymentCode = payNym.paymentCode().toAbbreviatedString();
+ boolean named = payNym.nymName() != null && !payNym.nymName().equals(abbreviatedPaymentCode);
+ String contact = named ? payNym.nymName() + " (" + abbreviatedPaymentCode + ")" : abbreviatedPaymentCode;
Optional<ButtonType> optButtonType = AppServices.showAlertDialog("Link PayNym?",
- "Linking to this contact will allow you to send to it directly (non-collaboratively) through unique private addresses you can generate independently.\n\n" +
+ "Linking to " + contact + " will allow you to send to it directly (non-collaboratively) through unique private addresses you can generate independently.\n\n" +
"It will cost " + MINIMUM_P2PKH_OUTPUT_SATS + " sats to create the link through a notification transaction, plus the mining fee. Send transaction?", Alert.AlertType.CONFIRMATION, previewType, ButtonType.CANCEL, sendType);
if(optButtonType.isPresent() && optButtonType.get() == sendType) {
broadcastNotificationTransaction(payNym);
### src/main/java/com/sparrowwallet/sparrow/wallet/SendController.java
@@ -1393,8 +1393,9 @@ public List<Wallet> addChildWallets(PaymentCode externalPaymentCode, PayNym payN
Wallet masterWallet = getWalletForm().getMasterWallet();
Storage storage = AppServices.get().getOpenWallets().get(masterWallet);
List<ScriptType> scriptTypes = PayNym.getSegwitScriptTypes();
+ String nymName = PayNym.getChildWalletNymName(masterWallet, externalPaymentCode, payNym);
for(ScriptType childScriptType : scriptTypes) {
- String label = (payNym == null ? externalPaymentCode.toAbbreviatedString() : payNym.nymName()) + " " + childScriptType.getName();
+ String label = nymName + " " + childScriptType.getName();
Wallet addedWallet = masterWallet.addChildWallet(externalPaymentCode, childScriptType, label);
if(!storage.isPersisted(addedWallet)) {
try {
### src/test/java/com/sparrowwallet/sparrow/paynym/PayNymTest.java
@@ -1,5 +1,8 @@
package com.sparrowwallet.sparrow.paynym;
+import com.sparrowwallet.drongo.bip47.PaymentCode;
+import com.sparrowwallet.drongo.protocol.ScriptType;
+import com.sparrowwallet.drongo.wallet.Wallet;
import org.junit.jupiter.api.Test;
import java.util.Collections;
@@ -14,6 +17,7 @@
*/
public class PayNymTest {
private static final String PAYMENT_CODE = "PM8TJTLJbPRGxSbc8EJi42Wrr6QbNSaSSVJ5Y3E4pbCYiTHUskHg13935Ubb7q8tx9GVbh2UuRnBc3WSyJHhUrw8KhprKnn9eDznYGieTzFcwQRya4GA";
+ private static final String OTHER_PAYMENT_CODE = "PM8TJS2JxQ5ztXUpBBRnpTbcUXbUHy2T1abfrb3KkAAtMEGNbey4oumH7Hc578WgQJhPjBxteQ5GHHToTYHE3A1w6p7tU6KSoFmWBVbFGjKPisZDbP97";
@Test
public void parsesAValidPaymentCode() {
@@ -38,6 +42,23 @@ public void omitsAPaymentCodeWithAnInvalidKey() {
assertTrue(PayNym.fromString(invalidKey, "id", "+name", true, Collections.emptyList(), Collections.emptyList()).isEmpty());
}
+ @Test
+ public void childWalletNameNotReusedForAnotherPaymentCode() throws Exception {
+ Wallet masterWallet = PayNymServiceTest.getWallet();
+ PaymentCode alice = new PaymentCode(PAYMENT_CODE);
+ PaymentCode bob = new PaymentCode(OTHER_PAYMENT_CODE);
+ masterWallet.addChildWallet(alice, ScriptType.P2WPKH, "+alice " + ScriptType.P2WPKH.getName());
+
+ PayNym namedAlice = PayNym.fromString(OTHER_PAYMENT_CODE, "id", "+alice", true, Collections.emptyList(), Collections.emptyList()).orElseThrow();
+ assertEquals(bob.toAbbreviatedString(), PayNym.getChildWalletNymName(masterWallet, bob, namedAlice));
+
+ PayNym namedBob = PayNym.fromString(OTHER_PAYMENT_CODE, "id", "+bob", true, Collections.emptyList(), Collections.emptyList()).orElseThrow();
+ assertEquals("+bob", PayNym.getChildWalletNymName(masterWallet, bob, namedBob));
+
+ PayNym aliceAgain = PayNym.fromString(PAYMENT_CODE, "id", "+alice", true, Collections.emptyList(), Collections.emptyList()).orElseThrow();
+ assertEquals("+alice", PayNym.getChildWalletNymName(masterWallet, alice, aliceAgain));
+ }
+
@Test
public void removesControlCharactersFromName() {
PayNym payNym = PayNym.fromString(PAYMENT_CODE, "id", " +name\u0000\u001f\u007f\u0085 ", true, Collections.emptyList(), Collections.emptyList()).orElseThrow();Why this scored 20/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.