1import { useCallback, useMemo, useEffect, useState } from 'react';
2import { Just } from 'folktale/maybe';
3import saveAs from 'file-saver';
4import { useWallet } from 'store/wallet';
5import { EMPTY_POINT, useRollerStore } from 'store/rollerStore';
6import { generateCode } from 'lib/networkCode';
8 attemptNetworkSeedDerivation,
9 deriveNetworkSeedFromUrbitWallet,
14import { stripSigPrefix } from 'form/formatters';
15import useRoller from './useRoller';
16import Point from './types/Point';
17import { toL1Details } from './utils/point';
18import { ETH_ZERO_ADDR } from './constants';
20interface useKeyfileGeneratorArgs {
26 * The default Keyfile Generator. Can be used by itself, or composed with
27 * other hooks (see `useActivationKeyfileGenerator` and
28 * `useSingleKeyfileGenerator`).
30 * @param point Point - (optional) the Point for which to generate keys
31 * @param seeds string[] - (required) one or more seeds consumed by deriveNetworkKeys
34function useKeyfileGenerator({ point, seeds }: useKeyfileGeneratorArgs) {
35 // exported keyfile generator properties
36 const [notice, setNotice] = useState('Deriving networking keys...');
37 const [downloaded, setDownloaded] = useState(false);
38 const [generating, setGenerating] = useState(true);
39 const [keyfile, setKeyfile] = useState<boolean | string>(false);
40 const [code, setCode] = useState(false);
43 const { point: storedPoint } = useRollerStore();
44 const resolvedPoint = useMemo(() => {
45 return point || storedPoint;
46 }, [point, storedPoint]);
48 const networkRevision = useMemo(() => {
49 return Number(resolvedPoint.keyRevisionNumber);
51 const canManage = useMemo(() => {
52 return resolvedPoint.isOwner || resolvedPoint.isManagementProxy;
55 const hasNetworkKeys = networkRevision > 0;
56 const available = canManage && hasNetworkKeys && !!keyfile;
58 const generate = useCallback(async () => {
59 // Point still loading ...
60 if (resolvedPoint.value === EMPTY_POINT.value) {
64 if (!hasNetworkKeys) {
66 setNotice('Network keys not yet set.');
68 `no networking keys available for revision ${networkRevision}`
73 // seeds were either generated non-deterministically,
74 // or still being derived ...
75 if (seeds.length === 0) {
78 'Custom or nondeterministic networking keys cannot be re-downloaded.'
80 console.log(`seed is nondeterminable for revision ${networkRevision}`);
84 const pairs = seeds.map(seed => deriveNetworkKeys(seed));
85 const firstPair = pairs[0];
87 if (!keysMatchChain(firstPair, resolvedPoint)) {
89 setNotice('Derived networking keys do not match on-chain details.');
90 console.log(`keys do not match details for revision ${networkRevision}`);
95 setCode(generateCode(firstPair));
99 resolvedPoint.continuityNumber,
100 pairs.map((pair, k) => {
101 return { revision: networkRevision + k, pair };
105 setGenerating(false);
106 }, [hasNetworkKeys, seeds, resolvedPoint, networkRevision]);
108 const filename = useMemo(() => {
109 return `${stripSigPrefix(resolvedPoint.patp)}-${networkRevision}.key`;
110 }, [resolvedPoint, networkRevision]);
112 const download = useCallback(() => {
113 if (typeof keyfile !== 'string') {
118 new Blob([keyfile], {
119 type: 'text/plain;charset=utf-8',
124 }, [filename, keyfile]);
141 return { ...values, bind: values };
144interface ActivationKeyfileGeneratorArgs {
146 wallets: UrbitWallet[];
150 * This Activation Keyfile Generator is used to derive a multikey during the
151 * Activation Flow. At the time of generation, there is no Point yet set in
152 * the Roller Store, so we need to look it up by Point ID (to retreive the
153 * current key revision via API).
155 * @param pointId number - (required) the point ID for which to generate keys
156 * @param wallets UrbitWallet[] - (required) for each wallet,
157 * a seed + revision pair is generated and consumed by `compileMultiKey`.
158 * It should be an array of [deterministic invite wallet, non-deterministic
159 * master ticket wallet] (the sending and receiving wallets)
161export const useActivationKeyfileGenerator = ({
164}: ActivationKeyfileGeneratorArgs) => {
166 const { api } = useRoller();
167 const [point, setPoint] = useState<Point>(EMPTY_POINT);
169 const resolvePoint = useCallback(async () => {
170 const resolved = new Point({
172 details: toL1Details(await api.getPoint(pointId)),
173 address: ETH_ZERO_ADDR,
183 // derive seeds from wallets
184 const [seeds, setSeeds] = useState<string[]>([]);
185 const deriveSeeds = useCallback(async () => {
186 if (!('keyRevisionNumber' in point)) {
190 const derivedSeeds = await Promise.all(
191 wallets.map(async (w, i) => {
192 return deriveNetworkSeedFromUrbitWallet(w, point.keyRevisionNumber + i);
198 .map(maybeSeed => maybeSeed.getOrElse(null))
199 .filter(s => s !== null)
201 }, [point, wallets]);
205 // eslint-disable-next-line react-hooks/exhaustive-deps
208 return useKeyfileGenerator({ point, seeds });
211interface SingleKeyfileGeneratorArgs {
216 * This Single Keyfile Generator is used to derive network keys in multiple
217 * views in the Bridge dashboard (Set Network Keys, Home > ID, Home > OS).
218 * By default, it uses the logged in wallet's seed phrase.
220 * * @param seed string - (optional) default: provided by the logged in wallet
222export const useSingleKeyfileGenerator = ({
224}: SingleKeyfileGeneratorArgs) => {
225 // use provided seed, or fallback to deriving from logged in wallet
226 const { point } = useRollerStore();
227 const { authMnemonic, authToken, urbitWallet, wallet }: any = useWallet();
228 const [seeds, setSeeds] = useState<string[]>([]);
230 const deriveSeeds = useCallback(async () => {
236 const derivedSeed = await attemptNetworkSeedDerivation({
243 revision: point.keyRevisionNumber,
246 if (Just.hasInstance(derivedSeed)) {
247 setSeeds([derivedSeed.value]);
249 }, [authMnemonic, authToken, point, seed, urbitWallet, wallet]);
255 return useKeyfileGenerator({ seeds });
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