1import React, { useCallback, useMemo, useRef, useState } from 'react';
2import { Just } from 'folktale/maybe';
3import { Grid, ToggleInput, CheckboxInput, Button } 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';
11import { useRollerStore } from 'store/roller';
13import useRoller from 'lib/useRoller';
14import { useLocalRouter } from 'lib/LocalRouter';
15import * as need from 'lib/need';
17 attemptNetworkSeedDerivation,
21import useEthereumTransaction from 'lib/useEthereumTransaction';
22import { GAS_LIMITS } from 'lib/constants';
23import { addHexPrefix } from 'lib/utils/address';
24import useKeyfileGenerator from 'lib/useKeyfileGenerator';
26import DownloadKeyfileButton from 'components/DownloadKeyfileButton';
27import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
28import NoticeBox from 'components/NoticeBox';
29import AlertBox from 'components/AlertBox';
31import { HexInput } from 'form/Inputs';
34 buildCheckboxValidator,
36} from 'form/validators';
37import BridgeForm from 'form/BridgeForm';
38import Condition from 'form/Condition';
39import FormError from 'form/FormError';
40import { convertToInt } from 'lib/convertToInt';
41import Window from 'components/L2/Window/Window';
42import HeaderPane from 'components/L2/Window/HeaderPane';
43import BodyPane from 'components/L2/Window/BodyPane';
44import { Box, Row } from '@tlon/indigo-react';
46function useSetKeys(manualNetworkSeed, setManualNetworkSeed) {
47 const { urbitWallet, wallet, authMnemonic, authToken } = useWallet();
48 const { pointCursor } = usePointCursor();
49 const { syncDetails, syncRekeyDate, getDetails } = usePointCache();
50 const { contracts } = useNetwork();
52 const _point = need.point(pointCursor);
53 const _contracts = need.contracts(contracts);
54 const _details = need.details(getDetails(_point));
56 const networkRevision = convertToInt(_details.keyRevisionNumber, 10);
57 const randomSeed = useRef();
60 available: keyfileAvailable,
61 generating: keyfileGenerating,
64 } = useKeyfileGenerator(manualNetworkSeed);
66 const buildNetworkSeed = useCallback(
68 if (manualSeed !== undefined) {
69 setManualNetworkSeed(manualSeed);
72 const newNetworkRevision = networkRevision + 1;
73 console.log(`deriving seed with revision ${newNetworkRevision}`);
75 const networkSeed = await attemptNetworkSeedDerivation({
82 revision: newNetworkRevision,
85 if (Just.hasInstance(networkSeed)) {
86 return networkSeed.value;
89 randomSeed.current = randomSeed.current || randomHex(32); // 32 bytes
90 setManualNetworkSeed(randomSeed.current);
92 return randomSeed.current;
107 const { completed: _completed, ...rest } = useEthereumTransaction(
109 async (manualSeed, isDiscontinuity) => {
110 const seed = await buildNetworkSeed(manualSeed);
111 const pair = deriveNetworkKeys(seed);
113 return azimuth.ecliptic.configureKeys(
116 addHexPrefix(pair.crypt.public),
117 addHexPrefix(pair.auth.public),
118 CRYPTO_SUITE_VERSION,
122 [_contracts, _point, buildNetworkSeed]
125 () => Promise.all([syncDetails(_point), syncRekeyDate(_point)]),
126 [_point, syncDetails, syncRekeyDate]
128 GAS_LIMITS.CONFIGURE_KEYS
131 // only treat the transaction as completed once we also have keys to download
132 const completed = _completed && keyfileAvailable && !keyfileGenerating;
142export default function UrbitOSNetworkingKeys({
144 setManualNetworkSeed,
146 const { pop } = useLocalRouter();
147 const { pointCursor } = usePointCursor();
148 const { getDetails } = usePointCache();
149 const { currentL2, currentL2Spawn } = useRollerStore();
150 const { configureNetworkingKeys, getPendingTransactions } = useRoller();
151 const [breach, setBreach] = useState(false);
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
170 } = useSetKeys(manualNetworkSeed, setManualNetworkSeed);
172 const validateForm = useCallback((values, errors) => {
173 if (values.useNetworkSeed && errors.networkSeed) {
178 const validate = useMemo(
182 useNetworkSeed: buildCheckboxValidator(),
183 networkSeed: buildHexValidator(64), // 64 chars
184 useDiscontinuity: buildCheckboxValidator(),
191 const onValues = useCallback(
192 ({ valid, values, form }) => {
195 values.useNetworkSeed ? values.networkSeed : undefined,
196 values.useDiscontinuity
198 setBreach(values.useDiscontinuity);
199 setManualNetworkSeed(values.useNetworkSeed);
204 if (!values.useNetworkSeed && values.networkSeed) {
205 form.change('networkSeed', '');
207 setManualNetworkSeed('');
210 [construct, unconstruct, setManualNetworkSeed, setBreach]
213 const setNetworkingKeys = useCallback(async () => {
215 console.log(breach, manualNetworkSeed);
217 const txHash = await configureNetworkingKeys({
221 // TODO: just use the tx hash instead?
222 getPendingTransactions(point);
223 // TODO: this is just so we get visual feedback that the tx has gone through
224 // but this should be addressed with the new UI flow (-> download keyfile)
226 // A question here is how to deal with the modals/messages about the keys not being
227 // set since they are in pending in the Roller...
228 // we could inspect if there's a changeKeys in the local list of pending txs,...
234 // setLoading(false);
239 getPendingTransactions,
242 configureNetworkingKeys,
245 const initialValues = useMemo(
247 useNetworkSeed: false,
248 useDiscontinuity: false,
253 const viewTitle = completed
254 ? 'Network Keys are now set'
255 : 'Set Networking Keys';
258 'You need this keyfile to authenticate with your OS. Please Download.';
263 <Row className="header-row">
269 <Grid.Item className="gray4 f5 mv2 align-self-start" full>
270 Network Keys authenticate Urbit ID with Urbit OS.
278 as={DownloadKeyfileButton}
283 <Grid.Item full as={AlertBox} className="mb8">
291 initialValues={initialValues}>
299 name="useNetworkSeed"
300 label="Use Custom Network Seed"
301 inverseLabel="Back to Derived Network Seed"
302 disabled={inputsLocked}
304 <Condition when="useNetworkSeed" is={true}>
305 <Grid.Item full as={NoticeBox} className="mb2">
306 When using a custom network seed, you'll need to download
307 your Arvo keyfile immediately after this transaction as
308 Bridge does not store your seed.
314 label="Network Seed (32 bytes)"
315 disabled={inputsLocked}
322 name="useDiscontinuity"
323 label="Breach Continuity"
324 disabled={inputsLocked}
328 <Grid.Item full as={FormError} />
332 {currentL2 && !currentL2Spawn ? (
339 onClick={setNetworkingKeys}>
345 as={InlineEthereumTransaction}
347 label={`${hasKeys ? 'Reset' : 'Set'} Networking Keys`}
348 onReturn={() => pop()}
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