feat: fixed processing the config message next is finishing the reg properly

we now fetch the display name correctly just need to refactor and test the error handling
pull/3056/head
William Grant 1 year ago
parent 6a2af3e088
commit 7baf6c5886

@ -18,7 +18,11 @@ import {
useProgress, useProgress,
useRecoveryPassword, useRecoveryPassword,
} from '../../../state/onboarding/selectors/registration'; } from '../../../state/onboarding/selectors/registration';
import { registerSingleDevice, signInByLinkingDevice } from '../../../util/accountManager'; import {
registerSingleDevice,
registrationDone,
signInByLinkingDevice,
} from '../../../util/accountManager';
import { setSignInByLinking, setSignWithRecoveryPhrase } from '../../../util/storage'; import { setSignInByLinking, setSignWithRecoveryPhrase } from '../../../util/storage';
import { Flex } from '../../basic/Flex'; import { Flex } from '../../basic/Flex';
import { SessionButton, SessionButtonColor } from '../../basic/SessionButton'; import { SessionButton, SessionButtonColor } from '../../basic/SessionButton';
@ -73,20 +77,22 @@ async function signInAndFetchDisplayName(
try { try {
await resetRegistration(); await resetRegistration();
await signInByLinkingDevice(recoveryPassword, 'english', loadingAnimationCallback); void signInByLinkingDevice(recoveryPassword, 'english', loadingAnimationCallback);
// TODO[epic-899] do we still need this? Should I change the polling timeout to ONBOARDING_RECOVERY_TIMEOUT? await PromiseUtils.waitForTask(() => {
await PromiseUtils.waitForTask(done => { window.Whisper.events.on(
window.Whisper.events.on('configurationMessageReceived', async (displayName: string) => { 'configurationMessageReceived',
window.log.debug( async (displayName: string, pubkey: string) => {
`WIP: [signInAndFetchDisplayName] waitForTask done with displayName: "${displayName}"` window.log.debug(
); `WIP: [signInAndFetchDisplayName] waitForTask done with displayName: "${displayName}"`
window.Whisper.events.off('configurationMessageReceived'); );
await setSignInByLinking(false); window.Whisper.events.off('configurationMessageReceived');
await setSignWithRecoveryPhrase(true); await setSignInByLinking(false);
done(displayName); await setSignWithRecoveryPhrase(true);
displayNameFromNetwork = displayName; displayNameFromNetwork = displayName;
}); await registrationDone(pubkey, displayName);
}
);
}, ONBOARDING_TIMES.RECOVERY_TIMEOUT); }, ONBOARDING_TIMES.RECOVERY_TIMEOUT);
if (!displayNameFromNetwork.length) { if (!displayNameFromNetwork.length) {
@ -100,8 +106,8 @@ async function signInAndFetchDisplayName(
window.log.debug( window.log.debug(
`WIP: [signInAndFetchDisplayName] we got a displayName from network: "${displayNameFromNetwork}"` `WIP: [signInAndFetchDisplayName] we got a displayName from network: "${displayNameFromNetwork}"`
); );
// Do not set the lastProfileUpdateTimestamp. // Do not set the lastProfileUpdateTimestamp.
// We expect to get a display name from a configuration message while we are loading messages of this user
return displayNameFromNetwork; return displayNameFromNetwork;
} }

@ -397,30 +397,38 @@ export class SwarmPolling {
); );
if (returnAndKeepInMemory) { if (returnAndKeepInMemory) {
let displayName = '';
try { try {
const ourKeyPair = await UserUtils.getIdentityKeyPair(); const keypair = await UserUtils.getUserED25519KeyPairBytes();
if (!ourKeyPair) { if (!keypair || !keypair.privKeyBytes) {
throw new Error('ourKeyPair not found'); throw new Error('edkeypair not found for current user');
} }
const privateKeyEd25519 = keypair.privKeyBytes;
// we take the lastest config message to create the wrapper in memory // we take the lastest config message to create the wrapper in memory
const configMessage = allDecryptedConfigMessages.at(-1)?.message; const incomingConfigMessage = allDecryptedConfigMessages.at(-1);
window.log.debug(`WIP: [SwarmPolling] configMessage: ${JSON.stringify(configMessage)}`);
await GenericWrapperActions.init(
'UserConfig',
new Uint8Array(ourKeyPair.privKey),
configMessage?.data || null
);
window.log.debug( window.log.debug(
`WIP: [SwarmPolling] dump: ${StringUtils.toHex(await GenericWrapperActions.dump('UserConfig'))}` `WIP: [SwarmPolling] configMessage: ${JSON.stringify(incomingConfigMessage)}`
); );
// TODO[epic=899] this is still not working if (!incomingConfigMessage) {
throw new Error('incomingConfigMessage not found');
}
await GenericWrapperActions.init('UserConfig', privateKeyEd25519, null);
await GenericWrapperActions.merge('UserConfig', [
{
data: incomingConfigMessage.message.data,
hash: incomingConfigMessage.messageHash,
},
]);
const userInfo = await UserConfigWrapperActions.getUserInfo(); const userInfo = await UserConfigWrapperActions.getUserInfo();
window.log.debug(`WIP: [SwarmPolling] userInfo: ${JSON.stringify(userInfo)}`); window.log.debug(`WIP: [SwarmPolling] userInfo: ${JSON.stringify(userInfo)}`);
if (!userInfo) { if (!userInfo) {
throw new Error('UserInfo not found'); throw new Error('UserInfo not found');
} }
return userInfo.name; displayName = userInfo.name;
} catch (e) { } catch (e) {
window.log.warn( window.log.warn(
'[SwarmPolling] LibSessionUtil.initializeLibSessionUtilWrappers failed with', '[SwarmPolling] LibSessionUtil.initializeLibSessionUtilWrappers failed with',
@ -429,6 +437,8 @@ export class SwarmPolling {
} finally { } finally {
await GenericWrapperActions.free('UserConfig'); await GenericWrapperActions.free('UserConfig');
} }
return displayName;
} }
await ConfigMessageHandler.handleConfigMessagesViaLibSession(allDecryptedConfigMessages); await ConfigMessageHandler.handleConfigMessagesViaLibSession(allDecryptedConfigMessages);
@ -650,7 +660,7 @@ export class SwarmPolling {
/** /**
* Only exposed as public for testing * Only exposed as public for testing
*/ */
public async pollOnceForDisplayName(pubkey: PubKey) { public async pollOnceForDisplayName(pubkey: PubKey): Promise<string> {
const polledPubkey = pubkey.key; const polledPubkey = pubkey.key;
const swarmSnodes = await snodePool.getSwarmFor(polledPubkey); const swarmSnodes = await snodePool.getSwarmFor(polledPubkey);
@ -704,9 +714,6 @@ export class SwarmPolling {
); );
try { try {
const displayName = await this.handleSharedConfigMessages(userConfigMessagesMerged, true); const displayName = await this.handleSharedConfigMessages(userConfigMessagesMerged, true);
window.log.debug(
`WIP: [pollForOurDisplayName] displayName ${JSON.stringify(displayName)}`
);
return displayName; return displayName;
} catch (e) { } catch (e) {
window.log.warn( window.log.warn(
@ -730,6 +737,7 @@ export class SwarmPolling {
try { try {
const displayName = await this.pollOnceForDisplayName(UserUtils.getOurPubKeyFromCache()); const displayName = await this.pollOnceForDisplayName(UserUtils.getOurPubKeyFromCache());
window.log.debug(`WIP: [pollForOurDisplayName] displayName ${displayName}`);
return displayName; return displayName;
} catch (e) { } catch (e) {
window?.log?.warn('pollForOurDisplayName exception: ', e); window?.log?.warn('pollForOurDisplayName exception: ', e);

@ -2,7 +2,7 @@ import { getConversationController } from '../session/conversations';
import { getSodiumRenderer } from '../session/crypto'; import { getSodiumRenderer } from '../session/crypto';
import { fromArrayBufferToBase64, fromHex, toHex } from '../session/utils/String'; import { fromArrayBufferToBase64, fromHex, toHex } from '../session/utils/String';
import { getOurPubKeyStrFromCache } from '../session/utils/User'; import { getOurPubKeyStrFromCache } from '../session/utils/User';
import { trigger } from '../shims/events'; import { configurationMessageReceived, trigger } from '../shims/events';
import { SettingsKey } from '../data/settings-key'; import { SettingsKey } from '../data/settings-key';
import { ConversationTypeEnum } from '../models/conversationAttributes'; import { ConversationTypeEnum } from '../models/conversationAttributes';
@ -79,6 +79,8 @@ export async function signInWithRecovery(
* @param mnemonic the mnemonic the user duly saved in a safe place. We will restore his sessionID based on this. * @param mnemonic the mnemonic the user duly saved in a safe place. We will restore his sessionID based on this.
* @param mnemonicLanguage 'english' only is supported * @param mnemonicLanguage 'english' only is supported
* @param loadingAnimationCallback a callback to trigger a loading animation * @param loadingAnimationCallback a callback to trigger a loading animation
*
* @returns the display name of the user if found on the network
*/ */
export async function signInByLinkingDevice( export async function signInByLinkingDevice(
mnemonic: string, mnemonic: string,
@ -100,9 +102,7 @@ export async function signInByLinkingDevice(
const pubKeyString = toHex(identityKeyPair.pubKey); const pubKeyString = toHex(identityKeyPair.pubKey);
// fetch configuration message to get the user's display name. // fetch configuration message to get the user's display name.
const displayName = await getSwarmPollingInstance().pollForOurDisplayName(); const displayName = await getSwarmPollingInstance().pollForOurDisplayName();
trigger(configurationMessageReceived, displayName, pubKeyString);
await registrationDone(pubKeyString, displayName);
return pubKeyString;
} }
/** /**
* This signs up a new user account. User has no recovery and does not try to link a device * This signs up a new user account. User has no recovery and does not try to link a device
@ -189,7 +189,7 @@ async function createAccount(identityKeyPair: SessionKeyPair) {
* @param ourPubkey the pubkey recovered from the seed * @param ourPubkey the pubkey recovered from the seed
* @param displayName the display name entered by the user, if any. This is not a display name found from a config message in the network. * @param displayName the display name entered by the user, if any. This is not a display name found from a config message in the network.
*/ */
async function registrationDone(ourPubkey: string, displayName: string) { export async function registrationDone(ourPubkey: string, displayName: string) {
window?.log?.info(`registration done with user provided displayName "${displayName}"`); window?.log?.info(`registration done with user provided displayName "${displayName}"`);
// initializeLibSessionUtilWrappers needs our publicKey to be set // initializeLibSessionUtilWrappers needs our publicKey to be set

Loading…
Cancel
Save