on c9bf94f help sign in
bridge/src/components/NetworkingKeys.js
123 lines 3.3 kB history raw
1import React from 'react';
2import { Grid, Flex, H5 } from 'indigo-react';
3import { Just, Nothing } from 'folktale/maybe';
5import { formatDotsWithTime } from 'lib/dateFormat';
6import { segmentNetworkKey, CURVE_ZERO_ADDR } from 'lib/keys';
8import { usePointCache } from 'store/pointCache';
10const chainKeyProp = name => d =>
11 d[name] === CURVE_ZERO_ADDR ? Nothing() : Just(d[name]);
13const renderNetworkKey = key => {
14 const segments = segmentNetworkKey(key);
16 if (!segments) {
17 return 'Unset';
18 }
20 return segments.join('\n');
21};
23export default function NetworkingKeys({ point }) {
24 const { getDetails, getRekeyDate } = usePointCache();
26 const details = getDetails(point);
28 const hasKeys = details.matchWith({
29 Nothing: () => false,
30 Just: ({ value: details }) => parseInt(details.keyRevisionNumber, 10) > 0,
31 // we actually don't mind the default NaN behavior of parseInt here,
32 // since NaN > 0 === false and that's a reasonable result
33 });
35 const renderNetworkKeySection = (title, key) => (
36 <>
37 <Grid.Item full as={H5} className="mt3 gray4">
38 {title}
39 </Grid.Item>
40 {key.matchWith({
41 Nothing: () => (
42 <Grid.Item full as="code" className="f5 mono gray4">
43 Unset
44 </Grid.Item>
45 ),
46 Just: ({ value: key }) => (
47 <>
48 <Grid.Item full as="div" className="f5 mono">
49 0x
50 </Grid.Item>
51 <Grid.Item full as="code" className="f5 mono wrap">
52 {renderNetworkKey(key)}
53 </Grid.Item>
54 </>
55 ),
56 })}
57 </>
58 );
60 const renderDetail = (title, value) => (
61 <Flex.Item as={Flex} row>
62 <Flex.Item style={{ minWidth: 192, marginBottom: 8 }} as={H5}>
63 {title}
64 </Flex.Item>
65 {value.matchWith({
66 Nothing: () => (
67 <Flex.Item as={H5} className="f5 gray4">
68 Unset
69 </Flex.Item>
70 ),
71 Just: ({ value }) => <Flex.Item as={H5}>{value}</Flex.Item>,
72 })}
73 </Flex.Item>
74 );
76 const revisionNumber = details
77 .map(d => d.keyRevisionNumber)
78 .matchWith({
79 Nothing: () => 0,
80 Just: ({ value }) => value,
81 });
83 const revisionTime = hasKeys
84 ? ` at ${getRekeyDate(point)
85 .map(date =>
86 date.matchWith({
87 Ok: r => formatDotsWithTime(r.value),
88 Error: () => 'No date available',
89 })
90 )
91 .matchWith({
92 Nothing: () => null,
93 Just: ({ value }) => value,
94 })}`
95 : '';
97 return (
98 <>
99 <h5 className="fw-bold">Network Keys</h5>
100 <h5 className="gray4">
101 Revision {revisionNumber} {revisionTime}
102 </h5>
103 {renderNetworkKeySection(
104 'Authentication Key',
105 details.chain(chainKeyProp('authenticationKey'))
106 )}
107 {renderNetworkKeySection(
108 'Encryption Key',
109 details.chain(chainKeyProp('encryptionKey'))
110 )}
111 <Grid.Item full as={Flex} col justify="between" className="mt3">
112 {renderDetail(
113 'Continuity Era',
114 details.map(d => d.continuityNumber)
115 )}
116 {renderDetail(
117 'Crypto Suite Version',
118 details.map(d => d.cryptoSuiteVersion)
119 )}
120 </Grid.Item>
121 </>
122 );