1import { useCallback, 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 } = 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 = need.details(getDetails(_point));
37 const networkRevision = convertToInt(_details.keyRevisionNumber, 10);
38 const { isOwner, isManagementProxy } = useCurrentPermissions();
40 const hasNetworkingKeys = networkRevision > 0;
42 (isOwner || isManagementProxy) && hasNetworkingKeys && !!keyfile;
44 const generate = useCallback(async () => {
45 if (!hasNetworkingKeys) {
47 setNotice('Networking keys not yet set.');
49 `no networking keys available for revision ${networkRevision}`
54 const networkSeed = manualNetworkSeed
55 ? Just(manualNetworkSeed)
56 : await attemptNetworkSeedDerivation({
61 revision: networkRevision,
64 if (Nothing.hasInstance(networkSeed)) {
67 'Custom or nondeterministic networking keys cannot be re-downloaded.'
69 console.log(`seed is nondeterminable for revision ${networkRevision}`);
73 const _networkSeed = networkSeed.value;
75 const pair = deriveNetworkKeys(_networkSeed);
77 setCode(generateCode(pair));
79 if (!keysMatchChain(pair, _details)) {
81 setNotice('Derived networking keys do not match on-chain details.');
82 console.log(`keys do not match details for revision ${networkRevision}`);
87 setKeyfile(compileNetworkingKey(pair, _point, networkRevision));
101 const download = useCallback(() => {
103 new Blob([keyfile], {
104 type: 'text/plain;charset=utf-8',
106 `${ob.patp(_point).slice(1)}-${networkRevision}.key`
107 // TODO: ^ unifiy "remove tilde" calls
110 }, [_point, keyfile, networkRevision]);
125 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