Skip to content

Commit

Permalink
Mark primary account as default
Browse files Browse the repository at this point in the history
  • Loading branch information
ajinkyaraj-23 committed Feb 26, 2025
1 parent 4ce0ef0 commit 346d867
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const useGetSecretKeyHandler = () => {
allFormValues.current?.secretKey,
allFormValues.current?.secretKeyPassword
);
setupPersistence(secretKey);
await restoreFromSecretKey(secretKey, password, DEFAULT_ACCOUNT_LABEL);
setupPersistence(secretKey);
};
};

Expand All @@ -60,9 +60,7 @@ const useGetMnemonicHandler = () => {
isVerified: !isNewMnemonic,
});

// Initialize persistence with mnemonic
setupPersistence(mnemonic);

if (isNewMnemonic) {
dispatch(accountsActions.setPassword(password));
dispatch(accountsActions.setCurrent(accounts[0].address.pkh));
Expand All @@ -77,7 +75,6 @@ const useGetVerificationHandler = () => {

return async (password: string) => {
const mnemonic = await getDecryptedMnemonic(currentAccount as MnemonicAccount, password);
setupPersistence(mnemonic);
return openWith(<ImportantNoticeModal mnemonic={mnemonic} />, { size: "xl" });
};
};
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/utils/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getOrCreateUserNonce, initializePersistence, makeStore } from "@umami/state";
import { accountsActions, getOrCreateUserNonce, initializePersistence, makeStore} from "@umami/state";

import { persistor, setupPersistor } from "./persistor";

Expand All @@ -17,6 +17,7 @@ export const setupPersistence = (key: string) => {
}
const { persistor } = initializePersistence(store, nonce);
setupPersistor(persistor);
store.dispatch(accountsActions.setDefaultAccount());
} catch (error) {
console.error("Failed to initialize persistence:", error);
return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/state/src/beacon/WalletClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const logout = (persistor: Persistor) =>
const migrationCompleted = localStorage.getItem("migration_to_2_3_5_completed");

localStorage.clear(); // TODO: fix for react-native

sessionStorage.clear();
if (migrationCompleted) {
localStorage.setItem("migration_to_2_3_5_completed", "true");
}
Expand Down
14 changes: 8 additions & 6 deletions packages/state/src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ export const makePersistConfigs = (storage_: Storage | undefined, password?: str
migrate: createAsyncMigrate(accountsMigrations, { debug: false }),
blacklist: ["password"],
transforms: [
encryptTransform({
secretKey: password,
onError: error => {
console.error("Error encrypting accounts state:", error);
},
}),
encryptTransform(
{
secretKey: password,
onError: error => {
console.error("Error encrypting accounts state:", error);
},
}, { blacklist: ["defaultAccount"] }
),
],
};

Expand Down
1 change: 1 addition & 0 deletions packages/state/src/slices/accounts/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type AccountsState = {
seedPhrases: Record<string, EncryptedData | undefined>;
secretKeys: Record<RawPkh, EncryptedData | undefined>;
current?: RawPkh | undefined;
defaultAccount?: ImplicitAccount | undefined;
password?: string | undefined;
alerts: {
isSocialLoginWarningShown: boolean;
Expand Down
24 changes: 24 additions & 0 deletions packages/state/src/slices/accounts/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { remove } from "lodash";

import { type AccountsState } from "./State";
import { changeMnemonicPassword } from "../../thunks/changeMnemonicPassword";
import { IDP } from "@umami/social-auth";

export const accountsInitialState: AccountsState = {
items: [],
Expand Down Expand Up @@ -98,6 +99,9 @@ export const accountsSlice = createSlice({
);
if (accountToRename) {
accountToRename.label = newName;
if (state.defaultAccount?.address.pkh === accountToRename.address.pkh) {
state.defaultAccount.label = newName;
}
}
},
/**
Expand Down Expand Up @@ -137,6 +141,11 @@ export const accountsSlice = createSlice({
setCurrent: (state, { payload: address }: { payload: RawPkh | undefined }) => {
state.current = address;
},
setDefaultAccount: (state) => {
if (!state.defaultAccount) {
state.defaultAccount = hideConfidentialData(state.items[0]);
}
},
setIsVerified: (
state,
{ payload: { pkh, isVerified } }: { payload: { pkh: RawPkh; isVerified: boolean } }
Expand Down Expand Up @@ -176,4 +185,19 @@ const concatUnique = (existingAccounts: ImplicitAccount[], newAccounts: Implicit
return [...existingAccounts, ...newAccounts];
};

function hideConfidentialData(acct: ImplicitAccount): ImplicitAccount {
// create a new object from account
const account = ({ ...acct } as ImplicitAccount);
if (account.type === "social") {
if (account.label.includes("@")) {
account.label = account.label.slice(0, 2) + "..." + account.label.slice(account.label.indexOf("@"));
}
}
account.pk = "********";
if (account.type === "mnemonic") {
account.seedFingerPrint = "********";
}
return account;
}

export const accountsActions = accountsSlice.actions;
1 change: 1 addition & 0 deletions packages/state/src/thunks/secretKeyAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ export const restoreFromSecretKey =
password,
});
dispatch(accountsActions.addAccount(account));
dispatch(accountsActions.setDefaultAccount());
dispatch(accountsActions.addSecretKey({ pkh: account.address.pkh, encryptedSecretKey }));
};

0 comments on commit 346d867

Please sign in to comment.