@@ -1,9 +1,8 @@
1−import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
1+import { useCallback, useEffect, useMemo, useState } from 'react';
22import * as azimuth from 'azimuth-js';
33import * as need from 'lib/need';
44import SecureLS from 'secure-ls';
55
6−import { Invite } from 'lib/types/Invite';
76import {
87 deriveNetworkSeedFromUrbitWallet,
98 attemptNetworkSeedDerivation,
@@ -13,24 +12,17 @@ import { useNetwork } from 'store/network';
1312import { usePointCursor } from 'store/pointCursor';
1413import { EMPTY_POINT, useRollerStore } from 'store/rollerStore';
1514import { useWallet } from 'store/wallet';
16−import {
17− getStoredInvites,
18− setStoredInvite,
19− setStoredInvites,
20−} from 'store/storage/roller';
2115import { usePointCache } from 'store/pointCache';
2216
2317import { getUpdatedPointMessage, isPlanet, toL1Details } from './utils/point';
2418import { convertToInt } from './convertToInt';
2519import { isDevelopment, isMainnet, isRopsten } from './flags';
2620import {
27− generateInviteWallet,
28− getPendingSpawns,
2921 submitL2Transaction,
3022 registerProxyAddress,
3123 isL2Spawn,
3224} from './utils/roller';
33−import { POINT_DOMINIONS, ROLLER_HOSTS, TEN_SECONDS } from './constants';
25+import { ROLLER_HOSTS, TEN_SECONDS } from './constants';
3426
3527import {
3628 Config,
@@ -39,7 +31,6 @@ import {
3931 RollerRPCAPI,
4032 Options,
4133 EthAddress,
42− UnspawnedPoints,
4334} from '@urbit/roller-api';
4435
4536import Point, { PointField, Points } from './types/Point';
@@ -60,18 +51,6 @@ interface UpdateParams {
6051 intervalTime?: number;
6152}
6253
63−const inviteTemplate = (
64− planet: number,
65− ticket = '',
66− owner = '',
67− hash = ''
68−): Invite => ({
69− planet,
70− ticket,
71− owner,
72− hash,
73−});
74−
7554export default function useRoller() {
7655 const {
7756 wallet,
@@ -89,22 +68,16 @@ export default function useRoller() {
8968 const {
9069 point,
9170 points,
92− pointList,
9371 nextQuotaTime,
94− addInvite,
9572 setNextBatchTime,
9673 setNextQuotaTime,
9774 setPendingTransactions,
98− setInvites,
99− setInviteGeneratingNum,
100− setInvitesLoading,
10175 setModalText,
10276 setPoints,
10377 updatePoint,
10478 storePendingL1Txn,
10579 deletePendingL1Txn,
10680 } = useRollerStore();
107− const generatingInvites = useRef(false);
10881 const [config, setConfig] = useState<Config | null>(null);
10982
11083 const options: Options = useMemo(() => {
@@ -344,247 +317,6 @@ export default function useRoller() {
344317 ]
345318 );
346319
347− const generateInviteInfo = async (planet: number, _authToken: string) => {
348− const { ticket, inviteWallet } = await generateInviteWallet(
349− planet,
350− _authToken
351− );
352−
353− return inviteTemplate(planet, ticket, inviteWallet.ownership.keys.address);
354− };
355−
356− const getInvites = useCallback(async () => {
357− if (generatingInvites.current) {
358− return;
359− }
360−
361− generatingInvites.current = true;
362− try {
363− setInvitesLoading(true);
364− const curPoint: number = Number(need.point(pointCursor));
365− const _authToken = authToken.getOrElse(null);
366− const _contracts = contracts.getOrElse(null);
367−
368− if (_authToken && _contracts) {
369− const spawnedPoints = point.isL2Spawn
370− ? await api.getSpawned(curPoint)
371− : [];
372−
373− const newPending = await api.getPendingByShip(curPoint);
374− setPendingTransactions(newPending);
375− const pendingSpawns = getPendingSpawns(newPending);
376−
377− const availablePoints = await azimuth.azimuth.getUnspawnedChildren(
378− _contracts,
379− curPoint
380− );
381−
382− const outgoingPoints = pointList
383− .filter(
384− ({ value, isPlanet }) => isPlanet && availablePoints.includes(value)
385− )
386− .map(({ value }) => value);
387−
388− const invitePlanets = spawnedPoints
389− .concat(
390− outgoingPoints.filter((p: number) => !spawnedPoints.includes(p))
391− )
392− .filter((p: number) => !pendingSpawns.has(p))
393− .sort();
394−
395− setInvites(curPoint, []);
396−
397− const newInvites: Invite[] = [];
398− // Iterate over all of the stored invites, generating wallet info as necessary
399− const storedInvites = getStoredInvites(ls);
400−
401− for (let i = 0; i < invitePlanets.length; i++) {
402− setInviteGeneratingNum(i + 1);
403− const planet = invitePlanets[i];
404− const storedInvite = storedInvites[planet];
405− const invite =
406− storedInvite || (await generateInviteInfo(planet, _authToken));
407−
408− // TODO: check if the invite point's owner still matches the deterministic wallet address
409− const planetInfo = await api.getPoint(planet);
410− setStoredInvite(ls, invite);
411−
412− if (
413− invite.owner.toLowerCase() ===
414− planetInfo.ownership?.owner?.address &&
415− planetInfo.dominion === POINT_DOMINIONS.L2
416− ) {
417− addInvite(curPoint, invite);
418− newInvites.push(invite);
419− }
420−
421− if (storedInvite) {
422− setStoredInvite(ls, invite);
423− }
424− }
425−
426− setInvitesLoading(false);
427− return newInvites;
428− }
429− } catch (error) {
430− console.warn('ERROR GETTING INVITES', error);
431− }
432−
433− generatingInvites.current = false;
434− }, [
435− api,
436− authToken,
437− contracts,
438− pointCursor,
439− ls,
440− point,
441− pointList,
442− addInvite,
443− setInvites,
444− setInvitesLoading,
445− setInviteGeneratingNum,
446− setPendingTransactions,
447− ]);
448−
449− const generateInviteCodes = useCallback(
450− async (numInvites: number) => {
451− const _point = need.point(pointCursor);
452− const _contracts = contracts.getOrElse(null);
453− const _web3 = web3.getOrElse(null);
454− const _wallet = wallet.getOrElse(null);
455− const _authToken = authToken.getOrElse(null);
456− const _details = getDetails(_point);
457−
458− if (!_contracts || !_web3 || !_wallet || !_authToken || !_details) {
459− // not using need because we want a custom error
460− throw new Error('Internal Error: Missing Contracts/Web3/Wallet');
461− }
462−
463− // TODO: remove when UI disables management proxy from spawning invites
464− // if (Just.hasInstance(authMnemonic)) {
465− // throw new Error(
466− // "Auth key Error: A management mnemonic can't create invite codes"
467− // );
468− // }
469− let planets: UnspawnedPoints = await api.getUnspawned(_point);
470−
471− const pendingTxs = await api.getPendingByShip(_point);
472−
473− const pendingSpawns = getPendingSpawns(pendingTxs);
474−
475− planets = planets.filter((point: number) => !pendingSpawns.has(point));
476−
477− const invites: Invite[] = [];
478−
479− const proxy = point.getSpawnProxy();
480−
481− if (proxy === undefined)
482− throw new Error("Error: Address doesn't match proxy");
483−
484− const nonce = await api.getNonce({
485− ship: _point,
486− proxy,
487− });
488−
489− for (let i = 0; i < numInvites && planets[i]; i++) {
490− setInviteGeneratingNum(i + 1);
491− const planet = planets[i];
492− const nonceInc = i + nonce;
493−
494− const { ticket, inviteWallet } = await generateInviteWallet(
495− planet,
496− _authToken
497− );
498−
499− await submitL2Transaction({
500− api,
501− address: _wallet.address,
502− wallet: _wallet,
503− ship: _point,
504− proxy,
505− nonce: nonceInc,
506− pointToSpawn: planet,
507− type: 'spawn',
508− walletType,
509− web3: _web3,
510− connector,
511− });
512−
513− const networkSeed = await deriveNetworkSeedFromUrbitWallet(
514− inviteWallet,
515− 1
516− );
517− await submitL2Transaction({
518− api,
519− wallet: _wallet,
520− ship: planet,
521− proxy: 'own',
522− nonce: 0,
523− networkSeed,
524− type: 'configureKeys',
525− walletType,
526− web3: _web3,
527− connector,
528− });
529−
530− await registerProxyAddress(
531− api,
532− _wallet,
533− planet,
534− 'own',
535− 'manage',
536− 1,
537− inviteWallet.management.keys.address,
538− walletType,
539− _web3,
540− connector
541− );
542−
543− await submitL2Transaction({
544− api,
545− wallet: _wallet,
546− ship: planet,
547− proxy: 'own',
548− type: 'transferPoint',
549− nonce: 2,
550− address: inviteWallet.ownership.keys.address,
551− walletType,
552− web3: _web3,
553− connector,
554− });
555−
556− invites.push({
557− hash: '',
558− ticket,
559− planet,
560− owner: inviteWallet.ownership.keys.address,
561− });
562− }
563−
564− setStoredInvites(ls, invites);
565− console.log('getting invites from code generation');
566− getInvites();
567− showNotification(
568− `Your invite${invites.length > 1 ? 's have' : ' has'} been generated!`
569− );
570− },
571− [
572− api,
573− authToken,
574− connector,
575− contracts,
576− pointCursor,
577− point,
578− wallet,
579− walletType,
580− web3,
581− getDetails,
582− ls,
583− getInvites,
584− setInviteGeneratingNum,
585− ]
586− );
587−
588320 const getPoints = useCallback(
589321 async (proxy: Proxy, address: EthAddress) => {
590322 const points: Ship[] =
@@ -1049,9 +781,7 @@ export default function useRoller() {
1049781 checkForUpdates,
1050782 config,
1051783 configureNetworkKeys,
1052− generateInviteCodes,
1053784 getAndUpdatePoint,
1054− getInvites,
1055785 getPoints,
1056786 getPointsDetails,
1057787 getPendingTransactions,