import React from 'react'; import { Grid, Flex, H5 } from 'indigo-react'; import { Just, Nothing } from 'folktale/maybe'; import { formatDotsWithTime } from 'lib/dateFormat'; import { segmentNetworkKey, CURVE_ZERO_ADDR } from 'lib/keys'; import { usePointCache } from 'store/pointCache'; const chainKeyProp = name => d => d[name] === CURVE_ZERO_ADDR ? Nothing() : Just(d[name]); const renderNetworkKey = key => { const segments = segmentNetworkKey(key); if (!segments) { return 'Unset'; } return segments.join('\n'); }; export default function NetworkingKeys({ point }) { const { getDetails, getRekeyDate } = usePointCache(); const details = getDetails(point); const hasKeys = details.matchWith({ Nothing: () => false, Just: ({ value: details }) => parseInt(details.keyRevisionNumber, 10) > 0, // we actually don't mind the default NaN behavior of parseInt here, // since NaN > 0 === false and that's a reasonable result }); const renderNetworkKeySection = (title, key) => ( <> {title} {key.matchWith({ Nothing: () => ( Unset ), Just: ({ value: key }) => ( <> 0x {renderNetworkKey(key)} ), })} ); const renderDetail = (title, value) => ( {title} {value.matchWith({ Nothing: () => ( Unset ), Just: ({ value }) => {value}, })} ); const revisionNumber = details .map(d => d.keyRevisionNumber) .matchWith({ Nothing: () => 0, Just: ({ value }) => value, }); const revisionTime = hasKeys ? ` at ${getRekeyDate(point) .map(date => date.matchWith({ Ok: r => formatDotsWithTime(r.value), Error: () => 'No date available', }) ) .matchWith({ Nothing: () => null, Just: ({ value }) => value, })}` : ''; return ( <>
Network Keys
Revision {revisionNumber} {revisionTime}
{renderNetworkKeySection( 'Authentication Key', details.chain(chainKeyProp('authenticationKey')) )} {renderNetworkKeySection( 'Encryption Key', details.chain(chainKeyProp('encryptionKey')) )} {renderDetail( 'Continuity Era', details.map(d => d.continuityNumber) )} {renderDetail( 'Crypto Suite Version', details.map(d => d.cryptoSuiteVersion) )} ); }