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
44 nondeterministicSeed: false,
46 cryptoSuiteVersion: 1,
49 txError: Maybe.Nothing(),
50 isManagementSeed: false,
54 showGasDetails: false,
60 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
62 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
63 this.handleSubmit = this.handleSubmit.bind(this)
64 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
65 this.handleSetTxn = this.handleSetTxn.bind(this)
66 this.handleSetStx = this.handleSetStx.bind(this)
67 this.handleSetNonce = this.handleSetNonce.bind(this)
68 this.handleSetChainId = this.handleSetChainId.bind(this)
69 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
70 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
71 this.toggleGasDetails = this.toggleGasDetails.bind(this)
76 showGasDetails: !this.state.showGasDetails
81 const { props } = this
85 const addr = props.wallet.matchWith({
86 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
88 throw BRIDGE_ERROR.MISSING_WALLET
92 this.determineManagementSeed(props.contracts.value, addr)
94 props.web3.matchWith({
97 const validWeb3 = w3.value
99 const getTxMetadata = [
100 validWeb3.eth.getTransactionCount(addr),
101 validWeb3.eth.net.getId(),
102 validWeb3.eth.getGasPrice()
105 Promise.all(getTxMetadata).then(r => {
109 gasPrice: fromWei(r[2], 'gwei'),
112 this.setState({...txMetadata})
119 async determineManagementSeed(ctrcs, addr) {
121 await azimuth.azimuth.getManagerFor(ctrcs, addr)
124 isManagementSeed: managing.length !== 0
128 //TODO use web3.utils.randomHex when it gets fixed, see web3.js#1490
132 for (var i = 0; i < len; i++) {
133 hex = hex + ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
134 'C', 'D', 'E', 'F'][Math.floor(Math.random() * 16)]
142 let seed = await attemptSeedDerivation(next, this.props)
143 let nondeterministicSeed = false;
145 if (seed.getOrElse('') === '') {
146 seed = Maybe.Just(this.randomHex(64));
147 nondeterministicSeed = true;
151 networkSeed: seed.getOrElse(''),
152 nondeterministicSeed: nondeterministicSeed
156 handleNetworkSeedInput(networkSeed) {
157 this.setState({ networkSeed })
160 handleCreateUnsignedTxn() {
161 const txn = this.createUnsignedTxn()
162 this.setState({ txn })
166 handleSetUserApproval(){
168 this.setState({ userApproval: !state.userApproval })
174 this.setState({ txn })
188 handleSetNonce(nonce){
189 this.setState({ nonce })
190 this.handleClearStx()
195 handleSetChainId(chainId){
196 this.setState({ chainId })
197 this.handleClearStx()
202 handleSetGasPrice(gasPrice){
203 this.setState({ gasPrice })
204 this.handleClearStx()
209 handleSetGasLimit(gasLimit){
210 this.setState({ gasLimit })
211 this.handleClearStx()
219 stx: Maybe.Nothing(),
229 txn: Maybe.Nothing(),
230 stx: Maybe.Nothing(),
236 handleClearTransaction() {
239 txn: Maybe.Nothing(),
240 stx: Maybe.Nothing(),
244 downloadKeyfile(networkSeed) {
245 const { pointCache } = this.props
246 const { pointCursor } = this.props
248 const point = pointCursor.matchWith({
249 Just: (pt) => pt.value,
251 throw BRIDGE_ERROR.MISSING_POINT
258 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
260 const revision = parseInt(pointDetails.keyRevisionNumber)
261 const keyfile = genKey(networkSeed, point, revision)
262 let blob = new Blob([keyfile], {type:"text/plain;charset=utf-8"});
263 saveAs(blob, `${ob.patp(point).slice(1)}-${revision}.key`)
267 const { props, state } = this
268 sendSignedTransaction(props.web3.value, state.stx)
270 props.setNetworkSeedCache(this.state.networkSeed)
271 props.setTxnHashCursor(sent)
273 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION, {promptKeyfile: true})
276 this.setState({ txError: err.map(val => val.merge()) })
280 createUnsignedTxn() {
281 const { state, props } = this
283 const validContracts = props.contracts.matchWith({
284 Just: (cs) => cs.value,
286 throw BRIDGE_ERROR.MISSING_CONTRACTS
290 const validPoint = props.pointCursor.matchWith({
291 Just: (shp) => shp.value,
293 throw BRIDGE_ERROR.MISSING_POINT
297 const hexRegExp = /[0-9A-Fa-f]{64}/g
299 if (hexRegExp.test(state.networkSeed)) {
300 // derive network keys
301 const pair = kg.deriveNetworkKeys(state.networkSeed)
303 const pencr = addHexPrefix(pair.crypt.public)
304 const pauth = addHexPrefix(pair.auth.public)
306 const txn = azimuth.ecliptic.configureKeys(
315 return Maybe.Just(txn)
318 return Maybe.Nothing()
322 const { props, state } = this
325 state.networkSeed.length === 64
326 && state.networkSeed.length === 64
328 const canSign = Maybe.Just.hasInstance(state.txn)
329 const canApprove = Maybe.Just.hasInstance(state.stx)
332 Maybe.Just.hasInstance(state.stx)
333 && state.userApproval === true
336 state.point in props.pointCache
337 ? props.pointCache[state.point]
338 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
344 { 'Set Network Keys For ' } <code>{ `${ob.patp(state.point)}` }</code>
347 <P className="mt-10">
349 `Set new authentication and encryption keys for your Arvo ship.`
353 { state.nondeterministicSeed &&
355 <h3 className={'mb-2'}>{'Warning'}</h3>
357 `Your network seed could not be derived automatically. We've
358 generated a random one for you, so you must download your Arvo
359 keyfile during this session after setting your keys.`
364 { pointDetails.keyRevisionNumber === '0'
366 <h3 className={'mb-2'}>{'Warning'}</h3>
368 'Once these keys have been set, your point is considered ' +
369 "'linked'. This operation cannot be undone."
374 <StatelessTransaction
377 contracts={props.contracts}
378 wallet={props.wallet}
379 walletType={props.walletType}
380 walletHdPath={props.walletHdPath}
381 networkType={props.networkType}
387 gasPrice={state.gasPrice}
388 chainId={state.chainId}
389 gasLimit={state.gasLimit}
390 showGasDetails={state.showGasDetails}
391 toggleGasDetails={this.toggleGasDetails}
393 userApproval={state.userApproval}
394 canGenerate={ canGenerate }
396 canApprove={ canApprove }
399 createUnsignedTxn={this.handleCreateUnsignedTxn}
400 setUserApproval={this.handleSetUserApproval}
401 setTxn={this.handleSetTxn}
402 setStx={this.handleSetStx}
403 setNonce={this.handleSetNonce}
404 setChainId={this.handleSetChainId}
405 setGasPrice={this.handleSetGasPrice}
406 setGasLimit={this.handleSetGasLimit}
407 handleSubmit={this.handleSubmit} />
410 Maybe.Nothing.hasInstance(state.txError)
412 : <Warning className={'mt-8'}>
413 <H3 style={{marginTop: 0, paddingTop: 0}}>
415 'There was an error sending your transaction.'
418 { state.txError.value }
428export 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