Skip to content

Commit

Permalink
fix: end to end setup worked!
Browse files Browse the repository at this point in the history
  • Loading branch information
Kodylow committed Oct 7, 2024
1 parent 66409b4 commit 2e9cc0b
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 29 deletions.
4 changes: 3 additions & 1 deletion apps/router/src/api/GuardianApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ export class GuardianApi {
/*** Setup RPC methods ***/

public setPassword = async (password: string): Promise<void> => {
return this.call(SetupRpc.setPassword, password);
this.password = password;
return this.call(SetupRpc.setPassword);
};

public setConfigGenConnections = async (
Expand Down Expand Up @@ -379,6 +380,7 @@ export class GuardianApi {

return result;
} catch (error: unknown) {
console.error('error auth', this.password);
console.error(`error calling '${method}' on websocket rpc : `, error);
throw 'error' in (error as { error: JsonRpcError })
? (error as { error: JsonRpcError }).error
Expand Down
12 changes: 2 additions & 10 deletions apps/router/src/api/ServiceCheckApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ServiceCheckApi {
serviceType: 'guardian',
serviceName: 'Guardian',
status: result.server,
requiresPassword: result.server === 'awaiting_password',
requiresPassword: result.server !== 'awaiting_password',
};
} catch (error) {
if (error instanceof JsonRpcError && error.code === -32600) {
Expand All @@ -62,15 +62,7 @@ export class ServiceCheckApi {
websocket: JsonRpcWebsocket,
password: string | null
) {
try {
return await websocket.call('status', [{ auth: password, params: null }]);
} catch (error) {
if (error instanceof JsonRpcError && error.code === -32600 && password) {
// If authentication fails with a password, try without it
return await websocket.call('status', [{ auth: null, params: null }]);
}
throw error;
}
return await websocket.call('status', [{ auth: password, params: null }]);
}

private async checkGateway(
Expand Down
12 changes: 9 additions & 3 deletions apps/router/src/context/guardian/SetupContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ function useInitialState(id: string): SetupState {
.toString()
.padStart(4, '0');
let state: SetupState = {
id,
role: null,
progress: SetupProgress.Start,
myName: randomName,
configGenParams: null,
password: null,
password: '',
numPeers: 0,
peers: [],
tosConfig: { showTos: false, tos: undefined },
Expand Down Expand Up @@ -106,11 +107,12 @@ export interface SetupContextValue {

export const SetupContext = createContext<SetupContextValue>({
state: {
id: '',
role: null,
progress: SetupProgress.Start,
myName: '',
configGenParams: null,
password: null,
password: '',
numPeers: 0,
peers: [],
tosConfig: { showTos: false, tos: undefined },
Expand Down Expand Up @@ -140,7 +142,11 @@ export const SetupContextProvider: React.FC<SetupContextProviderProps> = ({
const initialState = useInitialState(guardianId);
const [state, dispatch] = useReducer(reducer, {
...initialState,
password: decryptedServicePassword || initialState.password,
id: guardianId,
password:
decryptedServicePassword !== null
? decryptedServicePassword
: initialState.password,
});

useHandleSetupServerStatus(initServerStatus, dispatch);
Expand Down
6 changes: 5 additions & 1 deletion apps/router/src/context/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
import { GatewayContext, GatewayContextValue } from './gateway/GatewayContext';
import { GatewayApi } from '../api/GatewayApi';
import { useUnlockedService } from '../hooks/useUnlockedService';
import { useAuthContext } from '../hooks/useAuthContext';

export function useAppContext(): AppContextValue {
return useContext(AppContext);
Expand Down Expand Up @@ -302,6 +303,8 @@ export const useHandleBackgroundGuardianSetupActions = (
toggleConsensusPolling: SetupContextValue['toggleConsensusPolling'];
} => {
const api = useGuardianApi();
const { id } = useGuardianContext();
const { storeGuardianPassword } = useAuthContext();
const { password, myName } = state;
const [isPollingConsensus, setIsPollingConsensus] = useState(false);

Expand Down Expand Up @@ -344,6 +347,7 @@ export const useHandleBackgroundGuardianSetupActions = (
useCallback(
async ({ password: newPassword, myName, configs }) => {
if (!password) {
storeGuardianPassword(id, newPassword);
await api.setPassword(newPassword);

dispatch({
Expand Down Expand Up @@ -385,7 +389,7 @@ export const useHandleBackgroundGuardianSetupActions = (
await fetchConsensusState();
}
},
[password, api, dispatch, fetchConsensusState]
[password, api, dispatch, fetchConsensusState, id, storeGuardianPassword]
);

const connectToHost = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { FiEye, FiEyeOff } from 'react-icons/fi';
interface BasicSettingsFormProps {
myName: string;
setMyName: (name: string) => void;
password: string | null;
password: string;
setPassword: (password: string) => void;
}

Expand All @@ -41,22 +41,28 @@ export const BasicSettingsForm: React.FC<BasicSettingsFormProps> = ({
isOpen={true}
>
<FormControl>
<FormLabel>{t('set-config.guardian-name')}</FormLabel>
<FormLabel htmlFor='guardian-name'>
{t('set-config.guardian-name')}
</FormLabel>
<Input
id='guardian-name'
value={myName}
onChange={(ev) => setMyName(ev.currentTarget.value)}
/>
<FormHelperText>{t('set-config.guardian-name-help')}</FormHelperText>
</FormControl>
<FormControl>
<FormLabel>{t('set-config.admin-password')}</FormLabel>
<FormLabel htmlFor='admin-password'>
{t('set-config.admin-password')}
</FormLabel>
{password === null ? (
<Button onClick={() => setPassword(generatePassword())} w='100%'>
{t('set-config.admin-password-generate')}
</Button>
) : (
<InputGroup>
<Input
id='admin-password'
type={showPassword ? 'text' : 'password'}
value={password}
placeholder='Password'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
useGuardianSetupApi,
useGuardianSetupContext,
} from '../../../../../context/hooks';
import { useAuthContext } from '../../../../../hooks/useAuthContext';
import { useUnlockedService } from '../../../../../hooks/useUnlockedService';

interface Props {
next: () => void;
Expand All @@ -42,19 +44,21 @@ export const SetConfiguration: React.FC<Props> = ({ next }: Props) => {
const api = useGuardianSetupApi();
const {
state: {
id,
role,
configGenParams,
myName: stateMyName,
password: statePassword,
numPeers: stateNumPeers,
},
submitConfiguration,
} = useGuardianSetupContext();
const { storeGuardianPassword } = useAuthContext();
const theme = useTheme();
const isHost = role === GuardianRole.Host;
const isSolo = role === GuardianRole.Solo;
const [myName, setMyName] = useState(stateMyName);
const [password, setPassword] = useState(statePassword);
const { decryptedServicePassword } = useUnlockedService(id, 'guardian');
const [password, setPassword] = useState(decryptedServicePassword);
const [hostServerUrl, setHostServerUrl] = useState('');
const [defaultParams, setDefaultParams] = useState<ConfigGenParams>();
const [federationName, setFederationName] = useState('');
Expand Down Expand Up @@ -115,14 +119,8 @@ export const SetConfiguration: React.FC<Props> = ({ next }: Props) => {
}
}, [configGenParams, api]);

// Update password when updated from state
useEffect(() => {
setPassword(statePassword);
}, [statePassword]);

const hostCriteria = [
myName,
password,
federationName,
isValidNumber(numPeers, 4),
isValidNumber(blockConfirmations, 0, 200),
Expand All @@ -132,7 +130,6 @@ export const SetConfiguration: React.FC<Props> = ({ next }: Props) => {

const soloCriteria = [
myName,
password,
federationName,
isValidNumber(blockConfirmations, 0, 200),
isValidMeta(metaFields),
Expand Down Expand Up @@ -161,6 +158,8 @@ export const SetConfiguration: React.FC<Props> = ({ next }: Props) => {

const submitConfig = async () => {
if (password === null) return;
// Update the password in the auth context and the guardian api
storeGuardianPassword(id, password);
setError(undefined);
try {
if (!defaultParams)
Expand Down Expand Up @@ -191,6 +190,7 @@ export const SetConfiguration: React.FC<Props> = ({ next }: Props) => {
if (isHost || isSolo) {
// Hosts set their own connection name
// - They should submit both their local and the consensus config gen params.
console.log('submitConfig', password);
await submitConfiguration({
myName,
password,
Expand All @@ -216,6 +216,7 @@ export const SetConfiguration: React.FC<Props> = ({ next }: Props) => {
},
});
}

next();
} catch (err) {
setError(formatApiErrorMessage(err));
Expand Down Expand Up @@ -254,7 +255,7 @@ export const SetConfiguration: React.FC<Props> = ({ next }: Props) => {
<BasicSettingsForm
myName={myName}
setMyName={setMyName}
password={password}
password={password ?? ''}
setPassword={setPassword}
/>
<Button
Expand Down
3 changes: 2 additions & 1 deletion apps/router/src/types/guardian.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ export interface tosConfigState {
}

export interface SetupState {
id: string;
role: GuardianRole | null;
progress: SetupProgress;
myName: string;
password: string | null;
password: string;
ourCurrentId: number | null;
configGenParams: ConfigGenParams | null;
numPeers: number;
Expand Down

0 comments on commit 2e9cc0b

Please sign in to comment.