on 9b396e2 help sign in
bridge/src/views/SetKeys.js
408 lines 9.9 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 { WALLET_NAMES } from '../lib/wallet'
15import * as kg from '../../node_modules/urbit-key-generation/dist/index'
17import {
18 sendSignedTransaction,
19 fromWei,
20 } from '../lib/txn'
22import {
23 addressFromSecp256k1Public,
24 addHexPrefix
25} from '../lib/wallet'
27class SetKeys extends React.Component {
28 constructor(props) {
29 super(props)
31 const point = props.pointCursor.matchWith({
32 Just: (shp) => shp.value,
33 Nothing: () => {
34 throw BRIDGE_ERROR.MISSING_POINT
35 }
36 })
38 this.state = {
39 auth: '',
40 encr: '',
41 networkSeed: '',
42 point: point,
43 cryptoSuiteVersion: 1,
44 continuity: false,
45 txn: Maybe.Nothing(),
46 txError: Maybe.Nothing(),
47 isManagementSeed: false,
48 userApproval: false,
49 nonce: '',
50 gasPrice: '5',
51 showGasDetails: false,
52 chainId: '',
53 gasLimit: '600000',
54 stx: Maybe.Nothing(),
55 }
57 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
58 // Transaction
59 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
60 this.handleSubmit = this.handleSubmit.bind(this)
61 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
62 this.handleSetTxn = this.handleSetTxn.bind(this)
63 this.handleSetStx = this.handleSetStx.bind(this)
64 this.handleSetNonce = this.handleSetNonce.bind(this)
65 this.handleSetChainId = this.handleSetChainId.bind(this)
66 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
67 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
68 this.toggleGasDetails = this.toggleGasDetails.bind(this)
69 }
71 toggleGasDetails() {
72 this.setState({
73 showGasDetails: !this.state.showGasDetails
74 })
75 }
77 componentDidMount() {
78 const { props } = this
80 this.deriveSeed()
82 const addr = props.wallet.matchWith({
83 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
84 Nothing: () => {
85 throw BRIDGE_ERROR.MISSING_WALLET
86 }
87 })
89 this.determineManagementSeed(props.contracts.value, addr)
91 props.web3.matchWith({
92 Nothing: () => {},
93 Just: (w3) => {
94 const validWeb3 = w3.value
96 const getTxMetadata = [
97 validWeb3.eth.getTransactionCount(addr),
98 validWeb3.eth.net.getId(),
99 validWeb3.eth.getGasPrice()
100 ];
102 Promise.all(getTxMetadata).then(r => {
103 const txMetadata = {
104 nonce: r[0],
105 chainId: r[1],
106 gasPrice: fromWei(r[2], 'gwei'),
107 };
109 this.setState({...txMetadata})
111 })
112 }
113 });
114 }
116 async determineManagementSeed(ctrcs, addr) {
117 const managing =
118 await azimuth.azimuth.getManagerFor(ctrcs, addr)
120 this.setState({
121 isManagementSeed: managing.length !== 0
122 })
123 }
125 async deriveSeed() {
126 const next = true
127 const seed = await attemptSeedDerivation(next, this.props)
128 this.setState({
129 networkSeed: seed.getOrElse('')
130 })
131 }
135 handleNetworkSeedInput(networkSeed) {
136 this.setState({ networkSeed })
137 }
141 handleCreateUnsignedTxn() {
142 const txn = this.createUnsignedTxn()
143 this.setState({ txn })
144 }
147 handleSetUserApproval(){
148 const {state} = this
149 this.setState({ userApproval: !state.userApproval })
150 }
154 handleSetTxn(txn){
155 this.setState({ txn })
156 }
160 handleSetStx(stx){
161 this.setState({
162 stx,
163 userApproval: false,
164 })
165 }
169 handleSetNonce(nonce){
170 this.setState({ nonce })
171 this.handleClearStx()
172 }
176 handleSetChainId(chainId){
177 this.setState({ chainId })
178 this.handleClearStx()
179 }
183 handleSetGasPrice(gasPrice){
184 this.setState({ gasPrice })
185 this.handleClearStx()
186 }
190 handleSetGasLimit(gasLimit){
191 this.setState({ gasLimit })
192 this.handleClearStx()
193 }
197 handleClearStx() {
198 this.setState({
199 userApproval: false,
200 stx: Maybe.Nothing(),
201 })
202 }
207 handleClearTxn() {
208 this.setState({
209 userApproval: false,
210 txn: Maybe.Nothing(),
211 stx: Maybe.Nothing(),
212 })
213 }
217 handleClearTransaction() {
218 this.setState({
219 userApproval: false,
220 txn: Maybe.Nothing(),
221 stx: Maybe.Nothing(),
222 })
223 }
227 handleSubmit(){
228 const { props, state } = this
229 sendSignedTransaction(props.web3.value, state.stx)
230 .then(sent => {
231 props.setTxnHashCursor(sent)
232 props.popRoute()
233 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
234 })
235 .catch(err => {
236 this.setState({ txError: err.map(val => val.merge()) })
237 })
238 }
242 createUnsignedTxn() {
243 const { state, props } = this
245 const validContracts = props.contracts.matchWith({
246 Just: (cs) => cs.value,
247 Nothing: () => {
248 throw BRIDGE_ERROR.MISSING_CONTRACTS
249 }
250 })
252 const validPoint = props.pointCursor.matchWith({
253 Just: (shp) => shp.value,
254 Nothing: () => {
255 throw BRIDGE_ERROR.MISSING_POINT
256 }
257 })
259 const hexRegExp = /[0-9A-Fa-f]{64}/g
261 if (hexRegExp.test(state.networkSeed)) {
262 // derive network keys
263 const pair = kg.deriveNetworkKeys(state.networkSeed)
265 const pencr = addHexPrefix(pair.crypt.public)
266 const pauth = addHexPrefix(pair.auth.public)
268 const txn = azimuth.ecliptic.configureKeys(
269 validContracts,
270 validPoint,
271 pencr,
272 pauth,
273 1,
274 false
275 )
277 return Maybe.Just(txn)
278 }
280 return Maybe.Nothing()
281 }
283 render() {
284 const { props, state } = this
286 const canGenerate =
287 state.networkSeed.length === 64
288 && state.networkSeed.length === 64
290 const canSign = Maybe.Just.hasInstance(state.txn)
291 const canApprove = Maybe.Just.hasInstance(state.stx)
293 const canSend =
294 Maybe.Just.hasInstance(state.stx)
295 && state.userApproval === true
297 const pointDetails =
298 state.point in props.pointCache
299 ? props.pointCache[state.point]
300 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
302 const isManagementMnemonic =
303 this.state.isManagementSeed &&
304 props.walletType === WALLET_NAMES.MNEMONIC
306 const isMasterTicket =
307 props.walletType === WALLET_NAMES.TICKET ||
308 props.walletType === WALLET_NAMES.SHARD
310 const networkSeedDisabled = isManagementMnemonic || isMasterTicket;
312 return (
313 <Row>
314 <Col>
315 <H1>
316 { 'Set Network Keys For ' } <code>{ `${ob.patp(state.point)}` }</code>
317 </H1>
319 <P>
320 {
321 `Please enter a network seed for generating and setting your public
322 network authentication and encryption keys. Your network seed
323 must be a string of 64 characters (containing 0-9, A-Z, a-z).`
324 }
325 </P>
326 <P>
327 {
328 `If you've authenticated with a master ticket or management proxy
329 mnemonic, a seed will be generated for you automatically.`
330 }
331 </P>
333 { pointDetails.keyRevisionNumber === '0'
334 ? <Warning>
335 <h3 className={'mb-2'}>{'Warning'}</h3>
336 {
337 'Once these keys have been set, your point is considered ' +
338 "'linked'. This operation cannot be undone."
339 }
340 </Warning>
341 : <div /> }
343 <RequiredInput
344 className='mono'
345 prop-size='lg'
346 prop-format='innerLabel'
347 autoFocus
348 disabled= { networkSeedDisabled }
349 value={ state.networkSeed }
350 onChange={ this.handleNetworkSeedInput }>
351 <InnerLabel>{ 'Network seed' }</InnerLabel>
352 </RequiredInput>
354 <StatelessTransaction
355 // Upper scope
356 web3={props.web3}
357 contracts={props.contracts}
358 wallet={props.wallet}
359 walletType={props.walletType}
360 walletHdPath={props.walletHdPath}
361 networkType={props.networkType}
362 // Tx
363 txn={state.txn}
364 stx={state.stx}
365 // Tx details
366 nonce={state.nonce}
367 gasPrice={state.gasPrice}
368 chainId={state.chainId}
369 gasLimit={state.gasLimit}
370 showGasDetails={state.showGasDetails}
371 toggleGasDetails={this.toggleGasDetails}
372 // Checks
373 userApproval={state.userApproval}
374 canGenerate={ canGenerate }
375 canSign={ canSign }
376 canApprove={ canApprove }
377 canSend={ canSend }
378 // Methods
379 createUnsignedTxn={this.handleCreateUnsignedTxn}
380 setUserApproval={this.handleSetUserApproval}
381 setTxn={this.handleSetTxn}
382 setStx={this.handleSetStx}
383 setNonce={this.handleSetNonce}
384 setChainId={this.handleSetChainId}
385 setGasPrice={this.handleSetGasPrice}
386 setGasLimit={this.handleSetGasLimit}
387 handleSubmit={this.handleSubmit} />
389 {
390 Maybe.Nothing.hasInstance(state.txError)
391 ? ''
392 : <Warning className={'mt-8'}>
393 <H3 style={{marginTop: 0, paddingTop: 0}}>
394 {
395 'There was an error sending your transaction.'
396 }
397 </H3>
398 { state.txError.value }
399 </Warning>
400 }
402 </Col>
403 </Row>
404 )
405 }
408export default SetKeys