on master help sign in
fix: correctly reset store and set wallet type for new login flow
commita3bbc03a66e1194fbd3aca251780f67eb5fa876e authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 25 Jun 2019, 11:02 (7 years ago) parentadbcda8
10 files changed+81−24
src/lib/useWalletType.js+11 src/store/wallet.js+25−3 src/views/Login.js+2−3 src/views/Login/Keystore.js+7−3 src/views/Login/Ledger.js+7−2 src/views/Login/Mnemonic.js+5−2 src/views/Login/PrivateKey.js+5−2 src/views/Login/Shards.js+6−2 src/views/Login/Ticket.js+7−6 src/views/Login/Trezor.js+6−1
src/lib/useWalletType.jsnew file+11
@@ -0,0 +1,11 @@
1+import { useEffect } from 'react';
2+
3+import { useWallet } from 'store/wallet';
4+
5+export default function useWalletType(walletType) {
6+ const { setWalletType } = useWallet();
7+
8+ useEffect(() => {
9+ setWalletType(walletType);
10+ }, [setWalletType, walletType]);
11+}
src/store/wallet.js+25−3
@@ -13,11 +13,13 @@ import {
1313 DEFAULT_HD_PATH,
1414 addressFromSecp256k1Public,
1515 walletFromMnemonic,
16−} from '../lib/wallet';
17−import { BRIDGE_ERROR } from '../lib/error';
16+} from 'lib/wallet';
17+import { BRIDGE_ERROR } from 'lib/error';
1818
1919export const WalletContext = createContext(null);
2020
21+const kDefaultWalletType = WALLET_TYPES.TICKET;
22+
2123// NB (jtobin):
2224//
2325// Note that the 'wallet' prop has type depending on the 'walletType' prop.
@@ -34,7 +36,7 @@ function _useWallet(
3436 initialWallet = Maybe.Nothing(),
3537 initialMnemonic = Maybe.Nothing()
3638) {
37− const [walletType, _setWalletType] = useState(WALLET_TYPES.MNEMONIC);
39+ const [walletType, _setWalletType] = useState(kDefaultWalletType);
3840 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
3941 const [wallet, _setWallet] = useState(initialWallet);
4042 const [urbitWallet, _setUrbitWallet] = useState(Maybe.Nothing());
@@ -93,6 +95,24 @@ function _useWallet(
9395 [setWallet]
9496 );
9597
98+ const resetWallet = useCallback(() => {
99+ _setWalletType(kDefaultWalletType);
100+ setWalletHdPath(DEFAULT_HD_PATH);
101+ _setWallet(Maybe.Nothing());
102+ _setUrbitWallet(Maybe.Nothing());
103+ setAuthMnemonic(Maybe.Nothing());
104+ setNetworkSeed(Maybe.Nothing());
105+ setNetworkRevision(Maybe.Nothing());
106+ }, [
107+ _setWalletType,
108+ setWalletHdPath,
109+ _setWallet,
110+ _setUrbitWallet,
111+ setAuthMnemonic,
112+ setNetworkSeed,
113+ setNetworkRevision,
114+ ]);
115+
96116 return {
97117 //
98118 walletType,
@@ -113,6 +133,8 @@ function _useWallet(
113133 setNetworkSeed,
114134 networkRevision,
115135 setNetworkRevision,
136+ //
137+ resetWallet,
116138 };
117139}
118140
src/views/Login.js+2−3
@@ -41,7 +41,7 @@ export default function Login() {
4141 // globals
4242 const history = useHistory();
4343 const { contracts } = useNetwork();
44− const { wallet, setUrbitWallet } = useWallet();
44+ const { wallet, resetWallet } = useWallet();
4545 const { pointCursor, setPointCursor } = usePointCursor();
4646
4747 // inputs
@@ -49,8 +49,7 @@ export default function Login() {
4949
5050 // on-mount
5151 useLifecycle(() => {
52− setUrbitWallet(Nothing());
53− setPointCursor(Nothing());
52+ resetWallet();
5453 });
5554
5655 const doContinue = async () => {
src/views/Login/Keystore.js+7−3
@@ -2,18 +2,22 @@ import { Just, Nothing } from 'folktale/maybe';
22import { Ok, Error } from 'folktale/result';
33import React, { useState } from 'react';
44import * as keythereum from 'keythereum';
5+import { H3, Input } from 'indigo-react';
56
67import View from 'components/View';
78import { usePassphraseInput } from 'components/Inputs';
89import { ForwardButton } from 'components/Buttons';
9−import { H3, Input } from 'indigo-react';
1010import { InputCaption, UploadButton, Warning } from 'components/old/Base';
1111
12−import * as need from 'lib/need';
13−import { EthereumWallet } from 'lib/wallet';
1412import { useWallet } from 'store/wallet';
1513
14+import * as need from 'lib/need';
15+import { EthereumWallet, WALLET_TYPES } from 'lib/wallet';
16+import useWalletType from 'lib/useWalletType';
17+
1618export default function Keystore({ loginCompleted }) {
19+ useWalletType(WALLET_TYPES.KEYSTORE);
20+
1721 // globals
1822 const { wallet, setWallet } = useWallet();
1923
src/views/Login/Ledger.js+7−2
@@ -5,15 +5,18 @@ import Maybe from 'folktale/maybe';
55import Transport from '@ledgerhq/hw-transport-u2f';
66import Eth from '@ledgerhq/hw-app-eth';
77import * as secp256k1 from 'secp256k1';
8+import { H1, H2, P, Input } from 'indigo-react';
89
910import View from 'components/View';
1011import { ForwardButton } from 'components/Buttons';
11−import { H1, H2, P, Input } from 'indigo-react';
1212import { InnerLabelDropdown } from 'components/old/Base';
1313
14−import { LEDGER_LIVE_PATH, LEDGER_LEGACY_PATH } from 'lib/ledger';
1514import { useWallet } from 'store/wallet';
1615
16+import { LEDGER_LIVE_PATH, LEDGER_LEGACY_PATH } from 'lib/ledger';
17+import useWalletType from 'lib/useWalletType';
18+import { WALLET_TYPES } from 'lib/wallet';
19+
1720const pathOptions = [
1821 { title: 'Ledger Live', value: LEDGER_LIVE_PATH },
1922 { title: 'Ledger Legacy', value: LEDGER_LEGACY_PATH },
@@ -25,6 +28,8 @@ const chopHdPrefix = str => (str.slice(0, 2) === 'm/' ? str.slice(2) : str);
2528const addHdPrefix = str => (str.slice(0, 2) === 'm/' ? str : 'm/' + str);
2629
2730export default function Ledger({ loginCompleted }) {
31+ useWalletType(WALLET_TYPES.LEDGER);
32+
2833 const { wallet, setWallet, setWalletHdPath } = useWallet();
2934
3035 const [basePath, setBasePath] = useState(LEDGER_LIVE_PATH);
src/views/Login/Mnemonic.js+5−2
@@ -1,5 +1,6 @@
11import React, { useCallback, useEffect } from 'react';
22import { Just, Nothing } from 'folktale/maybe';
3+import { Input } from 'indigo-react';
34
45import View from 'components/View';
56import {
@@ -9,11 +10,13 @@ import {
910} from 'components/Inputs';
1011import { ForwardButton } from 'components/Buttons';
1112
12−import { walletFromMnemonic } from 'lib/wallet';
1313import { useWallet } from 'store/wallet';
14−import { Input } from 'indigo-react';
14+
15+import { walletFromMnemonic, WALLET_TYPES } from 'lib/wallet';
16+import useWalletType from 'lib/useWalletType';
1517
1618export default function Mnemonic({ advanced, loginCompleted }) {
19+ useWalletType(WALLET_TYPES.MNEMONIC);
1720 const {
1821 wallet,
1922 setWallet,
src/views/Login/PrivateKey.js+5−2
@@ -1,15 +1,18 @@
11import { Just, Nothing } from 'folktale/maybe';
22import React, { useEffect } from 'react';
3+import { Input } from 'indigo-react';
34
45import View from 'components/View';
56import { usePassphraseInput } from 'components/Inputs';
67import { ForwardButton } from 'components/Buttons';
78
8−import { EthereumWallet } from 'lib/wallet';
99import { useWallet } from 'store/wallet';
10−import { Input } from 'indigo-react';
10+
11+import { EthereumWallet, WALLET_TYPES } from 'lib/wallet';
12+import useWalletType from 'lib/useWalletType';
1113
1214export default function PrivateKey({ loginCompleted }) {
15+ useWalletType(WALLET_TYPES.PRIVATE_KEY);
1316 const { wallet, setWallet } = useWallet();
1417
1518 const privateKeyInput = usePassphraseInput({
src/views/Login/Shards.js+6−2
@@ -1,6 +1,7 @@
11import { Just, Nothing } from 'folktale/maybe';
22import React from 'react';
33import * as kg from 'urbit-key-generation/dist/index';
4+import { Input } from 'indigo-react';
45
56import View from 'components/View';
67import {
@@ -10,11 +11,14 @@ import {
1011} from 'components/Inputs';
1112import { ForwardButton } from 'components/Buttons';
1213
13−import { urbitWalletFromTicket } from 'lib/wallet';
1414import { useWallet } from 'store/wallet';
15−import { Input } from 'indigo-react';
15+
16+import { urbitWalletFromTicket, WALLET_TYPES } from 'lib/wallet';
17+import useWalletType from 'lib/useWalletType';
1618
1719export default function Shards({ loginCompleted }) {
20+ useWalletType(WALLET_TYPES.SHARDS);
21+
1822 const { wallet, setUrbitWallet } = useWallet();
1923
2024 const pointInput = usePointInput({
src/views/Login/Ticket.js+7−6
@@ -2,6 +2,7 @@ import { Just, Nothing } from 'folktale/maybe';
22import React, { useCallback, useState, useEffect } from 'react';
33import * as azimuth from 'azimuth-js';
44import * as ob from 'urbit-ob';
5+import { Input } from 'indigo-react';
56
67import View from 'components/View';
78import {
@@ -11,13 +12,13 @@ import {
1112} from 'components/Inputs';
1213import { ForwardButton } from 'components/Buttons';
1314
14−import * as need from 'lib/need';
15−import { WALLET_TYPES, urbitWalletFromTicket } from 'lib/wallet';
16−
1715import { useNetwork } from 'store/network';
1816import { useWallet } from 'store/wallet';
1917import { usePointCursor } from 'store/pointCursor';
20−import { Input } from 'indigo-react';
18+
19+import * as need from 'lib/need';
20+import { WALLET_TYPES, urbitWalletFromTicket } from 'lib/wallet';
21+import useWalletType from 'lib/useWalletType';
2122
2223//TODO should be part of InputWithStatus component
2324const INPUT_STATUS = {
@@ -27,9 +28,10 @@ const INPUT_STATUS = {
2728};
2829
2930export default function Ticket({ advanced, loginCompleted }) {
31+ useWalletType(WALLET_TYPES.TICKET);
3032 // globals
3133 const { contracts } = useNetwork();
32− const { wallet, setWalletType, setUrbitWallet } = useWallet();
34+ const { wallet, setUrbitWallet } = useWallet();
3335 const { setPointCursor } = usePointCursor();
3436
3537 // inputs
@@ -100,7 +102,6 @@ export default function Ticket({ advanced, loginCompleted }) {
100102 };
101103
102104 const doContinue = () => {
103− setWalletType(WALLET_TYPES.TICKET);
104105 setPointCursor(Just(ob.patp2dec(pointName)));
105106 loginCompleted();
106107 };
src/views/Login/Trezor.js+6−1
@@ -4,13 +4,16 @@ import Maybe from 'folktale/maybe';
44import { times } from 'lodash';
55import TrezorConnect from 'trezor-connect';
66import * as secp256k1 from 'secp256k1';
7+import { H1, P, Input } from 'indigo-react';
78
89import View from 'components/View';
910import { ForwardButton } from 'components/Buttons';
10−import { H1, P, Input } from 'indigo-react';
1111import { InnerLabelDropdown } from 'components/old/Base';
1212
1313import { TREZOR_PATH } from 'lib/trezor';
14+import useWalletType from 'lib/useWalletType';
15+import { WALLET_TYPES } from 'lib/wallet';
16+
1417import { useWallet } from 'store/wallet';
1518
1619const accountOptions = [
@@ -19,6 +22,8 @@ const accountOptions = [
1922];
2023
2124export default function Trezor({ loginCompleted }) {
25+ useWalletType(WALLET_TYPES.TREZOR);
26+
2227 const { wallet, setWallet, setWalletHdPath } = useWallet();
2328
2429 const [hdPath, setHdPath] = useState(TREZOR_PATH.replace(/x/g, 0));