on c8e855d help sign in
bridge/src/lib/types/Point.ts
248 lines 7.7 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 type PermissionLevel =
13 | 'own'
14 | 'manage'
15 | 'spawn'
16 | 'vote'
17 | 'transfer'
18 | null;
20export const shouldDisplayPermissionLevel = (level: PermissionLevel) =>
21 level === 'manage' || level === 'spawn' || level === 'vote';
23export enum PointField {
24 value = 'value',
25 layer = 'layer',
26 active = 'active',
27 authenticationKey = 'authenticationKey',
28 continuityNumber = 'continuityNumber',
29 cryptoSuiteVersion = 'cryptoSuiteVersion',
30 encryptionKey = 'encryptionKey',
31 escapeRequested = 'escapeRequested',
32 escapeRequestedTo = 'escapeRequestedTo',
33 hasSponsor = 'hasSponsor',
34 keyRevisionNumber = 'keyRevisionNumber',
35 managementProxy = 'managementProxy',
36 owner = 'owner',
37 spawnProxy = 'spawnProxy',
38 sponsor = 'sponsor',
39 transferProxy = 'transferProxy',
40 votingProxy = 'votingProxy',
41 isL2 = 'isL2',
42 isL2Spawn = 'isL2Spawn',
43 isL1 = 'isL1',
44 isGalaxy = 'isGalaxy',
45 isStar = 'isStar',
46 isPlanet = 'isPlanet',
47 isParent = 'isParent',
48 canMigrate = 'canMigrate',
49 networkKeysSet = 'networkKeysSet',
50 isOwner = 'isOwner',
51 isActiveOwner = 'isActiveOwner',
52 isManagementProxy = 'isManagementProxy',
53 isSpawnProxy = 'isSpawnProxy',
54 isVotingProxy = 'isVotingProxy',
55 isTransferProxy = 'isTransferProxy',
56 isManagementProxySet = 'isManagementProxySet',
57 isSpawnProxySet = 'isSpawnProxySet',
58 isVotingProxySet = 'isVotingProxySet',
59 isTransferProxySet = 'isTransferProxySet',
60 canManage = 'canManage',
61 canSpawn = 'canSpawn',
62 canTransfer = 'canTransfer',
63 canVote = 'canVote',
64 showInvites = 'showInvites',
67interface PointConstructorParams {
68 value: number;
69 details: L1Point;
70 address: string;
71 l2Quota?: number;
72 isPlaceholder?: boolean;
75export default class Point {
76 value: number;
77 patp: string;
78 layer: 1 | 2;
79 l2Quota: number;
80 active: boolean;
81 authenticationKey: string;
82 continuityNumber: string;
83 cryptoSuiteVersion: string;
84 encryptionKey: string;
85 escapeRequested: boolean;
86 escapeRequestedTo: string;
87 hasSponsor: boolean;
88 keyRevisionNumber: number;
89 managementProxy: string;
90 owner: string;
91 spawnProxy: string;
92 sponsor: string;
93 transferProxy: string;
94 votingProxy: string;
95 // Derived details
96 isL2: boolean;
97 isL2Spawn: boolean;
98 isL1: boolean;
99 isGalaxy: boolean;
100 isStar: boolean;
101 isPlanet: boolean;
102 isParent: boolean;
103 canMigrate: boolean;
104 networkKeysSet: boolean;
105 // usePermissionsForPoint details
106 isOwner: boolean;
107 isActiveOwner: boolean;
108 isManagementProxy: boolean;
109 isSpawnProxy: boolean;
110 isVotingProxy: boolean;
111 isTransferProxy: boolean;
112 isManagementProxySet: boolean;
113 isSpawnProxySet: boolean;
114 isVotingProxySet: boolean;
115 isTransferProxySet: boolean;
116 canManage: boolean;
117 couldSpawn: boolean;
118 canSpawn: boolean;
119 canTransfer: boolean;
120 canVote: boolean;
121 showInvites: boolean;
122 isDefault: boolean;
123 isPlaceholder: boolean;
124 ownManageSpawn: boolean;
125 shouldDisplay: boolean;
126 isOutgoing: boolean;
127 permissionLevel: PermissionLevel;
128 displayPermission: boolean;
130 constructor({
131 value,
132 details,
133 address,
134 l2Quota = 0,
135 isPlaceholder = false,
136 }: PointConstructorParams) {
137 this.value = value;
138 this.patp = value < 0 ? 'null' : ob.patp(value);
140 this.layer = details.layer || 1;
141 this.l2Quota = l2Quota;
142 this.isL2Spawn = Boolean(details.isL2Spawn);
143 this.active = details.active;
144 this.authenticationKey = details.authenticationKey;
145 this.continuityNumber = details.continuityNumber;
146 this.cryptoSuiteVersion = details.cryptoSuiteVersion;
147 this.encryptionKey = details.encryptionKey;
148 this.escapeRequested = details.escapeRequested;
149 this.escapeRequestedTo = details.escapeRequestedTo;
150 this.hasSponsor = details.hasSponsor;
151 this.keyRevisionNumber = Number(details.keyRevisionNumber);
152 this.managementProxy = details.managementProxy;
153 this.owner = details.owner;
154 this.spawnProxy = details.spawnProxy;
155 this.sponsor = details.sponsor;
156 this.transferProxy = details.transferProxy;
157 this.votingProxy = details.votingProxy;
158 // Derived details here
159 this.isL2 = this.layer === 2;
160 this.isL1 = this.layer === 1;
161 this.isGalaxy = isGalaxy(value);
162 this.isStar = isStar(value);
163 this.isPlanet = isPlanet(value);
164 this.isParent = !this.isPlanet;
165 this.networkKeysSet = Number(this.keyRevisionNumber) > 0;
166 // usePermissionsForPoint
167 this.isOwner = eqAddr(address, this.owner);
168 this.isActiveOwner = this.isOwner && this.active;
169 this.isManagementProxy = eqAddr(address, this.managementProxy);
170 this.isSpawnProxy = eqAddr(address, this.spawnProxy);
171 this.isVotingProxy = eqAddr(address, this.votingProxy);
172 this.isTransferProxy = eqAddr(address, this.transferProxy);
173 this.permissionLevel = this.isOwner
174 ? 'own'
175 : this.isManagementProxy
176 ? 'manage'
177 : this.isSpawnProxy
178 ? 'spawn'
179 : this.isVotingProxy
180 ? 'vote'
181 : this.isTransferProxy
182 ? 'transfer'
183 : null;
184 this.displayPermission = shouldDisplayPermissionLevel(this.permissionLevel);
185 this.isManagementProxySet = !isZeroAddress(this.managementProxy);
186 this.isSpawnProxySet = !isZeroAddress(this.spawnProxy);
187 this.isVotingProxySet = !isZeroAddress(this.votingProxy);
188 this.isTransferProxySet = !isZeroAddress(this.transferProxy);
189 this.canManage = this.isOwner || this.isManagementProxy;
190 this.canTransfer = this.isOwner || this.isTransferProxy;
191 this.couldSpawn = this.isParent && (this.isOwner || this.isSpawnProxy);
192 this.canSpawn =
193 this.isParent &&
194 (this.isOwner || this.isSpawnProxy) &&
195 this.networkKeysSet;
196 this.canVote =
197 this.isGalaxy && this.active && (this.isOwner || this.isVotingProxy);
198 this.canMigrate =
199 this.canManage &&
200 this.isL1 &&
201 ((this.isGalaxy && !this.isL2Spawn) || this.isStar || this.isPlanet);
202 this.showInvites = this.canManage || this.canSpawn;
203 this.isDefault = value === -1;
204 this.isPlaceholder = isPlaceholder;
205 this.ownManageSpawn =
206 !this.isPlaceholder &&
207 !this.isDefault &&
208 (this.isOwner || this.isSpawnProxy || this.isManagementProxy);
209 this.shouldDisplay = this.ownManageSpawn || this.isTransferProxy;
210 this.isOutgoing =
211 this.isTransferProxySet &&
212 !this.isTransferProxy &&
213 this.canManage &&
214 this.transferProxy !== DUMMY_L2_ADDRESS;
215 }
217 equals = (point: Point) => {
218 return Object.values(PointField).reduce(
219 (acc, field) => acc && this[field] === point[field],
220 true
221 );
222 };
224 getChangedField = (point: Point, targetField?: PointField) =>
225 targetField
226 ? this[targetField] !== point[targetField] && targetField
227 : Object.values(PointField).find(field => this[field] !== point[field]);
229 getAddressProxy = (proxyType: Proxy) =>
230 this.isOwner
231 ? 'own'
232 : proxyType === 'manage' && this.isManagementProxy
233 ? 'manage'
234 : proxyType === 'spawn' && this.isSpawnProxy
235 ? 'spawn'
236 : proxyType === 'transfer' && this.isTransferProxy
237 ? 'transfer'
238 : undefined;
240 getManagerProxy = () =>
241 this.isOwner ? 'own' : this.isManagementProxy ? 'manage' : undefined;
243 getSpawnProxy = () =>
244 this.isOwner ? 'own' : this.isSpawnProxy ? 'spawn' : undefined;
246 getTransferProxy = () =>
247 this.isOwner ? 'own' : this.isTransferProxy ? 'transfer' : undefined;