SELF-883 Feat/generate second address (#1328)

* feat: generate second address

* update setting store
This commit is contained in:
Seshanth.S
2025-10-29 22:59:23 +05:30
committed by GitHub
parent 553b82b51b
commit bfa276665b
2 changed files with 45 additions and 0 deletions

View File

@@ -319,6 +319,45 @@ export const AuthProvider = ({
return <AuthContext.Provider value={state}>{children}</AuthContext.Provider>;
};
function _generateAddressFromMnemonic(mnemonic: string, index: number): string {
const wallet = ethers.HDNodeWallet.fromPhrase(
mnemonic,
undefined,
`m/44'/60'/0'/0/${index}`,
);
return wallet.address;
}
/**
* Gets the second address if it exists, or generates and stores it if not.
* By Generate, it means we need the user's biometric auth.
*
* Flow is, when the user visits the points screen for the first time, we need to generate the points address.
*/
export async function getOrGeneratePointsAddress(): Promise<string> {
const pointsAddress = useSettingStore.getState().pointsAddress;
if (pointsAddress) {
return pointsAddress;
}
const mnemonicData = await _getSecurely<Mnemonic>(
keychainOptions => loadOrCreateMnemonic(keychainOptions),
str => JSON.parse(str),
{ requireAuth: true },
);
if (!mnemonicData?.data?.phrase) {
throw new Error(
'Failed to retrieve mnemonic for points address generation',
);
}
const pointsAddr = _generateAddressFromMnemonic(mnemonicData.data.phrase, 1);
useSettingStore.getState().setPointsAddress(pointsAddr);
return pointsAddr;
}
export async function hasSecretStored() {
const seed = await Keychain.getGenericPassword({ service: SERVICE_NAME });
return !!seed;

View File

@@ -28,6 +28,8 @@ interface PersistedSettingsState {
setSubscribedTopics: (topics: string[]) => void;
addSubscribedTopic: (topic: string) => void;
removeSubscribedTopic: (topic: string) => void;
pointsAddress: string | null;
setPointsAddress: (address: string | null) => void;
}
interface NonPersistedSettingsState {
@@ -96,6 +98,10 @@ export const useSettingStore = create<SettingsState>()(
subscribedTopics: state.subscribedTopics.filter(t => t !== topic),
})),
pointsAddress: null,
setPointsAddress: (address: string | null) =>
set({ pointsAddress: address }),
// Non-persisted state (will not be saved to storage)
hideNetworkModal: false,
setHideNetworkModal: (hideNetworkModal: boolean) => {