on 133c307 help sign in
bridge/src/Bridge.js
290 lines 8.2 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'
10import { ROUTE_NAMES, router } from './lib/router'
11import { NETWORK_NAMES } from './lib/network'
12import { WALLET_NAMES, DEFAULT_HD_PATH } from './lib/wallet'
13import { BRIDGE_ERROR } from './lib/error'
15import './style/index.css'
17class Bridge extends React.Component {
19 constructor(props) {
20 super(props)
22 const networkType =
23 process.env.NODE_ENV === 'development'
24 ? NETWORK_NAMES.LOCAL
25 : NETWORK_NAMES.MAINNET
27 // NB (jtobin):
28 //
29 // Note that the 'wallet' prop has type depending on the 'walletType' prop.
30 // These could be bound together in a single structure (so that
31 // 'walletType' acted more explicitly as a data constructor of sorts) but
32 // it doesn't necessarily help us much, since we branch on 'walletType'
33 // before setting 'wallet'.
34 //
35 // Wallets are always wrapped in Maybe. For most authentication
36 // mechanisms, Maybe wraps a BIP32 HD wallet; for Ethereum private keys,
37 // JSON keystore files, and Metamask authentication, it wraps an
38 // 'EthereumWallet'.
40 this.state = {
41 // routes
42 routeCrumbs: Stack([ ROUTE_NAMES.LANDING ]),
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) {
124 if (lodash.includes(ROUTE_NAMES, symbol)) {
125 this.setState((state, _) => ({
126 routeCrumbs: state.routeCrumbs.push(symbol),
127 }));
129 // Scroll to top of page with each route transition.
130 window.scrollTo(0, 0);
132 } else {
133 throw BRIDGE_ERROR.INVALID_ROUTE
134 }
135 }
137 skipRoute(len) {
138 this.setState((state, _) => ({
139 routeCrumbs: state.routeCrumbs.skip(len)
140 }))
141 }
143 popRoute() {
144 this.setState((state, _) => ({
145 routeCrumbs: state.routeCrumbs.shift()
146 }))
147 }
149 setNetworkType(symbol) {
150 if (lodash.includes(NETWORK_NAMES, symbol)) {
151 this.setState({
152 networkType: symbol
153 })
154 } else {
155 throw BRIDGE_ERROR.INVALID_NETWORK_TYPE
156 }
157 }
159 setNetworkSeedCache(networkSeed) {
160 this.setState({
161 networkSeedCache: networkSeed
162 })
163 }
165 setNetwork(web3, contracts) {
166 this.setState({
167 web3: web3,
168 contracts: contracts
169 })
170 }
172 setWalletType(symbol) {
173 if (lodash.includes(WALLET_NAMES, symbol)) {
174 this.setState({
175 walletType: symbol
176 })
177 } else {
178 throw BRIDGE_ERROR.INVALID_WALLET_TYPE
179 }
180 }
182 setWallet(wallet) {
183 this.setState({ wallet })
184 }
186 setWalletHdPath(walletHdPath) {
187 this.setState({ walletHdPath })
188 }
190 setUrbitWallet(urbitWallet) {
191 this.setState({ urbitWallet })
192 }
194 setAuthMnemonic(authMnemonic) {
195 this.setState({ authMnemonic })
196 }
198 setPointCursor(pointCursor) {
199 this.setState({ pointCursor })
200 }
202 addToPointCache(entry) {
203 this.setState((state, _) => ({
204 pointCache: Object.assign(state.pointCache, entry)
205 }))
206 }
208 setTxnHashCursor(txnHashCursor) {
209 this.setState({ txnHashCursor })
210 }
212 render() {
213 const {
214 routeCrumbs,
215 networkType,
216 walletType,
217 walletHdPath,
218 wallet,
219 urbitWallet,
220 authMnemonic,
221 networkSeedCache,
222 pointCursor,
223 pointCache,
224 txnHashCursor,
225 web3,
226 contracts
227 } = this.state
229 const View = router(routeCrumbs.peek())
231 return (
232 <Container>
233 <Row>
234 <Col className='col-md-1' />
235 <Col className='col-md-10' style={{maxWidth: '620px'}}>
236 <Header
237 routeCrumbs={ routeCrumbs }
238 skipRoute={ this.skipRoute }
239 networkType={ networkType }
240 wallet={ wallet }
241 pointCursor={ pointCursor } />
243 <Row className={ 'row wrapper' }>
245 <View
246 // router
247 pushRoute={ this.pushRoute }
248 popRoute={ this.popRoute }
249 // network
250 setNetworkType={ this.setNetworkType }
251 setNetwork={ this.setNetwork }
252 networkType={ networkType }
253 web3={ web3 }
254 contracts={ contracts }
255 // wallet
256 setWalletType={ this.setWalletType }
257 setWalletHdPath={ this.setWalletHdPath }
258 setWallet={ this.setWallet }
259 walletType={ walletType }
260 walletHdPath={ walletHdPath }
261 wallet={ wallet }
262 // urbit wallet
263 urbitWallet={ urbitWallet }
264 setUrbitWallet={ this.setUrbitWallet }
265 authMnemonic={ authMnemonic }
266 setAuthMnemonic={ this.setAuthMnemonic }
267 // point
268 setPointCursor={ this.setPointCursor }
269 addToPointCache={ this.addToPointCache }
270 pointCursor={ pointCursor }
271 pointCache={ pointCache }
272 networkSeedCache= { networkSeedCache }
273 setNetworkSeedCache= { this.setNetworkSeedCache }
274 // txn
275 setTxnHashCursor={ this.setTxnHashCursor }
276 txnHashCursor={ txnHashCursor } />
278 <div className={'push'} />
280 </Row>
282 <Footer />
283 </Col>
284 </Row>
285 </Container>
286 )
287 }
290export default Bridge