on 4efb153 help sign in
bridge/src/views/SetKeys.js
428 lines 11 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, 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'
19import {
20 sendSignedTransaction,
21 fromWei,
22 } from '../lib/txn'
24import {
25 addressFromSecp256k1Public,
26 addHexPrefix
27} from '../lib/wallet'
29class SetKeys extends React.Component {
30 constructor(props) {
31 super(props)
33 const point = props.pointCursor.matchWith({
34 Just: (shp) => shp.value,
35 Nothing: () => {
36 throw BRIDGE_ERROR.MISSING_POINT
37 }
38 })
40 this.state = {
41 auth: '',
42 encr: '',
43 networkSeed: '',
44 nondeterministicSeed: false,
45 point: point,
46 cryptoSuiteVersion: 1,
47 continuity: false,
48 txn: Maybe.Nothing(),
49 txError: Maybe.Nothing(),
50 isManagementSeed: false,
51 userApproval: false,
52 nonce: '',
53 gasPrice: '5',
54 showGasDetails: false,
55 chainId: '',
56 gasLimit: '600000',
57 stx: Maybe.Nothing(),
58 }
60 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
61 // Transaction
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)
72 }
74 toggleGasDetails() {
75 this.setState({
76 showGasDetails: !this.state.showGasDetails
77 })
78 }
80 componentDidMount() {
81 const { props } = this
83 this.deriveSeed()
85 const addr = props.wallet.matchWith({
86 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
87 Nothing: () => {
88 throw BRIDGE_ERROR.MISSING_WALLET
89 }
90 })
92 this.determineManagementSeed(props.contracts.value, addr)
94 props.web3.matchWith({
95 Nothing: () => {},
96 Just: (w3) => {
97 const validWeb3 = w3.value
99 const getTxMetadata = [
100 validWeb3.eth.getTransactionCount(addr),
101 validWeb3.eth.net.getId(),
102 validWeb3.eth.getGasPrice()
103 ];
105 Promise.all(getTxMetadata).then(r => {
106 const txMetadata = {
107 nonce: r[0],
108 chainId: r[1],
109 gasPrice: fromWei(r[2], 'gwei'),
110 };
112 this.setState({...txMetadata})
114 })
115 }
116 });
117 }
119 async determineManagementSeed(ctrcs, addr) {
120 const managing =
121 await azimuth.azimuth.getManagerFor(ctrcs, addr)
123 this.setState({
124 isManagementSeed: managing.length !== 0
125 })
126 }
128 //TODO use web3.utils.randomHex when it gets fixed, see web3.js#1490
129 randomHex(len) {
130 let hex = "";
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)]
135 }
137 return hex;
138 }
140 async deriveSeed() {
141 const next = true
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;
148 }
150 this.setState({
151 networkSeed: seed.getOrElse(''),
152 nondeterministicSeed: nondeterministicSeed
153 })
154 }
156 handleNetworkSeedInput(networkSeed) {
157 this.setState({ networkSeed })
158 }
160 handleCreateUnsignedTxn() {
161 const txn = this.createUnsignedTxn()
162 this.setState({ txn })
163 }
166 handleSetUserApproval(){
167 const {state} = this
168 this.setState({ userApproval: !state.userApproval })
169 }
173 handleSetTxn(txn){
174 this.setState({ txn })
175 }
179 handleSetStx(stx){
180 this.setState({
181 stx,
182 userApproval: false,
183 })
184 }
188 handleSetNonce(nonce){
189 this.setState({ nonce })
190 this.handleClearStx()
191 }
195 handleSetChainId(chainId){
196 this.setState({ chainId })
197 this.handleClearStx()
198 }
202 handleSetGasPrice(gasPrice){
203 this.setState({ gasPrice })
204 this.handleClearStx()
205 }
209 handleSetGasLimit(gasLimit){
210 this.setState({ gasLimit })
211 this.handleClearStx()
212 }
216 handleClearStx() {
217 this.setState({
218 userApproval: false,
219 stx: Maybe.Nothing(),
220 })
221 }
226 handleClearTxn() {
227 this.setState({
228 userApproval: false,
229 txn: Maybe.Nothing(),
230 stx: Maybe.Nothing(),
231 })
232 }
236 handleClearTransaction() {
237 this.setState({
238 userApproval: false,
239 txn: Maybe.Nothing(),
240 stx: Maybe.Nothing(),
241 })
242 }
244 downloadKeyfile(networkSeed) {
245 const { pointCache } = this.props
246 const { pointCursor } = this.props
248 const point = pointCursor.matchWith({
249 Just: (pt) => pt.value,
250 Nothing: () => {
251 throw BRIDGE_ERROR.MISSING_POINT
252 }
253 })
255 const pointDetails =
256 point in pointCache
257 ? pointCache[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`)
264 }
266 handleSubmit(){
267 const { props, state } = this
268 sendSignedTransaction(props.web3.value, state.stx)
269 .then(sent => {
270 props.setNetworkSeedCache(this.state.networkSeed)
271 props.setTxnHashCursor(sent)
272 props.popRoute()
273 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION, {promptKeyfile: true})
274 })
275 .catch(err => {
276 this.setState({ txError: err.map(val => val.merge()) })
277 })
278 }
280 createUnsignedTxn() {
281 const { state, props } = this
283 const validContracts = props.contracts.matchWith({
284 Just: (cs) => cs.value,
285 Nothing: () => {
286 throw BRIDGE_ERROR.MISSING_CONTRACTS
287 }
288 })
290 const validPoint = props.pointCursor.matchWith({
291 Just: (shp) => shp.value,
292 Nothing: () => {
293 throw BRIDGE_ERROR.MISSING_POINT
294 }
295 })
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(
307 validContracts,
308 validPoint,
309 pencr,
310 pauth,
311 1,
312 false
313 )
315 return Maybe.Just(txn)
316 }
318 return Maybe.Nothing()
319 }
321 render() {
322 const { props, state } = this
324 const canGenerate =
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)
331 const canSend =
332 Maybe.Just.hasInstance(state.stx)
333 && state.userApproval === true
335 const pointDetails =
336 state.point in props.pointCache
337 ? props.pointCache[state.point]
338 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
340 return (
341 <Row>
342 <Col>
343 <H1>
344 { 'Set Network Keys For ' } <code>{ `${ob.patp(state.point)}` }</code>
345 </H1>
347 <P className="mt-10">
348 {
349 `Set new authentication and encryption keys for your Arvo ship.`
350 }
351 </P>
353 { state.nondeterministicSeed &&
354 <Warning>
355 <h3 className={'mb-2'}>{'Warning'}</h3>
356 {
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.`
360 }
361 </Warning>
362 }
364 { pointDetails.keyRevisionNumber === '0'
365 ? <Warning>
366 <h3 className={'mb-2'}>{'Warning'}</h3>
367 {
368 'Once these keys have been set, your point is considered ' +
369 "'linked'. This operation cannot be undone."
370 }
371 </Warning>
372 : <div /> }
374 <StatelessTransaction
375 // Upper scope
376 web3={props.web3}
377 contracts={props.contracts}
378 wallet={props.wallet}
379 walletType={props.walletType}
380 walletHdPath={props.walletHdPath}
381 networkType={props.networkType}
382 // Tx
383 txn={state.txn}
384 stx={state.stx}
385 // Tx details
386 nonce={state.nonce}
387 gasPrice={state.gasPrice}
388 chainId={state.chainId}
389 gasLimit={state.gasLimit}
390 showGasDetails={state.showGasDetails}
391 toggleGasDetails={this.toggleGasDetails}
392 // Checks
393 userApproval={state.userApproval}
394 canGenerate={ canGenerate }
395 canSign={ canSign }
396 canApprove={ canApprove }
397 canSend={ canSend }
398 // Methods
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} />
409 {
410 Maybe.Nothing.hasInstance(state.txError)
411 ? ''
412 : <Warning className={'mt-8'}>
413 <H3 style={{marginTop: 0, paddingTop: 0}}>
414 {
415 'There was an error sending your transaction.'
416 }
417 </H3>
418 { state.txError.value }
419 </Warning>
420 }
422 </Col>
423 </Row>
424 )
425 }
428export default SetKeys