on 8fc9df1 help sign in
bridge/src/views/SetKeys.js
398 lines 9.6 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 chainId: '',
52 gasLimit: '600000',
53 stx: Maybe.Nothing(),
54 }
56 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
57 // Transaction
58 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
59 this.handleSubmit = this.handleSubmit.bind(this)
60 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
61 this.handleSetTxn = this.handleSetTxn.bind(this)
62 this.handleSetStx = this.handleSetStx.bind(this)
63 this.handleSetNonce = this.handleSetNonce.bind(this)
64 this.handleSetChainId = this.handleSetChainId.bind(this)
65 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
66 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
67 }
69 componentDidMount() {
70 const { props } = this
72 this.deriveSeed()
74 const addr = props.wallet.matchWith({
75 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
76 Nothing: () => {
77 throw BRIDGE_ERROR.MISSING_WALLET
78 }
79 })
81 this.determineManagementSeed(props.contracts.value, addr)
83 props.web3.matchWith({
84 Nothing: () => {},
85 Just: (w3) => {
86 const validWeb3 = w3.value
88 const getTxMetadata = [
89 validWeb3.eth.getTransactionCount(addr),
90 validWeb3.eth.net.getId(),
91 validWeb3.eth.getGasPrice()
92 ];
94 Promise.all(getTxMetadata).then(r => {
95 const txMetadata = {
96 nonce: r[0],
97 chainId: r[1],
98 gasPrice: fromWei(r[2], 'gwei'),
99 };
101 this.setState({...txMetadata})
103 })
104 }
105 });
106 }
108 async determineManagementSeed(ctrcs, addr) {
109 const managing =
110 await azimuth.azimuth.getManagerFor(ctrcs, addr)
112 this.setState({
113 isManagementSeed: managing.length !== 0
114 })
115 }
117 async deriveSeed() {
118 const next = true
119 const seed = await attemptSeedDerivation(next, this.props)
120 this.setState({
121 networkSeed: seed.getOrElse('')
122 })
123 }
127 handleNetworkSeedInput(networkSeed) {
128 this.setState({ networkSeed })
129 }
133 handleCreateUnsignedTxn() {
134 const txn = this.createUnsignedTxn()
135 this.setState({ txn })
136 }
139 handleSetUserApproval(){
140 const {state} = this
141 this.setState({ userApproval: !state.userApproval })
142 }
146 handleSetTxn(txn){
147 this.setState({ txn })
148 }
152 handleSetStx(stx){
153 this.setState({
154 stx,
155 userApproval: false,
156 })
157 }
161 handleSetNonce(nonce){
162 this.setState({ nonce })
163 this.handleClearStx()
164 }
168 handleSetChainId(chainId){
169 this.setState({ chainId })
170 this.handleClearStx()
171 }
175 handleSetGasPrice(gasPrice){
176 this.setState({ gasPrice })
177 this.handleClearStx()
178 }
182 handleSetGasLimit(gasLimit){
183 this.setState({ gasLimit })
184 this.handleClearStx()
185 }
189 handleClearStx() {
190 this.setState({
191 userApproval: false,
192 stx: Maybe.Nothing(),
193 })
194 }
199 handleClearTxn() {
200 this.setState({
201 userApproval: false,
202 txn: Maybe.Nothing(),
203 stx: Maybe.Nothing(),
204 })
205 }
209 handleClearTransaction() {
210 this.setState({
211 userApproval: false,
212 txn: Maybe.Nothing(),
213 stx: Maybe.Nothing(),
214 })
215 }
219 handleSubmit(){
220 const { props, state } = this
221 sendSignedTransaction(props.web3.value, state.stx)
222 .then(sent => {
223 props.setTxnHashCursor(sent)
224 props.popRoute()
225 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
226 })
227 .catch(err => {
228 this.setState({ txError: err.map(val => val.merge()) })
229 })
230 }
234 createUnsignedTxn() {
235 const { state, props } = this
237 const validContracts = props.contracts.matchWith({
238 Just: (cs) => cs.value,
239 Nothing: () => {
240 throw BRIDGE_ERROR.MISSING_CONTRACTS
241 }
242 })
244 const validPoint = props.pointCursor.matchWith({
245 Just: (shp) => shp.value,
246 Nothing: () => {
247 throw BRIDGE_ERROR.MISSING_POINT
248 }
249 })
251 const hexRegExp = /[0-9A-Fa-f]{64}/g
253 if (hexRegExp.test(state.networkSeed)) {
254 // derive network keys
255 const pair = kg.deriveNetworkKeys(state.networkSeed)
257 const pencr = addHexPrefix(pair.crypt.public)
258 const pauth = addHexPrefix(pair.auth.public)
260 const txn = azimuth.ecliptic.configureKeys(
261 validContracts,
262 validPoint,
263 pencr,
264 pauth,
265 1,
266 false
267 )
269 return Maybe.Just(txn)
270 }
272 return Maybe.Nothing()
273 }
275 render() {
276 const { props, state } = this
278 const canGenerate =
279 state.networkSeed.length === 64
280 && state.networkSeed.length === 64
282 const canSign = Maybe.Just.hasInstance(state.txn)
283 const canApprove = Maybe.Just.hasInstance(state.stx)
285 const canSend =
286 Maybe.Just.hasInstance(state.stx)
287 && state.userApproval === true
289 const pointDetails =
290 state.point in props.pointCache
291 ? props.pointCache[state.point]
292 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
294 const isManagementMnemonic =
295 this.state.isManagementSeed &&
296 props.walletType === WALLET_NAMES.MNEMONIC
298 const isMasterTicket =
299 props.walletType === WALLET_NAMES.TICKET ||
300 props.walletType === WALLET_NAMES.SHARD
302 const networkSeedDisabled = isManagementMnemonic || isMasterTicket;
304 return (
305 <Row>
306 <Col>
307 <H1>
308 { 'Set Network Keys For ' } <code>{ `${ob.patp(state.point)}` }</code>
309 </H1>
311 <P>
312 {
313 `Please enter a network seed for generating and setting your public
314 network authentication and encryption keys. Your network seed
315 must be a 32-byte-long hexadecimal string.`
316 }
317 </P>
318 <P>
319 {
320 `If you've authenticated with a master ticket or management proxy
321 mnemonic, a seed will be generated for you automatically.`
322 }
323 </P>
325 { pointDetails.keyRevisionNumber === '0'
326 ? <Warning>
327 <h3 className={'mb-2'}>{'Warning'}</h3>
328 {
329 'Once these keys have been set, your point is considered ' +
330 "'linked'. This operation cannot be undone."
331 }
332 </Warning>
333 : <div /> }
335 <RequiredInput
336 className='mono'
337 prop-size='lg'
338 prop-format='innerLabel'
339 autoFocus
340 disabled= { networkSeedDisabled }
341 value={ state.networkSeed }
342 onChange={ this.handleNetworkSeedInput }>
343 <InnerLabel>{ 'Network seed' }</InnerLabel>
344 </RequiredInput>
346 <StatelessTransaction
347 // Upper scope
348 web3={props.web3}
349 contracts={props.contracts}
350 wallet={props.wallet}
351 walletType={props.walletType}
352 walletHdPath={props.walletHdPath}
353 networkType={props.networkType}
354 // Tx
355 txn={state.txn}
356 stx={state.stx}
357 // Tx details
358 nonce={state.nonce}
359 gasPrice={state.gasPrice}
360 chainId={state.chainId}
361 gasLimit={state.gasLimit}
362 // Checks
363 userApproval={state.userApproval}
364 canGenerate={ canGenerate }
365 canSign={ canSign }
366 canApprove={ canApprove }
367 canSend={ canSend }
368 // Methods
369 createUnsignedTxn={this.handleCreateUnsignedTxn}
370 setUserApproval={this.handleSetUserApproval}
371 setTxn={this.handleSetTxn}
372 setStx={this.handleSetStx}
373 setNonce={this.handleSetNonce}
374 setChainId={this.handleSetChainId}
375 setGasPrice={this.handleSetGasPrice}
376 setGasLimit={this.handleSetGasLimit}
377 handleSubmit={this.handleSubmit} />
379 {
380 Maybe.Nothing.hasInstance(state.txError)
381 ? ''
382 : <Warning className={'mt-8'}>
383 <H3 style={{marginTop: 0, paddingTop: 0}}>
384 {
385 'There was an error sending your transaction.'
386 }
387 </H3>
388 { state.txError.value }
389 </Warning>
390 }
392 </Col>
393 </Row>
394 )
395 }
398export default SetKeys