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 {
31export default function useKeyfileGenerator({
34}: useKeyfileGeneratorArgs) {
35 const { urbitWallet, wallet, authMnemonic, authToken }: any = useWallet();
36 const { pointCursor }: any = usePointCursor();
37 const { syncDetails, getDetails }: any = usePointCache();
39 const [notice, setNotice] = useState('Deriving networking keys...');
40 const [downloaded, setDownloaded] = useState(false);
41 const [generating, setGenerating] = useState(true);
42 const [keyfile, setKeyfile] = useState<boolean | string>(false);
43 const [code, setCode] = useState(false);
45 const _point = point || need.point(pointCursor);
48 }, [_point, syncDetails]);
50 const details = getDetails(_point);
52 const networkRevision = details.matchWith({
53 Just: ({ value }) => convertToInt(value.keyRevisionNumber, 10),
56 const { isOwner, isManagementProxy } = useCurrentPermissions();
58 const hasNetworkKeys = networkRevision > 0;
60 (isOwner || isManagementProxy) && hasNetworkKeys && !!keyfile;
62 const generate = useCallback(async () => {
63 if (!hasNetworkKeys) {
65 setNotice('Network keys not yet set.');
67 `no networking keys available for revision ${networkRevision}`
72 const _details = need.details(details);
74 const networkSeed = seed
76 : await attemptNetworkSeedDerivation({
83 revision: networkRevision,
86 if (Nothing.hasInstance(networkSeed)) {
89 'Custom or nondeterministic networking keys cannot be re-downloaded.'
91 console.log(`seed is nondeterminable for revision ${networkRevision}`);
95 const _networkSeed = networkSeed.value;
97 const pair = deriveNetworkKeys(_networkSeed);
99 if (!keysMatchChain(pair, _details)) {
100 setGenerating(false);
101 setNotice('Derived networking keys do not match on-chain details.');
102 console.log(`keys do not match details for revision ${networkRevision}`);
107 setCode(generateCode(pair));
108 setKeyfile(compileNetworkKey(pair, _point, networkRevision));
109 setGenerating(false);
123 const filename = useMemo(() => {
124 return `${stripSigPrefix(ob.patp(_point))}-${networkRevision}.key`;
125 }, [_point, networkRevision]);
127 const download = useCallback(() => {
128 if (typeof keyfile !== 'string') {
133 new Blob([keyfile], {
134 type: 'text/plain;charset=utf-8',
139 }, [filename, keyfile]);
156 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