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';
21import { stripSigPrefix } from 'form/formatters';
23// overrideable defaults:
24// seed - derived from wallet, mnemonic, or token (if posisble)
25// point - the current pointCursor
26interface useKeyfileGeneratorArgs {
33 * Use `useMultikeyFileGenerator` instead
35export default function useKeyfileGenerator({
38}: useKeyfileGeneratorArgs) {
39 const { urbitWallet, wallet, authMnemonic, authToken }: any = useWallet();
40 const { pointCursor }: any = usePointCursor();
41 const { syncDetails, getDetails }: any = usePointCache();
43 const [notice, setNotice] = useState('Deriving networking keys...');
44 const [downloaded, setDownloaded] = useState(false);
45 const [generating, setGenerating] = useState(true);
46 const [keyfile, setKeyfile] = useState<boolean | string>(false);
47 const [code, setCode] = useState(false);
49 const _point = point || need.point(pointCursor);
52 }, [_point, syncDetails]);
54 const details = getDetails(_point);
56 const networkRevision = details.matchWith({
57 Just: ({ value }) => convertToInt(value.keyRevisionNumber, 10),
60 const { isOwner, isManagementProxy } = useCurrentPermissions();
62 const hasNetworkKeys = networkRevision > 0;
64 (isOwner || isManagementProxy) && hasNetworkKeys && !!keyfile;
66 const generate = useCallback(async () => {
67 if (!hasNetworkKeys) {
69 setNotice('Network keys not yet set.');
71 `no networking keys available for revision ${networkRevision}`
76 const _details = need.details(details);
78 const networkSeed = seed
80 : await attemptNetworkSeedDerivation({
87 revision: networkRevision,
90 if (Nothing.hasInstance(networkSeed)) {
93 'Custom or nondeterministic networking keys cannot be re-downloaded.'
95 console.log(`seed is nondeterminable for revision ${networkRevision}`);
99 const _networkSeed = networkSeed.value;
101 const pair = deriveNetworkKeys(_networkSeed);
103 if (!keysMatchChain(pair, _details)) {
104 setGenerating(false);
105 setNotice('Derived networking keys do not match on-chain details.');
106 console.log(`keys do not match details for revision ${networkRevision}`);
111 setCode(generateCode(pair));
112 setKeyfile(compileNetworkKey(pair, _point, networkRevision));
113 setGenerating(false);
127 const filename = useMemo(() => {
128 return `${stripSigPrefix(ob.patp(_point))}-${networkRevision}.key`;
129 }, [_point, networkRevision]);
131 const download = useCallback(() => {
132 if (typeof keyfile !== 'string') {
137 new Blob([keyfile], {
138 type: 'text/plain;charset=utf-8',
143 }, [filename, keyfile]);
160 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