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