From 7e73b65b4d4ba1fd3a8375cc6552d90f2fdf14b3 Mon Sep 17 00:00:00 2001 From: jojobyte <184880+jojobyte@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:54:36 -0600 Subject: [PATCH] fix(ui): :bug: prevent pairing with existing contact (xpub) --- src/rigs/add-contact.js | 118 +++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 44 deletions(-) diff --git a/src/rigs/add-contact.js b/src/rigs/add-contact.js index baf9162..dc96f33 100644 --- a/src/rigs/add-contact.js +++ b/src/rigs/add-contact.js @@ -5,7 +5,7 @@ import { formDataEntries, setClipboard, openBlobSVG, - sortContactsByAlias, + // sortContactsByAlias, // sortContactsByName, parseAddressField, generateContactPairingURI, @@ -28,7 +28,7 @@ export let addContactRig = (async function (globals) { let { setupDialog, appDialogs, appState, appTools, store, mainApp, wallet, userInfo, contactsList, - updateAllFunds, walletFunds, + updateAllFunds, } = globals; let aliases = {} @@ -164,6 +164,8 @@ export let addContactRig = (async function (globals) { // ) } + let newContact + if (existingContacts?.length > 0) { console.warn( `You've already paired with this contact`, @@ -175,53 +177,75 @@ export let addContactRig = (async function (globals) { } } ) - } - let newContact = await appTools.storedData.encryptItem( - store.contacts, - state.wallet.xkeyId, - { - ...state.contact, - updatedAt: (new Date()).toISOString(), - info: { - ...OIDC_CLAIMS, - ...(state.contact.info || {}), - ...info, + // newContact = existingContacts[0] + + let pairings = existingContacts.map(c => `@${c.alias}`) + if (pairings.length > 1) { + let lastPairing = pairings.pop() + pairings = `${pairings.join(', ')} & ${lastPairing}` + } else { + pairings = pairings[0] + } + + // TODO: maybe prompt to show original pairing info + // in the scenario where your contact + // lost their contacts list + target.contactAddr.setCustomValidity( + `You've already paired with this contact (@${preferred_username}) as ${pairings}`, + ) + target.reportValidity() + return; + } else { + newContact = await appTools.storedData.encryptItem( + store.contacts, + state.wallet.xkeyId, + { + ...state.contact, + updatedAt: (new Date()).toISOString(), + info: { + ...OIDC_CLAIMS, + ...(state.contact.info || {}), + ...info, + }, + outgoing, + alias: preferredAlias, + uri: value, }, - outgoing, - alias: preferredAlias, - uri: value, - }, - false, - ) + false, + ) - getStoreData( - store.contacts, - res => { - if (res) { - appState.contacts = res + getStoreData( + store.contacts, + res => { + if (res) { + appState.contacts = res - return contactsList.restate({ - contacts: res, - userInfo, - }) + return contactsList.restate({ + contacts: res, + userInfo, + }) + } + }, + res => async v => { + res.push(await appTools.storedData.decryptData(v)) } - }, - res => async v => { - res.push(await appTools.storedData.decryptData(v)) - } - ) + ) - state.contact = newContact + state.contact = newContact - if (xkeyOrAddr) { - target.contactAddr.value = xkeyOrAddr - } - if (name) { - target.contactName.value = name - } - if (preferred_username) { - target.contactAlias.value = preferredAlias + if (value) { + target.contactURI.value = value + } + if (xkeyOrAddr) { + target.contactAddr.value = xkeyOrAddr + } + if (name) { + target.contactName.value = name + } + if (preferred_username) { + target.contactAlias.value = preferredAlias + } } return @@ -303,6 +327,12 @@ export let addContactRig = (async function (globals) { +
Paste a Dash Address, Xprv/Xpub, or Link
@@ -559,7 +589,7 @@ export let addContactRig = (async function (globals) { sub: parsedAddr?.sub || '', name: event.target.contactName.value, }, - uri: event.target.contactAddr.value, + uri: event.target.contactURI.value, alias: currentAlias || event.target.contactAlias.value, }, false, @@ -574,7 +604,7 @@ export let addContactRig = (async function (globals) { if (res) { appState.contacts = res - updateAllFunds(state.wallet, walletFunds) + updateAllFunds(state.wallet) .then(funds => { // console.log('updateAllFunds then funds', funds) })