Skip to content

Commit 7e73b65

Browse files
committed
fix(ui): 🐛 prevent pairing with existing contact (xpub)
1 parent 54cd555 commit 7e73b65

File tree

1 file changed

+74
-44
lines changed

1 file changed

+74
-44
lines changed

src/rigs/add-contact.js

Lines changed: 74 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
formDataEntries,
66
setClipboard,
77
openBlobSVG,
8-
sortContactsByAlias,
8+
// sortContactsByAlias,
99
// sortContactsByName,
1010
parseAddressField,
1111
generateContactPairingURI,
@@ -28,7 +28,7 @@ export let addContactRig = (async function (globals) {
2828
let {
2929
setupDialog, appDialogs, appState, appTools, store,
3030
mainApp, wallet, userInfo, contactsList,
31-
updateAllFunds, walletFunds,
31+
updateAllFunds,
3232
} = globals;
3333

3434
let aliases = {}
@@ -164,6 +164,8 @@ export let addContactRig = (async function (globals) {
164164
// )
165165
}
166166

167+
let newContact
168+
167169
if (existingContacts?.length > 0) {
168170
console.warn(
169171
`You've already paired with this contact`,
@@ -175,53 +177,75 @@ export let addContactRig = (async function (globals) {
175177
}
176178
}
177179
)
178-
}
179180

180-
let newContact = await appTools.storedData.encryptItem(
181-
store.contacts,
182-
state.wallet.xkeyId,
183-
{
184-
...state.contact,
185-
updatedAt: (new Date()).toISOString(),
186-
info: {
187-
...OIDC_CLAIMS,
188-
...(state.contact.info || {}),
189-
...info,
181+
// newContact = existingContacts[0]
182+
183+
let pairings = existingContacts.map(c => `@${c.alias}`)
184+
if (pairings.length > 1) {
185+
let lastPairing = pairings.pop()
186+
pairings = `${pairings.join(', ')} & ${lastPairing}`
187+
} else {
188+
pairings = pairings[0]
189+
}
190+
191+
// TODO: maybe prompt to show original pairing info
192+
// in the scenario where your contact
193+
// lost their contacts list
194+
target.contactAddr.setCustomValidity(
195+
`You've already paired with this contact (@${preferred_username}) as ${pairings}`,
196+
)
197+
target.reportValidity()
198+
return;
199+
} else {
200+
newContact = await appTools.storedData.encryptItem(
201+
store.contacts,
202+
state.wallet.xkeyId,
203+
{
204+
...state.contact,
205+
updatedAt: (new Date()).toISOString(),
206+
info: {
207+
...OIDC_CLAIMS,
208+
...(state.contact.info || {}),
209+
...info,
210+
},
211+
outgoing,
212+
alias: preferredAlias,
213+
uri: value,
190214
},
191-
outgoing,
192-
alias: preferredAlias,
193-
uri: value,
194-
},
195-
false,
196-
)
215+
false,
216+
)
197217

198-
getStoreData(
199-
store.contacts,
200-
res => {
201-
if (res) {
202-
appState.contacts = res
218+
getStoreData(
219+
store.contacts,
220+
res => {
221+
if (res) {
222+
appState.contacts = res
203223

204-
return contactsList.restate({
205-
contacts: res,
206-
userInfo,
207-
})
224+
return contactsList.restate({
225+
contacts: res,
226+
userInfo,
227+
})
228+
}
229+
},
230+
res => async v => {
231+
res.push(await appTools.storedData.decryptData(v))
208232
}
209-
},
210-
res => async v => {
211-
res.push(await appTools.storedData.decryptData(v))
212-
}
213-
)
233+
)
214234

215-
state.contact = newContact
235+
state.contact = newContact
216236

217-
if (xkeyOrAddr) {
218-
target.contactAddr.value = xkeyOrAddr
219-
}
220-
if (name) {
221-
target.contactName.value = name
222-
}
223-
if (preferred_username) {
224-
target.contactAlias.value = preferredAlias
237+
if (value) {
238+
target.contactURI.value = value
239+
}
240+
if (xkeyOrAddr) {
241+
target.contactAddr.value = xkeyOrAddr
242+
}
243+
if (name) {
244+
target.contactName.value = name
245+
}
246+
if (preferred_username) {
247+
target.contactAlias.value = preferredAlias
248+
}
225249
}
226250

227251
return
@@ -303,6 +327,12 @@ export let addContactRig = (async function (globals) {
303327
</span>
304328
</button>
305329
</div>
330+
<input
331+
id="contactURI"
332+
type="hidden"
333+
name="contactURI"
334+
value=""
335+
/>
306336
<p>Paste a Dash Address, Xprv/Xpub, or Link</p>
307337
308338
<div class="error"></div>
@@ -559,7 +589,7 @@ export let addContactRig = (async function (globals) {
559589
sub: parsedAddr?.sub || '',
560590
name: event.target.contactName.value,
561591
},
562-
uri: event.target.contactAddr.value,
592+
uri: event.target.contactURI.value,
563593
alias: currentAlias || event.target.contactAlias.value,
564594
},
565595
false,
@@ -574,7 +604,7 @@ export let addContactRig = (async function (globals) {
574604
if (res) {
575605
appState.contacts = res
576606

577-
updateAllFunds(state.wallet, walletFunds)
607+
updateAllFunds(state.wallet)
578608
.then(funds => {
579609
// console.log('updateAllFunds then funds', funds)
580610
})

0 commit comments

Comments
 (0)