on 3360534 help sign in
bridge/src/Bridge.js
295 lines 8.5 kB history raw
1import Maybe from 'folktale/maybe'
2import * as lodash from 'lodash'
3import { Stack } from 'immutable'
4import React from 'react'
6import Footer from './components/Footer'
7import Header from './components/Header'
8import { Container, Row, Col } from './components/Base'
9import { AppNavigation } from './components/AppNavigation'
11import { ROUTE_NAMES, router } from './lib/router'
12import { NETWORK_NAMES } from './lib/network'
13import { WALLET_NAMES, DEFAULT_HD_PATH } from './lib/wallet'
14import { BRIDGE_ERROR } from './lib/error'
16class Bridge extends React.Component {
18 constructor(props) {
19 super(props)
21 const networkType =
22 process.env.NODE_ENV === 'development'
23 ? NETWORK_NAMES.LOCAL
24 : NETWORK_NAMES.MAINNET
26 // NB (jtobin):
27 //
28 // Note that the 'wallet' prop has type depending on the 'walletType' prop.
29 // These could be bound together in a single structure (so that
30 // 'walletType' acted more explicitly as a data constructor of sorts) but
31 // it doesn't necessarily help us much, since we branch on 'walletType'
32 // before setting 'wallet'.
33 //
34 // Wallets are always wrapped in Maybe. For most authentication
35 // mechanisms, Maybe wraps a BIP32 HD wallet; for Ethereum private keys,
36 // JSON keystore files, and Metamask authentication, it wraps an
37 // 'EthereumWallet'.
39 this.state = {
40 // routes
41 routeCrumbs: Stack([ ROUTE_NAMES.LANDING ]),
42 routeData: {},
43 // network
44 networkType: networkType,
45 web3: Maybe.Nothing(),
46 contracts: Maybe.Nothing(),
47 // wallet
48 walletType: WALLET_NAMES.MNEMONIC,
49 wallet: Maybe.Nothing(),
50 walletHdPath: DEFAULT_HD_PATH,
51 // urbit wallet-related
52 urbitWallet: Maybe.Nothing(),
53 authMnemonic: Maybe.Nothing(),
54 networkSeedCache: null,
55 // point
56 pointCursor: Maybe.Nothing(),
57 pointCache: {},
58 // txn
59 txnHashCursor: Maybe.Nothing()
60 }
62 this.pushRoute = this.pushRoute.bind(this)
63 this.popRoute = this.popRoute.bind(this)
64 this.skipRoute = this.skipRoute.bind(this)
65 this.setNetworkType = this.setNetworkType.bind(this)
66 this.setNetwork = this.setNetwork.bind(this)
67 this.setWalletType = this.setWalletType.bind(this)
68 this.setWallet = this.setWallet.bind(this)
69 this.setWalletHdPath = this.setWalletHdPath.bind(this)
70 this.setUrbitWallet = this.setUrbitWallet.bind(this)
71 this.setAuthMnemonic = this.setAuthMnemonic.bind(this)
72 this.setPointCursor = this.setPointCursor.bind(this)
73 this.setTxnHashCursor = this.setTxnHashCursor.bind(this)
74 this.setNetworkSeedCache = this.setNetworkSeedCache.bind(this)
75 this.addToPointCache = this.addToPointCache.bind(this)
76 }
78 componentDidMount() {
79 window.history.pushState(null, null, null);
81 window.onpopstate = (e) => {
82 window.history.pushState(null, null, null);
83 this.popRoute();
84 }
86 // NB (jtobin):
87 //
88 // If running in development, the following can be tweaked to get you set
89 // up with a desirable initial state.
91 // if (process.env.NODE_ENV === 'development') {
92 //
93 // const socket = 'ws://localhost:8545'
94 // const provider = new Web3.providers.WebsocketProvider(socket)
95 // const web3 = new Web3(provider)
96 // const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.DEV)
97 //
98 // const mnemonic = process.env.REACT_APP_DEV_MNEMONIC
99 // const hdpath = process.env.REACT_APP_HD_PATH
100 //
101 // this.setState({
102 // routeCrumbs: Stack([
103 // // ROUTE_NAMES.CREATE_GALAXY,
104 // // ROUTE_NAMES.SHIP,
105 // ROUTE_NAMES.SHIPS,
106 // // ROUTE_NAMES.MNEMONIC,
107 // ROUTE_NAMES.WALLET,
108 // ROUTE_NAMES.NETWORK,
109 // ROUTE_NAMES.LANDING
110 // ]),
111 // networkType: NETWORK_NAMES.LOCAL,
112 // pointCursor: Maybe.Just(0),
113 // web3: Maybe.Just(web3),
114 // contracts: Maybe.Just(contracts),
115 // walletType: WALLET_NAMES.MNEMONIC,
116 // wallet: walletFromMnemonic(mnemonic, hdpath),
117 // urbitWallet: Maybe.Nothing(),
118 // authMnemonic: Maybe.Just('benefit crew supreme gesture quantum web media hazard theory mercy wing kitten')
119 // })
120 // }
123 pushRoute(symbol, routeData) {
124 if (lodash.includes(ROUTE_NAMES, symbol)) {
125 this.setState((state, _) => ({
126 routeCrumbs: state.routeCrumbs.push(symbol),
127 routeData: routeData
128 }));
130 // Scroll to top of page with each route transition.
131 window.scrollTo(0, 0);
133 } else {
134 throw BRIDGE_ERROR.INVALID_ROUTE
135 }
136 }
138 skipRoute(len) {
139 this.setState((state, _) => ({
140 routeCrumbs: state.routeCrumbs.skip(len)
141 }))
142 }
144 popRoute() {
145 this.setState((state, _) => ({
146 routeCrumbs: state.routeCrumbs.shift()
147 }))
148 }
150 setNetworkType(symbol) {
151 if (lodash.includes(NETWORK_NAMES, symbol)) {
152 this.setState({
153 networkType: symbol
154 })
155 } else {
156 throw BRIDGE_ERROR.INVALID_NETWORK_TYPE
157 }
158 }
160 setNetworkSeedCache(networkSeed) {
161 this.setState({
162 networkSeedCache: networkSeed
163 })
164 }
166 setNetwork(web3, contracts) {
167 this.setState({
168 web3: web3,
169 contracts: contracts
170 })
171 }
173 setWalletType(symbol) {
174 if (lodash.includes(WALLET_NAMES, symbol)) {
175 this.setState({
176 walletType: symbol
177 })
178 } else {
179 throw BRIDGE_ERROR.INVALID_WALLET_TYPE
180 }
181 }
183 setWallet(wallet) {
184 this.setState({ wallet })
185 }
187 setWalletHdPath(walletHdPath) {
188 this.setState({ walletHdPath })
189 }
191 setUrbitWallet(urbitWallet) {
192 this.setState({ urbitWallet })
193 }
195 setAuthMnemonic(authMnemonic) {
196 this.setState({ authMnemonic })
197 }
199 setPointCursor(pointCursor) {
200 this.setState({ pointCursor })
201 }
203 addToPointCache(entry) {
204 this.setState((state, _) => ({
205 pointCache: Object.assign(state.pointCache, entry)
206 }))
207 }
209 setTxnHashCursor(txnHashCursor) {
210 this.setState({ txnHashCursor })
211 }
213 render() {
214 const {
215 routeCrumbs,
216 routeData,
217 networkType,
218 walletType,
219 walletHdPath,
220 wallet,
221 urbitWallet,
222 authMnemonic,
223 networkSeedCache,
224 pointCursor,
225 pointCache,
226 txnHashCursor,
227 web3,
228 contracts
229 } = this.state
231 const View = router(routeCrumbs.peek())
233 return (
234 <React.Fragment>
235 <Container>
236 <Row>
237 <Col className='col-md-1' />
238 <Col className='col-md-10' style={{maxWidth: '620px'}}>
239 <Header
240 routeCrumbs={ routeCrumbs }
241 skipRoute={ this.skipRoute }
242 networkType={ networkType }
243 wallet={ wallet }
244 pointCursor={ pointCursor } />
246 <Row className={ 'row wrapper' }>
248 <View
249 // router
250 pushRoute={ this.pushRoute }
251 popRoute={ this.popRoute }
252 routeData={ routeData }
253 // network
254 setNetworkType={ this.setNetworkType }
255 setNetwork={ this.setNetwork }
256 networkType={ networkType }
257 web3={ web3 }
258 contracts={ contracts }
259 // wallet
260 setWalletType={ this.setWalletType }
261 setWalletHdPath={ this.setWalletHdPath }
262 setWallet={ this.setWallet }
263 walletType={ walletType }
264 walletHdPath={ walletHdPath }
265 wallet={ wallet }
266 // urbit wallet
267 urbitWallet={ urbitWallet }
268 setUrbitWallet={ this.setUrbitWallet }
269 authMnemonic={ authMnemonic }
270 setAuthMnemonic={ this.setAuthMnemonic }
271 // point
272 setPointCursor={ this.setPointCursor }
273 addToPointCache={ this.addToPointCache }
274 pointCursor={ pointCursor }
275 pointCache={ pointCache }
276 networkSeedCache= { networkSeedCache }
277 setNetworkSeedCache= { this.setNetworkSeedCache }
278 // txn
279 setTxnHashCursor={ this.setTxnHashCursor }
280 txnHashCursor={ txnHashCursor } />
282 <div className={'push'} />
284 </Row>
286 <Footer />
287 </Col>
288 </Row>
289 </Container>
290 </React.Fragment>
291 )
292 }
295export default Bridge