on bb22285 help sign in
bridge/src/lib/types/Point.ts
223 lines 7.1 kB history raw
1import * as ob from 'urbit-ob';
2import { isGalaxy, isPlanet, isStar } from 'lib/utils/point';
3import { eqAddr, isZeroAddress } from 'lib/utils/address';
4import { L1Point } from './L1Point';
5import { Proxy } from '@urbit/roller-api';
6import { DUMMY_L2_ADDRESS } from 'lib/constants';
8export interface Points {
9 [key: number]: Point;
12export enum PointField {
13 value = 'value',
14 layer = 'layer',
15 active = 'active',
16 authenticationKey = 'authenticationKey',
17 continuityNumber = 'continuityNumber',
18 cryptoSuiteVersion = 'cryptoSuiteVersion',
19 encryptionKey = 'encryptionKey',
20 escapeRequested = 'escapeRequested',
21 escapeRequestedTo = 'escapeRequestedTo',
22 hasSponsor = 'hasSponsor',
23 keyRevisionNumber = 'keyRevisionNumber',
24 managementProxy = 'managementProxy',
25 owner = 'owner',
26 spawnProxy = 'spawnProxy',
27 sponsor = 'sponsor',
28 transferProxy = 'transferProxy',
29 votingProxy = 'votingProxy',
30 isL2 = 'isL2',
31 isL2Spawn = 'isL2Spawn',
32 isL1 = 'isL1',
33 isGalaxy = 'isGalaxy',
34 isStar = 'isStar',
35 isPlanet = 'isPlanet',
36 isParent = 'isParent',
37 canMigrate = 'canMigrate',
38 networkKeysSet = 'networkKeysSet',
39 isOwner = 'isOwner',
40 isActiveOwner = 'isActiveOwner',
41 isManagementProxy = 'isManagementProxy',
42 isSpawnProxy = 'isSpawnProxy',
43 isVotingProxy = 'isVotingProxy',
44 isTransferProxy = 'isTransferProxy',
45 isManagementProxySet = 'isManagementProxySet',
46 isSpawnProxySet = 'isSpawnProxySet',
47 isVotingProxySet = 'isVotingProxySet',
48 isTransferProxySet = 'isTransferProxySet',
49 canManage = 'canManage',
50 canSpawn = 'canSpawn',
51 canTransfer = 'canTransfer',
52 canVote = 'canVote',
53 showInvites = 'showInvites',
56interface PointConstructorParams {
57 value: number;
58 details: L1Point;
59 address: string;
60 l2Quota?: number;
61 isPlaceholder?: boolean;
64export default class Point {
65 value: number;
66 patp: string;
67 layer: 1 | 2;
68 l2Quota: number;
69 active: boolean;
70 authenticationKey: string;
71 continuityNumber: string;
72 cryptoSuiteVersion: string;
73 encryptionKey: string;
74 escapeRequested: boolean;
75 escapeRequestedTo: string;
76 hasSponsor: boolean;
77 keyRevisionNumber: string;
78 managementProxy: string;
79 owner: string;
80 spawnProxy: string;
81 sponsor: string;
82 transferProxy: string;
83 votingProxy: string;
84 // Derived details
85 isL2: boolean;
86 isL2Spawn: boolean;
87 isL1: boolean;
88 isGalaxy: boolean;
89 isStar: boolean;
90 isPlanet: boolean;
91 isParent: boolean;
92 canMigrate: boolean;
93 networkKeysSet: boolean;
94 // usePermissionsForPoint details
95 isOwner: boolean;
96 isActiveOwner: boolean;
97 isManagementProxy: boolean;
98 isSpawnProxy: boolean;
99 isVotingProxy: boolean;
100 isTransferProxy: boolean;
101 isManagementProxySet: boolean;
102 isSpawnProxySet: boolean;
103 isVotingProxySet: boolean;
104 isTransferProxySet: boolean;
105 canManage: boolean;
106 couldSpawn: boolean;
107 canSpawn: boolean;
108 canTransfer: boolean;
109 canVote: boolean;
110 showInvites: boolean;
111 isDefault: boolean;
112 isPlaceholder: boolean;
113 ownManageSpawn: boolean;
114 shouldDisplay: boolean;
115 isOutgoing: boolean;
117 constructor({
118 value,
119 details,
120 address,
121 l2Quota = 0,
122 isPlaceholder = false,
123 }: PointConstructorParams) {
124 this.value = value;
125 this.patp = value < 0 ? 'null' : ob.patp(value);
127 this.layer = details.layer || 1;
128 this.l2Quota = l2Quota;
129 this.isL2Spawn = Boolean(details.isL2Spawn);
130 this.active = details.active;
131 this.authenticationKey = details.authenticationKey;
132 this.continuityNumber = details.continuityNumber;
133 this.cryptoSuiteVersion = details.cryptoSuiteVersion;
134 this.encryptionKey = details.encryptionKey;
135 this.escapeRequested = details.escapeRequested;
136 this.escapeRequestedTo = details.escapeRequestedTo;
137 this.hasSponsor = details.hasSponsor;
138 this.keyRevisionNumber = details.keyRevisionNumber;
139 this.managementProxy = details.managementProxy;
140 this.owner = details.owner;
141 this.spawnProxy = details.spawnProxy;
142 this.sponsor = details.sponsor;
143 this.transferProxy = details.transferProxy;
144 this.votingProxy = details.votingProxy;
145 // Derived details here
146 this.isL2 = this.layer === 2;
147 this.isL1 = this.layer === 1;
148 this.isGalaxy = isGalaxy(value);
149 this.isStar = isStar(value);
150 this.isPlanet = isPlanet(value);
151 this.isParent = !this.isPlanet;
152 this.networkKeysSet = Number(this.keyRevisionNumber) > 0;
153 // usePermissionsForPoint
154 this.isOwner = eqAddr(address, this.owner);
155 this.isActiveOwner = this.isOwner && this.active;
156 this.isManagementProxy = eqAddr(address, this.managementProxy);
157 this.isSpawnProxy = eqAddr(address, this.spawnProxy);
158 this.isVotingProxy = eqAddr(address, this.votingProxy);
159 this.isTransferProxy = eqAddr(address, this.transferProxy);
160 this.isManagementProxySet = !isZeroAddress(this.managementProxy);
161 this.isSpawnProxySet = !isZeroAddress(this.spawnProxy);
162 this.isVotingProxySet = !isZeroAddress(this.votingProxy);
163 this.isTransferProxySet = !isZeroAddress(this.transferProxy);
164 this.canManage = this.isOwner || this.isManagementProxy;
165 this.canTransfer = this.isOwner || this.isTransferProxy;
166 this.couldSpawn = this.isParent && (this.isOwner || this.isSpawnProxy);
167 this.canSpawn =
168 this.isParent &&
169 (this.isOwner || this.isSpawnProxy) &&
170 this.networkKeysSet;
171 this.canVote =
172 this.isGalaxy && this.active && (this.isOwner || this.isVotingProxy);
173 this.canMigrate =
174 this.canManage &&
175 this.isL1 &&
176 ((this.isGalaxy && !this.isL2Spawn) || this.isStar || this.isPlanet);
177 this.showInvites = this.canManage || this.canSpawn;
178 this.isDefault = value === -1;
179 this.isPlaceholder = isPlaceholder;
180 this.ownManageSpawn =
181 !this.isPlaceholder &&
182 !this.isDefault &&
183 (this.isOwner || this.isSpawnProxy || this.isManagementProxy);
184 this.shouldDisplay = this.ownManageSpawn || this.isTransferProxy;
185 this.isOutgoing =
186 this.isTransferProxySet &&
187 !this.isTransferProxy &&
188 this.canManage &&
189 this.transferProxy !== DUMMY_L2_ADDRESS;
190 }
192 equals = (point: Point) => {
193 return Object.values(PointField).reduce(
194 (acc, field) => acc && this[field] === point[field],
195 true
196 );
197 };
199 getChangedField = (point: Point, targetField?: PointField) =>
200 targetField
201 ? this[targetField] !== point[targetField] && targetField
202 : Object.values(PointField).find(field => this[field] !== point[field]);
204 getAddressProxy = (proxyType: Proxy) =>
205 this.isOwner
206 ? 'own'
207 : proxyType === 'manage' && this.isManagementProxy
208 ? 'manage'
209 : proxyType === 'spawn' && this.isSpawnProxy
210 ? 'spawn'
211 : proxyType === 'transfer' && this.isTransferProxy
212 ? 'transfer'
213 : undefined;
215 getManagerProxy = () =>
216 this.isOwner ? 'own' : this.isManagementProxy ? 'manage' : undefined;
218 getSpawnProxy = () =>
219 this.isOwner ? 'own' : this.isSpawnProxy ? 'spawn' : undefined;
221 getTransferProxy = () =>
222 this.isOwner ? 'own' : this.isTransferProxy ? 'transfer' : undefined;