mirror of
https://github.com/selfxyz/self.git
synced 2026-01-10 07:08:10 -05:00
SELF-883 Feat/generate second address (#1328)
* feat: generate second address * update setting store
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user