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, Form, 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'
16 sendSignedTransaction,
21 addressFromSecp256k1Public,
25class SetKeys extends React.Component {
29 const point = props.pointCursor.matchWith({
30 Just: (shp) => shp.value,
32 throw BRIDGE_ERROR.MISSING_POINT
41 cryptoSuiteVersion: 1,
44 txError: Maybe.Nothing(),
53 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
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)
68 const { props } = this
72 const addr = props.wallet.matchWith({
73 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
75 throw BRIDGE_ERROR.MISSING_WALLET
79 props.web3.matchWith({
82 const validWeb3 = w3.value
84 const getTxMetadata = [
85 validWeb3.eth.getTransactionCount(addr),
86 validWeb3.eth.net.getId(),
87 validWeb3.eth.getGasPrice()
90 Promise.all(getTxMetadata).then(r => {
94 gasPrice: fromWei(r[2], 'gwei'),
97 this.setState({...txMetadata})
108 const seed = await attemptSeedDerivation(next, this.props)
110 networkSeed: seed.getOrElse('')
116 handleNetworkSeedInput(networkSeed) {
117 this.setState({ networkSeed })
122 handleCreateUnsignedTxn() {
123 const txn = this.createUnsignedTxn()
124 this.setState({ txn })
128 handleSetUserApproval(){
130 this.setState({ userApproval: !state.userApproval })
136 this.setState({ txn })
150 handleSetNonce(nonce){
151 this.setState({ nonce })
152 this.handleClearStx()
157 handleSetChainId(chainId){
158 this.setState({ chainId })
159 this.handleClearStx()
164 handleSetGasPrice(gasPrice){
165 this.setState({ gasPrice })
166 this.handleClearStx()
171 handleSetGasLimit(gasLimit){
172 this.setState({ gasLimit })
173 this.handleClearStx()
181 stx: Maybe.Nothing(),
191 txn: Maybe.Nothing(),
192 stx: Maybe.Nothing(),
198 handleClearTransaction() {
201 txn: Maybe.Nothing(),
202 stx: Maybe.Nothing(),
209 const { props, state } = this
210 sendSignedTransaction(props.web3.value, state.stx)
212 props.setTxnCursor(sent)
214 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
217 // Note that value.value is due to wrapped Maybe.Just + Result.Error
218 this.setState({ txError: Maybe.Just(err.value.value) })
224 createUnsignedTxn() {
225 const { state, props } = this
227 const validContracts = props.contracts.matchWith({
228 Just: (cs) => cs.value,
230 throw BRIDGE_ERROR.MISSING_CONTRACTS
234 const validPoint = props.pointCursor.matchWith({
235 Just: (shp) => shp.value,
237 throw BRIDGE_ERROR.MISSING_POINT
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(
259 return Maybe.Just(txn)
262 return Maybe.Nothing()
266 const { props, state } = this
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)
276 Maybe.Just.hasInstance(state.stx)
277 && state.userApproval === true
280 state.point in props.pointCache
281 ? props.pointCache[state.point]
282 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
289 { 'Set Network Keys For ' } <code>{ `${ob.patp(state.point)}` }</code>
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.`
301 `If you've authenticated with a master ticket or management proxy
302 mnemonic, a seed will be generated for you automatically.`
306 { pointDetails.keyRevisionNumber === '0'
308 <h3 className={'mb-2'}>{'Warning'}</h3>
310 'Once these keys have been set, your Urbit is considered ' +
311 "'Linked'. This operation cannot be undone."
320 prop-format='innerLabel'
322 value={ state.networkSeed }
323 onChange={ this.handleNetworkSeedInput }>
324 <InnerLabel>{ 'Network seed' }</InnerLabel>
327 <StatelessTransaction
330 contracts={props.contracts}
331 wallet={props.wallet}
332 walletType={props.walletType}
333 walletHdPath={props.walletHdPath}
339 gasPrice={state.gasPrice}
340 chainId={state.chainId}
341 gasLimit={state.gasLimit}
343 userApproval={state.userApproval}
344 canGenerate={ canGenerate }
346 canApprove={ canApprove }
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} />
360 Maybe.Nothing.hasInstance(state.txError)
362 : <Warning className={'mt-8'}>
363 <H3 style={{marginTop: 0, paddingTop: 0}}>
365 'There was an error sending your transaction.'
368 { state.txError.value }
380export default SetKeys
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list