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, genKey } from '../lib/keys'
13import saveAs from 'file-saver'
15import { WALLET_NAMES } from '../lib/wallet'
17import * as kg from '../../node_modules/urbit-key-generation/dist/index'
20 sendSignedTransaction,
25 addressFromSecp256k1Public,
29class SetKeys extends React.Component {
33 const point = props.pointCursor.matchWith({
34 Just: (shp) => shp.value,
36 throw BRIDGE_ERROR.MISSING_POINT
45 cryptoSuiteVersion: 1,
48 txError: Maybe.Nothing(),
49 isManagementSeed: false,
53 showGasDetails: false,
59 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
61 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
62 this.handleSubmit = this.handleSubmit.bind(this)
63 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
64 this.handleSetTxn = this.handleSetTxn.bind(this)
65 this.handleSetStx = this.handleSetStx.bind(this)
66 this.handleSetNonce = this.handleSetNonce.bind(this)
67 this.handleSetChainId = this.handleSetChainId.bind(this)
68 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
69 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
70 this.toggleGasDetails = this.toggleGasDetails.bind(this)
75 showGasDetails: !this.state.showGasDetails
80 const { props } = this
84 const addr = props.wallet.matchWith({
85 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
87 throw BRIDGE_ERROR.MISSING_WALLET
91 this.determineManagementSeed(props.contracts.value, addr)
93 props.web3.matchWith({
96 const validWeb3 = w3.value
98 const getTxMetadata = [
99 validWeb3.eth.getTransactionCount(addr),
100 validWeb3.eth.net.getId(),
101 validWeb3.eth.getGasPrice()
104 Promise.all(getTxMetadata).then(r => {
108 gasPrice: fromWei(r[2], 'gwei'),
111 this.setState({...txMetadata})
118 async determineManagementSeed(ctrcs, addr) {
120 await azimuth.azimuth.getManagerFor(ctrcs, addr)
123 isManagementSeed: managing.length !== 0
130 for (var i = 0; i < len; i++) {
131 hex = hex + ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
132 'C', 'D', 'E', 'F'][Math.floor(Math.random() * 16)]
140 let seed = await attemptSeedDerivation(next, this.props)
142 if (seed.getOrElse('') === '') {
143 seed = Maybe.Just(this.randomHex(64));
147 networkSeed: seed.getOrElse('')
151 handleNetworkSeedInput(networkSeed) {
152 this.setState({ networkSeed })
155 handleCreateUnsignedTxn() {
156 const txn = this.createUnsignedTxn()
157 this.setState({ txn })
161 handleSetUserApproval(){
163 this.setState({ userApproval: !state.userApproval })
169 this.setState({ txn })
183 handleSetNonce(nonce){
184 this.setState({ nonce })
185 this.handleClearStx()
190 handleSetChainId(chainId){
191 this.setState({ chainId })
192 this.handleClearStx()
197 handleSetGasPrice(gasPrice){
198 this.setState({ gasPrice })
199 this.handleClearStx()
204 handleSetGasLimit(gasLimit){
205 this.setState({ gasLimit })
206 this.handleClearStx()
214 stx: Maybe.Nothing(),
224 txn: Maybe.Nothing(),
225 stx: Maybe.Nothing(),
231 handleClearTransaction() {
234 txn: Maybe.Nothing(),
235 stx: Maybe.Nothing(),
239 downloadKeyfile(networkSeed) {
240 const { pointCache } = this.props
241 const { pointCursor } = this.props
243 const point = pointCursor.matchWith({
244 Just: (pt) => pt.value,
246 throw BRIDGE_ERROR.MISSING_POINT
253 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
255 const revision = parseInt(pointDetails.keyRevisionNumber)
256 const keyfile = genKey(networkSeed, point, revision)
257 let blob = new Blob([keyfile], {type:"text/plain;charset=utf-8"});
258 saveAs(blob, `${ob.patp(point).slice(1)}-${revision}.key`)
262 const { props, state } = this
263 sendSignedTransaction(props.web3.value, state.stx)
265 props.setNetworkSeedCache(this.state.networkSeed)
266 this.downloadKeyfile(this.state.networkSeed)
267 props.setTxnHashCursor(sent)
269 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
272 this.setState({ txError: err.map(val => val.merge()) })
278 createUnsignedTxn() {
279 const { state, props } = this
281 const validContracts = props.contracts.matchWith({
282 Just: (cs) => cs.value,
284 throw BRIDGE_ERROR.MISSING_CONTRACTS
288 const validPoint = props.pointCursor.matchWith({
289 Just: (shp) => shp.value,
291 throw BRIDGE_ERROR.MISSING_POINT
295 const hexRegExp = /[0-9A-Fa-f]{64}/g
297 if (hexRegExp.test(state.networkSeed)) {
298 // derive network keys
299 const pair = kg.deriveNetworkKeys(state.networkSeed)
301 const pencr = addHexPrefix(pair.crypt.public)
302 const pauth = addHexPrefix(pair.auth.public)
304 const txn = azimuth.ecliptic.configureKeys(
313 return Maybe.Just(txn)
316 return Maybe.Nothing()
320 const { props, state } = this
323 state.networkSeed.length === 64
324 && state.networkSeed.length === 64
326 const canSign = Maybe.Just.hasInstance(state.txn)
327 const canApprove = Maybe.Just.hasInstance(state.stx)
330 Maybe.Just.hasInstance(state.stx)
331 && state.userApproval === true
334 state.point in props.pointCache
335 ? props.pointCache[state.point]
336 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
342 { 'Set Network Keys For ' } <code>{ `${ob.patp(state.point)}` }</code>
345 <P className="mt-10">
347 `Set new authentication and encryption keys for your Arvo ship.`
353 `Once the transaction is sent, a keyfile will be downloaded, enabling you to instantiate your ship on the network.`
357 { pointDetails.keyRevisionNumber === '0'
359 <h3 className={'mb-2'}>{'Warning'}</h3>
361 'Once these keys have been set, your point is considered ' +
362 "'linked'. This operation cannot be undone."
367 <StatelessTransaction
370 contracts={props.contracts}
371 wallet={props.wallet}
372 walletType={props.walletType}
373 walletHdPath={props.walletHdPath}
374 networkType={props.networkType}
380 gasPrice={state.gasPrice}
381 chainId={state.chainId}
382 gasLimit={state.gasLimit}
383 showGasDetails={state.showGasDetails}
384 toggleGasDetails={this.toggleGasDetails}
386 userApproval={state.userApproval}
387 canGenerate={ canGenerate }
389 canApprove={ canApprove }
392 createUnsignedTxn={this.handleCreateUnsignedTxn}
393 setUserApproval={this.handleSetUserApproval}
394 setTxn={this.handleSetTxn}
395 setStx={this.handleSetStx}
396 setNonce={this.handleSetNonce}
397 setChainId={this.handleSetChainId}
398 setGasPrice={this.handleSetGasPrice}
399 setGasLimit={this.handleSetGasLimit}
400 handleSubmit={this.handleSubmit} />
403 Maybe.Nothing.hasInstance(state.txError)
405 : <Warning className={'mt-8'}>
406 <H3 style={{marginTop: 0, paddingTop: 0}}>
408 'There was an error sending your transaction.'
411 { state.txError.value }
421export 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