1import React, { useCallback, useEffect, useState } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import * as azimuth from 'azimuth-js';
4import { Grid, Input, H4, ErrorText } from 'indigo-react';
6import View from 'components/View';
7import { ForwardButton } from 'components/Buttons';
8import Passport from 'components/Passport';
10import { useHistory } from 'store/history';
12import { useTicketInput } from 'lib/useInputs';
13import * as need from 'lib/need';
14import { ROUTE_NAMES } from 'lib/routeNames';
15import { useSyncKnownPoints } from 'lib/useSyncPoints';
16import FooterButton from 'components/FooterButton';
17import { blinkIf } from 'components/Blinky';
18import { DEFAULT_HD_PATH, walletFromMnemonic } from 'lib/wallet';
19import { useNetwork } from 'store/network';
20import { generateWallet } from 'lib/invite';
21import { generateTemporaryOwnershipWallet } from 'lib/walletgen';
22import { useActivateFlow } from './ActivateFlow';
23import { useLocalRouter } from 'lib/LocalRouter';
24import useImpliedTicket from 'lib/useImpliedTicket';
25import useHasDisclaimed from 'lib/useHasDisclaimed';
26import useBreakpoints from 'lib/useBreakpoints';
27import WarningBox from 'components/WarningBox';
29export default function ActivateCode() {
30 const history = useHistory();
31 const { names, push } = useLocalRouter();
32 const { contracts } = useNetwork();
33 const impliedTicket = useImpliedTicket();
34 const [hasDisclaimed] = useHasDisclaimed();
35 const [generalError, setGeneralError] = useState();
36 const [deriving, setDeriving] = useState(false);
43 } = useActivateFlow();
44 // this is a pretty naive way to detect if we're on a mobile device
45 // (i.e. we're checking the width of the screen)
46 // but it will suffice for the 99% case and if someone wants to get around it
47 // well by golly they're allowed to turn their phone into landscape mode
49 const activationAllowed = useBreakpoints([false, true, true]);
51 const [ticketInput, { pass: validTicket, data: ticket }] = useTicketInput({
53 label: 'Activation Code',
54 initialValue: impliedTicket || '',
56 disabled: !activationAllowed,
59 const goToLogin = useCallback(() => history.popAndPush(ROUTE_NAMES.LOGIN), [
62 const goToPassport = useCallback(() => {
65 push(names.DISCLAIMER);
67 }, [names, push, hasDisclaimed]);
69 const pass = derivedWallet.matchWith({
76 const _contracts = need.contracts(contracts);
79 // when the ticket becomes valid, derive the point
81 const { seed } = await generateTemporaryOwnershipWallet(ticket);
83 //TODO isn't all this accessible in the ownership object?
84 const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
85 const _inviteWallet = need.wallet(inviteWallet);
87 const owned = await azimuth.azimuth.getOwnedPoints(
91 const transferring = await azimuth.azimuth.getTransferringFor(
95 const incoming = [...owned, ...transferring];
97 let realPoint = Nothing();
98 let wallet = Nothing();
100 if (incoming.length > 0) {
101 const pointNum = parseInt(incoming[0], 10);
102 realPoint = Just(pointNum);
103 wallet = Just(await generateWallet(pointNum));
105 if (incoming.length > 1) {
107 'This invite code has multiple points available.\n' +
108 "Once you've activated this point, activate the next with the same process."
111 setGeneralError(false);
115 'Invite code has no claimable point.\n' +
116 'Check your invite code and try again?'
120 setDerivedPoint(realPoint);
121 setDerivedWallet(wallet);
122 setInviteWallet(inviteWallet);
126 setGeneralError(false);
137 // when we know the derived point, ensure we have the data to display it
138 useSyncKnownPoints([derivedPoint.getOrElse(null)].filter(p => p !== null));
143 <Grid.Item as={Passport} point={derivedPoint} full />
144 <Grid.Item as={H4} className="mt3 mb2" full>
148 <Grid.Item as={Input} {...ticketInput} full />
152 <ErrorText>{generalError}</ErrorText>
159 disabled={!pass || deriving || !activationAllowed}
160 accessory={blinkIf(deriving)}
161 onClick={goToPassport}
164 {deriving && 'Deriving...'}
168 {!activationAllowed && (
169 <Grid.Item full as={WarningBox} className="mt4">
170 For your security, please access Bridge on a desktop device.
174 <FooterButton as={ForwardButton} onClick={goToLogin}>
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