Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unchain unlock call from enable screen #3143

Merged
merged 7 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/app/router/Prompt/Prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ function Prompt() {
<Providers>
<HashRouter>
<Routes>
<Route
index
element={
<Navigate
to={`/${navigationState.action}`}
replace
state={navigationState}
/>
}
/>
<Route
path="/"
element={
Expand All @@ -68,16 +78,6 @@ function Prompt() {
</RequireAuth>
}
>
<Route
index
element={
<Navigate
to={`/${navigationState.action}`}
replace
state={navigationState}
/>
}
/>
<Route
path="public/alby/enable"
element={
Expand Down
6 changes: 6 additions & 0 deletions src/app/screens/Unlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useLocation, useNavigate } from "react-router-dom";
import Hyperlink from "~/app/components/Hyperlink";
import PasswordViewAdornment from "~/app/components/PasswordViewAdornment";
import { useAccount } from "~/app/context/AccountContext";
import { useNavigationState } from "~/app/hooks/useNavigationState";
import msg from "~/common/lib/msg";
import utils from "~/common/lib/utils";

Expand All @@ -17,6 +18,7 @@ function Unlock() {
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const navigate = useNavigate();
const navState = useNavigationState();
const location = useLocation() as {
state: { from?: { pathname?: string } };
};
Expand Down Expand Up @@ -45,6 +47,10 @@ function Unlock() {
setLoading(true);
auth
.unlock(password, () => {
if (navState.action === "unlock") {
msg.reply({ unlocked: true });
return;
}
navigate(from, { replace: true });
setLoading(false);
})
Expand Down
21 changes: 20 additions & 1 deletion src/extension/background-script/actions/alby/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,33 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
let isUnlocked = await state.getState().isUnlocked();
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();

const enabledFor = new Set(allowance?.enabledFor);

if (!isUnlocked) {
try {
const response = await utils.openPrompt<{ unlocked: boolean }>({
args: {},
origin: { internal: true },
action: "unlock",
});

isUnlocked = response.data.unlocked;
} catch (e) {
console.error(e);
if (e instanceof Error) {
return { error: e.message };
} else {
return { error: "Failed to unlock" };
}
}
}

if (isUnlocked && allowance && allowance.enabled && enabledFor.has("alby")) {
return {
data: { enabled: true },
Expand Down
21 changes: 20 additions & 1 deletion src/extension/background-script/actions/liquid/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
let isUnlocked = await state.getState().isUnlocked();
const account = await state.getState().getAccount();
const allowance = await db.allowances
.where("host")
Expand All @@ -19,6 +19,25 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => {

const enabledFor = new Set(allowance?.enabledFor);

if (!isUnlocked) {
try {
const response = await utils.openPrompt<{ unlocked: boolean }>({
args: {},
origin: { internal: true },
action: "unlock",
});

isUnlocked = response.data.unlocked;
} catch (e) {
console.error(e);
if (e instanceof Error) {
return { error: e.message };
} else {
return { error: "Failed to unlock" };
}
}
}

if (
isUnlocked &&
allowance &&
Expand Down
20 changes: 19 additions & 1 deletion src/extension/background-script/actions/nostr/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
let isUnlocked = await state.getState().isUnlocked();
const account = await state.getState().getAccount();
const allowance = await db.allowances
.where("host")
Expand All @@ -25,6 +25,24 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => {

const enabledFor = new Set(allowance?.enabledFor);

if (!isUnlocked) {
try {
const response = await utils.openPrompt<{ unlocked: boolean }>({
args: {},
origin: { internal: true },
action: "unlock",
});

isUnlocked = response.data.unlocked;
} catch (e) {
if (e instanceof Error) {
return { error: e.message };
} else {
return { error: "Failed to unlock" };
}
}
}

if (
isUnlocked &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we still need the unlock check? because if your new check fails for whatever reason then it's not unlocked and this action should not run.

allowance &&
Expand Down
21 changes: 20 additions & 1 deletion src/extension/background-script/actions/webbtc/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
let isUnlocked = await state.getState().isUnlocked();
const account = await state.getState().getAccount();
const allowance = await db.allowances
.where("host")
Expand All @@ -19,6 +19,25 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => {

const enabledFor = new Set(allowance?.enabledFor);

if (!isUnlocked) {
try {
const response = await utils.openPrompt<{ unlocked: boolean }>({
args: {},
origin: { internal: true },
action: "unlock",
});

isUnlocked = response.data.unlocked;
} catch (e) {
console.error(e);
if (e instanceof Error) {
return { error: e.message };
} else {
return { error: "Failed to unlock" };
}
}
}

if (
isUnlocked &&
allowance &&
Expand Down
21 changes: 20 additions & 1 deletion src/extension/background-script/actions/webln/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,33 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => {
const host = getHostFromSender(sender);
if (!host) return;

const isUnlocked = await state.getState().isUnlocked();
let isUnlocked = await state.getState().isUnlocked();
const allowance = await db.allowances
.where("host")
.equalsIgnoreCase(host)
.first();

const enabledFor = new Set(allowance?.enabledFor);

if (!isUnlocked) {
try {
const response = await utils.openPrompt<{ unlocked: boolean }>({
args: {},
origin: { internal: true },
action: "unlock",
});

isUnlocked = response.data.unlocked;
} catch (e) {
console.error(e);
if (e instanceof Error) {
return { error: e.message };
} else {
return { error: "Failed to unlock" };
}
}
}

if (isUnlocked && allowance && allowance.enabled && enabledFor.has("webln")) {
return {
data: { enabled: true },
Expand Down
Loading