on a8a2d64 help sign in
bridge/src/components/Passport.js
311 lines 8.1 kB history raw
1import React from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import ob from 'urbit-ob';
4import BN from 'bn.js';
5import 'style/anim.css';
7import * as need from 'lib/need';
8import { chunkStr, Matrix, walk, rand } from 'lib/card';
9import usePermissionsForPoint from 'lib/usePermissionsForPoint';
10import { useSyncOwnedPoints } from 'lib/useSyncPoints';
11import { buildKeyType } from 'lib/point';
13import { useWallet } from 'store/wallet';
15import { sigil, reactRenderer } from 'urbit-sigil-js';
17function makeSigil(size, patp, colors) {
18 const config = {
19 patp: patp,
20 colors: colors,
21 margin: 0,
22 renderer: reactRenderer,
23 };
25 // Planet
26 if (patp.length === 14) {
27 return sigil({
28 patp: patp,
29 size: size,
30 ...config,
31 });
32 }
33 // Star
34 if (patp.length === 7) {
35 return sigil({
36 patp: patp,
37 width: size * 2,
38 height: size,
39 full: true,
40 ...config,
41 });
42 }
43 // Galaxy
44 if (patp.length === 4) {
45 return sigil({
46 patp: patp,
47 size: size,
48 full: true,
49 ...config,
50 });
51 }
54const symbols = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
56/**
57 * Address is Maybe<string>
58 * point is Maybe<string>
59 * animationMode is 'slide' | 'step' | 'none'
60 *
61 */
62function Passport({
63 address,
64 point,
65 inverted,
66 animationMode = 'none',
67 keyType,
68}) {
69 const [cols, rows, tile] = [35, 12, 12];
71 const loading = Nothing.hasInstance(address) || Nothing.hasInstance(point);
72 const defaultMatrix = () => {
73 return Matrix.new(rows, cols);
74 };
76 const patp = ob.patp(point.value);
78 const makeMatrix = addr => {
79 // remove the 0x
80 const onlyTheNumberPart = addr.substring(2);
81 // make bigNum from hex
82 const int = new BN(onlyTheNumberPart, 16);
83 // make a bigger number
84 const big = int.pow(new BN(10));
85 // parse that to a string and pad
86 const b10 = big.toString(10).padStart(32, '0');
87 // split the big number into parts
88 const chunkSize = Math.ceil(b10.length / 8);
89 const chunks = chunkStr(b10, chunkSize).map(c => new BN(c));
90 // generate an empty matrix
91 const startMatrix = Matrix.new(rows, cols);
92 // do a few random walks starting at diff coordinates
93 const [, matrix] = chunks.reduce((acc, chunk) => {
94 return walk(chunk, acc[1]);
95 // start with the 'random' points (not actually random, they are deterministic)
96 }, rand(big, startMatrix));
98 return matrix;
99 };
101 const matrix = address.matchWith({
102 Just: ({ value }) => makeMatrix(value),
103 Nothing: defaultMatrix,
104 });
106 const bgColor = inverted ? 'white' : 'black';
108 const fgColor = inverted ? 'black' : 'white';
110 return (
111 <div
112 style={{
113 fontFamily: 'Inter',
114 backgroundColor: bgColor,
115 border: inverted ? '2px solid #E6E6E6' : '2px solid black',
116 borderRadius: '20px',
117 marginBottom: '16px',
118 width: `${16 + 16 + cols * tile}px`,
119 minWidth: `${16 + 16 + cols * tile}px`,
120 display: 'flex',
121 flexDirection: 'column',
122 }}>
123 <div
124 style={{
125 height: '64px',
126 padding: '16px',
127 display: 'flex',
128 }}>
129 <div
130 style={{
131 width: '100%',
132 display: 'flex',
133 alignItems: 'center',
134 }}>
135 {Just.hasInstance(point) && makeSigil(64, patp, [bgColor, fgColor])}
136 <div
137 style={{
138 marginLeft: '16px',
139 }}>
140 <div
141 style={{
142 fontWeight: '600',
143 color: fgColor,
144 fontSize: '16px',
145 fontFamily: 'Source Code Pro',
146 }}>
147 {Just.hasInstance(point) && patp}
148 </div>
149 {keyType !== '' ? (
150 <div
151 style={{
152 marginTop: '8px',
153 fontWeight: '500',
154 color: fgColor,
155 fontSize: '14px',
156 fontFamily: 'Source Code Pro',
157 }}>
158 {keyType}
159 </div>
160 ) : (
161 <div />
162 )}
163 </div>
164 </div>
165 </div>
166 <div
167 style={{
168 display: 'flex',
169 flexDirection: 'column',
170 padding: '16px',
171 paddingTop: '0px',
172 filter: inverted ? 'invert(1)' : '',
173 }}>
174 {matrix.map((row, _row) => {
175 return (
176 <div
177 key={`card:row:${_row}`}
178 style={{ display: 'flex', justifyContent: 'space-between' }}>
179 {row.map((v, _col) => {
180 return (
181 <Cell
182 row={_row}
183 col={_col}
184 value={typeof symbols[v] === 'undefined' ? 13 : v}
185 spriteSheet={'a'}
186 spriteSheetHeight={12}
187 spriteSheetWidth={168}
188 inverted={inverted}
189 loading={loading}
190 animationMode={animationMode}
191 />
192 );
193 })}
194 </div>
195 );
196 })}
197 </div>
198 </div>
199 );
202const Cell = props => {
203 return (
204 <div
205 key={`card:cell:${props.row}:${props.col}:${props.value}`}
206 className={
207 props.loading
208 ? 'blink'
209 : props.animationMode === 'none'
210 ? ''
211 : `${props.animationMode}-h-${props.value}`
212 }
213 style={{
214 backgroundImage: `url(/spritesheet_a.png)`,
215 backgroundPosition:
216 props.animationMode === 'none'
217 ? `-${props.spriteSheetHeight * props.value}px 0px`
218 : '',
219 backgroundSize: `${props.spriteSheetWidth}px ${props.spriteSheetHeight}px`,
220 width: `${props.spriteSheetHeight}px`,
221 maxWidth: `${props.spriteSheetHeight}px`,
222 height: `${props.spriteSheetHeight}px`,
223 maxHeight: `${props.spriteSheetHeight}px`,
224 animationDelay: `${props.value * 50}ms`,
225 }}
226 />
227 );
228};
230/**
231 * point is number
232 *
233 */
234function MiniPassport({ point, inverted, ...rest }) {
235 useSyncOwnedPoints([point]);
236 const { wallet } = useWallet();
237 const address = need.addressFromWallet(wallet);
239 const patp = ob.patp(point);
240 const permissions = usePermissionsForPoint(address, point);
241 const keyType = buildKeyType(permissions);
242 return (
243 <div
244 style={{
245 fontFamily: 'Inter',
246 backgroundColor: inverted ? 'white' : 'black',
247 border: inverted ? '2px solid #E6E6E6' : '2px solid black',
248 borderRadius: '20px',
249 marginBottom: '16px',
250 maxWidth: `${230}px`,
251 display: 'flex',
252 flexDirection: 'column',
253 }}
254 {...rest}>
255 <div
256 style={{
257 height: '44px',
258 padding: '16px',
259 display: 'flex',
260 }}>
261 <div
262 style={{
263 width: '100%',
264 display: 'flex',
265 alignItems: 'center',
266 }}>
267 {makeSigil(
268 44,
269 patp,
270 inverted ? ['white', 'black'] : ['black', 'white']
271 )}
272 <div
273 style={{
274 marginLeft: '16px',
275 display: 'flex',
276 flexDirection: 'column',
277 justifyContent: 'space-between',
278 }}>
279 <div
280 style={{
281 fontWeight: '600',
282 color: inverted ? 'black' : 'white',
283 fontSize: '14px',
284 fontFamily: 'Source Code Pro',
285 }}>
286 {patp}
287 </div>
288 {keyType !== '' ? (
289 <div
290 style={{
291 marginTop: '8px',
292 fontWeight: '500',
293 color: inverted ? 'black' : 'white',
294 fontSize: '12px',
295 fontFamily: 'Source Code Pro',
296 }}>
297 {keyType}
298 </div>
299 ) : (
300 <div />
301 )}
302 </div>
303 </div>
304 </div>
305 </div>
306 );
309Passport.Mini = MiniPassport;
311export default Passport;