on bc552c8 help sign in
bridge/src/views/Permissions.js
153 lines 4.2 kB history raw
1import React, { useState, useCallback } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import * as need from 'lib/need';
4import { azimuth } from 'azimuth-js';
5import { Grid } from 'indigo-react';
7import View from 'components/View';
8import { ForwardButton } from 'components/Buttons';
9import FooterButton from 'components/FooterButton';
10import { matchBlinky } from 'components/Blinky';
12import { ROUTE_NAMES } from 'lib/routeNames';
13import { eqAddr } from 'lib/wallet';
15import useLifecycle from 'lib/useLifecycle';
16import { useNetwork } from 'store/network';
17import { useHistory } from 'store/history';
18import { useWallet } from 'store/wallet';
19import { usePointCursor } from 'store/pointCursor';
20import { usePointCache } from 'store/pointCache';
22//TODO consolidate with azimuth-js' getActivationBlock
23const getRekeyDate = async (web3, contracts, point) => {
24 const logs = await contracts.azimuth.getPastEvents('ChangedKeys', {
25 fromBlock: 0,
26 toBlock: 'latest',
27 filter: { point: [point] },
28 });
29 if (logs.length === 0) {
30 return null;
31 } else {
32 // last log is most recent
33 const block = await web3.eth.getBlock(logs[logs.length - 1].blockNumber);
34 return new Date(block.timestamp * 1000);
35 }
36};
38export default function Permissions() {
39 const history = useHistory();
40 const { web3, contracts } = useNetwork();
41 const { wallet } = useWallet();
42 const { pointCursor } = usePointCursor();
43 const { pointCache } = usePointCache();
45 const [rekeyDate, setRekeyDate] = useState(Nothing());
47 const point = need.point(pointCursor);
48 const pointSize = azimuth.getPointSize(point);
49 const userAddress = need.wallet(wallet).address;
51 useLifecycle(() => {
52 getRekeyDate(need.web3(web3), need.contracts(contracts), point).then(
53 res => {
54 if (res === null) setRekeyDate(Just('unknown'));
55 else setRekeyDate(Just(res.toString()));
56 }
57 );
58 });
60 const goSetManagement = useCallback(
61 () => history.push(ROUTE_NAMES.SET_MANAGEMENT_PROXY),
62 [history]
63 );
65 const goSetSpawn = useCallback(
66 () => history.push(ROUTE_NAMES.SET_SPAWN_PROXY),
67 [history]
68 );
70 const goSetVoting = useCallback(
71 () => history.push(ROUTE_NAMES.SET_VOTING_PROXY),
72 [history]
73 );
75 const goSetKeys = useCallback(() => history.push(ROUTE_NAMES.SET_KEYS), [
76 history,
77 ]);
79 const pointDetails = need.fromPointCache(pointCache, point);
81 const isOwner = eqAddr(userAddress, pointDetails.owner);
82 const canManage =
83 isOwner || eqAddr(userAddress, pointDetails.managementProxy);
85 const proxyAction = (name, address, onClick) => {
86 if (address === '0x0000000000000000000000000000000000000000')
87 address = 'Unset';
88 return (
89 <Grid.Item full>
90 <ForwardButton disabled={!isOwner} onClick={onClick} detail={address}>
91 {`${name} proxy address`}
92 </ForwardButton>
93 </Grid.Item>
94 );
95 };
97 let spawnProxyAction = null;
98 if (pointSize !== azimuth.PointSize.Planet) {
99 spawnProxyAction = proxyAction(
100 'Spawn',
101 pointDetails.spawnProxy,
102 goSetSpawn
103 );
104 }
106 let votingProxyAction = null;
107 if (pointSize === azimuth.PointSize.Galaxy) {
108 votingProxyAction = proxyAction(
109 'Voting',
110 pointDetails.votingProxy,
111 goSetVoting
112 );
113 }
115 const netKeysText =
116 pointDetails.keyRevisionNumber === 0 ? (
117 'Not yet configured'
118 ) : (
119 <>
120 {`Revision #${pointDetails.keyRevisionNumber}, since `}
121 {matchBlinky(rekeyDate)}
122 </>
123 );
125 return (
126 <View>
127 <Grid className="pt2">
128 <Grid.Item full as={ForwardButton} disabled detail={pointDetails.owner}>
129 Ownership address
130 </Grid.Item>
131 {proxyAction(
132 'Management',
133 pointDetails.managementProxy,
134 goSetManagement
135 )}
136 {spawnProxyAction}
137 {votingProxyAction}
138 <Grid.Item
139 full
140 as={ForwardButton}
141 disabled={!canManage}
142 onClick={goSetKeys}
143 detail={netKeysText}>
144 Network keys
145 </Grid.Item>
146 </Grid>
148 <FooterButton detail="Transfer this identity to a new owner" disabled>
149 Transfer
150 </FooterButton>
151 </View>
152 );