import { AuthMethod, ReducerStateBackup } from "anastasis-core"; import { ComponentChildren, Fragment, h, VNode } from "preact"; import { useState } from "preact/hooks"; import { useAnastasisContext } from "../../context/anastasis"; import { authMethods, AuthMethodSetupProps, AuthMethodWithRemove, KnownAuthMethods, } from "./authMethod"; import { AnastasisClientFrame } from "./index"; const getKeys = Object.keys as (obj: T) => Array; export function AuthenticationEditorScreen(): VNode { const [noProvidersAck, setNoProvidersAck] = useState(false); const [selectedMethod, setSelectedMethod] = useState< KnownAuthMethods | undefined >(undefined); const [tooFewAuths, setTooFewAuths] = useState(false); // const [addingProvider, setAddingProvider] = useState(undefined) const reducer = useAnastasisContext(); if (!reducer) { return
no reducer in context
; } if ( !reducer.currentReducerState || reducer.currentReducerState.backup_state === undefined ) { return
invalid state
; } const configuredAuthMethods: AuthMethod[] = reducer.currentReducerState.authentication_methods ?? []; const haveMethodsConfigured = configuredAuthMethods.length > 0; function removeByIndex(index: number): void { if (reducer) reducer.transition("delete_authentication", { authentication_method: index, }); } const camByType: { [s: string]: AuthMethodWithRemove[] } = {}; for (let index = 0; index < configuredAuthMethods.length; index++) { const cam = { ...configuredAuthMethods[index], remove: () => removeByIndex(index), }; const prevValue = camByType[cam.type] || []; prevValue.push(cam); camByType[cam.type] = prevValue; } const providers = reducer.currentReducerState.authentication_providers!; const authAvailableSet = new Set(); for (const provKey of Object.keys(providers)) { const p = providers[provKey]; if ("http_status" in p && !("error_code" in p) && p.methods) { for (const meth of p.methods) { authAvailableSet.add(meth.type); } } } if (selectedMethod) { const cancel = (): void => setSelectedMethod(undefined); const addMethod = (args: any): void => { reducer.transition("add_authentication", args); setSelectedMethod(undefined); }; const AuthSetup = authMethods[selectedMethod].setup ?? AuthMethodNotImplemented; return ( {!authAvailableSet.has(selectedMethod) && ( { null; }} >

We have found no Anastasis providers that support this authentication method. You can add a provider manually. To add a provider you must know the provider URL (e.g. https://provider.com)

Learn more about Anastasis providers

)}
); } function MethodButton(props: { method: KnownAuthMethods }): VNode { if (authMethods[props.method].skip) return
; return (
); } const errors = !haveMethodsConfigured ? "There is not enough authentication methods." : undefined; const handleNext = async () => { const st = reducer.currentReducerState as ReducerStateBackup; if ((st.authentication_methods ?? []).length <= 2) { setTooFewAuths(true); } else { await reducer.transition("next", {}); } }; return (
{getKeys(authMethods).map((method) => ( ))}
{tooFewAuths ? ( setTooFewAuths(false)} description="Too few auth methods configured" label="Proceed anyway" onConfirm={() => reducer.transition("next", {})} > You have selected fewer than three authentication methods. We recommend that you add at least three. ) : null} {authAvailableSet.size === 0 && ( setNoProvidersAck(true)} description="No providers founds" label="Add a provider manually" onConfirm={() => { null; }} >

We have found no Anastasis providers for your chosen country / currency. You can add a providers manually. To add a provider you must know the provider URL (e.g. https://provider.com)

Learn more about Anastasis providers

)}

When recovering your wallet, you will be asked to verify your identity via the methods you configure here. The list of authentication method is defined by the backup provider list.

{authAvailableSet.size > 0 && (

We couldn't find provider for some of the authentication methods.

)}
); } function AuthMethodNotImplemented(props: AuthMethodSetupProps): VNode { return (

This auth method is not implemented yet, please choose another one.

); } function ConfirmModal({ active, description, onCancel, onConfirm, children, danger, disabled, label = "Confirm", }: ConfirmModelProps): VNode { return (
); } interface ConfirmModelProps { active?: boolean; description?: string; onCancel?: () => void; onConfirm?: () => void; label?: string; children?: ComponentChildren; danger?: boolean; disabled?: boolean; }