diff --git a/src/app/screens/Accounts/Show/index.tsx b/src/app/screens/Accounts/Show/index.tsx index f08f8dbc7a..9ca86c7a27 100644 --- a/src/app/screens/Accounts/Show/index.tsx +++ b/src/app/screens/Accounts/Show/index.tsx @@ -80,6 +80,11 @@ function AccountScreen() { const response = await msg.request("getAccount", { id, }); + // for backwards compatibility + // TODO: remove. if you ask when, then it's probably now. + if (!response.id) { + response.id = id; + } setAccount(response); setAccountName(response.name); diff --git a/src/extension/background-script/migrations/__tests__/ensureAccountId.test.ts b/src/extension/background-script/migrations/__tests__/ensureAccountId.test.ts new file mode 100644 index 0000000000..0631ad0968 --- /dev/null +++ b/src/extension/background-script/migrations/__tests__/ensureAccountId.test.ts @@ -0,0 +1,42 @@ +import state from "~/extension/background-script/state"; + +import migrate from "../index"; + +afterEach(() => { + jest.clearAllMocks(); +}); + +jest.mock("~/extension/background-script/state"); +const mockState = { + saveToStorage: jest.fn, + accounts: { + "8b7f1dc6-ab87-4c6c-bca5-19fa8632731e": { + config: "config-123-456", + connector: "lndhub", + name: "Alby", + nostrPrivateKey: "nostr-123-456", + }, + "1e1e8ea6-493e-480b-9855-303d37506e97": { + config: "config-123-456", + connector: "lndhub", + id: "1e1e8ea6-493e-480b-9855-303d37506e97", + name: "Alby", + }, + }, +}; +state.getState = jest.fn().mockReturnValue(mockState); + +describe("Ensure account ID", () => { + test("add account ID where missing", async () => { + let accounts = state.getState().accounts; + expect(accounts["8b7f1dc6-ab87-4c6c-bca5-19fa8632731e"].id).toEqual( + undefined + ); + await migrate(); + + accounts = state.getState().accounts; + Object.keys(accounts).forEach((accountId) => { + expect(accounts[accountId].id).toEqual(accountId); + }); + }); +}); diff --git a/src/extension/background-script/migrations/index.ts b/src/extension/background-script/migrations/index.ts index f6ff8e56d2..2eb09b6f8c 100644 --- a/src/extension/background-script/migrations/index.ts +++ b/src/extension/background-script/migrations/index.ts @@ -36,8 +36,23 @@ const migrations = { state.setState({ accounts, }); + // will be persisted by setMigrated } }, + + ensureAccountId: async () => { + const { accounts } = state.getState(); + Object.keys(accounts).forEach((accountId) => { + if (!accounts[accountId].id) { + console.info(`updating ${accountId}`); + accounts[accountId].id = accountId; + } + }); + state.setState({ + accounts, + }); + // will be persisted by setMigrated + }, }; const migrate = async () => { @@ -48,6 +63,11 @@ const migrate = async () => { await migrations["migrateisUsingGlobalNostrKey"](); await setMigrated("migrateisUsingGlobalNostrKey"); } + if (shouldMigrate("ensureAccountId")) { + console.info("Running migration for: ensureAccountId"); + await migrations["ensureAccountId"](); + await setMigrated("ensureAccountId"); + } }; export default migrate;