on 6952d2f help sign in
bridge/src/views/SetKeys.js
422 lines 10 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 point: point,
45 cryptoSuiteVersion: 1,
46 continuity: false,
47 txn: Maybe.Nothing(),
48 txError: Maybe.Nothing(),
49 isManagementSeed: false,
50 userApproval: false,
51 nonce: '',
52 gasPrice: '5',
53 showGasDetails: false,
54 chainId: '',
55 gasLimit: '600000',
56 stx: Maybe.Nothing(),
57 }
59 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
60 // Transaction
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)
71 }
73 toggleGasDetails() {
74 this.setState({
75 showGasDetails: !this.state.showGasDetails
76 })
77 }
79 componentDidMount() {
80 const { props } = this
82 this.deriveSeed()
84 const addr = props.wallet.matchWith({
85 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
86 Nothing: () => {
87 throw BRIDGE_ERROR.MISSING_WALLET
88 }
89 })
91 this.determineManagementSeed(props.contracts.value, addr)
93 props.web3.matchWith({
94 Nothing: () => {},
95 Just: (w3) => {
96 const validWeb3 = w3.value
98 const getTxMetadata = [
99 validWeb3.eth.getTransactionCount(addr),
100 validWeb3.eth.net.getId(),
101 validWeb3.eth.getGasPrice()
102 ];
104 Promise.all(getTxMetadata).then(r => {
105 const txMetadata = {
106 nonce: r[0],
107 chainId: r[1],
108 gasPrice: fromWei(r[2], 'gwei'),
109 };
111 this.setState({...txMetadata})
113 })
114 }
115 });
116 }
118 async determineManagementSeed(ctrcs, addr) {
119 const managing =
120 await azimuth.azimuth.getManagerFor(ctrcs, addr)
122 this.setState({
123 isManagementSeed: managing.length !== 0
124 })
125 }
127 //TODO use web3.utils.randomHex when it gets fixed, see web3.js#1490
128 randomHex(len) {
129 let hex = "";
131 for (var i = 0; i < len; i++) {
132 hex = hex + ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
133 'C', 'D', 'E', 'F'][Math.floor(Math.random() * 16)]
134 }
136 return hex;
137 }
139 async deriveSeed() {
140 const next = true
141 let seed = await attemptSeedDerivation(next, this.props)
143 if (seed.getOrElse('') === '') {
144 seed = Maybe.Just(this.randomHex(64));
145 }
147 this.setState({
148 networkSeed: seed.getOrElse('')
149 })
150 }
152 handleNetworkSeedInput(networkSeed) {
153 this.setState({ networkSeed })
154 }
156 handleCreateUnsignedTxn() {
157 const txn = this.createUnsignedTxn()
158 this.setState({ txn })
159 }
162 handleSetUserApproval(){
163 const {state} = this
164 this.setState({ userApproval: !state.userApproval })
165 }
169 handleSetTxn(txn){
170 this.setState({ txn })
171 }
175 handleSetStx(stx){
176 this.setState({
177 stx,
178 userApproval: false,
179 })
180 }
184 handleSetNonce(nonce){
185 this.setState({ nonce })
186 this.handleClearStx()
187 }
191 handleSetChainId(chainId){
192 this.setState({ chainId })
193 this.handleClearStx()
194 }
198 handleSetGasPrice(gasPrice){
199 this.setState({ gasPrice })
200 this.handleClearStx()
201 }
205 handleSetGasLimit(gasLimit){
206 this.setState({ gasLimit })
207 this.handleClearStx()
208 }
212 handleClearStx() {
213 this.setState({
214 userApproval: false,
215 stx: Maybe.Nothing(),
216 })
217 }
222 handleClearTxn() {
223 this.setState({
224 userApproval: false,
225 txn: Maybe.Nothing(),
226 stx: Maybe.Nothing(),
227 })
228 }
232 handleClearTransaction() {
233 this.setState({
234 userApproval: false,
235 txn: Maybe.Nothing(),
236 stx: Maybe.Nothing(),
237 })
238 }
240 downloadKeyfile(networkSeed) {
241 const { pointCache } = this.props
242 const { pointCursor } = this.props
244 const point = pointCursor.matchWith({
245 Just: (pt) => pt.value,
246 Nothing: () => {
247 throw BRIDGE_ERROR.MISSING_POINT
248 }
249 })
251 const pointDetails =
252 point in pointCache
253 ? pointCache[point]
254 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
256 const revision = parseInt(pointDetails.keyRevisionNumber)
257 const keyfile = genKey(networkSeed, point, revision)
258 let blob = new Blob([keyfile], {type:"text/plain;charset=utf-8"});
259 saveAs(blob, `${ob.patp(point).slice(1)}-${revision}.key`)
260 }
262 handleSubmit(){
263 const { props, state } = this
264 sendSignedTransaction(props.web3.value, state.stx)
265 .then(sent => {
266 props.setNetworkSeedCache(this.state.networkSeed)
267 // this.downloadKeyfile(this.state.networkSeed)
268 props.setTxnHashCursor(sent)
269 props.popRoute()
270 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
271 })
272 .catch(err => {
273 this.setState({ txError: err.map(val => val.merge()) })
274 })
275 }
279 createUnsignedTxn() {
280 const { state, props } = this
282 const validContracts = props.contracts.matchWith({
283 Just: (cs) => cs.value,
284 Nothing: () => {
285 throw BRIDGE_ERROR.MISSING_CONTRACTS
286 }
287 })
289 const validPoint = props.pointCursor.matchWith({
290 Just: (shp) => shp.value,
291 Nothing: () => {
292 throw BRIDGE_ERROR.MISSING_POINT
293 }
294 })
296 const hexRegExp = /[0-9A-Fa-f]{64}/g
298 if (hexRegExp.test(state.networkSeed)) {
299 // derive network keys
300 const pair = kg.deriveNetworkKeys(state.networkSeed)
302 const pencr = addHexPrefix(pair.crypt.public)
303 const pauth = addHexPrefix(pair.auth.public)
305 const txn = azimuth.ecliptic.configureKeys(
306 validContracts,
307 validPoint,
308 pencr,
309 pauth,
310 1,
311 false
312 )
314 return Maybe.Just(txn)
315 }
317 return Maybe.Nothing()
318 }
320 render() {
321 const { props, state } = this
323 const canGenerate =
324 state.networkSeed.length === 64
325 && state.networkSeed.length === 64
327 const canSign = Maybe.Just.hasInstance(state.txn)
328 const canApprove = Maybe.Just.hasInstance(state.stx)
330 const canSend =
331 Maybe.Just.hasInstance(state.stx)
332 && state.userApproval === true
334 const pointDetails =
335 state.point in props.pointCache
336 ? props.pointCache[state.point]
337 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
339 return (
340 <Row>
341 <Col>
342 <H1>
343 { 'Set Network Keys For ' } <code>{ `${ob.patp(state.point)}` }</code>
344 </H1>
346 <P className="mt-10">
347 {
348 `Set new authentication and encryption keys for your Arvo ship.`
349 }
350 </P>
352 <P>
353 {
354 `Once the transaction is sent, please generate your Arvo keyfile immediately.`
355 }
356 </P>
358 { pointDetails.keyRevisionNumber === '0'
359 ? <Warning>
360 <h3 className={'mb-2'}>{'Warning'}</h3>
361 {
362 'Once these keys have been set, your point is considered ' +
363 "'linked'. This operation cannot be undone."
364 }
365 </Warning>
366 : <div /> }
368 <StatelessTransaction
369 // Upper scope
370 web3={props.web3}
371 contracts={props.contracts}
372 wallet={props.wallet}
373 walletType={props.walletType}
374 walletHdPath={props.walletHdPath}
375 networkType={props.networkType}
376 // Tx
377 txn={state.txn}
378 stx={state.stx}
379 // Tx details
380 nonce={state.nonce}
381 gasPrice={state.gasPrice}
382 chainId={state.chainId}
383 gasLimit={state.gasLimit}
384 showGasDetails={state.showGasDetails}
385 toggleGasDetails={this.toggleGasDetails}
386 // Checks
387 userApproval={state.userApproval}
388 canGenerate={ canGenerate }
389 canSign={ canSign }
390 canApprove={ canApprove }
391 canSend={ canSend }
392 // Methods
393 createUnsignedTxn={this.handleCreateUnsignedTxn}
394 setUserApproval={this.handleSetUserApproval}
395 setTxn={this.handleSetTxn}
396 setStx={this.handleSetStx}
397 setNonce={this.handleSetNonce}
398 setChainId={this.handleSetChainId}
399 setGasPrice={this.handleSetGasPrice}
400 setGasLimit={this.handleSetGasLimit}
401 handleSubmit={this.handleSubmit} />
403 {
404 Maybe.Nothing.hasInstance(state.txError)
405 ? ''
406 : <Warning className={'mt-8'}>
407 <H3 style={{marginTop: 0, paddingTop: 0}}>
408 {
409 'There was an error sending your transaction.'
410 }
411 </H3>
412 { state.txError.value }
413 </Warning>
414 }
416 </Col>
417 </Row>
418 )
419 }
422export default SetKeys