1import * as need from 'lib/need';
2import * as ob from 'urbit-ob';
3import { WARNING } from 'form/helpers';
4import { FORM_ERROR } from 'final-form';
5import { Just } from 'folktale/maybe';
6import BridgeForm from 'form/BridgeForm';
16 buildCheckboxValidator,
17} from 'form/validators';
18import FormError from 'form/FormError';
19import { Grid } from 'indigo-react';
20import { useCallback, useMemo, useRef } from 'react';
21import { useActivateFlow } from './useActivateFlow';
22import WarningBox from 'components/WarningBox';
23import useRoller from 'lib/useRoller';
24import useImpliedTicket from 'lib/useImpliedTicket';
25import { generateWallet, makeTicket } from 'lib/walletgen';
26import { urbitWalletFromTicket, walletFromMnemonic } from 'lib/wallet';
27import { DEFAULT_HD_PATH, POINT_PROXIES } from 'lib/constants';
28import useBreakpoints from 'lib/useBreakpoints';
29import { Ship } from '@urbit/roller-api';
30import { convertToInt } from 'lib/convertToInt';
31import { Box } from '@tlon/indigo-react';
32import { timeout } from 'lib/timeout';
33import withFadeable from './withFadeable';
34import ActivateButton from './ActivateButton';
35import TicketInputAccessory from 'components/form/TicketInputAccessory';
36import { validateActivationTicket } from 'lib/validators';
38interface ActivateCodeFormProps {
39 afterSubmit: VoidFunction;
42const ActivateCodeForm = ({ afterSubmit }: ActivateCodeFormProps) => {
43 const { api, getPoints } = useRoller();
44 const { impliedTicket, impliedPatp } = useImpliedTicket();
45 const didWarn = useRef<boolean>(false);
50 setDerivedPointDominion,
51 setInviteMasterTicketWallet,
55 }: any = useActivateFlow();
57 // this is a pretty naive way to detect if we're on a mobile device
58 // (i.e. we're checking the width of the screen)
59 // but it will suffice for the 99% case and if someone wants to get around it
60 // well by golly they're allowed to turn their phone into landscape mode
63 const activationAllowed = useBreakpoints([false, true, true]);
65 const validateForm = useCallback(
67 didWarn.current = false;
69 if (hasErrors(errors)) {
76 const validate = useMemo(
80 ticket: buildPatqValidator([validateActivationTicket]),
81 showTicket: buildCheckboxValidator(),
88 const hasInitialValues = useMemo(
89 () => Boolean(impliedTicket && impliedPatp),
90 [impliedTicket, impliedPatp]
93 const getTicketAndPoint = useCallback(
95 const segments = invite.split('-');
97 ticket: segments.slice(0, 4).join('-'),
99 ? ob.patp2dec(impliedPatp)
100 : ob.patp2dec(`~${segments.slice(4, segments.length).join('-')}`),
106 const loadPoints = useCallback(
107 async (address: string): Promise<Ship[]> => {
108 const owned = await getPoints(POINT_PROXIES.OWN, address);
109 const transferring = await getPoints(POINT_PROXIES.TRANSFER, address);
110 const incoming = [...owned, ...transferring];
111 console.log('incoming points', incoming);
117 // derive and set our state on submission
118 const onSubmit = useCallback(
120 // Allow UI animation to start
124 const { ticket, point } = getTicketAndPoint(
125 values.ticket || impliedTicket
128 const inviteWallet = Just(await urbitWalletFromTicket(ticket, point));
129 setInviteWallet(inviteWallet);
130 const _inviteWallet: UrbitWallet = need.wallet(inviteWallet);
131 const inviteAddress = _inviteWallet.ownership.keys.address;
133 const sendWallet = await walletFromMnemonic(
134 _inviteWallet.ownership.seed,
137 setSendWallet(sendWallet);
139 let incoming = await loadPoints(inviteAddress);
142 if (incoming.length > 0) {
143 if (incoming.length > 1 && !didWarn.current) {
144 didWarn.current = true;
148 'This invite code has multiple points available. ' +
149 "Once you've activated this point, " +
150 'activate the next with the same process.',
154 const point = convertToInt(incoming[0], 10);
155 const rollerPoint = await api.getPoint(point!);
157 setDerivedPatp(Just(ob.patp(point)));
158 setDerivedPoint(Just(point));
159 setDerivedPointDominion(Just(rollerPoint.dominion));
161 const newMasterTicket = await makeTicket(point);
162 setInviteMasterTicketWallet(
163 Just(await generateWallet(point, newMasterTicket, true))
170 'Invite code has no claimable point.\n' +
171 'Check your invite code and try again?',
184 setDerivedPointDominion,
185 setInviteMasterTicketWallet,
190 const initialValues = useMemo(
192 ticket: hasInitialValues
193 ? `${impliedTicket}-${impliedPatp?.slice(1)}`
197 [impliedTicket, impliedPatp, hasInitialValues]
202 className="w-full h-full flex-col"
204 justifyContent="flex-end">
208 afterSubmit={afterSubmit}
209 initialValues={initialValues}>
210 {({ values, valid, submitting, handleSubmit }: any) => (
212 {!hasInitialValues && (
217 accessory={<TicketInputAccessory name="showTicket" />}
218 type={values.showTicket ? 'text' : 'password'}
220 disabled={!activationAllowed}
221 placeholder={`${PLACEHOLDER_TICKET}-${PLACEHOLDER_PLANET.slice(
227 <Grid.Item full as={FormError} className="mb2" />
230 disabled={(!valid && !hasInitialValues) || submitting}
231 onClick={handleSubmit}>
232 {submitting ? 'Claiming...' : 'Claim'}
235 {!activationAllowed && (
236 <Grid.Item full as={WarningBox} className="mt4">
237 For your security, please access Bridge on a desktop device.
247export default ActivateCodeForm;
249export const FadeableActivateCodeForm = withFadeable(ActivateCodeForm);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list