1import { useCallback, useMemo, useEffect, useState } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import saveAs from 'file-saver';
4import ob from 'urbit-ob';
6import { usePointCache } from 'store/pointCache';
7import { useWallet } from 'store/wallet';
8import { usePointCursor } from 'store/pointCursor';
10import * as need from 'lib/need';
11import { generateCode } from 'lib/networkCode';
14 attemptNetworkSeedDerivation,
19import useCurrentPermissions from './useCurrentPermissions';
20import { convertToInt } from './convertToInt';
22export default function useKeyfileGenerator(manualNetworkSeed) {
23 const { urbitWallet, wallet, authMnemonic, authToken } = useWallet();
24 const { pointCursor } = usePointCursor();
25 const { getDetails } = usePointCache();
27 const [notice, setNotice] = useState('Deriving networking keys...');
28 const [downloaded, setDownloaded] = useState(false);
29 const [generating, setGenerating] = useState(true);
30 const [keyfile, setKeyfile] = useState(false);
32 const [code, setCode] = useState(false);
34 const _point = need.point(pointCursor);
35 const details = getDetails(_point);
37 const networkRevision = details.matchWith({
38 Just: ({ value }) => convertToInt(value.keyRevisionNumber, 10),
41 const { isOwner, isManagementProxy } = useCurrentPermissions();
43 const hasNetworkingKeys = networkRevision > 0;
45 (isOwner || isManagementProxy) && hasNetworkingKeys && !!keyfile;
47 const generate = useCallback(async () => {
48 if (!hasNetworkingKeys) {
50 setNotice('Networking keys not yet set.');
52 `no networking keys available for revision ${networkRevision}`
57 const _details = need.details(details);
59 const networkSeed = manualNetworkSeed
60 ? Just(manualNetworkSeed)
61 : await attemptNetworkSeedDerivation({
68 revision: networkRevision,
71 if (Nothing.hasInstance(networkSeed)) {
74 'Custom or nondeterministic networking keys cannot be re-downloaded.'
76 console.log(`seed is nondeterminable for revision ${networkRevision}`);
80 const _networkSeed = networkSeed.value;
82 const pair = deriveNetworkKeys(_networkSeed);
84 if (!keysMatchChain(pair, _details)) {
86 setNotice('Derived networking keys do not match on-chain details.');
87 console.log(`keys do not match details for revision ${networkRevision}`);
92 setCode(generateCode(pair));
93 setKeyfile(compileNetworkingKey(pair, _point, networkRevision));
108 const filename = useMemo(() => {
109 return `${ob.patp(_point).slice(1)}-${networkRevision}.key`;
110 // TODO: ^ unifiy "remove tilde" calls
111 }, [_point, networkRevision]);
113 const download = useCallback(() => {
115 new Blob([keyfile], {
116 type: 'text/plain;charset=utf-8',
121 }, [filename, keyfile]);
138 return { ...values, bind: values };
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