fix: cross-wallet duplicate address detection on import #117

Closed
clawbot wants to merge 1 commits from fix/cross-wallet-duplicate-detection into main

View File

@@ -97,18 +97,26 @@ async function importMnemonic(ctx) {
const pw = validatePassword(); const pw = validatePassword();
if (!pw) return; if (!pw) return;
const { xpub, firstAddress } = hdWalletFromMnemonic(mnemonic); const { xpub, firstAddress } = hdWalletFromMnemonic(mnemonic);
const duplicate = state.wallets.find( const addrDup = state.wallets.find((w) =>
(w) => w.addresses.some(
w.type === "hd" && (a) => a.address.toLowerCase() === firstAddress.toLowerCase(),
w.addresses[0] && ),
w.addresses[0].address.toLowerCase() === firstAddress.toLowerCase(),
); );
if (duplicate) { if (addrDup) {
showFlash( showFlash(
"This recovery phrase is already added (" + duplicate.name + ").", "An address from this phrase already exists in " +
addrDup.name +
".",
); );
return; 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 encrypted = await encryptWithPassword(mnemonic, pw);
const walletNum = state.wallets.length + 1; const walletNum = state.wallets.length + 1;
const wallet = { const wallet = {
@@ -162,16 +170,11 @@ async function importPrivateKey(ctx) {
} }
const pw = validatePassword(); const pw = validatePassword();
if (!pw) return; if (!pw) return;
const duplicate = state.wallets.find( const duplicate = state.wallets.find((w) =>
(w) => w.addresses.some((a) => a.address.toLowerCase() === addr.toLowerCase()),
w.type === "key" &&
w.addresses[0] &&
w.addresses[0].address.toLowerCase() === addr.toLowerCase(),
); );
if (duplicate) { if (duplicate) {
showFlash( showFlash("This address already exists in " + duplicate.name + ".");
"This private key is already added (" + duplicate.name + ").",
);
return; return;
} }
const encrypted = await encryptWithPassword(key, pw); const encrypted = await encryptWithPassword(key, pw);
@@ -208,14 +211,22 @@ async function importXprvKey(ctx) {
return; return;
} }
const { xpub, firstAddress } = result; const { xpub, firstAddress } = result;
const duplicate = state.wallets.find( const addrDup = state.wallets.find((w) =>
(w) => w.addresses.some(
(w.type === "hd" || w.type === "xprv") && (a) => a.address.toLowerCase() === firstAddress.toLowerCase(),
w.addresses[0] && ),
w.addresses[0].address.toLowerCase() === firstAddress.toLowerCase(),
); );
if (duplicate) { if (addrDup) {
showFlash("This key is already added (" + duplicate.name + ")."); 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; return;
} }
const pw = validatePassword(); const pw = validatePassword();