on master help sign in
api: retrieve latest nonce from Roller api
commitbaa23b621716004e0b1119ee4d23a11f6667eb86 authoryosoyubik <yosoyubik@gmail.com> dateWed 29 Sep 2021, 16:31 (5 years ago) parent6b5f7eb
6 files changed+71−282
src/lib/useRoller.ts+49−73 src/lib/utils/nonce.ts−157 src/lib/utils/proxy.ts+17 src/store/roller.ts+1−39 src/views/Point/Point.tsx+4−12 src/views/UrbitID/SetProxy.js−1
src/lib/useRoller.ts+49−73
@@ -22,12 +22,7 @@ import {
2222} from 'store/storage/roller';
2323import { usePointCache } from 'store/pointCache';
2424import { getOutgoingPoints } from 'views/Points';
25−import {
26− getSpawnNonce,
27− getOwnerNonce,
28− getManagementNonce,
29− getTransferNonce,
30−} from './utils/nonce';
25+import { getProxy } from './utils/proxy';
3126import { isPlanet } from './utils/point';
3227
3328import {
@@ -78,9 +73,6 @@ export default function useRoller() {
7873 setPendingTransactions,
7974 setInvites,
8075 currentL2,
81− nonces,
82− increaseNonce,
83− setNonce,
8476 } = useRollerStore();
8577 const [config, setConfig] = useState<Config | null>(null);
8678
@@ -158,12 +150,9 @@ export default function useRoller() {
158150 new Set<number>()
159151 );
160152
161− console.log(pendingTxs, pendingSpawns);
162−
163153 planets = planets.filter((point: number) => !pendingSpawns.has(point));
164154
165− const starInfo = nonces[_point];
166− console.log(starInfo);
155+ const starInfo = await api.getPoint(_point);
167156 const tickets: {
168157 ticket: string;
169158 planet: number;
@@ -171,16 +160,19 @@ export default function useRoller() {
171160 }[] = [];
172161 const requests: Promise<string>[] = [];
173162
174− const operator =
175− getSpawnNonce(starInfo, _wallet.address) ||
176− getOwnerNonce(starInfo, _wallet.address);
163+ const proxy = getProxy(starInfo.ownership!, _wallet.address);
177164
178− if (operator === undefined)
165+ if (proxy === undefined)
179166 throw new Error("Error: Address doesn't match proxy");
180− console.log('operator', operator);
167+
168+ const nonce = await api.getNonce({
169+ ship: _point,
170+ proxy: proxy,
171+ });
172+
181173 for (let i = 0; i < numInvites && planets[i]; i++) {
182174 const planet = planets[i];
183− const nonceInc = i + operator.nonce!;
175+ const nonceInc = i + nonce;
184176
185177 const { ticket, inviteWallet } = await generateInviteWallet(
186178 planet,
@@ -191,11 +183,10 @@ export default function useRoller() {
191183 api,
192184 _wallet,
193185 _point,
194− operator.proxy!,
186+ proxy,
195187 nonceInc,
196188 planet
197189 );
198− increaseNonce(_point, operator.proxy!);
199190
200191 const networkSeed = await deriveNetworkSeedFromUrbitWallet(
201192 inviteWallet,
@@ -205,7 +196,7 @@ export default function useRoller() {
205196 api,
206197 _wallet,
207198 planet,
208− operator.proxy!,
199+ proxy,
209200 0,
210201 networkSeed
211202 );
@@ -214,7 +205,7 @@ export default function useRoller() {
214205 api,
215206 _wallet,
216207 planet,
217− operator.proxy!,
208+ proxy,
218209 'manage',
219210 1,
220211 inviteWallet.management.keys.address
@@ -224,7 +215,7 @@ export default function useRoller() {
224215 api,
225216 _wallet,
226217 planet,
227− operator.proxy!,
218+ proxy,
228219 2,
229220 inviteWallet.ownership.keys.address
230221 );
@@ -250,7 +241,6 @@ export default function useRoller() {
250241 status: 'pending',
251242 }));
252243
253− console.log('latest update', nonces[_point]);
254244 setPendingInvites(_point, pendingInvites);
255245 },
256246 [
@@ -262,8 +252,6 @@ export default function useRoller() {
262252 web3,
263253 getDetails,
264254 authMnemonic,
265− nonces,
266− increaseNonce,
267255 ]
268256 );
269257
@@ -431,12 +419,17 @@ export default function useRoller() {
431419 throw new Error('Internal Error: Missing Wallet/Details');
432420 }
433421
434− const pointDetails = nonces[_point];
435− const operator =
436− getManagementNonce(pointDetails, _wallet.address) ||
437− getOwnerNonce(pointDetails, _wallet.address);
438− if (operator === undefined)
422+ const pointDetails = await api.getPoint(_point);
423+ const proxy = getProxy(pointDetails.ownership!, _wallet.address);
424+
425+ if (proxy === undefined)
439426 throw new Error("Error: Address doesn't match proxy");
427+
428+ const nonce = await api.getNonce({
429+ ship: _point,
430+ proxy: proxy,
431+ });
432+
440433 const networkRevision = convertToInt(azimuthPoint.network.keys.life, 10);
441434 const nextRevision = networkRevision + 1;
442435 const networkSeed = customNetworkSeed
@@ -454,8 +447,8 @@ export default function useRoller() {
454447 api,
455448 _wallet,
456449 _point,
457− operator.proxy!,
458− operator.nonce!,
450+ proxy!,
451+ nonce,
459452 networkSeed,
460453 breach
461454 );
@@ -479,22 +472,10 @@ export default function useRoller() {
479472 fromWallet,
480473 toWallet,
481474 }: ReticketParams) => {
482− let pointDetails = nonces[point];
483475 const azimuthPoint = await api.getPoint(point);
476+ const proxy = 'own';
477+ let nonce = await api.getNonce({ ship: point, proxy });
484478
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);
493−
494− if (operator === undefined)
495− throw new Error("Error: Address doesn't match proxy");
496−
497− let nonce = operator.nonce!;
498479 let requests = [];
499480
500481 // 1. Update networking keys
@@ -508,12 +489,11 @@ export default function useRoller() {
508489 api,
509490 fromWallet,
510491 point,
511− operator.proxy!,
492+ proxy,
512493 nonce,
513494 networkSeed
514495 );
515496 nonce = nonce + 1;
516− // increaseNonce(point, operator.proxy!);
517497 requests.push(configureKeysRequest);
518498
519499 // 2. Set Management Proxy
@@ -521,13 +501,12 @@ export default function useRoller() {
521501 api,
522502 fromWallet,
523503 point,
524− operator.proxy!,
504+ proxy,
525505 'manage',
526506 nonce,
527507 manager
528508 );
529509 nonce = nonce + 1;
530− // increaseNonce(point, operator.proxy!);
531510 requests.push(registerMgmtRequest);
532511
533512 // 3. Set Spawn Proxy
@@ -536,13 +515,12 @@ export default function useRoller() {
536515 api,
537516 fromWallet,
538517 point,
539− operator.proxy!,
518+ proxy,
540519 'spawn',
541520 nonce,
542521 toWallet.spawn.keys.address
543522 );
544523 nonce = nonce + 1;
545− // increaseNonce(point, operator.proxy!);
546524 requests.push(registerSpawnRequest);
547525 }
548526 // 4. Transfer point
@@ -550,12 +528,10 @@ export default function useRoller() {
550528 api,
551529 fromWallet,
552530 point,
553− operator.proxy!,
531+ proxy,
554532 nonce,
555533 to
556534 );
557− // increaseNonce(point, operator.proxy!);
558− setNonce(point, pointDetails, operator.proxy!, nonce);
559535 requests.push(transferTxRequest);
560536
561537 const hashes = await Promise.all(requests);
@@ -572,21 +548,21 @@ export default function useRoller() {
572548 throw new Error('Internal Error: Missing Wallet');
573549 }
574550
575− const pointDetails = nonces[_point];
576− const operator =
577− getManagementNonce(pointDetails, _wallet.address) ||
578− getOwnerNonce(pointDetails, _wallet.address);
551+ const pointDetails = await api.getPoint(_point);
552+ const proxy = getProxy(pointDetails.ownership!, _wallet.address);
579553
580− if (operator === undefined)
554+ if (proxy === undefined)
581555 throw new Error("Error: Address doesn't match proxy");
582556
557+ const nonce = await api.getNonce({ ship: _point, proxy });
558+
583559 const txHash = await registerProxyAddress(
584560 api,
585561 _wallet,
586562 _point,
587− operator.proxy!,
563+ proxy,
588564 proxyType,
589− operator.nonce!,
565+ nonce,
590566 address
591567 );
592568
@@ -594,7 +570,7 @@ export default function useRoller() {
594570
595571 return pendingTx;
596572 },
597− [api, pointCursor, wallet, nonces]
573+ [api, pointCursor, wallet]
598574 );
599575
600576 const transferPoint = useCallback(
@@ -607,20 +583,20 @@ export default function useRoller() {
607583 throw new Error('Internal Error: Missing Wallet');
608584 }
609585
610− const pointDetails = nonces[_point];
611− const operator =
612− getTransferNonce(pointDetails, _wallet.address) ||
613− getOwnerNonce(pointDetails, _wallet.address);
586+ const pointDetails = await api.getPoint(_point);
587+ const proxy = getProxy(pointDetails.ownership!, _wallet.address);
614588
615− if (operator === undefined)
589+ if (proxy === undefined)
616590 throw new Error("Error: Address doesn't match proxy");
617591
592+ const nonce = await api.getNonce({ ship: _point, proxy });
593+
618594 const txHash = await transferPointRequest(
619595 api,
620596 _wallet,
621597 _point,
622− operator.proxy!,
623− operator.nonce!,
598+ proxy,
599+ nonce,
624600 address,
625601 reset || false
626602 );
@@ -628,7 +604,7 @@ export default function useRoller() {
628604
629605 return pendingTx;
630606 },
631− [api, pointCursor, wallet, nonces]
607+ [api, pointCursor, wallet]
632608 );
633609
634610 // On load, get initial config
src/lib/utils/nonce.tsdeleted−157
@@ -1,157 +0,0 @@
1−import { Ownership } from '@urbit/roller-api';
2−
3−export const getProxyAndNonce = (
4− owner: Ownership,
5− address: string
6−): { proxy?: string; nonce?: number } =>
7− owner.managementProxy?.address === address
8− ? { proxy: 'manage', nonce: owner.managementProxy.nonce }
9− : owner.owner?.address === address
10− ? { proxy: 'own', nonce: owner.owner.nonce }
11− : owner.spawnProxy?.address === address
12− ? { proxy: 'spawn', nonce: owner.spawn?.owner.nonce }
13− : owner.votingProxy?.address === address
14− ? { proxy: 'vote', nonce: owner.votingProxy?.owner.nonce }
15− : owner.transferProxy?.address === address
16− ? {
17− proxy: 'transfer',
18− nonce: owner.votingProxy?.nonce,
19− }
20− : { proxy: undefined, nonce: undefined };
21−
22−export const getManagementNonce = (
23− owner: Ownership,
24− address: string
25−): { nonce?: number; proxy?: string } | undefined =>
26− owner.managementProxy?.address === address
27− ? { nonce: owner.managementProxy.nonce, proxy: 'manage' }
28− : undefined;
29−
30−export const getTransferNonce = (
31− owner: Ownership,
32− address: string
33−): { nonce?: number; proxy?: string } | undefined =>
34− owner.transferProxy?.address === address
35− ? { nonce: owner.transferProxy.nonce, proxy: 'transfer' }
36− : undefined;
37−
38−export const getOwnerNonce = (
39− owner: Ownership,
40− address: string
41−): { nonce?: number; proxy?: string } | undefined =>
42− owner.owner?.address === address
43− ? { nonce: owner.owner.nonce, proxy: 'own' }
44− : undefined;
45−
46−export const getSpawnNonce = (
47− owner: Ownership,
48− address: string
49−): { nonce?: number; proxy?: string } | undefined =>
50− owner.spawnProxy?.address === address
51− ? { nonce: owner.spawnProxy.nonce, proxy: 'spawn' }
52− : undefined;
53−
54−export const getVotingNonce = (
55− owner: Ownership,
56− address: string
57−): { nonce?: number; proxy?: string } | undefined =>
58− owner.votingProxy?.address === address
59− ? { nonce: owner.votingProxy.nonce, proxy: 'vote' }
60− : undefined;
61−
62−export const increaseProxyNonce = (
63− owner: Ownership,
64− proxy: string
65−): Ownership | undefined =>
66− 'manage' === proxy
67− ? {
68− ...owner,
69− managementProxy: {
70− ...owner.managementProxy,
71− nonce: owner.managementProxy?.nonce
72− ? owner.managementProxy?.nonce + 1
73− : 1,
74− },
75− }
76− : 'own' === proxy
77− ? {
78− ...owner,
79− owner: {
80− ...owner.owner,
81− nonce: owner.owner?.nonce ? owner.owner?.nonce + 1 : 1,
82− },
83− }
84− : 'spawn' === proxy
85− ? {
86− ...owner,
87− spawnProxy: {
88− ...owner.spawnProxy,
89− nonce: owner.spawnProxy?.nonce ? owner.spawnProxy?.nonce + 1 : 1,
90− },
91− }
92− : 'vote' === proxy
93− ? {
94− ...owner,
95− votingProxy: {
96− ...owner.votingProxy,
97− nonce: owner.votingProxy?.nonce ? owner.votingProxy?.nonce + 1 : 1,
98− },
99− }
100− : 'transfer' === proxy
101− ? {
102− ...owner,
103− transferProxy: {
104− ...owner.transferProxy,
105− nonce: owner.transferProxy?.nonce
106− ? owner.transferProxy?.nonce + 1
107− : 1,
108− },
109− }
110− : undefined;
111−
112−export const setProxyNonce = (
113− owner: Ownership,
114− proxy: string,
115− nonce: number
116−): Ownership | undefined =>
117− 'manage' === proxy
118− ? {
119− ...owner,
120− managementProxy: {
121− ...owner.managementProxy,
122− nonce,
123− },
124− }
125− : 'own' === proxy
126− ? {
127− ...owner,
128− owner: {
129− ...owner.owner,
130− nonce,
131− },
132− }
133− : 'spawn' === proxy
134− ? {
135− ...owner,
136− spawnProxy: {
137− ...owner.spawnProxy,
138− nonce,
139− },
140− }
141− : 'vote' === proxy
142− ? {
143− ...owner,
144− votingProxy: {
145− ...owner.voteProxy,
146− nonce,
147− },
148− }
149− : 'transfer' === proxy
150− ? {
151− ...owner,
152− transferProxy: {
153− ...owner.transferProxy,
154− nonce,
155− },
156− }
157− : undefined;
src/lib/utils/proxy.tsnew file+17
@@ -0,0 +1,17 @@
1+import { Ownership } from '@urbit/roller-api';
2+
3+export const getProxy = (
4+ owner: Ownership,
5+ address: string
6+): string | undefined =>
7+ owner.managementProxy?.address === address
8+ ? 'manage'
9+ : owner.owner?.address === address
10+ ? 'own'
11+ : owner.spawnProxy?.address === address
12+ ? 'spawn'
13+ : owner.votingProxy?.address === address
14+ ? 'vote'
15+ : owner.transferProxy?.address === address
16+ ? 'transfer'
17+ : undefined;
src/store/roller.ts+1−39
@@ -1,13 +1,7 @@
11import create from 'zustand';
2−import {
3− L2Point,
4− PendingTransaction,
5− Ownership,
6− Ship,
7−} from '@urbit/roller-api';
2+import { L2Point, PendingTransaction } from '@urbit/roller-api';
83
94import { HOUR, isL2, isL2Spawn } from 'lib/utils/roller';
10−import { increaseProxyNonce, setProxyNonce } from 'lib/utils/nonce';
115import { Invite } from 'lib/types/Invite';
126
137export interface RollerStore {
@@ -17,17 +11,6 @@ export interface RollerStore {
1711 currentPoint: L2Point | null;
1812 currentL2: boolean;
1913 currentL2Spawn: boolean;
20− nonces: {
21− [point: Ship]: Ownership;
22− };
23− increaseNonce: (point: Ship, proxy: string) => void;
24− setNonce: (
25− point: Ship,
26− owner: Ownership,
27− proxy: string,
28− nonce: number
29− ) => void;
30− setNonces: (point: number, owner: Ownership) => void;
3114 invites: Invite[];
3215 recentlyCompleted: number;
3316 setNextBatchTime: (nextBatchTime: number) => void;
@@ -46,27 +29,6 @@ export const useRollerStore = create<RollerStore>(set => ({
4629 currentL2Spawn: false,
4730 invites: [],
4831 recentlyCompleted: 0,
49− nonces: {},
50− increaseNonce: (point: Ship, proxy: string) =>
51− set(state => {
52− const owner = increaseProxyNonce(state.nonces[point], proxy);
53− console.log(point, proxy, owner);
54− if (!owner) throw new Error("Can't increase nonce for this proxy");
55− return { ...state, nonces: { ...state.nonces, [point]: owner } };
56− }),
57− setNonce: (point: Ship, owner: Ownership, proxy: string, nonce: number) =>
58− set(state => {
59− const updatedOwner = setProxyNonce(owner, proxy, nonce);
60− if (!updatedOwner) throw new Error("Can't set nonce for this proxy");
61− return {
62− ...state,
63− nonces: { ...state.nonces, [point]: updatedOwner },
64− };
65− }),
66− setNonces: (point: Ship, owner: Ownership) =>
67− set(state => {
68− return { ...state, nonces: { ...state.nonces, [point]: owner } };
69− }),
7032 setNextBatchTime: (nextBatchTime: number) => set(() => ({ nextBatchTime })),
7133 setNextRoll: (nextRoll: string) => set(() => ({ nextRoll })),
7234 setPendingTransactions: (pendingTransactions: PendingTransaction[]) =>
src/views/Point/Point.tsx+4−12
@@ -45,8 +45,6 @@ export default function Point() {
4545 nextRoll,
4646 invites,
4747 setCurrentPoint,
48− setNonces,
49− increaseNonce,
5048 } = useRollerStore();
5149 const networkKeysSet = useHasNetworkKeysSet();
5250 const [showModal, setShowModal] = useState(false);
@@ -64,23 +62,16 @@ export default function Point() {
6462 const loadL2Info = useCallback(async () => {
6563 const getTransactions = async () => {
6664 const pointInfo = await api.getPoint(Number(point));
67− const pendingTxs = await api.getPendingByShip(Number(point));
65+
6866 if (isDevelopment) {
6967 console.log('POINT INFO', pointInfo);
7068 }
71− setNonces(point, pointInfo.ownership);
72−
73− for (let index = 0; index < pendingTxs.length; index++) {
74− const proxy = pendingTxs[index].rawTx?.from?.proxy;
75− console.log(pointInfo.ownership, proxy);
76− increaseNonce(point, proxy);
77− }
7869 setCurrentPoint(pointInfo);
7970 await getInvites(isL2(pointInfo.dominion));
8071 };
8172
8273 await getTransactions();
83− }, [api, point, setCurrentPoint, increaseNonce, getInvites, setNonces]);
74+ }, [api, point, getInvites, setCurrentPoint]);
8475
8576 useEffect(() => {
8677 loadL1Info();
@@ -148,7 +139,6 @@ export default function Point() {
148139 </>
149140 );
150141 })();
151−
152142 const address = need.addressFromWallet(wallet);
153143 const spawnedPending = pendingTransactions.filter(
154144 ({ rawTx }) => rawTx?.tx?.type === 'spawn'
@@ -197,6 +187,8 @@ export default function Point() {
197187 ? 'Management Address Changed'
198188 : pendingTx.rawTx?.tx?.type === 'configure-keys'
199189 ? 'Network Keys Configured'
190+ : pendingTx.rawTx?.tx?.type === 'transfer-point'
191+ ? 'Point Transfered'
200192 : ''}
201193 </div>
202194 <div className="rollup-timer">
src/views/UrbitID/SetProxy.js−1
@@ -161,7 +161,6 @@ export default function SetProxy() {
161161 ]);
162162
163163 const validateForm = useCallback((values, errors) => {
164− console.log('hola');
165164 if (!values.unset && errors.address) {
166165 return errors;
167166 }