-
Notifications
You must be signed in to change notification settings - Fork 28
/
siwe-panel.react.js
104 lines (97 loc) · 2.95 KB
/
siwe-panel.react.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// @flow
import * as React from 'react';
import Animated from 'react-native-reanimated';
import WebView from 'react-native-webview';
import { siweActionTypes, siwe } from 'lib/actions/user-actions';
import type {
SIWEServerCall,
SIWEResult,
LogInExtraInfo,
LogInStartingPayload,
} from 'lib/types/account-types';
import {
useServerCall,
useDispatchActionPromise,
type DispatchActionPromise,
} from 'lib/utils/action-utils';
import { commCoreModule } from '../native-modules';
import { NavContext } from '../navigation/navigation-context';
import { useSelector } from '../redux/redux-utils';
import { nativeLogInExtraInfoSelector } from '../selectors/account-selectors';
import { defaultLandingURLPrefix } from '../utils/url-utils';
const commSIWE = `${defaultLandingURLPrefix}/siwe`;
type BaseProps = {
+setActiveAlert: (activeAlert: boolean) => void,
+opacityValue: Animated.Node,
};
type Props = {
...BaseProps,
// Redux state
+logInExtraInfo: () => LogInExtraInfo,
// Redux dispatch functions
+dispatchActionPromise: DispatchActionPromise,
// async functions that hit server APIs
+siweAction: (siweInfo: SIWEServerCall) => Promise<SIWEResult>,
};
function SIWEPanel({
logInExtraInfo,
dispatchActionPromise,
siweAction,
}: Props) {
const handleSIWE = React.useCallback(
async ({ address, message, signature }) => {
const extraInfo = logInExtraInfo();
await commCoreModule.initializeCryptoAccount(address);
const { ed25519 } = await commCoreModule.getUserPublicKey();
dispatchActionPromise(
siweActionTypes,
siweAction({
address,
message,
signature,
publicKey: ed25519,
...extraInfo,
}),
undefined,
({ calendarQuery: extraInfo.calendarQuery }: LogInStartingPayload),
);
},
[logInExtraInfo, dispatchActionPromise, siweAction],
);
const handleMessage = React.useCallback(
event => {
const {
nativeEvent: { data },
} = event;
const { address, message, signature } = JSON.parse(data);
if (address && signature) {
handleSIWE({ address, message, signature });
}
},
[handleSIWE],
);
const source = React.useMemo(() => ({ uri: commSIWE }), []);
return <WebView source={source} onMessage={handleMessage} />;
}
const ConnectedSIWEPanel: React.ComponentType<BaseProps> = React.memo<BaseProps>(
function ConnectedRegisterPanel(props: BaseProps) {
const navContext = React.useContext(NavContext);
const logInExtraInfo = useSelector(state =>
nativeLogInExtraInfoSelector({
redux: state,
navContext,
}),
);
const dispatchActionPromise = useDispatchActionPromise();
const callSiwe = useServerCall(siwe);
return (
<SIWEPanel
{...props}
logInExtraInfo={logInExtraInfo}
dispatchActionPromise={dispatchActionPromise}
siweAction={callSiwe}
/>
);
},
);
export default ConnectedSIWEPanel;