on b408168 help sign in
bridge/src/views/SetKeys.js
378 lines 9.0 kB history raw
1import React from 'react'
2import Maybe from 'folktale/maybe'
3import * as azimuth from 'azimuth-js'
4import * as ob from 'urbit-ob'
5import { Row, Col, H1, P, Warning, H3 } from '../components/Base'
6import { RequiredInput, InnerLabel } from '../components/Base'
8import StatelessTransaction from '../components/StatelessTransaction'
9import { BRIDGE_ERROR } from '../lib/error'
10import { ROUTE_NAMES } from '../lib/router'
11import { attemptSeedDerivation } from '../lib/keys'
13import * as kg from '../../node_modules/urbit-key-generation/dist/index'
15import {
16 sendSignedTransaction,
17 fromWei,
18 } from '../lib/txn'
20import {
21 addressFromSecp256k1Public,
22 addHexPrefix
23} from '../lib/wallet'
25class SetKeys extends React.Component {
26 constructor(props) {
27 super(props)
29 const point = props.pointCursor.matchWith({
30 Just: (shp) => shp.value,
31 Nothing: () => {
32 throw BRIDGE_ERROR.MISSING_POINT
33 }
34 })
36 this.state = {
37 auth: '',
38 encr: '',
39 networkSeed: '',
40 point: point,
41 cryptoSuiteVersion: 1,
42 continuity: false,
43 txn: Maybe.Nothing(),
44 txError: Maybe.Nothing(),
45 userApproval: false,
46 nonce: '',
47 gasPrice: '5',
48 chainId: '',
49 gasLimit: '600000',
50 stx: Maybe.Nothing(),
51 }
53 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
54 // Transaction
55 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
56 this.handleSubmit = this.handleSubmit.bind(this)
57 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
58 this.handleSetTxn = this.handleSetTxn.bind(this)
59 this.handleSetStx = this.handleSetStx.bind(this)
60 this.handleSetNonce = this.handleSetNonce.bind(this)
61 this.handleSetChainId = this.handleSetChainId.bind(this)
62 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
63 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
64 }
67 componentDidMount() {
68 const { props } = this
70 this.deriveSeed()
72 const addr = props.wallet.matchWith({
73 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
74 Nothing: () => {
75 throw BRIDGE_ERROR.MISSING_WALLET
76 }
77 })
79 props.web3.matchWith({
80 Nothing: () => {},
81 Just: (w3) => {
82 const validWeb3 = w3.value
84 const getTxMetadata = [
85 validWeb3.eth.getTransactionCount(addr),
86 validWeb3.eth.net.getId(),
87 validWeb3.eth.getGasPrice()
88 ];
90 Promise.all(getTxMetadata).then(r => {
91 const txMetadata = {
92 nonce: r[0],
93 chainId: r[1],
94 gasPrice: fromWei(r[2], 'gwei'),
95 };
97 this.setState({...txMetadata})
99 })
100 }
101 });
103 }
106 async deriveSeed() {
107 const next = true
108 const seed = await attemptSeedDerivation(next, this.props)
109 this.setState({
110 networkSeed: seed.getOrElse('')
111 })
112 }
116 handleNetworkSeedInput(networkSeed) {
117 this.setState({ networkSeed })
118 }
122 handleCreateUnsignedTxn() {
123 const txn = this.createUnsignedTxn()
124 this.setState({ txn })
125 }
128 handleSetUserApproval(){
129 const {state} = this
130 this.setState({ userApproval: !state.userApproval })
131 }
135 handleSetTxn(txn){
136 this.setState({ txn })
137 }
141 handleSetStx(stx){
142 this.setState({
143 stx,
144 userApproval: false,
145 })
146 }
150 handleSetNonce(nonce){
151 this.setState({ nonce })
152 this.handleClearStx()
153 }
157 handleSetChainId(chainId){
158 this.setState({ chainId })
159 this.handleClearStx()
160 }
164 handleSetGasPrice(gasPrice){
165 this.setState({ gasPrice })
166 this.handleClearStx()
167 }
171 handleSetGasLimit(gasLimit){
172 this.setState({ gasLimit })
173 this.handleClearStx()
174 }
178 handleClearStx() {
179 this.setState({
180 userApproval: false,
181 stx: Maybe.Nothing(),
182 })
183 }
188 handleClearTxn() {
189 this.setState({
190 userApproval: false,
191 txn: Maybe.Nothing(),
192 stx: Maybe.Nothing(),
193 })
194 }
198 handleClearTransaction() {
199 this.setState({
200 userApproval: false,
201 txn: Maybe.Nothing(),
202 stx: Maybe.Nothing(),
203 })
204 }
208 handleSubmit(){
209 const { props, state } = this
210 sendSignedTransaction(props.web3.value, state.stx)
211 .then(sent => {
212 props.setTxnCursor(sent)
213 props.popRoute()
214 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
215 })
216 .catch(err => {
217 // Note that value.value is due to wrapped Maybe.Just + Result.Error
218 this.setState({ txError: Maybe.Just(err.value.value) })
219 })
220 }
224 createUnsignedTxn() {
225 const { state, props } = this
227 const validContracts = props.contracts.matchWith({
228 Just: (cs) => cs.value,
229 Nothing: () => {
230 throw BRIDGE_ERROR.MISSING_CONTRACTS
231 }
232 })
234 const validPoint = props.pointCursor.matchWith({
235 Just: (shp) => shp.value,
236 Nothing: () => {
237 throw BRIDGE_ERROR.MISSING_POINT
238 }
239 })
241 const hexRegExp = /[0-9A-Fa-f]{64}/g
243 if (hexRegExp.test(state.networkSeed)) {
244 // derive network keys
245 const pair = kg.deriveNetworkKeys(state.networkSeed)
247 const pencr = addHexPrefix(pair.crypt.public)
248 const pauth = addHexPrefix(pair.auth.public)
250 const txn = azimuth.ecliptic.configureKeys(
251 validContracts,
252 validPoint,
253 pencr,
254 pauth,
255 1,
256 false
257 )
259 return Maybe.Just(txn)
260 }
262 return Maybe.Nothing()
263 }
265 render() {
266 const { props, state } = this
268 const canGenerate =
269 state.networkSeed.length === 64
270 && state.networkSeed.length === 64
272 const canSign = Maybe.Just.hasInstance(state.txn)
273 const canApprove = Maybe.Just.hasInstance(state.stx)
275 const canSend =
276 Maybe.Just.hasInstance(state.stx)
277 && state.userApproval === true
279 const pointDetails =
280 state.point in props.pointCache
281 ? props.pointCache[state.point]
282 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
285 return (
286 <Row>
287 <Col>
288 <H1>
289 { 'Set Network Keys For ' } <code>{ `${ob.patp(state.point)}` }</code>
290 </H1>
292 <P>
293 {
294 `Please enter a network seed for generating and setting your public
295 network authentication and encryption keys. Your network seed
296 must be a 32-byte-long hexadecimal string.`
297 }
298 </P>
299 <P>
300 {
301 `If you've authenticated with a master ticket or management proxy
302 mnemonic, a seed will be generated for you automatically.`
303 }
304 </P>
306 { pointDetails.keyRevisionNumber === '0'
307 ? <Warning>
308 <h3 className={'mb-2'}>{'Warning'}</h3>
309 {
310 'Once these keys have been set, your Urbit is considered ' +
311 "'Linked'. This operation cannot be undone."
312 }
313 </Warning>
314 : <div /> }
316 <RequiredInput
317 className='mono'
318 prop-size='lg'
319 prop-format='innerLabel'
320 autoFocus
321 value={ state.networkSeed }
322 onChange={ this.handleNetworkSeedInput }>
323 <InnerLabel>{ 'Network seed' }</InnerLabel>
324 </RequiredInput>
326 <StatelessTransaction
327 // Upper scope
328 web3={props.web3}
329 contracts={props.contracts}
330 wallet={props.wallet}
331 walletType={props.walletType}
332 walletHdPath={props.walletHdPath}
333 networkType={props.networkType}
334 // Tx
335 txn={state.txn}
336 stx={state.stx}
337 // Tx details
338 nonce={state.nonce}
339 gasPrice={state.gasPrice}
340 chainId={state.chainId}
341 gasLimit={state.gasLimit}
342 // Checks
343 userApproval={state.userApproval}
344 canGenerate={ canGenerate }
345 canSign={ canSign }
346 canApprove={ canApprove }
347 canSend={ canSend }
348 // Methods
349 createUnsignedTxn={this.handleCreateUnsignedTxn}
350 setUserApproval={this.handleSetUserApproval}
351 setTxn={this.handleSetTxn}
352 setStx={this.handleSetStx}
353 setNonce={this.handleSetNonce}
354 setChainId={this.handleSetChainId}
355 setGasPrice={this.handleSetGasPrice}
356 setGasLimit={this.handleSetGasLimit}
357 handleSubmit={this.handleSubmit} />
359 {
360 Maybe.Nothing.hasInstance(state.txError)
361 ? ''
362 : <Warning className={'mt-8'}>
363 <H3 style={{marginTop: 0, paddingTop: 0}}>
364 {
365 'There was an error sending your transaction.'
366 }
367 </H3>
368 { state.txError.value }
369 </Warning>
370 }
372 </Col>
373 </Row>
374 )
375 }
378export default SetKeys