on master help sign in
wip set networking keys
commitdd13e790bc78e511dd4247f70e1b5ccecebf35e4 authoryosoyubik <yosoyubik@gmail.com> dateWed 29 Sep 2021, 05:51 (5 years ago) parent0bb5d7e
3 files changed+111−43
src/lib/useRoller.ts+46−29 src/lib/utils/roller.ts+8−3 src/views/UrbitOS/NetworkingKeys.js+57−11
src/lib/useRoller.ts+46−29
@@ -413,14 +413,47 @@ export default function useRoller() {
413413 ]
414414 );
415415
416− // TODO: extract from #transferPoint
417− // interface ConfigureKeysParams {
416+ interface ConfigureKeysParams {
417+ breach: boolean;
418+ customNetworkSeed?: string;
419+ }
418420
419− // }
421+ const configureNetworkingKeys = async ({
422+ breach,
423+ customNetworkSeed,
424+ }: ConfigureKeysParams) => {
425+ const _point = need.point(pointCursor);
426+ const _wallet = wallet.getOrElse(null);
427+ console.log(_point, _wallet);
428+ if (!_wallet) {
429+ // not using need because we want a custom error
430+ throw new Error('Internal Error: Missing Wallet');
431+ }
420432
421− // const configureKeys = async () => {
433+ const pointDetails = nonces[_point];
434+ const operator =
435+ getManagementNonce(pointDetails, _wallet.address) ||
436+ getOwnerNonce(pointDetails, _wallet.address);
422437
423− // }
438+ if (operator === undefined)
439+ throw new Error("Error: Address doesn't match proxy");
440+ const networkRevision = convertToInt(pointDetails.network.keys.life, 10);
441+ const nextRevision = networkRevision + 1;
442+ console.log('nextRevision', nextRevision);
443+ const txHash = await configureKeys(
444+ api,
445+ _wallet,
446+ _point,
447+ operator.proxy!,
448+ operator.nonce!,
449+ _wallet,
450+ nextRevision,
451+ customNetworkSeed
452+ );
453+ const pendingTx = await api.getPendingTx(txHash);
454+
455+ return pendingTx;
456+ };
424457
425458 interface AcceptInviteParams {
426459 point: Ship;
@@ -454,34 +487,17 @@ export default function useRoller() {
454487 // TODO: extract this to a useRoller#configureKeys function?
455488 const networkRevision = convertToInt(azimuthPoint.network.keys.life, 10);
456489 const nextRevision = networkRevision + 1;
457− const fromOwnerProxy: From = {
458− ship: point,
459− proxy: 'own',
460− };
461490
462− const seed = await deriveNetworkSeedFromUrbitWallet(toWallet, nextRevision);
463− const keys = deriveNetworkKeys(seed.value);
464− const keysData = {
465− auth: ensureHexPrefix(keys.auth.public),
466− breach: false,
467− cryptoSuite: CRYPTO_SUITE_VERSION.toString(),
468− encrypt: ensureHexPrefix(keys.crypt.public),
469− };
470−
471− const keysHash: Hash = await api.hashTransaction(
491+ const configureKeysRequest = await configureKeys(
492+ api,
493+ fromWallet,
494+ point,
495+ operator.proxy!,
472496 nonce,
473− fromOwnerProxy,
474− 'configureKeys',
475− keysData
497+ toWallet,
498+ nextRevision
476499 );
477500
478− const keysSigningKey = fromWallet.privateKey;
479− const configureKeysRequest = await api.configureKeys(
480− signTransactionHash(keysHash, Buffer.from(keysSigningKey, 'hex')),
481− fromOwnerProxy,
482− ownerAddress,
483− keysData
484− );
485501 // TODO: not needed? next time we operate with this point we will login, and get
486502 // the latest nonce (updated with any nonce increase from possible pending txs)
487503 // increaseNonce(point, operator.proxy!);
@@ -619,5 +635,6 @@ export default function useRoller() {
619635 generateInviteCodes,
620636 transferPoint,
621637 setProxyAddress,
638+ configureNetworkingKeys,
622639 };
623640}
src/lib/utils/roller.ts+8−3
@@ -75,17 +75,22 @@ export const spawn = async (
7575export const configureKeys = async (
7676 api: any,
7777 _wallet: any,
78− _point: number,
78+ _point: Ship,
7979 proxy: string,
8080 nonce: number,
81− urbitWallet: any
81+ urbitWallet: any,
82+ nextRevision?: number,
83+ customNetworkSeed?: string
8284) => {
8385 const from = {
8486 ship: _point, //ship to configure keys
8587 proxy,
8688 };
8789
88− const networkSeed = await deriveNetworkSeedFromUrbitWallet(urbitWallet, 1);
90+ const networkSeed =
91+ customNetworkSeed ||
92+ (await deriveNetworkSeedFromUrbitWallet(urbitWallet, nextRevision || 1));
93+
8994 // TODO: do something here?
9095 if (Nothing.hasInstance(networkSeed)) {
9196 console.log("Network key Error: couldn't derive network keys");
src/views/UrbitOS/NetworkingKeys.js+57−11
@@ -1,6 +1,6 @@
1−import React, { useCallback, useMemo, useRef } from 'react';
1+import React, { useCallback, useMemo, useRef, useState } from 'react';
22import { Just } from 'folktale/maybe';
3−import { Grid, ToggleInput, CheckboxInput } from 'indigo-react';
3+import { Grid, ToggleInput, CheckboxInput, Button } from 'indigo-react';
44import * as azimuth from 'azimuth-js';
55import { randomHex } from 'web3-utils';
66
@@ -8,7 +8,9 @@ import { usePointCursor } from 'store/pointCursor';
88import { usePointCache } from 'store/pointCache';
99import { useNetwork } from 'store/network';
1010import { useWallet } from 'store/wallet';
11+import { useRollerStore } from 'store/roller';
1112
13+import useRoller from 'lib/useRoller';
1214import { useLocalRouter } from 'lib/LocalRouter';
1315import * as need from 'lib/need';
1416import {
@@ -50,7 +52,7 @@ function useSetKeys(manualNetworkSeed, setManualNetworkSeed) {
5052
5153 const networkRevision = convertToInt(_details.keyRevisionNumber, 10);
5254 const randomSeed = useRef();
53−
55+ console.log(manualNetworkSeed);
5456 const {
5557 available: keyfileAvailable,
5658 generating: keyfileGenerating,
@@ -141,6 +143,9 @@ export default function UrbitOSNetworkingKeys({
141143 const { pop } = useLocalRouter();
142144 const { pointCursor } = usePointCursor();
143145 const { getDetails } = usePointCache();
146+ const { currentL2 } = useRollerStore();
147+ const { configureNetworkingKeys, getPendingTransactions } = useRoller();
148+ const [breach, setBreach] = useState(false);
144149
145150 const point = need.point(pointCursor);
146151 const details = getDetails(point);
@@ -187,17 +192,46 @@ export default function UrbitOSNetworkingKeys({
187192 values.useNetworkSeed ? values.networkSeed : undefined,
188193 values.useDiscontinuity
189194 );
195+ setBreach(values.useDiscontinuity);
196+ setManualNetworkSeed(values.useNetworkSeed);
190197 } else {
191198 unconstruct();
192199 }
193200
194201 if (!values.useNetworkSeed && values.networkSeed) {
195202 form.change('networkSeed', '');
203+ setBreach(false);
204+ setManualNetworkSeed('');
196205 }
197206 },
198− [construct, unconstruct]
207+ [construct, unconstruct, setManualNetworkSeed, setBreach]
199208 );
200209
210+ const setNetworkingKeys = useCallback(async () => {
211+ // setLoading(true);
212+ console.log(breach, manualNetworkSeed);
213+ try {
214+ const txHash = await configureNetworkingKeys({
215+ breach,
216+ manualNetworkSeed,
217+ });
218+ // TODO: just use the tx hash instead?
219+ getPendingTransactions(point);
220+ pop();
221+ } catch (error) {
222+ // setError(error);
223+ } finally {
224+ // setLoading(false);
225+ }
226+ }, [
227+ breach,
228+ manualNetworkSeed,
229+ getPendingTransactions,
230+ point,
231+ pop,
232+ configureNetworkingKeys,
233+ ]);
234+
201235 const initialValues = useMemo(
202236 () => ({
203237 useNetworkSeed: false,
@@ -280,13 +314,25 @@ export default function UrbitOSNetworkingKeys({
280314 </>
281315 )}
282316
283− <Grid.Item
284− full
285− as={InlineEthereumTransaction}
286− {...bind}
287− label={`${hasKeys ? 'Reset' : 'Set'} Networking Keys`}
288− onReturn={() => pop()}
289− />
317+ {currentL2 ? (
318+ <Grid.Item
319+ as={Button}
320+ full
321+ className=""
322+ center
323+ solid
324+ onClick={setNetworkingKeys}>
325+ {'Sign Transaction'}
326+ </Grid.Item>
327+ ) : (
328+ <Grid.Item
329+ full
330+ as={InlineEthereumTransaction}
331+ {...bind}
332+ label={`${hasKeys ? 'Reset' : 'Set'} Networking Keys`}
333+ onReturn={() => pop()}
334+ />
335+ )}
290336 </>
291337 )}
292338 </BridgeForm>