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';
7import { PLACEHOLDER_PLANET, PLACEHOLDER_TICKET, TicketInput } from 'form/Inputs';
12 buildCheckboxValidator,
13} from 'form/validators';
14import FormError from 'form/FormError';
15import { Grid } from 'indigo-react';
16import { useCallback, useMemo, useRef } from 'react';
17import { useActivateFlow } from './ActivateFlow';
18import WarningBox from 'components/WarningBox';
19import useRoller from 'lib/useRoller';
20import useImpliedTicket from 'lib/useImpliedTicket';
21import { generateWallet } from 'lib/walletgen';
22import { urbitWalletFromTicket, walletFromMnemonic } from 'lib/wallet';
23import { DEFAULT_HD_PATH, POINT_PROXIES } from 'lib/constants';
24import useBreakpoints from 'lib/useBreakpoints.tsx';
25import { Ship } from '@urbit/roller-api';
26import { convertToInt } from 'lib/convertToInt';
27import { Box } from '@tlon/indigo-react';
28import { timeout } from 'lib/timeout';
29import withFadeable from './withFadeable';
30import ActivateButton from './ActivateButton';
31import TicketInputAccessory from 'components/form/TicketInputAccessory';
32import { validateActivationTicket } from 'lib/validators';
34interface ActivateCodeFormProps {
35 afterSubmit: VoidFunction;
38const ActivateCodeForm = ({ afterSubmit }: ActivateCodeFormProps) => {
39 const { api, getPoints } = useRoller();
40 const { impliedTicket, impliedPatp } = useImpliedTicket();
41 const didWarn = useRef<boolean>(false);
46 setDerivedPointDominion,
51 }: any = useActivateFlow();
53 // this is a pretty naive way to detect if we're on a mobile device
54 // (i.e. we're checking the width of the screen)
55 // but it will suffice for the 99% case and if someone wants to get around it
56 // well by golly they're allowed to turn their phone into landscape mode
58 const activationAllowed = useBreakpoints([false, true, true]);
60 const validateForm = useCallback(
62 didWarn.current = false;
64 if (hasErrors(errors)) {
71 const validate = useMemo(
75 ticket: buildPatqValidator([validateActivationTicket]),
76 showTicket: buildCheckboxValidator(),
83 const hasInitialValues = useMemo(
84 () => Boolean(impliedTicket && impliedPatp),
85 [impliedTicket, impliedPatp]
88 const getTicketAndPoint = useCallback(
90 const segments = invite.split('-');
92 ticket: segments.slice(0, 4).join('-'),
94 ? ob.patp2dec(impliedPatp)
95 : ob.patp2dec(`~${segments.slice(4, segments.length).join('-')}`),
101 const loadPoints = useCallback(
102 async (address: string): Promise<Ship[]> => {
103 const owned = await getPoints(POINT_PROXIES.OWN, address);
104 const transferring = await getPoints(POINT_PROXIES.TRANSFER, address);
105 const incoming = [...owned, ...transferring];
106 console.log('incoming points', incoming);
112 // derive and set our state on submission
113 const onSubmit = useCallback(
115 // Allow UI animation to start
119 const { ticket, point } = getTicketAndPoint(
120 impliedTicket || values.ticket
123 const inviteWallet = Just(await urbitWalletFromTicket(ticket, point));
124 setInviteWallet(inviteWallet);
125 const _inviteWallet: UrbitWallet = need.wallet(inviteWallet);
126 const inviteAddress = _inviteWallet.ownership.keys.address;
128 const sendWallet = await walletFromMnemonic(
129 _inviteWallet.ownership.seed,
132 setSendWallet(sendWallet);
134 let incoming = await loadPoints(inviteAddress);
137 if (incoming.length > 0) {
138 if (incoming.length > 1 && !didWarn.current) {
139 didWarn.current = true;
143 'This invite code has multiple points available. ' +
144 "Once you've activated this point, " +
145 'activate the next with the same process.',
149 const point = convertToInt(incoming[0], 10);
150 const rollerPoint = await api.getPoint(point!);
152 setDerivedPatp(Just(ob.patp(point)));
153 setDerivedPoint(Just(point));
154 setDerivedPointDominion(Just(rollerPoint.dominion));
155 setDerivedWallet(Just(await generateWallet(point, ticket, true)));
161 'Invite code has no claimable point.\n' +
162 'Check your invite code and try again?',
173 setDerivedPointDominion,
181 const initialValues = useMemo(
183 ticket: hasInitialValues
184 ? `${impliedTicket}-${impliedPatp?.slice(1)}`
188 [impliedTicket, impliedPatp, hasInitialValues]
193 className="w-full h-full flex-col"
195 justifyContent="flex-end">
199 afterSubmit={afterSubmit}
200 initialValues={initialValues}>
201 {({ valid, validating, values, submitting, handleSubmit }) => (
203 {!hasInitialValues && (
208 accessory={<TicketInputAccessory name="showTicket" />}
209 type={values.showTicket ? 'text' : 'password'}
211 disabled={!activationAllowed}
212 placeholder={`${PLACEHOLDER_TICKET}-${PLACEHOLDER_PLANET.slice(
218 <Grid.Item full as={FormError} className="mb2" />
221 disabled={(!valid && !hasInitialValues) || submitting}
222 onClick={handleSubmit}>
223 {submitting ? 'Activating...' : 'Activate'}
226 {!activationAllowed && (
227 <Grid.Item full as={WarningBox} className="mt4">
228 For your security, please access Bridge on a desktop device.
238export default ActivateCodeForm;
240export 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