on 41620df help sign in
bridge/src/views/Admin/AdminNetworkingKeys.js
220 lines 5.8 kB history raw
1import React, { useCallback, useState } from 'react';
2import { Nothing } from 'folktale/maybe';
3import { Grid, Text, H5, Flex } from 'indigo-react';
5import { usePointCursor } from 'store/pointCursor';
6import { usePointCache } from 'store/pointCache';
8import { useLocalRouter } from 'lib/LocalRouter';
9import * as need from 'lib/need';
10import { segmentNetworkKey } from 'lib/keys';
11import { formatDotsWithTime } from 'lib/dateFormat';
13import ViewHeader from 'components/ViewHeader';
14import MiniBackButton from 'components/MiniBackButton';
15import {
16 GenerateButton,
17 BootArvoButton,
18 ForwardButton,
19} from 'components/Buttons';
20import FooterButton from 'components/FooterButton';
21import WarningBox from 'components/WarningBox';
22import DownloadKeyfileButton from 'components/DownloadKeyfileButton';
24const renderNetworkKey = key => {
25 const segments = segmentNetworkKey(key);
27 if (!segments) {
28 return 'Unset';
29 }
31 return segments.join('\n');
32};
34function useSetKeys() {
35 const setting = false;
36 const didSet = false;
37 const setKeys = useCallback(async () => {}, []);
39 return {
40 setting,
41 didSet,
42 setKeys,
43 };
46export default function AdminNetworkingKeys() {
47 const { push, names, pop } = useLocalRouter();
48 const { pointCursor } = usePointCursor();
49 const { getDetails, getRekeyDate } = usePointCache();
51 const point = need.point(pointCursor);
52 const details = getDetails(point);
54 const hasKeys = details.matchWith({
55 Nothing: () => false,
56 Just: ({ value: details }) => parseInt(details.keyRevisionNumber, 10) > 0,
57 });
58 const [configuring, setConfiguring] = useState(false);
60 const { setKeys, setting, didSet } = useSetKeys();
62 const goRelocate = useCallback(() => push(names.RELOCATE), [push, names]);
63 const configureTransaction = useCallback(() => setConfiguring(true), [
64 setConfiguring,
65 ]);
67 const inDefaultState = !configuring && !setting && !didSet;
69 const renderTitle = () => {
70 if (didSet) {
71 return 'Networking keys are now set. Download your Keyfile to authenticate Arvo.';
72 }
74 if (setting) {
75 return 'Setting Network Keys...';
76 }
78 if (!hasKeys) {
79 return 'Networking keys have not yet been set.';
80 }
82 return 'Networking';
83 };
85 const renderButton = () => {
86 if (didSet) {
87 return <Grid.Item full as={DownloadKeyfileButton} solid />;
88 }
90 if (configuring || setting) {
91 return <Grid.Item full as={GenerateButton} solid onClick={setKeys} />;
92 }
94 return (
95 <Grid.Item full as={ForwardButton} solid onClick={configureTransaction}>
96 {hasKeys ? 'Reset' : 'Set'} Networking Keys
97 </Grid.Item>
98 );
99 };
101 const renderNetworkKeySection = (title, key) => (
102 <>
103 <Grid.Item full as={H5} className="mt3 gray4">
104 {title}
105 </Grid.Item>
106 {key.matchWith({
107 Nothing: () => (
108 <Grid.Item full as={Text} className="f5 gray4">
109 Unset
110 </Grid.Item>
111 ),
112 Just: ({ value: key }) => (
113 <>
114 <Grid.Item full as="code" className="f5 wrap">
115 0x
116 </Grid.Item>
117 <Grid.Item full as="code" className="f5 wrap">
118 {renderNetworkKey(key)}
119 </Grid.Item>
120 </>
121 ),
122 })}
123 </>
124 );
126 const renderDetail = (title, value) => (
127 <Flex.Item as={Flex} col>
128 <Flex.Item as={H5} className="gray4">
129 {title}
130 </Flex.Item>
131 {value.matchWith({
132 Nothing: () => (
133 <Flex.Item as={Text} className="f5 gray4">
134 Unset
135 </Flex.Item>
136 ),
137 Just: ({ value }) => <Flex.Item as={Text}>{value}</Flex.Item>,
138 })}
139 </Flex.Item>
140 );
142 const renderDetails = () => {
143 return (
144 <>
145 {renderNetworkKeySection(
146 'Authentication',
147 details.map(d => d.authenticationKey)
148 )}
149 {renderNetworkKeySection(
150 'Encryption',
151 details.map(d => d.encryptionKey)
152 )}
153 <Grid.Item full as={Flex} row justify="between" className="mt3">
154 {renderDetail('Revision', details.map(d => d.keyRevisionNumber))}
155 {renderDetail('Continuity Era', details.map(d => d.continuityNumber))}
156 {renderDetail(
157 'Crypto Suite Ver.',
158 details.map(d => d.cryptoSuiteVersion)
159 )}
160 </Grid.Item>
161 {hasKeys && (
162 <Grid.Item full as={Flex} row justify="between" className="mt3">
163 {renderDetail(
164 'Last Set',
165 getRekeyDate(point).map(date =>
166 date.matchWith({
167 Ok: r => formatDotsWithTime(r.value),
168 Error: () => Nothing(),
169 })
170 )
171 )}
172 </Grid.Item>
173 )}
174 </>
175 );
176 };
178 return (
179 <>
180 <Grid>
181 <Grid.Item full as={MiniBackButton} onClick={() => pop()} />
183 <Grid.Item full as={ViewHeader}>
184 {renderTitle()}
185 </Grid.Item>
187 {inDefaultState && (
188 <Grid.Item full as={Text} className="mb3">
189 {hasKeys
190 ? 'Here are your public keys that authenticate your Arvo.'
191 : 'Set your networking keys to authenticate with Arvo.'}
192 </Grid.Item>
193 )}
195 {didSet && (
196 <Grid.Item full as={WarningBox} className="mb3">
197 You need this keyfile to authenticate with Arvo.
198 </Grid.Item>
199 )}
201 {renderButton()}
203 {inDefaultState && renderDetails()}
205 {didSet && (
206 <>
207 <Grid.Item full as={BootArvoButton} disabled />
208 <Grid.Divider />
209 </>
210 )}
211 </Grid>
213 {inDefaultState && (
214 <FooterButton onClick={goRelocate} disabled>
215 Relocate
216 </FooterButton>
217 )}
218 </>
219 );