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 ob from 'urbit-ob';
6import { randomHex } from 'web3-utils';
8import { usePointCursor } from 'store/pointCursor';
9import { usePointCache } from 'store/pointCache';
10import { useNetwork } from 'store/network';
11import { useWallet } from 'store/wallet';
13import { useLocalRouter } from 'lib/LocalRouter';
14import * as need from 'lib/need';
17 attemptNetworkSeedDerivation,
22import { formatDotsWithTime } from 'lib/dateFormat';
23import useEthereumTransaction from 'lib/useEthereumTransaction';
24import { GAS_LIMITS } from 'lib/constants';
25import { addHexPrefix } from 'lib/wallet';
26import useKeyfileGenerator from 'lib/useKeyfileGenerator';
28import ViewHeader from 'components/ViewHeader';
29import { BootUrbitOSButton } from 'components/Buttons';
30import FooterButton from 'components/FooterButton';
31import DownloadKeyfileButton from 'components/DownloadKeyfileButton';
32import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
33import NoticeBox from 'components/NoticeBox';
35import { HexInput } from 'form/Inputs';
38 buildCheckboxValidator,
40} from 'form/validators';
41import BridgeForm from 'form/BridgeForm';
42import Condition from 'form/Condition';
43import FormError from 'form/FormError';
44import convertToInt from 'lib/convertToInt';
46const chainKeyProp = name => d =>
47 d[name] === CURVE_ZERO_ADDR ? Nothing() : Just(d[name]);
49const renderNetworkKey = key => {
50 const segments = segmentNetworkKey(key);
56 return segments.join('\n');
59function useSetKeys() {
60 const { urbitWallet, wallet, authMnemonic } = useWallet();
61 const { pointCursor } = usePointCursor();
62 const { syncDetails, syncRekeyDate, getDetails } = usePointCache();
63 const { contracts } = useNetwork();
65 const _point = need.point(pointCursor);
66 const _contracts = need.contracts(contracts);
67 const _details = need.details(getDetails(_point));
69 const networkRevision = convertToInt(_details.keyRevisionNumber, 10);
70 const randomSeed = useRef();
72 // NOTE: nd = 'nondeterministic' (can also be a 'manual' seed)
73 const [ndNetworkSeed, setNdNetworkSeed] = useState();
76 available: keyfileAvailable,
79 } = useKeyfileGenerator(ndNetworkSeed);
81 const buildNetworkSeed = useCallback(
83 if (manualSeed !== undefined) {
84 setNdNetworkSeed(manualSeed);
87 const newNetworkRevision = networkRevision + 1;
88 console.log(`deriving seed with revision ${newNetworkRevision}`);
90 const networkSeed = await attemptNetworkSeedDerivation({
95 revision: newNetworkRevision,
98 if (Just.hasInstance(networkSeed)) {
99 return networkSeed.value;
102 randomSeed.current = randomSeed.current || randomHex(32); // 32 bytes
103 setNdNetworkSeed(randomSeed.current);
105 return randomSeed.current;
108 [_details, authMnemonic, networkRevision, urbitWallet, wallet]
111 const { completed: _completed, ...rest } = useEthereumTransaction(
113 async (manualSeed, isDiscontinuity) => {
114 const seed = await buildNetworkSeed(manualSeed);
115 const pair = deriveNetworkKeys(seed);
117 return azimuth.ecliptic.configureKeys(
120 addHexPrefix(pair.crypt.public),
121 addHexPrefix(pair.auth.public),
122 CRYPTO_SUITE_VERSION,
126 [_contracts, _point, buildNetworkSeed]
129 () => Promise.all([syncDetails(_point), syncRekeyDate(_point)]),
130 [_point, syncDetails, syncRekeyDate]
132 GAS_LIMITS.CONFIGURE_KEYS
135 // only treat the transaction as completed once we also have keys to download
136 const completed = _completed && keyfileAvailable;
147export default function AdminNetworkingKeys() {
148 const { push, names, pop } = useLocalRouter();
149 const { pointCursor } = usePointCursor();
150 const { getDetails, getRekeyDate } = usePointCache();
152 const point = need.point(pointCursor);
153 const details = getDetails(point);
155 const hasKeys = details.matchWith({
156 Nothing: () => false,
157 Just: ({ value: details }) => parseInt(details.keyRevisionNumber, 10) > 0,
158 // we actually don't mind the default NaN behavior of parseInt here,
159 // since NaN > 0 === false and that's a reasonable result
162 const [usedDiscontinuity, setUsedDiscontinuity] = useState(false);
176 const validateForm = useCallback((values, errors) => {
177 if (values.useNetworkSeed && errors.networkSeed) {
182 const validate = useMemo(
186 useNetworkSeed: buildCheckboxValidator(),
187 networkSeed: buildHexValidator(64), // 64 chars
188 useDiscontinuity: buildCheckboxValidator(),
195 const onValues = useCallback(
196 ({ valid, values, form }) => {
199 values.useNetworkSeed ? values.networkSeed : undefined,
200 values.useDiscontinuity
202 setUsedDiscontinuity(values.useDiscontinuity);
207 if (!values.useNetworkSeed && values.networkSeed) {
208 form.change('networkSeed', '');
211 [construct, unconstruct, setUsedDiscontinuity]
214 const initialValues = useMemo(
216 useNetworkSeed: false,
217 useDiscontinuity: false,
222 const goRelocate = useCallback(() => push(names.RELOCATE), [push, names]);
224 const usageMessage = useMemo(() => {
225 const need = 'You need this keyfile to authenticate with Arvo.';
230 urbit -w {ob.patp(point).slice(1)} -k {filename}
234 if (usedDiscontinuity) {
237 {need} Since you broke continuity, (re)move your old files, and {boot}
243 {need} If you have already booted Arvo, run{' '}
244 <code>|rekey '0wkeyfile.contents'</code>. Otherwise, {boot}
248 }, [usedDiscontinuity, filename, point]);
250 const renderTitle = () => {
252 return 'Networking keys are now set. Download your Keyfile to authenticate Arvo.';
256 return 'Setting Network Keys...';
260 return 'Networking keys have not yet been set.';
266 const renderNetworkKeySection = (title, key) => (
268 <Grid.Item full as={H5} className="mt3 gray4">
273 <Grid.Item full as="code" className="f5 mono gray4">
277 Just: ({ value: key }) => (
279 <Grid.Item full as="code" className="f5 mono wrap">
282 <Grid.Item full as="code" className="f5 mono wrap">
283 {renderNetworkKey(key)}
291 const renderDetail = (title, value) => (
292 <Flex.Item as={Flex} col>
293 <Flex.Item as={H5} className="gray4">
298 <Flex.Item as={Text} className="f5 gray4">
302 Just: ({ value }) => <Flex.Item as={Text}>{value}</Flex.Item>,
307 const renderDetails = () => {
310 {renderNetworkKeySection(
312 details.chain(chainKeyProp('authenticationKey'))
314 {renderNetworkKeySection(
316 details.chain(chainKeyProp('encryptionKey'))
318 <Grid.Item full as={Flex} row justify="between" className="mt3">
319 {renderDetail('Revision', details.map(d => d.keyRevisionNumber))}
320 {renderDetail('Continuity Era', details.map(d => d.continuityNumber))}
323 details.map(d => d.cryptoSuiteVersion)
327 <Grid.Item full as={Flex} row justify="between" className="mt3">
330 getRekeyDate(point).map(date =>
332 Ok: r => formatDotsWithTime(r.value),
333 Error: () => Nothing(),
346 <Grid.Item full as={ViewHeader}>
350 <Grid.Item full as={Text} className="mb3">
352 ? 'Here are your public keys that authenticate your Arvo.'
353 : 'Set your networking keys to authenticate with Arvo.'}
358 <Grid.Item full as={NoticeBox} className="mb3">
361 <Grid.Item full as={DownloadKeyfileButton} solid {...keyfileBind} />
369 initialValues={initialValues}>
375 name="useNetworkSeed"
376 label="Use Custom Network Seed"
377 inverseLabel="Back to Derived Network Seed"
378 disabled={inputsLocked}
380 <Condition when="useNetworkSeed" is={true}>
381 <Grid.Item full as={NoticeBox} className="mb2">
382 When using a custom network seed, you'll need to download
383 your Arvo keyfile immediately after this transaction is
384 Bridge does not store your seed.
390 label="Network Seed (32 bytes)"
391 disabled={inputsLocked}
397 name="useDiscontinuity"
398 label="Breach Continuity"
399 disabled={inputsLocked}
402 <Grid.Item full as={FormError} />
406 as={InlineEthereumTransaction}
408 label={`${hasKeys ? 'Reset' : 'Set'} Networking Keys`}
409 onReturn={() => pop()}
416 {isDefaultState && renderDetails()}
420 <Grid.Item full as={BootUrbitOSButton} />
427 <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