From b7d1cdbad4e89af41057cfe3133df07e0f1f4ab5 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Mon, 13 May 2024 16:27:03 +0530 Subject: [PATCH 1/4] feat: unchain unlock call from enable screen (nostr) --- src/app/router/Prompt/Prompt.tsx | 20 +++++++++---------- src/app/screens/Unlock/index.tsx | 3 +++ .../background-script/actions/nostr/enable.ts | 16 ++++++++++++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/app/router/Prompt/Prompt.tsx b/src/app/router/Prompt/Prompt.tsx index 3e1880d9e8..2ec23f3d4c 100644 --- a/src/app/router/Prompt/Prompt.tsx +++ b/src/app/router/Prompt/Prompt.tsx @@ -60,6 +60,16 @@ function Prompt() { + + } + /> } > - - } - /> { + if (navState.action === "unlock") msg.reply({ unlocked: true }); navigate(from, { replace: true }); setLoading(false); }) diff --git a/src/extension/background-script/actions/nostr/enable.ts b/src/extension/background-script/actions/nostr/enable.ts index 316c908d06..64139c7e8a 100644 --- a/src/extension/background-script/actions/nostr/enable.ts +++ b/src/extension/background-script/actions/nostr/enable.ts @@ -25,8 +25,22 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { const enabledFor = new Set(allowance?.enabledFor); + if (!isUnlocked) { + try { + await utils.openPrompt<{ unlocked: boolean }>({ + args: {}, + origin: { internal: true }, + action: "unlock", + }); + } catch (e) { + console.error(e); + if (e instanceof Error) { + return { error: e.message }; + } + } + } + if ( - isUnlocked && allowance && allowance.enabled && account?.nostrPrivateKey && From 84979fe890c925977f9dec0c2092f284779b56f9 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Fri, 17 May 2024 11:09:48 +0530 Subject: [PATCH 2/4] feat: separate unlock from enable for other providers --- .../background-script/actions/alby/enable.ts | 17 ++++++++++++++++- .../background-script/actions/liquid/enable.ts | 16 +++++++++++++++- .../background-script/actions/webbtc/enable.ts | 16 +++++++++++++++- .../background-script/actions/webln/enable.ts | 17 ++++++++++++++++- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/extension/background-script/actions/alby/enable.ts b/src/extension/background-script/actions/alby/enable.ts index b47c7b26e1..6e50f93059 100644 --- a/src/extension/background-script/actions/alby/enable.ts +++ b/src/extension/background-script/actions/alby/enable.ts @@ -18,7 +18,22 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { const enabledFor = new Set(allowance?.enabledFor); - if (isUnlocked && allowance && allowance.enabled && enabledFor.has("alby")) { + if (!isUnlocked) { + try { + await utils.openPrompt<{ unlocked: boolean }>({ + args: {}, + origin: { internal: true }, + action: "unlock", + }); + } catch (e) { + console.error(e); + if (e instanceof Error) { + return { error: e.message }; + } + } + } + + if (allowance && allowance.enabled && enabledFor.has("alby")) { return { data: { enabled: true }, }; diff --git a/src/extension/background-script/actions/liquid/enable.ts b/src/extension/background-script/actions/liquid/enable.ts index 331ccc3319..2c243d1ced 100644 --- a/src/extension/background-script/actions/liquid/enable.ts +++ b/src/extension/background-script/actions/liquid/enable.ts @@ -19,8 +19,22 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { const enabledFor = new Set(allowance?.enabledFor); + if (!isUnlocked) { + try { + await utils.openPrompt<{ unlocked: boolean }>({ + args: {}, + origin: { internal: true }, + action: "unlock", + }); + } catch (e) { + console.error(e); + if (e instanceof Error) { + return { error: e.message }; + } + } + } + if ( - isUnlocked && allowance && allowance.enabled && account?.mnemonic && diff --git a/src/extension/background-script/actions/webbtc/enable.ts b/src/extension/background-script/actions/webbtc/enable.ts index 26f0d008be..564dd031d9 100644 --- a/src/extension/background-script/actions/webbtc/enable.ts +++ b/src/extension/background-script/actions/webbtc/enable.ts @@ -19,8 +19,22 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { const enabledFor = new Set(allowance?.enabledFor); + if (!isUnlocked) { + try { + await utils.openPrompt<{ unlocked: boolean }>({ + args: {}, + origin: { internal: true }, + action: "unlock", + }); + } catch (e) { + console.error(e); + if (e instanceof Error) { + return { error: e.message }; + } + } + } + if ( - isUnlocked && allowance && allowance.enabled && account?.mnemonic && diff --git a/src/extension/background-script/actions/webln/enable.ts b/src/extension/background-script/actions/webln/enable.ts index f1be38ee93..64fde6a9cc 100644 --- a/src/extension/background-script/actions/webln/enable.ts +++ b/src/extension/background-script/actions/webln/enable.ts @@ -18,7 +18,22 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { const enabledFor = new Set(allowance?.enabledFor); - if (isUnlocked && allowance && allowance.enabled && enabledFor.has("webln")) { + if (!isUnlocked) { + try { + await utils.openPrompt<{ unlocked: boolean }>({ + args: {}, + origin: { internal: true }, + action: "unlock", + }); + } catch (e) { + console.error(e); + if (e instanceof Error) { + return { error: e.message }; + } + } + } + + if (allowance && allowance.enabled && enabledFor.has("webln")) { return { data: { enabled: true }, }; From 87523ff8355d9e8646ac38d0def8354106190a54 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Wed, 22 May 2024 15:52:04 +0530 Subject: [PATCH 3/4] fix: add return --- src/app/screens/Unlock/index.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/app/screens/Unlock/index.tsx b/src/app/screens/Unlock/index.tsx index 597d4773ac..22e4ed8211 100644 --- a/src/app/screens/Unlock/index.tsx +++ b/src/app/screens/Unlock/index.tsx @@ -47,7 +47,10 @@ function Unlock() { setLoading(true); auth .unlock(password, () => { - if (navState.action === "unlock") msg.reply({ unlocked: true }); + if (navState.action === "unlock") { + msg.reply({ unlocked: true }); + return; + } navigate(from, { replace: true }); setLoading(false); }) From fa602a0ebacb07a2ffcb73d1da6118ca2f146871 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Thu, 23 May 2024 11:51:53 +0530 Subject: [PATCH 4/4] feat: add addtional unlock checks return custom error message object when error is not instance of Error --- src/extension/background-script/actions/alby/enable.ts | 10 +++++++--- .../background-script/actions/liquid/enable.ts | 9 +++++++-- .../background-script/actions/nostr/enable.ts | 10 +++++++--- .../background-script/actions/webbtc/enable.ts | 9 +++++++-- .../background-script/actions/webln/enable.ts | 10 +++++++--- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/extension/background-script/actions/alby/enable.ts b/src/extension/background-script/actions/alby/enable.ts index 6e50f93059..4aa6cd8c91 100644 --- a/src/extension/background-script/actions/alby/enable.ts +++ b/src/extension/background-script/actions/alby/enable.ts @@ -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 allowance = await db.allowances .where("host") .equalsIgnoreCase(host) @@ -20,20 +20,24 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { if (!isUnlocked) { try { - await utils.openPrompt<{ unlocked: boolean }>({ + 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 (allowance && allowance.enabled && enabledFor.has("alby")) { + if (isUnlocked && allowance && allowance.enabled && enabledFor.has("alby")) { return { data: { enabled: true }, }; diff --git a/src/extension/background-script/actions/liquid/enable.ts b/src/extension/background-script/actions/liquid/enable.ts index 2c243d1ced..5f3f37936b 100644 --- a/src/extension/background-script/actions/liquid/enable.ts +++ b/src/extension/background-script/actions/liquid/enable.ts @@ -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") @@ -21,20 +21,25 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { if (!isUnlocked) { try { - await utils.openPrompt<{ unlocked: boolean }>({ + 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 && account?.mnemonic && diff --git a/src/extension/background-script/actions/nostr/enable.ts b/src/extension/background-script/actions/nostr/enable.ts index 2f354fb150..6c3d0c1f70 100644 --- a/src/extension/background-script/actions/nostr/enable.ts +++ b/src/extension/background-script/actions/nostr/enable.ts @@ -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") @@ -27,20 +27,24 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { if (!isUnlocked) { try { - await utils.openPrompt<{ unlocked: boolean }>({ + 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 && account?.nostrPrivateKey && diff --git a/src/extension/background-script/actions/webbtc/enable.ts b/src/extension/background-script/actions/webbtc/enable.ts index 564dd031d9..dfc2221371 100644 --- a/src/extension/background-script/actions/webbtc/enable.ts +++ b/src/extension/background-script/actions/webbtc/enable.ts @@ -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") @@ -21,20 +21,25 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { if (!isUnlocked) { try { - await utils.openPrompt<{ unlocked: boolean }>({ + 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 && account?.mnemonic && diff --git a/src/extension/background-script/actions/webln/enable.ts b/src/extension/background-script/actions/webln/enable.ts index 64fde6a9cc..560b2ce9dd 100644 --- a/src/extension/background-script/actions/webln/enable.ts +++ b/src/extension/background-script/actions/webln/enable.ts @@ -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 allowance = await db.allowances .where("host") .equalsIgnoreCase(host) @@ -20,20 +20,24 @@ const enable = async (message: MessageAllowanceEnable, sender: Sender) => { if (!isUnlocked) { try { - await utils.openPrompt<{ unlocked: boolean }>({ + 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 (allowance && allowance.enabled && enabledFor.has("webln")) { + if (isUnlocked && allowance && allowance.enabled && enabledFor.has("webln")) { return { data: { enabled: true }, };