on master help sign in
feat: extract urbitWallet and authMnemonic into state container
commitfeca660baa032ea4a3fbf360b67c3b8ce46c3bf0 authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateMon 17 Jun 2019, 11:13 (7 years ago) parent1faa15c
3 files changed+62−62
src/bridge/Bridge.js+24−54 src/bridge/store/wallet.js+35−7 src/bridge/views/InviteTicket.js+3−1
src/bridge/Bridge.js+24−54
@@ -9,10 +9,10 @@ import nest from './lib/nest';
99import { router } from './lib/router';
1010import { ROUTE_NAMES } from './lib/routeNames';
1111import { NETWORK_TYPES } from './lib/network';
12−import { DEFAULT_HD_PATH, walletFromMnemonic } from './lib/wallet';
12+import { walletFromMnemonic } from './lib/wallet';
1313import { isDevelopment } from './lib/flags';
1414
15−import { HistoryProvider, withHistory, useHistory } from './store/history';
15+import { HistoryProvider, useHistory } from './store/history';
1616import { TxnConfirmationsProvider } from './store/txnConfirmations';
1717import { OnlineProvider } from './store/online';
1818import { NetworkProvider } from './store/network';
@@ -42,6 +42,9 @@ const kInitialWallet = kIsStubbed
4242 process.env.REACT_APP_HD_PATH
4343 )
4444 : undefined;
45+const kInitialMnemonic = kIsStubbed
46+ ? process.env.REACT_APP_DEV_MNEMONIC
47+ : Nothing();
4548
4649// the router itself is just a component that renders a specific view
4750// depending on the history
@@ -52,23 +55,26 @@ const Router = function(props) {
5255 return <Route {...props} />;
5356};
5457
55−// NB(shrugs): separate component because it needs withHistory
58+// NB(shrugs): separate component because it needs useHistory
5659// this will be must better structured as part of the UI overhaul
57−const VariableWidthColumn = withHistory(({ history, children }) => (
60+const VariableWidthColumn = function({ children }) {
61+ const history = useHistory();
5862 // For the invite acceptance flow, widen the screen to use the full
5963 // container, and hide the breadcrumbs
60− <Col
61− className={
62− history.includes(ROUTE_NAMES.INVITE_TICKET)
63− ? 'col-md-12'
64− : 'col-md-offset-1 col-md-10'
65− }
66− style={
67− history.includes(ROUTE_NAMES.INVITE_TICKET) ? {} : { maxWidth: '620px' }
68− }>
69− {children}
70− </Col>
71−));
64+ return (
65+ <Col
66+ className={
67+ history.includes(ROUTE_NAMES.INVITE_TICKET)
68+ ? 'col-md-12'
69+ : 'col-md-offset-1 col-md-10'
70+ }
71+ style={
72+ history.includes(ROUTE_NAMES.INVITE_TICKET) ? {} : { maxWidth: '620px' }
73+ }>
74+ {children}
75+ </Col>
76+ );
77+};
7278
7379// nest all of the providers within each other to avoid hella depth
7480const AllProviders = nest([
@@ -84,9 +90,6 @@ class Bridge extends React.Component {
8490 super(props);
8591
8692 this.state = {
87− // urbit wallet-related
88− urbitWallet: Nothing(),
89− authMnemonic: Nothing(),
9093 networkSeedCache: null,
9194 // point
9295 pointCursor: Nothing(),
@@ -96,8 +99,6 @@ class Bridge extends React.Component {
9699 txnConfirmations: {},
97100 };
98101
99− this.setUrbitWallet = this.setUrbitWallet.bind(this);
100− this.setAuthMnemonic = this.setAuthMnemonic.bind(this);
101102 this.setPointCursor = this.setPointCursor.bind(this);
102103 this.setTxnHashCursor = this.setTxnHashCursor.bind(this);
103104 this.setNetworkSeedCache = this.setNetworkSeedCache.bind(this);
@@ -110,12 +111,8 @@ class Bridge extends React.Component {
110111 // If running in development, the following can be tweaked to get you set
111112 // up with a desirable initial state.
112113 if (kIsStubbed) {
113− const mnemonic = process.env.REACT_APP_DEV_MNEMONIC;
114−
115114 this.setState({
116115 pointCursor: Just(0),
117− urbitWallet: Nothing(),
118− authMnemonic: Just(mnemonic),
119116 });
120117 }
121118 }
@@ -127,28 +124,6 @@ class Bridge extends React.Component {
127124 });
128125 }
129126
130− // also sets wallet to ownership address
131− setUrbitWallet(urbitWallet) {
132− let wallet = this.state.wallet;
133− if (Just.hasInstance(urbitWallet)) {
134− const mnemonic = urbitWallet.value.ownership.seed;
135− wallet = walletFromMnemonic(
136− mnemonic,
137− DEFAULT_HD_PATH,
138− urbitWallet.value.meta.passphrase
139− );
140− wallet.map(wal => {
141− wal.address = urbitWallet.value.ownership.keys.address;
142− return wal;
143− });
144− }
145− this.setState({ urbitWallet, wallet });
146− }
147−
148− setAuthMnemonic(authMnemonic) {
149− this.setState({ authMnemonic });
150− }
151−
152127 setPointCursor(pointCursor) {
153128 this.setState({ pointCursor });
154129 }
@@ -165,8 +140,6 @@ class Bridge extends React.Component {
165140
166141 render() {
167142 const {
168− urbitWallet,
169− authMnemonic,
170143 networkSeedCache,
171144 networkRevisionCache,
172145 pointCursor,
@@ -178,7 +151,8 @@ class Bridge extends React.Component {
178151 <AllProviders
179152 initialRoutes={kInitialRoutes}
180153 initialNetworkType={kInitialNetworkType}
181− initialWallet={kInitialWallet}>
154+ initialWallet={kInitialWallet}
155+ initialMnemonic={kInitialMnemonic}>
182156 <Container>
183157 <Row>
184158 <VariableWidthColumn>
@@ -186,10 +160,6 @@ class Bridge extends React.Component {
186160
187161 <Row className={'row wrapper'}>
188162 <Router
189− urbitWallet={urbitWallet}
190− setUrbitWallet={this.setUrbitWallet}
191− authMnemonic={authMnemonic}
192− setAuthMnemonic={this.setAuthMnemonic}
193163 setPointCursor={this.setPointCursor}
194164 addToPointCache={this.addToPointCache}
195165 pointCursor={pointCursor}
src/bridge/store/wallet.js+35−7
@@ -6,6 +6,7 @@ import {
66 WALLET_TYPES,
77 DEFAULT_HD_PATH,
88 addressFromSecp256k1Public,
9+ walletFromMnemonic,
910} from '../lib/wallet';
1011import { BRIDGE_ERROR } from '../lib/error';
1112
@@ -23,10 +24,15 @@ export const WalletContext = createContext(null);
2324// mechanisms, Maybe wraps a BIP32 HD wallet; for Ethereum private keys,
2425// JSON keystore files, and Metamask authentication, it wraps an
2526// 'EthereumWallet'.
26−function _useWallet(initialWallet = Maybe.Nothing()) {
27+function _useWallet(
28+ initialWallet = Maybe.Nothing(),
29+ initialMnemonic = Maybe.Nothing()
30+) {
2731 const [walletType, _setWalletType] = useState(WALLET_TYPES.MNEMONIC);
2832 const [walletHdPath, setWalletHdPath] = useState(DEFAULT_HD_PATH);
2933 const [wallet, _setWallet] = useState(initialWallet);
34+ const [urbitWallet, _setUrbitWallet] = useState(Maybe.Nothing());
35+ const [authMnemonic, setAuthMnemonic] = useState(initialMnemonic);
3036
3137 const setWalletType = walletType => {
3238 if (!includes(WALLET_TYPES, walletType)) {
@@ -46,6 +52,28 @@ function _useWallet(initialWallet = Maybe.Nothing()) {
4652 _setWallet(wallet);
4753 };
4854
55+ const setUrbitWallet = urbitWallet => {
56+ if (Maybe.Just.hasInstance(urbitWallet)) {
57+ // when an urbit wallet is set, also derive
58+ // a normal bip32 wallet using the ownership address
59+ const wallet = walletFromMnemonic(
60+ urbitWallet.value.ownership.seed,
61+ DEFAULT_HD_PATH,
62+ urbitWallet.value.meta.passphrase
63+ );
64+
65+ // manually set the public address
66+ wallet.map(wal => {
67+ wal.address = urbitWallet.value.ownership.keys.address;
68+ return wal;
69+ });
70+
71+ setWallet(wallet);
72+ }
73+
74+ _setUrbitWallet(urbitWallet);
75+ };
76+
4977 return {
5078 //
5179 walletType,
@@ -57,15 +85,15 @@ function _useWallet(initialWallet = Maybe.Nothing()) {
5785 wallet,
5886 setWallet,
5987 //
60− // urbitWallet
61− // setUrbitWallet
62− // authMnemonic
63− // setAuthMnemonic
88+ urbitWallet,
89+ setUrbitWallet,
90+ authMnemonic,
91+ setAuthMnemonic,
6492 };
6593}
6694
67−export function WalletProvider({ initialWallet, children }) {
68− const wallet = _useWallet(initialWallet);
95+export function WalletProvider({ initialWallet, initialMnemonic, children }) {
96+ const wallet = _useWallet(initialWallet, initialMnemonic);
6997
7098 return (
7199 <WalletContext.Provider value={wallet}>{children}</WalletContext.Provider>
src/bridge/views/InviteTicket.js+3−1
@@ -29,6 +29,7 @@ import {
2929} from '../lib/invite';
3030import { generateWallet, startTransactions } from '../lib/invite';
3131import { withNetwork } from '../store/network';
32+import { withWallet } from '../store/wallet';
3233
3334class InviteTicket extends React.Component {
3435 constructor(props) {
@@ -425,5 +426,6 @@ class InviteTicket extends React.Component {
425426
426427export default compose(
427428 withNetwork,
428− withHistory
429+ withHistory,
430+ withWallet
429431)(InviteTicket);