Skip to content

Commit

Permalink
fix(ui): 🐛 prevent pairing with existing contact (xpub)
Browse files Browse the repository at this point in the history
  • Loading branch information
jojobyte committed Apr 13, 2024
1 parent 54cd555 commit 7e73b65
Showing 1 changed file with 74 additions and 44 deletions.
118 changes: 74 additions & 44 deletions src/rigs/add-contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
formDataEntries,
setClipboard,
openBlobSVG,
sortContactsByAlias,
// sortContactsByAlias,
// sortContactsByName,
parseAddressField,
generateContactPairingURI,
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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`,
Expand All @@ -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
Expand Down Expand Up @@ -303,6 +327,12 @@ export let addContactRig = (async function (globals) {
</span>
</button>
</div>
<input
id="contactURI"
type="hidden"
name="contactURI"
value=""
/>
<p>Paste a Dash Address, Xprv/Xpub, or Link</p>
<div class="error"></div>
Expand Down Expand Up @@ -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,
Expand All @@ -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)
})
Expand Down

0 comments on commit 7e73b65

Please sign in to comment.