on ee83871 help sign in
bridge/src/lib/useKeyfileGenerator.js
131 lines 3.6 kB history raw
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';
13import {
14 attemptNetworkSeedDerivation,
15 keysMatchChain,
16 deriveNetworkKeys,
17 compileNetworkingKey,
18} from './keys';
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 = getDetails(_point);
37 const networkRevision = details.matchWith({
38 Just: ({ value }) => convertToInt(value.keyRevisionNumber, 10),
39 Nothing: () => 0,
40 });
41 const { isOwner, isManagementProxy } = useCurrentPermissions();
43 const hasNetworkingKeys = networkRevision > 0;
44 const available =
45 (isOwner || isManagementProxy) && hasNetworkingKeys && !!keyfile;
47 const generate = useCallback(async () => {
48 if (!hasNetworkingKeys) {
49 setGenerating(false);
50 setNotice('Networking keys not yet set.');
51 console.log(
52 `no networking keys available for revision ${networkRevision}`
53 );
54 return;
55 }
57 const _details = need.details(details);
59 const networkSeed = manualNetworkSeed
60 ? Just(manualNetworkSeed)
61 : await attemptNetworkSeedDerivation({
62 urbitWallet,
63 wallet,
64 authMnemonic,
65 details: _details,
66 revision: networkRevision,
67 });
69 if (Nothing.hasInstance(networkSeed)) {
70 setGenerating(false);
71 setNotice(
72 'Custom or nondeterministic networking keys cannot be re-downloaded.'
73 );
74 console.log(`seed is nondeterminable for revision ${networkRevision}`);
75 return;
76 }
78 const _networkSeed = networkSeed.value;
80 const pair = deriveNetworkKeys(_networkSeed);
82 setCode(generateCode(pair));
84 if (!keysMatchChain(pair, _details)) {
85 setGenerating(false);
86 setNotice('Derived networking keys do not match on-chain details.');
87 console.log(`keys do not match details for revision ${networkRevision}`);
88 return;
89 }
91 setNotice();
92 setKeyfile(compileNetworkingKey(pair, _point, networkRevision));
93 setGenerating(false);
94 }, [
95 networkRevision,
96 hasNetworkingKeys,
97 manualNetworkSeed,
98 urbitWallet,
99 wallet,
100 authMnemonic,
101 setCode,
102 details,
103 _point,
104 ]);
106 const download = useCallback(() => {
107 saveAs(
108 new Blob([keyfile], {
109 type: 'text/plain;charset=utf-8',
110 }),
111 `${ob.patp(_point).slice(1)}-${networkRevision}.key`
112 // TODO: ^ unifiy "remove tilde" calls
113 );
114 setDownloaded(true);
115 }, [_point, keyfile, networkRevision]);
117 useEffect(() => {
118 generate();
119 }, [generate]);
121 const values = {
122 generating,
123 available,
124 downloaded,
125 download,
126 notice,
127 code,
128 };
130 return { ...values, bind: values };