Skip to content

Commit

Permalink
Add Send QR Contacts Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
JSKitty committed Aug 21, 2023
1 parent 5a50b9b commit c8b8f6e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
59 changes: 36 additions & 23 deletions scripts/contacts-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,13 @@ export async function guiAddContact() {
* Prompt the user to add a new Contact, safely checking for duplicates
* @param {String} strName - The Name of the Contact
* @param {String} strPubkey - The Public Key of the Contact
* @returns {Promise<boolean>} - `true` if accepted, `false` if rejected by the user
* @returns {Promise<boolean>} - `true` if contact was added, `false` if not
*/
export async function guiAddContactPrompt(strName, strPubkey) {
export async function guiAddContactPrompt(
strName,
strPubkey,
fDuplicateNotif = true
) {
// Verify the name
if (strName.length < 1)
return createAlert(
Expand Down Expand Up @@ -554,31 +558,40 @@ export async function guiAddContactPrompt(strName, strPubkey) {
const cContactByPubkey = getContactBy(cAccount, { pubkey: strPubkey });

// If both Name and Key are saved, then they just tried re-adding the same Contact twice
if (cContactByName && cContactByPubkey)
return createAlert(
'warning',
'<b>Contact already exists!</b><br>You already saved this contact',
[],
3000
);
if (cContactByName && cContactByPubkey) {
if (fDuplicateNotif)
createAlert(
'warning',
'<b>Contact already exists!</b><br>You already saved this contact',
[],
3000
);
return true;
}

// If the Name is saved, but not key, then this *could* be a kind of Username-based phishing attempt
if (cContactByName && !cContactByPubkey)
return createAlert(
'warning',
'<b>Contact name already exists!</b><br>This could potentially be a phishing attempt, beware!',
[],
4000
);
if (cContactByName && !cContactByPubkey) {
if (fDuplicateNotif)
createAlert(
'warning',
'<b>Contact name already exists!</b><br>This could potentially be a phishing attempt, beware!',
[],
4000
);
return true;
}

// If the Key is saved, but not the name: perhaps the Contact changed their name?
if (!cContactByName && cContactByPubkey)
return createAlert(
'warning',
`<b>Contact already exists, but under a different name!</b><br>You have ${strName} saved as <b>${cContactByPubkey.label}!</b> in your contacts`,
[],
5000
);
if (!cContactByName && cContactByPubkey) {
if (fDuplicateNotif)
createAlert(
'warning',
`<b>Contact already exists, but under a different name!</b><br>You have ${strName} saved as <b>${cContactByPubkey.label}!</b> in your contacts`,
[],
5000
);
return true;
}

// Render an 'Add to Contacts' UI
const strHTML = `
Expand Down
22 changes: 22 additions & 0 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,28 @@ export async function openSendQRScanner() {
);
}

// MPW Contact Request URI
if (cScan.data.includes('addcontact=') && cScan.data.includes(':')) {
// We'll split away the `addcontact`, then we *should* be left with a valid Contact URI
const strURI = cScan.data.split('addcontact=')[1];

// Sanity check the URI
if (strURI.includes(':')) {
// Split to 'name' and 'pubkey'
const arrParts = strURI.split(':');

// Prompt the user to (optionally) add the Contact, before the Tx
const fUseName = await guiAddContactPrompt(
sanitizeHTML(arrParts[0]),
arrParts[1],
false
);

// Prompt for payment
return guiPreparePayment(fUseName ? arrParts[0] : arrParts[1]);
}
}

// No idea what this is...
createAlert(
'warning',
Expand Down

0 comments on commit c8b8f6e

Please sign in to comment.