Compare commits
1 Commits
fix/cross-
...
issue-99-b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d1d584b52 |
@@ -56,104 +56,37 @@
|
|||||||
< Back
|
< Back
|
||||||
</button>
|
</button>
|
||||||
<h2 class="font-bold mb-2">Add Wallet</h2>
|
<h2 class="font-bold mb-2">Add Wallet</h2>
|
||||||
|
<p class="mb-2">
|
||||||
<!-- Mode selector tabs -->
|
Enter your 12 or 24 word recovery phrase below, or click the
|
||||||
|
button to roll the die for a new one.
|
||||||
|
</p>
|
||||||
|
<div class="mb-1 flex justify-end">
|
||||||
|
<button
|
||||||
|
id="btn-generate-phrase"
|
||||||
|
class="border border-border px-2 py-1 hover:bg-fg hover:text-bg cursor-pointer text-xs"
|
||||||
|
title="Generate a random recovery phrase"
|
||||||
|
>
|
||||||
|
[⚀]
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div class="mb-2">
|
||||||
|
<textarea
|
||||||
|
id="wallet-mnemonic"
|
||||||
|
rows="3"
|
||||||
|
class="border border-border p-1 w-full font-mono text-sm bg-bg text-fg resize-y"
|
||||||
|
placeholder="word word word ..."
|
||||||
|
></textarea>
|
||||||
|
</div>
|
||||||
<div
|
<div
|
||||||
class="flex border-b border-border mb-3"
|
id="add-wallet-phrase-warning"
|
||||||
id="add-wallet-tabs"
|
class="text-xs mb-2 border border-border border-dashed p-2 hidden"
|
||||||
>
|
>
|
||||||
<button
|
Write these words down and keep them safe. Anyone with them
|
||||||
id="tab-mnemonic"
|
can take your funds; if you lose them, your wallet is gone.
|
||||||
class="px-3 py-1.5 cursor-pointer text-xs font-bold border border-border border-b-bg bg-bg -mb-px"
|
|
||||||
>
|
|
||||||
From Phrase
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
id="tab-privkey"
|
|
||||||
class="px-3 py-1.5 cursor-pointer text-xs text-muted border border-dashed border-border-light border-b-transparent -mb-px hover:bg-fg hover:text-bg"
|
|
||||||
>
|
|
||||||
From Key
|
|
||||||
</button>
|
|
||||||
<button
|
|
||||||
id="tab-xprv"
|
|
||||||
class="px-3 py-1.5 cursor-pointer text-xs text-muted border border-dashed border-border-light border-b-transparent -mb-px hover:bg-fg hover:text-bg"
|
|
||||||
>
|
|
||||||
From xprv
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Mnemonic form section -->
|
|
||||||
<div id="add-wallet-section-mnemonic">
|
|
||||||
<p class="mb-2">
|
|
||||||
Enter your 12 or 24 word recovery phrase below, or click
|
|
||||||
the button to roll the die for a new one.
|
|
||||||
</p>
|
|
||||||
<div class="mb-1 flex justify-end">
|
|
||||||
<button
|
|
||||||
id="btn-generate-phrase"
|
|
||||||
class="border border-border px-2 py-1 hover:bg-fg hover:text-bg cursor-pointer text-xs"
|
|
||||||
title="Generate a random recovery phrase"
|
|
||||||
>
|
|
||||||
[⚀]
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
<div class="mb-2">
|
|
||||||
<textarea
|
|
||||||
id="wallet-mnemonic"
|
|
||||||
rows="3"
|
|
||||||
class="border border-border p-1 w-full font-mono text-sm bg-bg text-fg resize-y"
|
|
||||||
placeholder="word word word ..."
|
|
||||||
></textarea>
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
id="add-wallet-phrase-warning"
|
|
||||||
class="text-xs mb-2 border border-border border-dashed p-2 hidden"
|
|
||||||
>
|
|
||||||
Write these words down and keep them safe. Anyone with
|
|
||||||
them can take your funds; if you lose them, your wallet
|
|
||||||
is gone.
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Private key form section -->
|
|
||||||
<div id="add-wallet-section-privkey" class="hidden">
|
|
||||||
<p class="mb-2">
|
|
||||||
Paste your private key below. This wallet will have a
|
|
||||||
single address.
|
|
||||||
</p>
|
|
||||||
<div class="mb-2">
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
id="import-private-key"
|
|
||||||
class="border border-border p-1 w-full font-mono text-sm bg-bg text-fg"
|
|
||||||
placeholder="0x..."
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Extended key (xprv) form section -->
|
|
||||||
<div id="add-wallet-section-xprv" class="hidden">
|
|
||||||
<p class="mb-2">
|
|
||||||
Paste your extended private key (xprv) below. This will
|
|
||||||
import the HD wallet and scan for used addresses.
|
|
||||||
</p>
|
|
||||||
<div class="mb-2">
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
id="import-xprv-key"
|
|
||||||
class="border border-border p-1 w-full font-mono text-sm bg-bg text-fg"
|
|
||||||
placeholder="xprv..."
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- Shared password fields -->
|
|
||||||
<div class="mb-2" id="add-wallet-password-section">
|
<div class="mb-2" id="add-wallet-password-section">
|
||||||
<label class="block mb-1">Choose a password</label>
|
<label class="block mb-1">Choose a password</label>
|
||||||
<p
|
<p class="text-xs text-muted mb-1">
|
||||||
class="text-xs text-muted mb-1"
|
|
||||||
id="add-wallet-password-hint"
|
|
||||||
>
|
|
||||||
This password encrypts your recovery phrase on this
|
This password encrypts your recovery phrase on this
|
||||||
device. You will need it to send funds.
|
device. You will need it to send funds.
|
||||||
</p>
|
</p>
|
||||||
@@ -174,6 +107,64 @@
|
|||||||
<button
|
<button
|
||||||
id="btn-add-wallet-confirm"
|
id="btn-add-wallet-confirm"
|
||||||
class="border border-border px-2 py-1 hover:bg-fg hover:text-bg cursor-pointer"
|
class="border border-border px-2 py-1 hover:bg-fg hover:text-bg cursor-pointer"
|
||||||
|
>
|
||||||
|
Add
|
||||||
|
</button>
|
||||||
|
<div class="mt-3 text-xs text-muted">
|
||||||
|
Have a private key instead?
|
||||||
|
<button
|
||||||
|
id="btn-add-wallet-import-key"
|
||||||
|
class="underline cursor-pointer bg-transparent border-none text-fg text-xs font-mono p-0"
|
||||||
|
>
|
||||||
|
Import private key
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- ============ IMPORT PRIVATE KEY ============ -->
|
||||||
|
<div id="view-import-key" class="view hidden">
|
||||||
|
<button
|
||||||
|
id="btn-import-key-back"
|
||||||
|
class="border border-border px-2 py-1 hover:bg-fg hover:text-bg cursor-pointer mb-2"
|
||||||
|
>
|
||||||
|
< Back
|
||||||
|
</button>
|
||||||
|
<h2 class="font-bold mb-2">Import Private Key</h2>
|
||||||
|
<p class="mb-2">
|
||||||
|
Paste your private key below. This wallet will have a single
|
||||||
|
address.
|
||||||
|
</p>
|
||||||
|
<div class="mb-2">
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
id="import-private-key"
|
||||||
|
class="border border-border p-1 w-full font-mono text-sm bg-bg text-fg"
|
||||||
|
placeholder="0x..."
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="mb-2" id="import-key-password-section">
|
||||||
|
<label class="block mb-1">Choose a password</label>
|
||||||
|
<p class="text-xs text-muted mb-1">
|
||||||
|
This password encrypts your private key on this device.
|
||||||
|
You will need it to send funds.
|
||||||
|
</p>
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
id="import-key-password"
|
||||||
|
class="border border-border p-1 w-full font-mono text-sm bg-bg text-fg"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="mb-2" id="import-key-password-confirm-section">
|
||||||
|
<label class="block mb-1">Confirm password</label>
|
||||||
|
<input
|
||||||
|
type="password"
|
||||||
|
id="import-key-password-confirm"
|
||||||
|
class="border border-border p-1 w-full font-mono text-sm bg-bg text-fg"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
id="btn-import-key-confirm"
|
||||||
|
class="border border-border px-2 py-1 hover:bg-fg hover:text-bg cursor-pointer"
|
||||||
>
|
>
|
||||||
Import
|
Import
|
||||||
</button>
|
</button>
|
||||||
@@ -586,19 +577,6 @@
|
|||||||
<div id="confirm-fee-amount" class="text-xs"></div>
|
<div id="confirm-fee-amount" class="text-xs"></div>
|
||||||
</div>
|
</div>
|
||||||
<div id="confirm-warnings" class="mb-2 hidden"></div>
|
<div id="confirm-warnings" class="mb-2 hidden"></div>
|
||||||
<div
|
|
||||||
id="confirm-recipient-warning"
|
|
||||||
class="mb-2"
|
|
||||||
style="visibility: hidden"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
class="border border-red-500 border-dashed p-2 text-xs font-bold text-red-500"
|
|
||||||
>
|
|
||||||
WARNING: The recipient address has ZERO transaction
|
|
||||||
history. This may indicate a fresh or unused address.
|
|
||||||
Double-check the address before sending.
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div
|
<div
|
||||||
id="confirm-errors"
|
id="confirm-errors"
|
||||||
class="mb-2 border border-border border-dashed p-2 hidden"
|
class="mb-2 border border-border border-dashed p-2 hidden"
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const { $, showView } = require("./views/helpers");
|
|||||||
const home = require("./views/home");
|
const home = require("./views/home");
|
||||||
const welcome = require("./views/welcome");
|
const welcome = require("./views/welcome");
|
||||||
const addWallet = require("./views/addWallet");
|
const addWallet = require("./views/addWallet");
|
||||||
|
const importKey = require("./views/importKey");
|
||||||
const addressDetail = require("./views/addressDetail");
|
const addressDetail = require("./views/addressDetail");
|
||||||
const addressToken = require("./views/addressToken");
|
const addressToken = require("./views/addressToken");
|
||||||
const send = require("./views/send");
|
const send = require("./views/send");
|
||||||
@@ -53,6 +54,7 @@ const ctx = {
|
|||||||
renderWalletList,
|
renderWalletList,
|
||||||
doRefreshAndRender,
|
doRefreshAndRender,
|
||||||
showAddWalletView: () => addWallet.show(),
|
showAddWalletView: () => addWallet.show(),
|
||||||
|
showImportKeyView: () => importKey.show(),
|
||||||
showAddressDetail: () => addressDetail.show(),
|
showAddressDetail: () => addressDetail.show(),
|
||||||
showAddressToken: () => addressToken.show(),
|
showAddressToken: () => addressToken.show(),
|
||||||
showAddTokenView: () => addToken.show(),
|
showAddTokenView: () => addToken.show(),
|
||||||
@@ -215,6 +217,7 @@ async function init() {
|
|||||||
|
|
||||||
welcome.init(ctx);
|
welcome.init(ctx);
|
||||||
addWallet.init(ctx);
|
addWallet.init(ctx);
|
||||||
|
importKey.init(ctx);
|
||||||
home.init(ctx);
|
home.init(ctx);
|
||||||
addressDetail.init(ctx);
|
addressDetail.init(ctx);
|
||||||
addressToken.init(ctx);
|
addressToken.init(ctx);
|
||||||
|
|||||||
@@ -3,296 +3,114 @@ const {
|
|||||||
generateMnemonic,
|
generateMnemonic,
|
||||||
hdWalletFromMnemonic,
|
hdWalletFromMnemonic,
|
||||||
isValidMnemonic,
|
isValidMnemonic,
|
||||||
addressFromPrivateKey,
|
|
||||||
hdWalletFromXprv,
|
|
||||||
isValidXprv,
|
|
||||||
} = require("../../shared/wallet");
|
} = require("../../shared/wallet");
|
||||||
const { encryptWithPassword } = require("../../shared/vault");
|
const { encryptWithPassword } = require("../../shared/vault");
|
||||||
const { state, saveState } = require("../../shared/state");
|
const { state, saveState } = require("../../shared/state");
|
||||||
const { scanForAddresses } = require("../../shared/balances");
|
const { scanForAddresses } = require("../../shared/balances");
|
||||||
|
|
||||||
let currentMode = "mnemonic";
|
|
||||||
|
|
||||||
const MODES = ["mnemonic", "privkey", "xprv"];
|
|
||||||
|
|
||||||
const PASSWORD_HINTS = {
|
|
||||||
mnemonic:
|
|
||||||
"This password encrypts your recovery phrase on this device. You will need it to send funds.",
|
|
||||||
privkey:
|
|
||||||
"This password encrypts your private key on this device. You will need it to send funds.",
|
|
||||||
xprv: "This password encrypts your key on this device. You will need it to send funds.",
|
|
||||||
};
|
|
||||||
|
|
||||||
function switchMode(mode) {
|
|
||||||
currentMode = mode;
|
|
||||||
for (const m of MODES) {
|
|
||||||
$("add-wallet-section-" + m).classList.toggle("hidden", m !== mode);
|
|
||||||
const tab = $("tab-" + m);
|
|
||||||
const isActive = m === mode;
|
|
||||||
// Active: bold, solid border on top/sides, no bottom border (connects to content)
|
|
||||||
tab.classList.toggle("font-bold", isActive);
|
|
||||||
tab.classList.toggle("border-solid", isActive);
|
|
||||||
tab.classList.toggle("border-border", isActive);
|
|
||||||
tab.classList.toggle("border-b-bg", isActive);
|
|
||||||
tab.classList.toggle("bg-bg", isActive);
|
|
||||||
// Inactive: muted text, dashed border on top/sides, transparent bottom, hover invert
|
|
||||||
tab.classList.toggle("text-muted", !isActive);
|
|
||||||
tab.classList.toggle("border-dashed", !isActive);
|
|
||||||
tab.classList.toggle("border-border-light", !isActive);
|
|
||||||
tab.classList.toggle("border-b-transparent", !isActive);
|
|
||||||
tab.classList.toggle("hover:bg-fg", !isActive);
|
|
||||||
tab.classList.toggle("hover:text-bg", !isActive);
|
|
||||||
}
|
|
||||||
$("add-wallet-password-hint").textContent = PASSWORD_HINTS[mode];
|
|
||||||
}
|
|
||||||
|
|
||||||
function show() {
|
function show() {
|
||||||
$("wallet-mnemonic").value = "";
|
$("wallet-mnemonic").value = "";
|
||||||
$("import-private-key").value = "";
|
|
||||||
$("import-xprv-key").value = "";
|
|
||||||
$("add-wallet-password").value = "";
|
$("add-wallet-password").value = "";
|
||||||
$("add-wallet-password-confirm").value = "";
|
$("add-wallet-password-confirm").value = "";
|
||||||
$("add-wallet-phrase-warning").classList.add("hidden");
|
$("add-wallet-phrase-warning").classList.add("hidden");
|
||||||
switchMode("mnemonic");
|
|
||||||
showView("add-wallet");
|
showView("add-wallet");
|
||||||
}
|
}
|
||||||
|
|
||||||
function validatePassword() {
|
|
||||||
const pw = $("add-wallet-password").value;
|
|
||||||
const pw2 = $("add-wallet-password-confirm").value;
|
|
||||||
if (!pw) {
|
|
||||||
showFlash("Please choose a password.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (pw.length < 12) {
|
|
||||||
showFlash("Password must be at least 12 characters.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (pw !== pw2) {
|
|
||||||
showFlash("Passwords do not match.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return pw;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function importMnemonic(ctx) {
|
|
||||||
const mnemonic = $("wallet-mnemonic").value.trim();
|
|
||||||
if (!mnemonic) {
|
|
||||||
showFlash("Enter a recovery phrase or press the die to generate one.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const words = mnemonic.split(/\s+/);
|
|
||||||
if (words.length !== 12 && words.length !== 24) {
|
|
||||||
showFlash(
|
|
||||||
"Recovery phrase must be 12 or 24 words. You entered " +
|
|
||||||
words.length +
|
|
||||||
".",
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!isValidMnemonic(mnemonic)) {
|
|
||||||
showFlash("Invalid recovery phrase. Check for typos.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const pw = validatePassword();
|
|
||||||
if (!pw) return;
|
|
||||||
const { xpub, firstAddress } = hdWalletFromMnemonic(mnemonic);
|
|
||||||
const addrDup = state.wallets.find((w) =>
|
|
||||||
w.addresses.some(
|
|
||||||
(a) => a.address.toLowerCase() === firstAddress.toLowerCase(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
if (addrDup) {
|
|
||||||
showFlash(
|
|
||||||
"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 = {
|
|
||||||
type: "hd",
|
|
||||||
name: "Wallet " + walletNum,
|
|
||||||
xpub: xpub,
|
|
||||||
encryptedSecret: encrypted,
|
|
||||||
nextIndex: 1,
|
|
||||||
addresses: [
|
|
||||||
{ address: firstAddress, balance: "0.0000", tokenBalances: [] },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
state.wallets.push(wallet);
|
|
||||||
state.hasWallet = true;
|
|
||||||
await saveState();
|
|
||||||
ctx.renderWalletList();
|
|
||||||
showView("main");
|
|
||||||
|
|
||||||
// Scan for used HD addresses beyond index 0.
|
|
||||||
showFlash("Scanning for addresses...", 30000);
|
|
||||||
const scan = await scanForAddresses(xpub, state.rpcUrl);
|
|
||||||
if (scan.addresses.length > 1) {
|
|
||||||
wallet.addresses = scan.addresses.map((a) => ({
|
|
||||||
address: a.address,
|
|
||||||
balance: "0.0000",
|
|
||||||
tokenBalances: [],
|
|
||||||
}));
|
|
||||||
wallet.nextIndex = scan.nextIndex;
|
|
||||||
await saveState();
|
|
||||||
ctx.renderWalletList();
|
|
||||||
showFlash("Found " + scan.addresses.length + " addresses.");
|
|
||||||
} else {
|
|
||||||
showFlash("Ready.", 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.doRefreshAndRender();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function importPrivateKey(ctx) {
|
|
||||||
const key = $("import-private-key").value.trim();
|
|
||||||
if (!key) {
|
|
||||||
showFlash("Please enter your private key.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let addr;
|
|
||||||
try {
|
|
||||||
addr = addressFromPrivateKey(key);
|
|
||||||
} catch (e) {
|
|
||||||
showFlash("Invalid private key.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const pw = validatePassword();
|
|
||||||
if (!pw) return;
|
|
||||||
const duplicate = state.wallets.find((w) =>
|
|
||||||
w.addresses.some((a) => a.address.toLowerCase() === addr.toLowerCase()),
|
|
||||||
);
|
|
||||||
if (duplicate) {
|
|
||||||
showFlash("This address already exists in " + duplicate.name + ".");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const encrypted = await encryptWithPassword(key, pw);
|
|
||||||
const walletNum = state.wallets.length + 1;
|
|
||||||
state.wallets.push({
|
|
||||||
type: "key",
|
|
||||||
name: "Wallet " + walletNum,
|
|
||||||
encryptedSecret: encrypted,
|
|
||||||
addresses: [{ address: addr, balance: "0.0000", tokenBalances: [] }],
|
|
||||||
});
|
|
||||||
state.hasWallet = true;
|
|
||||||
await saveState();
|
|
||||||
ctx.renderWalletList();
|
|
||||||
showView("main");
|
|
||||||
|
|
||||||
ctx.doRefreshAndRender();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function importXprvKey(ctx) {
|
|
||||||
const xprv = $("import-xprv-key").value.trim();
|
|
||||||
if (!xprv) {
|
|
||||||
showFlash("Please enter your extended private key.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!isValidXprv(xprv)) {
|
|
||||||
showFlash("Invalid extended private key.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let result;
|
|
||||||
try {
|
|
||||||
result = hdWalletFromXprv(xprv);
|
|
||||||
} catch (e) {
|
|
||||||
showFlash("Invalid extended private key.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const { xpub, firstAddress } = result;
|
|
||||||
const addrDup = state.wallets.find((w) =>
|
|
||||||
w.addresses.some(
|
|
||||||
(a) => a.address.toLowerCase() === firstAddress.toLowerCase(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
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();
|
|
||||||
if (!pw) return;
|
|
||||||
const encrypted = await encryptWithPassword(xprv, pw);
|
|
||||||
const walletNum = state.wallets.length + 1;
|
|
||||||
const wallet = {
|
|
||||||
type: "xprv",
|
|
||||||
name: "Wallet " + walletNum,
|
|
||||||
xpub: xpub,
|
|
||||||
encryptedSecret: encrypted,
|
|
||||||
nextIndex: 1,
|
|
||||||
addresses: [
|
|
||||||
{ address: firstAddress, balance: "0.0000", tokenBalances: [] },
|
|
||||||
],
|
|
||||||
};
|
|
||||||
state.wallets.push(wallet);
|
|
||||||
state.hasWallet = true;
|
|
||||||
await saveState();
|
|
||||||
ctx.renderWalletList();
|
|
||||||
showView("main");
|
|
||||||
|
|
||||||
// Scan for used HD addresses beyond index 0.
|
|
||||||
showFlash("Scanning for addresses...", 30000);
|
|
||||||
const scan = await scanForAddresses(xpub, state.rpcUrl);
|
|
||||||
if (scan.addresses.length > 1) {
|
|
||||||
wallet.addresses = scan.addresses.map((a) => ({
|
|
||||||
address: a.address,
|
|
||||||
balance: "0.0000",
|
|
||||||
tokenBalances: [],
|
|
||||||
}));
|
|
||||||
wallet.nextIndex = scan.nextIndex;
|
|
||||||
await saveState();
|
|
||||||
ctx.renderWalletList();
|
|
||||||
showFlash("Found " + scan.addresses.length + " addresses.");
|
|
||||||
} else {
|
|
||||||
showFlash("Ready.", 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx.doRefreshAndRender();
|
|
||||||
}
|
|
||||||
|
|
||||||
function init(ctx) {
|
function init(ctx) {
|
||||||
// Tab click handlers
|
|
||||||
$("tab-mnemonic").addEventListener("click", () => switchMode("mnemonic"));
|
|
||||||
$("tab-privkey").addEventListener("click", () => switchMode("privkey"));
|
|
||||||
$("tab-xprv").addEventListener("click", () => switchMode("xprv"));
|
|
||||||
|
|
||||||
// Generate mnemonic
|
|
||||||
$("btn-generate-phrase").addEventListener("click", () => {
|
$("btn-generate-phrase").addEventListener("click", () => {
|
||||||
$("wallet-mnemonic").value = generateMnemonic();
|
$("wallet-mnemonic").value = generateMnemonic();
|
||||||
$("add-wallet-phrase-warning").classList.remove("hidden");
|
$("add-wallet-phrase-warning").classList.remove("hidden");
|
||||||
});
|
});
|
||||||
|
|
||||||
// Import / confirm
|
|
||||||
$("btn-add-wallet-confirm").addEventListener("click", async () => {
|
$("btn-add-wallet-confirm").addEventListener("click", async () => {
|
||||||
if (currentMode === "mnemonic") {
|
const mnemonic = $("wallet-mnemonic").value.trim();
|
||||||
await importMnemonic(ctx);
|
if (!mnemonic) {
|
||||||
} else if (currentMode === "privkey") {
|
showFlash(
|
||||||
await importPrivateKey(ctx);
|
"Enter a recovery phrase or press the die to generate one.",
|
||||||
} else if (currentMode === "xprv") {
|
);
|
||||||
await importXprvKey(ctx);
|
return;
|
||||||
}
|
}
|
||||||
|
const words = mnemonic.split(/\s+/);
|
||||||
|
if (words.length !== 12 && words.length !== 24) {
|
||||||
|
showFlash(
|
||||||
|
"Recovery phrase must be 12 or 24 words. You entered " +
|
||||||
|
words.length +
|
||||||
|
".",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!isValidMnemonic(mnemonic)) {
|
||||||
|
showFlash("Invalid recovery phrase. Check for typos.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const pw = $("add-wallet-password").value;
|
||||||
|
const pw2 = $("add-wallet-password-confirm").value;
|
||||||
|
if (!pw) {
|
||||||
|
showFlash("Please choose a password.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pw.length < 12) {
|
||||||
|
showFlash("Password must be at least 12 characters.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pw !== pw2) {
|
||||||
|
showFlash("Passwords do not match.");
|
||||||
|
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(),
|
||||||
|
);
|
||||||
|
if (duplicate) {
|
||||||
|
showFlash(
|
||||||
|
"This recovery phrase is already added (" +
|
||||||
|
duplicate.name +
|
||||||
|
").",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const encrypted = await encryptWithPassword(mnemonic, pw);
|
||||||
|
const walletNum = state.wallets.length + 1;
|
||||||
|
const wallet = {
|
||||||
|
type: "hd",
|
||||||
|
name: "Wallet " + walletNum,
|
||||||
|
xpub: xpub,
|
||||||
|
encryptedSecret: encrypted,
|
||||||
|
nextIndex: 1,
|
||||||
|
addresses: [
|
||||||
|
{ address: firstAddress, balance: "0.0000", tokenBalances: [] },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
state.wallets.push(wallet);
|
||||||
|
state.hasWallet = true;
|
||||||
|
await saveState();
|
||||||
|
ctx.renderWalletList();
|
||||||
|
showView("main");
|
||||||
|
|
||||||
|
// Scan for used HD addresses beyond index 0.
|
||||||
|
showFlash("Scanning for addresses...", 30000);
|
||||||
|
const scan = await scanForAddresses(xpub, state.rpcUrl);
|
||||||
|
if (scan.addresses.length > 1) {
|
||||||
|
wallet.addresses = scan.addresses.map((a) => ({
|
||||||
|
address: a.address,
|
||||||
|
balance: "0.0000",
|
||||||
|
tokenBalances: [],
|
||||||
|
}));
|
||||||
|
wallet.nextIndex = scan.nextIndex;
|
||||||
|
await saveState();
|
||||||
|
ctx.renderWalletList();
|
||||||
|
showFlash("Found " + scan.addresses.length + " addresses.");
|
||||||
|
} else {
|
||||||
|
showFlash("Ready.", 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.doRefreshAndRender();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Back button
|
|
||||||
$("btn-add-wallet-back").addEventListener("click", () => {
|
$("btn-add-wallet-back").addEventListener("click", () => {
|
||||||
if (!state.hasWallet) {
|
if (!state.hasWallet) {
|
||||||
showView("welcome");
|
showView("welcome");
|
||||||
@@ -301,6 +119,11 @@ function init(ctx) {
|
|||||||
showView("main");
|
showView("main");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$("btn-add-wallet-import-key").addEventListener(
|
||||||
|
"click",
|
||||||
|
ctx.showImportKeyView,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { init, show };
|
module.exports = { init, show };
|
||||||
|
|||||||
@@ -243,11 +243,7 @@ function show(txInfo) {
|
|||||||
state.viewData = { pendingTx: txInfo };
|
state.viewData = { pendingTx: txInfo };
|
||||||
showView("confirm-tx");
|
showView("confirm-tx");
|
||||||
|
|
||||||
// Reset recipient warning to hidden (space always reserved, no layout shift)
|
|
||||||
$("confirm-recipient-warning").style.visibility = "hidden";
|
|
||||||
|
|
||||||
estimateGas(txInfo);
|
estimateGas(txInfo);
|
||||||
checkRecipientHistory(txInfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function estimateGas(txInfo) {
|
async function estimateGas(txInfo) {
|
||||||
@@ -290,26 +286,6 @@ async function estimateGas(txInfo) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkRecipientHistory(txInfo) {
|
|
||||||
const el = $("confirm-recipient-warning");
|
|
||||||
try {
|
|
||||||
const provider = getProvider(state.rpcUrl);
|
|
||||||
// Skip warning for contract addresses — they may legitimately
|
|
||||||
// have zero outgoing transactions (getTransactionCount returns
|
|
||||||
// the nonce, i.e. sent-tx count only).
|
|
||||||
const code = await provider.getCode(txInfo.to);
|
|
||||||
if (code && code !== "0x") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const txCount = await provider.getTransactionCount(txInfo.to);
|
|
||||||
if (txCount === 0) {
|
|
||||||
el.style.visibility = "visible";
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
log.errorf("recipient history check failed:", e.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function init(ctx) {
|
function init(ctx) {
|
||||||
$("btn-confirm-send").addEventListener("click", async () => {
|
$("btn-confirm-send").addEventListener("click", async () => {
|
||||||
const password = $("confirm-tx-password").value;
|
const password = $("confirm-tx-password").value;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const { state, saveState } = require("../../shared/state");
|
|||||||
const VIEWS = [
|
const VIEWS = [
|
||||||
"welcome",
|
"welcome",
|
||||||
"add-wallet",
|
"add-wallet",
|
||||||
|
"import-key",
|
||||||
"main",
|
"main",
|
||||||
"address",
|
"address",
|
||||||
"address-token",
|
"address-token",
|
||||||
|
|||||||
@@ -239,7 +239,7 @@ function render(ctx) {
|
|||||||
html += `<div>`;
|
html += `<div>`;
|
||||||
html += `<div class="flex justify-between items-center bg-section py-1 px-2" style="margin:0 -0.5rem">`;
|
html += `<div class="flex justify-between items-center bg-section py-1 px-2" style="margin:0 -0.5rem">`;
|
||||||
html += `<span class="font-bold cursor-pointer wallet-name underline decoration-dashed" data-wallet="${wi}">${wallet.name}</span>`;
|
html += `<span class="font-bold cursor-pointer wallet-name underline decoration-dashed" data-wallet="${wi}">${wallet.name}</span>`;
|
||||||
if (wallet.type === "hd" || wallet.type === "xprv") {
|
if (wallet.type === "hd") {
|
||||||
html += `<button class="btn-add-address border border-border px-1 hover:bg-fg hover:text-bg cursor-pointer text-xs" data-wallet="${wi}" title="Add another address to this wallet">+</button>`;
|
html += `<button class="btn-add-address border border-border px-1 hover:bg-fg hover:text-bg cursor-pointer text-xs" data-wallet="${wi}" title="Add another address to this wallet">+</button>`;
|
||||||
}
|
}
|
||||||
html += `</div>`;
|
html += `</div>`;
|
||||||
|
|||||||
69
src/popup/views/importKey.js
Normal file
69
src/popup/views/importKey.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
const { $, showView, showFlash } = require("./helpers");
|
||||||
|
const { addressFromPrivateKey } = require("../../shared/wallet");
|
||||||
|
const { encryptWithPassword } = require("../../shared/vault");
|
||||||
|
const { state, saveState } = require("../../shared/state");
|
||||||
|
|
||||||
|
function show() {
|
||||||
|
$("import-private-key").value = "";
|
||||||
|
$("import-key-password").value = "";
|
||||||
|
$("import-key-password-confirm").value = "";
|
||||||
|
showView("import-key");
|
||||||
|
}
|
||||||
|
|
||||||
|
function init(ctx) {
|
||||||
|
$("btn-import-key-confirm").addEventListener("click", async () => {
|
||||||
|
const key = $("import-private-key").value.trim();
|
||||||
|
if (!key) {
|
||||||
|
showFlash("Please enter your private key.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let addr;
|
||||||
|
try {
|
||||||
|
addr = addressFromPrivateKey(key);
|
||||||
|
} catch (e) {
|
||||||
|
showFlash("Invalid private key.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const pw = $("import-key-password").value;
|
||||||
|
const pw2 = $("import-key-password-confirm").value;
|
||||||
|
if (!pw) {
|
||||||
|
showFlash("Please choose a password.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pw.length < 12) {
|
||||||
|
showFlash("Password must be at least 12 characters.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pw !== pw2) {
|
||||||
|
showFlash("Passwords do not match.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const encrypted = await encryptWithPassword(key, pw);
|
||||||
|
const walletNum = state.wallets.length + 1;
|
||||||
|
state.wallets.push({
|
||||||
|
type: "key",
|
||||||
|
name: "Wallet " + walletNum,
|
||||||
|
encryptedSecret: encrypted,
|
||||||
|
addresses: [
|
||||||
|
{ address: addr, balance: "0.0000", tokenBalances: [] },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
state.hasWallet = true;
|
||||||
|
await saveState();
|
||||||
|
ctx.renderWalletList();
|
||||||
|
showView("main");
|
||||||
|
|
||||||
|
ctx.doRefreshAndRender();
|
||||||
|
});
|
||||||
|
|
||||||
|
$("btn-import-key-back").addEventListener("click", () => {
|
||||||
|
if (!state.hasWallet) {
|
||||||
|
showView("welcome");
|
||||||
|
} else {
|
||||||
|
ctx.renderWalletList();
|
||||||
|
showView("main");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { init, show };
|
||||||
@@ -158,9 +158,8 @@ function render() {
|
|||||||
loadCalldata(tx.hash, tx.to);
|
loadCalldata(tx.hash, tx.to);
|
||||||
}
|
}
|
||||||
|
|
||||||
const isoStr = isoDate(tx.timestamp);
|
$("tx-detail-time").textContent =
|
||||||
$("tx-detail-time").innerHTML =
|
isoDate(tx.timestamp) + " (" + timeAgo(tx.timestamp) + ")";
|
||||||
copyableHtml(isoStr) + " (" + escapeHtml(timeAgo(tx.timestamp)) + ")";
|
|
||||||
$("tx-detail-status").textContent = tx.isError ? "Failed" : "Success";
|
$("tx-detail-status").textContent = tx.isError ? "Failed" : "Success";
|
||||||
showView("transaction");
|
showView("transaction");
|
||||||
|
|
||||||
|
|||||||
@@ -24,26 +24,6 @@ function hdWalletFromMnemonic(mnemonic) {
|
|||||||
return { xpub, firstAddress };
|
return { xpub, firstAddress };
|
||||||
}
|
}
|
||||||
|
|
||||||
function hdWalletFromXprv(xprv) {
|
|
||||||
const root = HDNodeWallet.fromExtendedKey(xprv);
|
|
||||||
if (!root.privateKey) {
|
|
||||||
throw new Error("Not an extended private key (xprv).");
|
|
||||||
}
|
|
||||||
const node = root.derivePath("44'/60'/0'/0");
|
|
||||||
const xpub = node.neuter().extendedKey;
|
|
||||||
const firstAddress = node.deriveChild(0).address;
|
|
||||||
return { xpub, firstAddress };
|
|
||||||
}
|
|
||||||
|
|
||||||
function isValidXprv(key) {
|
|
||||||
try {
|
|
||||||
const node = HDNodeWallet.fromExtendedKey(key);
|
|
||||||
return !!node.privateKey;
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function addressFromPrivateKey(key) {
|
function addressFromPrivateKey(key) {
|
||||||
const w = new Wallet(key);
|
const w = new Wallet(key);
|
||||||
return w.address;
|
return w.address;
|
||||||
@@ -58,11 +38,6 @@ function getSignerForAddress(walletData, addrIndex, decryptedSecret) {
|
|||||||
);
|
);
|
||||||
return node.deriveChild(addrIndex);
|
return node.deriveChild(addrIndex);
|
||||||
}
|
}
|
||||||
if (walletData.type === "xprv") {
|
|
||||||
const root = HDNodeWallet.fromExtendedKey(decryptedSecret);
|
|
||||||
const node = root.derivePath("44'/60'/0'/0");
|
|
||||||
return node.deriveChild(addrIndex);
|
|
||||||
}
|
|
||||||
return new Wallet(decryptedSecret);
|
return new Wallet(decryptedSecret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,8 +49,6 @@ module.exports = {
|
|||||||
generateMnemonic,
|
generateMnemonic,
|
||||||
deriveAddressFromXpub,
|
deriveAddressFromXpub,
|
||||||
hdWalletFromMnemonic,
|
hdWalletFromMnemonic,
|
||||||
hdWalletFromXprv,
|
|
||||||
isValidXprv,
|
|
||||||
addressFromPrivateKey,
|
addressFromPrivateKey,
|
||||||
getSignerForAddress,
|
getSignerForAddress,
|
||||||
isValidMnemonic,
|
isValidMnemonic,
|
||||||
|
|||||||
Reference in New Issue
Block a user