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,
77 generating: keyfileGenerating,
80 } = useKeyfileGenerator(ndNetworkSeed);
82 const buildNetworkSeed = useCallback(
84 if (manualSeed !== undefined) {
85 setNdNetworkSeed(manualSeed);
88 const newNetworkRevision = networkRevision + 1;
89 console.log(`deriving seed with revision ${newNetworkRevision}`);
91 const networkSeed = await attemptNetworkSeedDerivation({
96 revision: newNetworkRevision,
99 if (Just.hasInstance(networkSeed)) {
100 return networkSeed.value;
103 randomSeed.current = randomSeed.current || randomHex(32); // 32 bytes
104 setNdNetworkSeed(randomSeed.current);
106 return randomSeed.current;
109 [_details, authMnemonic, networkRevision, urbitWallet, wallet]
112 const { completed: _completed, ...rest } = useEthereumTransaction(
114 async (manualSeed, isDiscontinuity) => {
115 const seed = await buildNetworkSeed(manualSeed);
116 const pair = deriveNetworkKeys(seed);
118 return azimuth.ecliptic.configureKeys(
121 addHexPrefix(pair.crypt.public),
122 addHexPrefix(pair.auth.public),
123 CRYPTO_SUITE_VERSION,
127 [_contracts, _point, buildNetworkSeed]
130 () => Promise.all([syncDetails(_point), syncRekeyDate(_point)]),
131 [_point, syncDetails, syncRekeyDate]
133 GAS_LIMITS.CONFIGURE_KEYS
136 // only treat the transaction as completed once we also have keys to download
137 const completed = _completed && keyfileAvailable && !keyfileGenerating;
148export default function AdminNetworkingKeys() {
149 const { push, names, pop } = useLocalRouter();
150 const { pointCursor } = usePointCursor();
151 const { getDetails, getRekeyDate } = usePointCache();
153 const point = need.point(pointCursor);
154 const details = getDetails(point);
156 const hasKeys = details.matchWith({
157 Nothing: () => false,
158 Just: ({ value: details }) => parseInt(details.keyRevisionNumber, 10) > 0,
159 // we actually don't mind the default NaN behavior of parseInt here,
160 // since NaN > 0 === false and that's a reasonable result
163 const [usedDiscontinuity, setUsedDiscontinuity] = useState(false);
177 const validateForm = useCallback((values, errors) => {
178 if (values.useNetworkSeed && errors.networkSeed) {
183 const validate = useMemo(
187 useNetworkSeed: buildCheckboxValidator(),
188 networkSeed: buildHexValidator(64), // 64 chars
189 useDiscontinuity: buildCheckboxValidator(),
196 const onValues = useCallback(
197 ({ valid, values, form }) => {
200 values.useNetworkSeed ? values.networkSeed : undefined,
201 values.useDiscontinuity
203 setUsedDiscontinuity(values.useDiscontinuity);
208 if (!values.useNetworkSeed && values.networkSeed) {
209 form.change('networkSeed', '');
212 [construct, unconstruct, setUsedDiscontinuity]
215 const initialValues = useMemo(
217 useNetworkSeed: false,
218 useDiscontinuity: false,
223 const goRelocate = useCallback(() => push(names.RELOCATE), [push, names]);
225 const usageMessage = useMemo(() => {
226 const need = 'You need this keyfile to authenticate with Arvo.';
231 urbit -w {ob.patp(point).slice(1)} -k {filename}
235 if (usedDiscontinuity) {
238 {need} Since you broke continuity, (re)move your old files, and {boot}
244 {need} If you have already booted Arvo, run{' '}
245 <code>|rekey '0wkeyfile.contents'</code>. Otherwise, {boot}
249 }, [usedDiscontinuity, filename, point]);
251 const renderTitle = () => {
253 return 'Networking keys are now set. Download your Keyfile to authenticate Arvo.';
257 return 'Setting Network Keys...';
261 return 'Networking keys have not yet been set.';
267 const renderNetworkKeySection = (title, key) => (
269 <Grid.Item full as={H5} className="mt3 gray4">
274 <Grid.Item full as="code" className="f5 mono gray4">
278 Just: ({ value: key }) => (
280 <Grid.Item full as="code" className="f5 mono wrap">
283 <Grid.Item full as="code" className="f5 mono wrap">
284 {renderNetworkKey(key)}
292 const renderDetail = (title, value) => (
293 <Flex.Item as={Flex} col>
294 <Flex.Item as={H5} className="gray4">
299 <Flex.Item as={Text} className="f5 gray4">
303 Just: ({ value }) => <Flex.Item as={Text}>{value}</Flex.Item>,
308 const renderDetails = () => {
311 {renderNetworkKeySection(
313 details.chain(chainKeyProp('authenticationKey'))
315 {renderNetworkKeySection(
317 details.chain(chainKeyProp('encryptionKey'))
319 <Grid.Item full as={Flex} row justify="between" className="mt3">
320 {renderDetail('Revision', details.map(d => d.keyRevisionNumber))}
321 {renderDetail('Continuity Era', details.map(d => d.continuityNumber))}
324 details.map(d => d.cryptoSuiteVersion)
328 <Grid.Item full as={Flex} row justify="between" className="mt3">
331 getRekeyDate(point).map(date =>
333 Ok: r => formatDotsWithTime(r.value),
334 Error: () => Nothing(),
347 <Grid.Item full as={ViewHeader}>
351 <Grid.Item full as={Text} className="mb3">
353 ? 'Here are your public keys that authenticate your Arvo.'
354 : 'Set your networking keys to authenticate with Arvo.'}
359 <Grid.Item full as={NoticeBox} className="mb3">
362 <Grid.Item full as={DownloadKeyfileButton} solid {...keyfileBind} />
370 initialValues={initialValues}>
376 name="useNetworkSeed"
377 label="Use Custom Network Seed"
378 inverseLabel="Back to Derived Network Seed"
379 disabled={inputsLocked}
381 <Condition when="useNetworkSeed" is={true}>
382 <Grid.Item full as={NoticeBox} className="mb2">
383 When using a custom network seed, you'll need to download
384 your Arvo keyfile immediately after this transaction is
385 Bridge does not store your seed.
391 label="Network Seed (32 bytes)"
392 disabled={inputsLocked}
398 name="useDiscontinuity"
399 label="Breach Continuity"
400 disabled={inputsLocked}
403 <Grid.Item full as={FormError} />
407 as={InlineEthereumTransaction}
409 label={`${hasKeys ? 'Reset' : 'Set'} Networking Keys`}
410 onReturn={() => pop()}
417 {isDefaultState && renderDetails()}
421 <Grid.Item full as={BootUrbitOSButton} />
428 <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