on afdb680 help sign in
bridge/src/lib/types/Point.ts
125 lines 3.9 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';
6export interface Points {
7 [key: number]: Point;
8}
10export enum Relationship {
11 own,
12 transfer,
13 manage,
14 vote,
15 spawn,
18export default class Point {
19 value: number;
20 patp: string;
21 layer: 1 | 2;
22 active: boolean;
23 authenticationKey: string;
24 continuityNumber: string;
25 cryptoSuiteVersion: string;
26 encryptionKey: string;
27 escapeRequested: boolean;
28 escapeRequestedTo: string;
29 hasSponsor: boolean;
30 keyRevisionNumber: string;
31 managementProxy: string;
32 owner: string;
33 spawnProxy: string;
34 sponsor: string;
35 transferProxy: string;
36 votingProxy: string;
37 relationship: Relationship;
38 // Derived details
39 isL2: boolean;
40 isL2Spawn: boolean;
41 isL1: boolean;
42 isGalaxy: boolean;
43 isStar: boolean;
44 isPlanet: boolean;
45 isParent: boolean;
46 canMigrate: boolean;
47 networkKeysSet: boolean;
48 // usePermissionsForPoint details
49 isOwner: boolean;
50 isActiveOwner: boolean;
51 isManagementProxy: boolean;
52 isSpawnProxy: boolean;
53 isVotingProxy: boolean;
54 isTransferProxy: boolean;
55 isManagementProxySet: boolean;
56 isSpawnProxySet: boolean;
57 isVotingProxySet: boolean;
58 isTransferProxySet: boolean;
59 canManage: boolean;
60 canSpawn: boolean;
61 canTransfer: boolean;
62 canVote: boolean;
63 showInvites: boolean;
65 constructor(
66 value: number,
67 relationship: Relationship,
68 details: L1Point,
69 address: string
70 ) {
71 this.value = value;
72 this.patp = value < 0 ? 'null' : ob.patp(value);
73 this.relationship = relationship;
75 this.layer = details.layer;
76 this.isL2Spawn = Boolean(details.isL2Spawn);
77 this.active = details.active;
78 this.authenticationKey = details.authenticationKey;
79 this.continuityNumber = details.continuityNumber;
80 this.cryptoSuiteVersion = details.cryptoSuiteVersion;
81 this.encryptionKey = details.encryptionKey;
82 this.escapeRequested = details.escapeRequested;
83 this.escapeRequestedTo = details.escapeRequestedTo;
84 this.hasSponsor = details.hasSponsor;
85 this.keyRevisionNumber = details.keyRevisionNumber;
86 this.managementProxy = details.managementProxy;
87 this.owner = details.owner;
88 this.spawnProxy = details.spawnProxy;
89 this.sponsor = details.sponsor;
90 this.transferProxy = details.transferProxy;
91 this.votingProxy = details.votingProxy;
92 // Derived details here
93 this.isL2 = this.layer === 2;
94 this.isL1 = this.layer === 1;
95 this.isGalaxy = isGalaxy(value);
96 this.isStar = isStar(value);
97 this.isPlanet = isPlanet(value);
98 this.isParent = !this.isPlanet;
99 this.networkKeysSet = Number(this.keyRevisionNumber) > 0;
100 // usePermissionsForPoint
101 this.isOwner = eqAddr(address, this.owner);
102 this.isActiveOwner = this.isOwner && this.active;
103 this.isManagementProxy = eqAddr(address, this.managementProxy);
104 this.isSpawnProxy = eqAddr(address, this.spawnProxy);
105 this.isVotingProxy = eqAddr(address, this.votingProxy);
106 this.isTransferProxy = eqAddr(address, this.transferProxy);
107 this.isManagementProxySet = !isZeroAddress(this.managementProxy);
108 this.isSpawnProxySet = !isZeroAddress(this.spawnProxy);
109 this.isVotingProxySet = !isZeroAddress(this.votingProxy);
110 this.isTransferProxySet = !isZeroAddress(this.transferProxy);
111 this.canManage = this.isOwner || this.isManagementProxy;
112 this.canTransfer = this.isOwner || this.isTransferProxy;
113 this.canSpawn =
114 this.isParent &&
115 (this.isOwner || this.isSpawnProxy) &&
116 this.networkKeysSet;
117 this.canVote =
118 this.isGalaxy && this.active && (this.isOwner || this.isVotingProxy);
119 this.canMigrate =
120 this.canManage &&
121 this.isL1 &&
122 ((this.isGalaxy && !this.isL2Spawn) || this.isStar || this.isPlanet);
123 this.showInvites = this.canManage || this.canSpawn;
124 }