diff --git a/src/popup/views/addWallet.js b/src/popup/views/addWallet.js index 9fcfed6..bb068df 100644 --- a/src/popup/views/addWallet.js +++ b/src/popup/views/addWallet.js @@ -97,18 +97,26 @@ async function importMnemonic(ctx) { const pw = validatePassword(); if (!pw) return; const { xpub, firstAddress } = hdWalletFromMnemonic(mnemonic); - const duplicate = state.wallets.find( - (w) => - w.type === "hd" && - w.addresses[0] && - w.addresses[0].address.toLowerCase() === firstAddress.toLowerCase(), + const addrDup = state.wallets.find((w) => + w.addresses.some( + (a) => a.address.toLowerCase() === firstAddress.toLowerCase(), + ), ); - if (duplicate) { + if (addrDup) { showFlash( - "This recovery phrase is already added (" + duplicate.name + ").", + "An address from this phrase already exists in " + + addrDup.name + + ".", ); return; } + const xpubDup = state.wallets.find( + (w) => (w.type === "hd" || w.type === "xprv") && w.xpub === xpub, + ); + if (xpubDup) { + showFlash("This recovery phrase matches " + xpubDup.name + "."); + return; + } const encrypted = await encryptWithPassword(mnemonic, pw); const walletNum = state.wallets.length + 1; const wallet = { @@ -162,16 +170,11 @@ async function importPrivateKey(ctx) { } const pw = validatePassword(); if (!pw) return; - const duplicate = state.wallets.find( - (w) => - w.type === "key" && - w.addresses[0] && - w.addresses[0].address.toLowerCase() === addr.toLowerCase(), + const duplicate = state.wallets.find((w) => + w.addresses.some((a) => a.address.toLowerCase() === addr.toLowerCase()), ); if (duplicate) { - showFlash( - "This private key is already added (" + duplicate.name + ").", - ); + showFlash("This address already exists in " + duplicate.name + "."); return; } const encrypted = await encryptWithPassword(key, pw); @@ -208,14 +211,22 @@ async function importXprvKey(ctx) { return; } const { xpub, firstAddress } = result; - const duplicate = state.wallets.find( - (w) => - (w.type === "hd" || w.type === "xprv") && - w.addresses[0] && - w.addresses[0].address.toLowerCase() === firstAddress.toLowerCase(), + const addrDup = state.wallets.find((w) => + w.addresses.some( + (a) => a.address.toLowerCase() === firstAddress.toLowerCase(), + ), ); - if (duplicate) { - showFlash("This key is already added (" + duplicate.name + ")."); + if (addrDup) { + showFlash( + "An address from this key already exists in " + addrDup.name + ".", + ); + return; + } + const xpubDup = state.wallets.find( + (w) => (w.type === "hd" || w.type === "xprv") && w.xpub === xpub, + ); + if (xpubDup) { + showFlash("This key matches " + xpubDup.name + "."); return; } const pw = validatePassword();