1import React, { useCallback, useMemo, useRef, useState } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import { Grid, Text, H5, Flex, ToggleInput, CheckboxInput } from 'indigo-react';
4import * as azimuth from 'azimuth-js';
5import { randomHex } from 'web3-utils';
7import { usePointCursor } from 'store/pointCursor';
8import { usePointCache } from 'store/pointCache';
9import { useNetwork } from 'store/network';
10import { useWallet } from 'store/wallet';
12import { useLocalRouter } from 'lib/LocalRouter';
13import * as need from 'lib/need';
16 attemptNetworkSeedDerivation,
21import { formatDotsWithTime } from 'lib/dateFormat';
22import useEthereumTransaction from 'lib/useEthereumTransaction';
23import { GAS_LIMITS } from 'lib/constants';
24import { addHexPrefix } from 'lib/wallet';
25import useKeyfileGenerator from 'lib/useKeyfileGenerator';
27import ViewHeader from 'components/ViewHeader';
28import { BootArvoButton } from 'components/Buttons';
29import FooterButton from 'components/FooterButton';
30import DownloadKeyfileButton from 'components/DownloadKeyfileButton';
31import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
32import NoticeBox from 'components/NoticeBox';
34import { HexInput } from 'form/Inputs';
37 buildCheckboxValidator,
39} from 'form/validators';
40import BridgeForm from 'form/BridgeForm';
41import Condition from 'form/Condition';
42import FormError from 'form/FormError';
43import convertToInt from 'lib/convertToInt';
45const chainKeyProp = name => d =>
46 d[name] === CURVE_ZERO_ADDR ? Nothing() : Just(d[name]);
48const renderNetworkKey = key => {
49 const segments = segmentNetworkKey(key);
55 return segments.join('\n');
58function useSetKeys() {
59 const { urbitWallet, wallet, authMnemonic } = useWallet();
60 const { pointCursor } = usePointCursor();
61 const { syncDetails, syncRekeyDate, getDetails } = usePointCache();
62 const { contracts } = useNetwork();
64 const _point = need.point(pointCursor);
65 const _contracts = need.contracts(contracts);
66 const _details = need.details(getDetails(_point));
68 const networkRevision = convertToInt(_details.keyRevisionNumber, 10);
69 const randomSeed = useRef();
71 // NOTE: nd = 'nondeterministic' (can also be a 'manual' seed)
72 const [ndNetworkSeed, setNdNetworkSeed] = useState();
75 available: keyfileAvailable,
77 } = useKeyfileGenerator(ndNetworkSeed);
79 const buildNetworkSeed = useCallback(
81 if (manualSeed !== undefined) {
82 setNdNetworkSeed(manualSeed);
85 const newNetworkRevision = networkRevision + 1;
86 console.log(`deriving seed with revision ${newNetworkRevision}`);
88 const networkSeed = await attemptNetworkSeedDerivation({
93 revision: newNetworkRevision,
96 if (Just.hasInstance(networkSeed)) {
97 return networkSeed.value;
100 randomSeed.current = randomSeed.current || randomHex(64);
101 setNdNetworkSeed(randomSeed.current);
103 return randomSeed.current;
106 [_details, authMnemonic, networkRevision, urbitWallet, wallet]
109 const { completed: _completed, ...rest } = useEthereumTransaction(
111 async (manualSeed, isDiscontinuity) => {
112 const seed = await buildNetworkSeed(manualSeed);
113 const pair = deriveNetworkKeys(seed);
115 return azimuth.ecliptic.configureKeys(
118 addHexPrefix(pair.crypt.public),
119 addHexPrefix(pair.auth.public),
120 CRYPTO_SUITE_VERSION,
124 [_contracts, _point, buildNetworkSeed]
127 () => Promise.all([syncDetails(_point), syncRekeyDate(_point)]),
128 [_point, syncDetails, syncRekeyDate]
130 GAS_LIMITS.CONFIGURE_KEYS
133 // only treat the transaction as completed once we also have keys to download
134 const completed = _completed && keyfileAvailable;
144export default function AdminNetworkingKeys() {
145 const { push, names, pop } = useLocalRouter();
146 const { pointCursor } = usePointCursor();
147 const { getDetails, getRekeyDate } = usePointCache();
149 const point = need.point(pointCursor);
150 const details = getDetails(point);
152 const hasKeys = details.matchWith({
153 Nothing: () => false,
154 Just: ({ value: details }) => parseInt(details.keyRevisionNumber, 10) > 0,
155 // we actually don't mind the default NaN behavior of parseInt here,
156 // since NaN > 0 === false and that's a reasonable result
170 const validateForm = useCallback((values, errors) => {
171 if (values.useNetworkSeed && errors.networkSeed) {
176 const validate = useMemo(
180 useNetworkSeed: buildCheckboxValidator(),
181 networkSeed: buildHexValidator(32),
182 useDiscontinuity: buildCheckboxValidator(),
189 const onValues = useCallback(
190 ({ valid, values, form }) => {
193 values.useNetworkSeed ? values.networkSeed : undefined,
194 values.useDiscontinuity
200 if (!values.useNetworkSeed && values.networkSeed) {
201 form.change('networkSeed', '');
204 [construct, unconstruct]
207 const initialValues = useMemo(
209 useNetworkSeed: false,
210 useDiscontinuity: false,
215 const goRelocate = useCallback(() => push(names.RELOCATE), [push, names]);
217 const renderTitle = () => {
219 return 'Networking keys are now set. Download your Keyfile to authenticate Arvo.';
223 return 'Setting Network Keys...';
227 return 'Networking keys have not yet been set.';
233 const renderNetworkKeySection = (title, key) => (
235 <Grid.Item full as={H5} className="mt3 gray4">
240 <Grid.Item full as="code" className="f5 mono gray4">
244 Just: ({ value: key }) => (
246 <Grid.Item full as="code" className="f5 mono wrap">
249 <Grid.Item full as="code" className="f5 mono wrap">
250 {renderNetworkKey(key)}
258 const renderDetail = (title, value) => (
259 <Flex.Item as={Flex} col>
260 <Flex.Item as={H5} className="gray4">
265 <Flex.Item as={Text} className="f5 gray4">
269 Just: ({ value }) => <Flex.Item as={Text}>{value}</Flex.Item>,
274 const renderDetails = () => {
277 {renderNetworkKeySection(
279 details.chain(chainKeyProp('authenticationKey'))
281 {renderNetworkKeySection(
283 details.chain(chainKeyProp('encryptionKey'))
285 <Grid.Item full as={Flex} row justify="between" className="mt3">
286 {renderDetail('Revision', details.map(d => d.keyRevisionNumber))}
287 {renderDetail('Continuity Era', details.map(d => d.continuityNumber))}
290 details.map(d => d.cryptoSuiteVersion)
294 <Grid.Item full as={Flex} row justify="between" className="mt3">
297 getRekeyDate(point).map(date =>
299 Ok: r => formatDotsWithTime(r.value),
300 Error: () => Nothing(),
313 <Grid.Item full as={ViewHeader}>
317 <Grid.Item full as={Text} className="mb3">
319 ? 'Here are your public keys that authenticate your Arvo.'
320 : 'Set your networking keys to authenticate with Arvo.'}
325 <Grid.Item full as={NoticeBox} className="mb3">
326 You need this keyfile to authenticate with Arvo. If you have
327 already booted Arvo, run <code>|rekey '0wkeyfile.contents'</code>
329 <Grid.Item full as={DownloadKeyfileButton} solid {...keyfileBind} />
337 initialValues={initialValues}>
343 name="useNetworkSeed"
344 label="Use Custom Network Seed"
345 inverseLabel="Back to Derived Network Seed"
346 disabled={inputsLocked}
348 <Condition when="useNetworkSeed" is={true}>
349 <Grid.Item full as={NoticeBox} className="mb2">
350 When using a custom network seed, you'll need to download
351 your Arvo keyfile immediately after this transaction is
352 Bridge does not store your seed.
358 label="Network Seed (64 bytes)"
359 disabled={inputsLocked}
365 name="useDiscontinuity"
366 label="Trigger New Continuity Era"
367 disabled={inputsLocked}
370 <Grid.Item full as={FormError} />
374 as={InlineEthereumTransaction}
376 label={`${hasKeys ? 'Reset' : 'Set'} Networking Keys`}
377 onReturn={() => pop()}
384 {isDefaultState && renderDetails()}
388 <Grid.Item full as={BootArvoButton} />
395 <FooterButton onClick={goRelocate} disabled>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list