on master help sign in
reticket: add support for L2
commit6b5f7eb6a19044253b5c5d29f749bd211c99a104 authoryosoyubik <yosoyubik@gmail.com> dateWed 29 Sep 2021, 12:49 (5 years ago) parent2d3487f
5 files changed+121−69
src/lib/useRoller.ts+45−22 src/lib/utils/roller.ts+25−23 src/store/roller.ts+12−7 src/views/Activate/MasterKeyTransfer.tsx+8−3 src/views/UrbitID/ResetKeys/ResetExecute.js+31−14
src/lib/useRoller.ts+45−22
@@ -80,6 +80,7 @@ export default function useRoller() {
8080 currentL2,
8181 nonces,
8282 increaseNonce,
83+ setNonce,
8384 } = useRollerStore();
8485 const [config, setConfig] = useState<Config | null>(null);
8586
@@ -463,7 +464,7 @@ export default function useRoller() {
463464 return pendingTx;
464465 };
465466
466− interface AcceptInviteParams {
467+ interface ReticketParams {
467468 point: Ship;
468469 to: EthAddress;
469470 manager: EthAddress;
@@ -471,25 +472,30 @@ export default function useRoller() {
471472 toWallet: any; // TODO: wallet type
472473 }
473474
474− const acceptInvite = async ({
475+ const performL2Reticket = async ({
475476 point,
476477 to,
477478 manager,
478479 fromWallet,
479480 toWallet,
480− }: AcceptInviteParams) => {
481+ }: ReticketParams) => {
482+ let pointDetails = nonces[point];
481483 const azimuthPoint = await api.getPoint(point);
482− console.log('azimuthPoint', azimuthPoint);
483− const ownerAddress = azimuthPoint?.ownership?.owner?.address!;
484484
485− const operator =
486− azimuthPoint.ownership &&
487− getOwnerNonce(azimuthPoint.ownership, ownerAddress);
485+ console.log(pointDetails);
486+ if (pointDetails === undefined) {
487+ // This is an invite acceptance
488+ pointDetails = azimuthPoint.ownership!;
489+ console.log(pointDetails);
490+ }
491+ const ownerAddress = pointDetails.owner?.address!;
492+ const operator = pointDetails && getOwnerNonce(pointDetails, ownerAddress);
488493
489494 if (operator === undefined)
490495 throw new Error("Error: Address doesn't match proxy");
491496
492− const nonce = operator.nonce!;
497+ let nonce = operator.nonce!;
498+ let requests = [];
493499
494500 // 1. Update networking keys
495501 const networkRevision = convertToInt(azimuthPoint.network.keys.life, 10);
@@ -506,36 +512,53 @@ export default function useRoller() {
506512 nonce,
507513 networkSeed
508514 );
509−
510− // TODO: not needed? next time we operate with this point we will login, and get
511− // the latest nonce (updated with any nonce increase from possible pending txs)
515+ nonce = nonce + 1;
512516 // increaseNonce(point, operator.proxy!);
517+ requests.push(configureKeysRequest);
513518
514519 // 2. Set Management Proxy
515− const registerProxyAddressRequest = await registerProxyAddress(
520+ const registerMgmtRequest = await registerProxyAddress(
516521 api,
517522 fromWallet,
518523 point,
519524 operator.proxy!,
520525 'manage',
521− nonce + 1,
526+ nonce,
522527 manager
523528 );
529+ nonce = nonce + 1;
530+ // increaseNonce(point, operator.proxy!);
531+ requests.push(registerMgmtRequest);
524532
525− // 3. Transfer point
533+ // 3. Set Spawn Proxy
534+ if (!isPlanet(Number(point))) {
535+ const registerSpawnRequest = await registerProxyAddress(
536+ api,
537+ fromWallet,
538+ point,
539+ operator.proxy!,
540+ 'spawn',
541+ nonce,
542+ toWallet.spawn.keys.address
543+ );
544+ nonce = nonce + 1;
545+ // increaseNonce(point, operator.proxy!);
546+ requests.push(registerSpawnRequest);
547+ }
548+ // 4. Transfer point
526549 const transferTxRequest = await transferPointRequest(
527550 api,
528551 fromWallet,
529552 point,
530553 operator.proxy!,
531− nonce + 2,
554+ nonce,
532555 to
533556 );
534− const hashes = await Promise.all([
535− configureKeysRequest,
536− registerProxyAddressRequest,
537− transferTxRequest,
538− ]);
557+ // increaseNonce(point, operator.proxy!);
558+ setNonce(point, pointDetails, operator.proxy!, nonce);
559+ requests.push(transferTxRequest);
560+
561+ const hashes = await Promise.all(requests);
539562 return hashes;
540563 };
541564
@@ -636,7 +659,7 @@ export default function useRoller() {
636659
637660 return {
638661 api,
639− acceptInvite,
662+ performL2Reticket,
640663 config,
641664 getPoints,
642665 getInvites,
src/lib/utils/roller.ts+25−23
@@ -23,6 +23,9 @@ export const HOUR = MINUTE * 60;
2323export const padZero = (amount: number) =>
2424 `${amount < 10 && amount > 0 ? '0' : ''}${amount}`;
2525
26+export const hasPoint = (point: number) => (invite: Invite) =>
27+ invite.planet === point;
28+
2629export const getTimeToNextBatch = (nextBatch: number, now: number) => {
2730 const toNext = nextBatch - now;
2831 const hours = Math.floor(toNext / HOUR);
@@ -98,7 +101,7 @@ export const configureKeys = async (
98101 encrypt: addHexPrefix(pair.crypt.public),
99102 auth: addHexPrefix(pair.auth.public),
100103 cryptoSuite: String(CRYPTO_SUITE_VERSION),
101− breach,
104+ breach: breach || false,
102105 };
103106
104107 const hash = await api.hashTransaction(nonce, from, 'configureKeys', data);
@@ -142,6 +145,26 @@ const proxyType = (proxy: Proxy) => {
142145 }
143146};
144147
148+const setProxy = async (
149+ api: any,
150+ proxyAddressType: string,
151+ sig: Signature,
152+ from: From,
153+ address: EthAddress,
154+ data: AddressParams
155+) => {
156+ switch (proxyAddressType) {
157+ case 'manage':
158+ return await api.setManagementProxy(sig, from, address, data);
159+ case 'spawn':
160+ return await api.setSpawnProxy(sig, from, address, data);
161+ case 'transfer':
162+ return await api.setTrasferProxy(sig, from, address, data);
163+ default:
164+ throw new Error(`Unknown proxyType ${proxyAddressType}`);
165+ }
166+};
167+
145168export const registerProxyAddress = async (
146169 api: any,
147170 _wallet: any,
@@ -172,25 +195,4 @@ export const registerProxyAddress = async (
172195 );
173196};
174197
175−const setProxy = async (
176− api: any,
177− proxyAddressType: string,
178− sig: Signature,
179− from: From,
180− address: EthAddress,
181− data: AddressParams
182−) => {
183− switch (proxyAddressType) {
184− case 'manage':
185− return await api.setManagementProxy(sig, from, address, data);
186− case 'spawn':
187− return await api.setSpawnProxy(sig, from, address, data);
188− case 'transfer':
189− return await api.setTrasferProxy(sig, from, address, data);
190− default:
191− throw new Error(`Unknown proxyType ${proxyAddressType}`);
192− }
193−};
194−
195−export const hasPoint = (point: number) => (invite: Invite) =>
196− invite.planet === point;
198+export const reticketL2Point = async () => {};
src/store/roller.ts+12−7
@@ -1,5 +1,10 @@
11import create from 'zustand';
2−import { L2Point, PendingTransaction, Ownership } from '@urbit/roller-api';
2+import {
3+ L2Point,
4+ PendingTransaction,
5+ Ownership,
6+ Ship,
7+} from '@urbit/roller-api';
38
49import { HOUR, isL2, isL2Spawn } from 'lib/utils/roller';
510import { increaseProxyNonce, setProxyNonce } from 'lib/utils/nonce';
@@ -13,11 +18,11 @@ export interface RollerStore {
1318 currentL2: boolean;
1419 currentL2Spawn: boolean;
1520 nonces: {
16− [point: number]: Ownership;
21+ [point: Ship]: Ownership;
1722 };
18− increaseNonce: (point: number, proxy: string) => void;
23+ increaseNonce: (point: Ship, proxy: string) => void;
1924 setNonce: (
20− point: number,
25+ point: Ship,
2126 owner: Ownership,
2227 proxy: string,
2328 nonce: number
@@ -42,14 +47,14 @@ export const useRollerStore = create<RollerStore>(set => ({
4247 invites: [],
4348 recentlyCompleted: 0,
4449 nonces: {},
45− increaseNonce: (point: number, proxy: string) =>
50+ increaseNonce: (point: Ship, proxy: string) =>
4651 set(state => {
4752 const owner = increaseProxyNonce(state.nonces[point], proxy);
4853 console.log(point, proxy, owner);
4954 if (!owner) throw new Error("Can't increase nonce for this proxy");
5055 return { ...state, nonces: { ...state.nonces, [point]: owner } };
5156 }),
52− setNonce: (point: number, owner: Ownership, proxy: string, nonce: number) =>
57+ setNonce: (point: Ship, owner: Ownership, proxy: string, nonce: number) =>
5358 set(state => {
5459 const updatedOwner = setProxyNonce(owner, proxy, nonce);
5560 if (!updatedOwner) throw new Error("Can't set nonce for this proxy");
@@ -58,7 +63,7 @@ export const useRollerStore = create<RollerStore>(set => ({
5863 nonces: { ...state.nonces, [point]: updatedOwner },
5964 };
6065 }),
61− setNonces: (point: number, owner: Ownership) =>
66+ setNonces: (point: Ship, owner: Ownership) =>
6267 set(state => {
6368 return { ...state, nonces: { ...state.nonces, [point]: owner } };
6469 }),
src/views/Activate/MasterKeyTransfer.tsx+8−3
@@ -14,7 +14,7 @@ import DangerBox from './DangerBox';
1414import View from 'components/View';
1515
1616const MasterKeyTransfer = () => {
17− const { acceptInvite } = useRoller();
17+ const { performL2Reticket } = useRoller();
1818 const {
1919 derivedPatp,
2020 derivedPoint,
@@ -27,7 +27,7 @@ const MasterKeyTransfer = () => {
2727 setError(undefined);
2828
2929 try {
30− await acceptInvite({
30+ await performL2Reticket({
3131 point: derivedPoint.value,
3232 to: derivedWallet.value.ownership.keys.address,
3333 manager: derivedWallet.value.management.keys.address,
@@ -38,7 +38,12 @@ const MasterKeyTransfer = () => {
3838 console.error(error);
3939 setError(error);
4040 }
41− }, [derivedWallet, acceptInvite, derivedPoint.value, inviteWallet.value]);
41+ }, [
42+ derivedWallet,
43+ performL2Reticket,
44+ derivedPoint.value,
45+ inviteWallet.value,
46+ ]);
4247
4348 useFadeIn();
4449
src/views/UrbitID/ResetKeys/ResetExecute.js+31−14
@@ -16,7 +16,9 @@ import { useWallet } from 'store/wallet';
1616import { usePointCursor } from 'store/pointCursor';
1717import { usePointCache } from 'store/pointCache';
1818import { useHistory } from 'store/history';
19+import { useRollerStore } from 'store/roller';
1920
21+import useRoller from 'lib/useRoller';
2022import { useWalletConnect } from 'lib/useWalletConnect';
2123
2224import { RestartButton, ForwardButton } from 'components/Buttons';
@@ -53,6 +55,8 @@ export default function ResetExecute({ newWallet, setNewWallet }) {
5355 walletType,
5456 walletHdPath,
5557 } = useWallet();
58+ const { performL2Reticket } = useRoller();
59+ const { currentL2 } = useRollerStore();
5660 const { pointCursor } = usePointCursor();
5761 const { getDetails } = usePointCache();
5862 const {
@@ -92,20 +96,33 @@ export default function ResetExecute({ newWallet, setNewWallet }) {
9296 walletType === WALLET_TYPES.WALLET_CONNECT ? wcSend : undefined;
9397
9498 try {
95− await reticketPointBetweenWallets({
96− fromWallet: need.wallet(wallet),
97− fromWalletType: walletType,
98− fromWalletHdPath: walletHdPath,
99− toWallet: newWallet.value.wallet,
100− point: point,
101− web3: need.web3(web3),
102− contracts: need.contracts(contracts),
103− networkType,
104− onUpdate: handleUpdate,
105− nextRevision: networkRevision + 1,
106− txnSigner,
107− txnSender,
108− });
99+ if (currentL2) {
100+ console.log(newWallet.value);
101+ // FIXME: the useEffect is called twice, which is fine since the duplicate
102+ // L2 txs will be discarded
103+ //
104+ await performL2Reticket({
105+ point,
106+ to: newWallet.value.wallet.ownership.keys.address,
107+ manager: newWallet.value.wallet.management.keys.address,
108+ toWallet: newWallet.value.wallet,
109+ fromWallet: need.wallet(wallet),
110+ });
111+ } else
112+ await reticketPointBetweenWallets({
113+ fromWallet: need.wallet(wallet),
114+ fromWalletType: walletType,
115+ fromWalletHdPath: walletHdPath,
116+ toWallet: newWallet.value.wallet,
117+ point: point,
118+ web3: need.web3(web3),
119+ contracts: need.contracts(contracts),
120+ networkType,
121+ onUpdate: handleUpdate,
122+ nextRevision: networkRevision + 1,
123+ txnSigner,
124+ txnSender,
125+ });
109126 } catch (err) {
110127 console.error(err);
111128 setGeneralError(err);