From 646b9ef83896ab9039ecde19a0eea2839f8535c6 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Mon, 8 Jan 2024 15:30:11 +0530 Subject: [PATCH 1/4] feat: default masterkey generation --- .../background-script/actions/accounts/add.ts | 26 ++++++++++++++++++- .../actions/mnemonic/generateMnemonic.ts | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/extension/background-script/actions/accounts/add.ts b/src/extension/background-script/actions/accounts/add.ts index 3276a78c7a..300b04956a 100644 --- a/src/extension/background-script/actions/accounts/add.ts +++ b/src/extension/background-script/actions/accounts/add.ts @@ -1,8 +1,13 @@ import { v4 as uuidv4 } from "uuid"; import { encryptData } from "~/common/lib/crypto"; +import { getUniqueAccountName } from "~/common/utils/validations"; +import edit from "~/extension/background-script/actions/accounts/edit"; +import { + generateMnemonic, + setMnemonic, +} from "~/extension/background-script/actions/mnemonic"; import state from "~/extension/background-script/state"; import type { MessageAccountAdd } from "~/types"; -import { getUniqueAccountName } from "~/common/utils/validations"; const add = async (message: MessageAccountAdd) => { const newAccount = message.args; @@ -24,12 +29,31 @@ const add = async (message: MessageAccountAdd) => { name, }; + const mnemonic = await generateMnemonic(); + setMnemonic({ + args: { + id: accountId, + mnemonic: mnemonic.data, + }, + action: "setMnemonic", + origin: { internal: true }, + }); + state.setState({ accounts: tmpAccounts }); if (!currentAccountId) { state.setState({ currentAccountId: accountId }); } + edit({ + args: { + id: accountId, + useMnemonicForLnurlAuth: true, + }, + action: "editAccount", + origin: { internal: true }, + }); + // make sure we immediately persist the new account await state.getState().saveToStorage(); return { data: { accountId: accountId } }; diff --git a/src/extension/background-script/actions/mnemonic/generateMnemonic.ts b/src/extension/background-script/actions/mnemonic/generateMnemonic.ts index 3be94808dc..b25d6867b1 100644 --- a/src/extension/background-script/actions/mnemonic/generateMnemonic.ts +++ b/src/extension/background-script/actions/mnemonic/generateMnemonic.ts @@ -2,7 +2,7 @@ import * as bip39 from "@scure/bip39"; import { wordlist } from "@scure/bip39/wordlists/english"; import type { MessageMnemonicGenerate } from "~/types"; -const generateMnemonic = async (message: MessageMnemonicGenerate) => { +const generateMnemonic = async (message?: MessageMnemonicGenerate) => { return { data: bip39.generateMnemonic(wordlist, 128), }; From 41742a579b0fc0bd1089370f58e89e9bc655f425 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Mon, 8 Jan 2024 15:35:48 +0530 Subject: [PATCH 2/4] chore: params in generate mnemonic --- src/extension/background-script/actions/accounts/add.ts | 5 ++++- .../background-script/actions/mnemonic/generateMnemonic.ts | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/extension/background-script/actions/accounts/add.ts b/src/extension/background-script/actions/accounts/add.ts index 300b04956a..df38e0d0ad 100644 --- a/src/extension/background-script/actions/accounts/add.ts +++ b/src/extension/background-script/actions/accounts/add.ts @@ -29,7 +29,10 @@ const add = async (message: MessageAccountAdd) => { name, }; - const mnemonic = await generateMnemonic(); + const mnemonic = await generateMnemonic({ + action: "generateMnemonic", + origin: { internal: true }, + }); setMnemonic({ args: { id: accountId, diff --git a/src/extension/background-script/actions/mnemonic/generateMnemonic.ts b/src/extension/background-script/actions/mnemonic/generateMnemonic.ts index b25d6867b1..3be94808dc 100644 --- a/src/extension/background-script/actions/mnemonic/generateMnemonic.ts +++ b/src/extension/background-script/actions/mnemonic/generateMnemonic.ts @@ -2,7 +2,7 @@ import * as bip39 from "@scure/bip39"; import { wordlist } from "@scure/bip39/wordlists/english"; import type { MessageMnemonicGenerate } from "~/types"; -const generateMnemonic = async (message?: MessageMnemonicGenerate) => { +const generateMnemonic = async (message: MessageMnemonicGenerate) => { return { data: bip39.generateMnemonic(wordlist, 128), }; From d2e15891169916f9c03d784475ce4bc2b279a7aa Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Tue, 16 Jan 2024 15:58:49 +0530 Subject: [PATCH 3/4] feat: generate mnemonic for existing account which don't have yet --- .../actions/accounts/unlock.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/extension/background-script/actions/accounts/unlock.ts b/src/extension/background-script/actions/accounts/unlock.ts index 16217f93c4..db1ee4fc93 100644 --- a/src/extension/background-script/actions/accounts/unlock.ts +++ b/src/extension/background-script/actions/accounts/unlock.ts @@ -1,4 +1,9 @@ import { decryptData } from "~/common/lib/crypto"; +import edit from "~/extension/background-script/actions/accounts/edit"; +import { + generateMnemonic, + setMnemonic, +} from "~/extension/background-script/actions/mnemonic"; import state from "~/extension/background-script/state"; import i18n from "~/i18n/i18nConfig"; import type { MessageAccountUnlock } from "~/types"; @@ -31,6 +36,32 @@ const unlock = async (message: MessageAccountUnlock) => { // if everything is fine we keep the password in memory await state.getState().password(password); + + // for existing alby accounts we can't write migiration to auto generate mnemonic because of unavailability of password field. + if (!account.mnemonic) { + const mnemonic = await generateMnemonic({ + action: "generateMnemonic", + origin: { internal: true }, + }); + + setMnemonic({ + args: { + id: account.id, + mnemonic: mnemonic.data, + }, + action: "setMnemonic", + origin: { internal: true }, + }); + + edit({ + args: { + id: account.id, + useMnemonicForLnurlAuth: true, + }, + action: "editAccount", + origin: { internal: true }, + }); + } // load the connector to make sure it is initialized for the future calls // with this we prevent potentially multiple action calls trying to initialize the connector in parallel // we have to be careful here: if the unlock fails (e.g. because of an error in getConnector() the user From f8f42bf61722d0dd467ebb381e94a8292825b0d4 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Tue, 23 Jan 2024 11:24:57 +0530 Subject: [PATCH 4/4] chore: not to generate master key for existing account --- .../actions/accounts/unlock.ts | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/src/extension/background-script/actions/accounts/unlock.ts b/src/extension/background-script/actions/accounts/unlock.ts index db1ee4fc93..595afd78d0 100644 --- a/src/extension/background-script/actions/accounts/unlock.ts +++ b/src/extension/background-script/actions/accounts/unlock.ts @@ -1,9 +1,4 @@ import { decryptData } from "~/common/lib/crypto"; -import edit from "~/extension/background-script/actions/accounts/edit"; -import { - generateMnemonic, - setMnemonic, -} from "~/extension/background-script/actions/mnemonic"; import state from "~/extension/background-script/state"; import i18n from "~/i18n/i18nConfig"; import type { MessageAccountUnlock } from "~/types"; @@ -37,31 +32,6 @@ const unlock = async (message: MessageAccountUnlock) => { // if everything is fine we keep the password in memory await state.getState().password(password); - // for existing alby accounts we can't write migiration to auto generate mnemonic because of unavailability of password field. - if (!account.mnemonic) { - const mnemonic = await generateMnemonic({ - action: "generateMnemonic", - origin: { internal: true }, - }); - - setMnemonic({ - args: { - id: account.id, - mnemonic: mnemonic.data, - }, - action: "setMnemonic", - origin: { internal: true }, - }); - - edit({ - args: { - id: account.id, - useMnemonicForLnurlAuth: true, - }, - action: "editAccount", - origin: { internal: true }, - }); - } // load the connector to make sure it is initialized for the future calls // with this we prevent potentially multiple action calls trying to initialize the connector in parallel // we have to be careful here: if the unlock fails (e.g. because of an error in getConnector() the user