Skip to content

Commit

Permalink
Merge branch 'getAlby:master' into getAlby#2663
Browse files Browse the repository at this point in the history
  • Loading branch information
Rithvik-padma committed Sep 12, 2023
2 parents 6b9e853 + b2c8063 commit f35cf78
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/app/router/Prompt/Prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ function Prompt() {
<Route path="public/nostr/onboard" element={<Onboard />} />
<Route path="public/liquid/onboard" element={<Onboard />} />
<Route path="public/webbtc/onboard" element={<Onboard />} />
<Route path="public/webln/onboard" element={<Onboard />} />
<Route
path="public/confirmRequestPermission"
element={<ConfirmRequestPermission />}
Expand Down
40 changes: 13 additions & 27 deletions src/app/screens/Accounts/NostrSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import PasswordViewAdornment from "~/app/components/PasswordViewAdornment";
import toast from "~/app/components/Toast";
import TextField from "~/app/components/form/TextField";
import api, { GetAccountRes } from "~/common/lib/api";
import { default as nostr, default as nostrlib } from "~/common/lib/nostr";
import { default as nostr } from "~/common/lib/nostr";

function NostrSettings() {
const { t: tCommon } = useTranslation("common");
Expand All @@ -33,7 +33,8 @@ function NostrSettings() {
const priv = await api.nostr.getPrivateKey(id);
if (priv) {
setCurrentPrivateKey(priv);
setNostrPrivateKey(priv);
const nsec = nostr.hexToNip19(priv);
setNostrPrivateKey(nsec);
}
const accountResponse = await api.getAccount(id);
setHasMnemonic(accountResponse.hasMnemonic);
Expand Down Expand Up @@ -82,15 +83,11 @@ function NostrSettings() {
}

const derivedNostrPrivateKey = await api.nostr.generatePrivateKey(id);
setNostrPrivateKey(derivedNostrPrivateKey);
setNostrPrivateKey(nostr.hexToNip19(derivedNostrPrivateKey));
}

// TODO: simplify this method - would be good to have a dedicated "remove nostr key" button
async function handleSaveNostrPrivateKey() {
if (nostrPrivateKey === currentPrivateKey) {
throw new Error("private key hasn't changed");
}

if (
currentPrivateKey &&
prompt(
Expand Down Expand Up @@ -165,9 +162,7 @@ function NostrSettings() {
</Alert>
)}

{hasMnemonic &&
currentPrivateKey &&
nostrPrivateKey === currentPrivateKey ? (
{hasMnemonic && currentPrivateKey ? (
hasImportedNostrKey ? (
<Alert type="warn">
{t("nostr.settings.imported_key_warning")}
Expand Down Expand Up @@ -216,28 +211,19 @@ function NostrSettings() {
onClick={handleDeleteKeys}
/>
)}
{hasImportedNostrKey &&
nostrPrivateKey === currentPrivateKey &&
hasMnemonic && (
<Button
outline
label={t("nostr.settings.derive")}
onClick={handleDeriveNostrKeyFromSecretKey}
/>
)}
{hasImportedNostrKey && hasMnemonic && (
<Button
outline
label={t("nostr.settings.derive")}
onClick={handleDeriveNostrKeyFromSecretKey}
/>
)}
</div>
</div>
</ContentBox>
<div className="flex justify-center my-6 gap-4">
<Button label={tCommon("actions.cancel")} onClick={onCancel} />
<Button
type="submit"
label={tCommon("actions.save")}
disabled={
nostrlib.normalizeToHex(nostrPrivateKey) === currentPrivateKey
}
primary
/>
<Button type="submit" label={tCommon("actions.save")} primary />
</div>
</Container>
</form>
Expand Down
16 changes: 9 additions & 7 deletions src/extension/background-script/actions/accounts/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,22 @@ export default select;
// Send a notification message to the content script
// which will then be posted to the window so websites can sync with the switched account
async function notifyAccountChanged() {
try {
const tabs = await browser.tabs.query({});
// Send message to tabs with URLs starting with "http" or "https"
const tabs = await browser.tabs.query({});
// Send message to tabs with URLs starting with "http" or "https"
if (tabs) {
const validTabs = tabs.filter((tab) => {
const currentUrl = tab.url || "";
return currentUrl.startsWith("http") || currentUrl.startsWith("https");
});

for (const tab of validTabs) {
if (tab.id) {
await browser.tabs.sendMessage(tab.id, { action: "accountChanged" });
try {
if (tab.id) {
await browser.tabs.sendMessage(tab.id, { action: "accountChanged" });
}
} catch (error) {
console.error("Failed to notify account changed", error);
}
}
} catch (error) {
console.error("Failed to notify account changed", error);
}
}
1 change: 1 addition & 0 deletions src/extension/background-script/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const routes = {
addAccount: accounts.promptAdd,
},
webln: {
onboard: onboard.prompt,
enable: allowances.enable,
getInfo: ln.getInfo,
sendPaymentOrPrompt: webln.sendPaymentOrPrompt,
Expand Down
4 changes: 2 additions & 2 deletions src/extension/content-script/liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ async function init() {

// Overrides the enable action so the user can go through onboarding to setup their keys
if (!account || !account.hasMnemonic) {
const account = await api.getAccount();
account = await api.getAccount();
if (!account.hasMnemonic) {
messageWithOrigin.action = `public/liquid/onboard`;
messageWithOrigin.action = ev.data.action = `public/liquid/onboard`;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/extension/content-script/webbtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ async function init() {

// Overrides the enable action so the user can go through onboarding to setup their keys
if (!account || !account.hasMnemonic) {
const account = await api.getAccount();
account = await api.getAccount();
if (!account.hasMnemonic) {
messageWithOrigin.action = `public/webbtc/onboard`;
messageWithOrigin.action = ev.data.action = `public/webbtc/onboard`;
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/extension/content-script/webln.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import browser from "webextension-polyfill";

import api from "~/common/lib/api";
import extractLightningData from "./batteries";
import getOriginData from "./originData";
import shouldInject from "./shouldInject";
Expand All @@ -25,6 +26,7 @@ const disabledCalls = ["webln/enable"];

let isEnabled = false; // store if webln is enabled for this content page
let isRejected = false; // store if the webln enable call failed. if so we do not prompt again
let account;

async function init() {
const inject = await shouldInject();
Expand All @@ -49,7 +51,7 @@ async function init() {
// message listener to listen to inpage webln/webbtc calls
// those calls get passed on to the background script
// (the inpage script can not do that directly, but only the inpage script can make webln available to the page)
window.addEventListener("message", (ev) => {
window.addEventListener("message", async (ev) => {
// Only accept messages from the current window
if (
ev.source !== window ||
Expand Down Expand Up @@ -89,10 +91,22 @@ async function init() {
origin: getOriginData(),
};

// Overrides the enable action so the user can go through onboarding to setup their keys

// Overrides the enable action so the user can go through onboarding to setup their keys
if (!account || !account.hasMnemonic) {
account = await api.getAccount();
if (!account.hasMnemonic) {
messageWithOrigin.action = ev.data.action = `public/webln/onboard`;
}
}

const replyFunction = (response) => {
// if it is the enable call we store if webln is enabled for this content script
if (ev.data.action === "webln/enable") {
isEnabled = response.data?.enabled;
const enabledEvent = new Event("webln:enabled");
window.dispatchEvent(enabledEvent);
if (response.error) {
console.error(response.error);
console.info("Enable was rejected ignoring further webln calls");
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/002-walletFeatures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test.describe("Wallet features", () => {
await confirmPasswordInput.type("g3tal6y");

await (await findByText($document, "Change")).click();
await page.waitForSelector(".Toastify");
await findByText($document, "Passcode changed successfully");

await browser.close();
});
Expand Down

0 comments on commit f35cf78

Please sign in to comment.