on 6952d2f help sign in
bridge/src/views/GenKeyfile.js
165 lines 3.9 kB history raw
1import React from 'react'
2import Maybe from 'folktale/maybe'
4import { Button } from '../components/Base'
5import { RequiredInput, InnerLabel } from '../components/Base'
6import { Row, Col, H1, P } from '../components/Base'
7import { Code } from '../components/Base'
9import * as ob from 'urbit-ob'
10import * as kg from '../../node_modules/urbit-key-generation/dist/index'
11import saveAs from 'file-saver'
13import { attemptSeedDerivation, genKey } from '../lib/keys'
14import { BRIDGE_ERROR } from '../lib/error'
15import {
16 addHexPrefix
17 } from '../lib/wallet'
19class GenKeyfile extends React.Component {
20 constructor(props) {
21 super(props)
23 this.state = {
24 keyfile: '',
25 networkSeed: ''
26 }
28 this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this)
29 this.handleKeyfileChange = this.handleKeyfileChange.bind(this)
30 }
32 componentDidMount() {
33 this.deriveSeed()
34 }
36 async deriveSeed() {
37 const next = false
38 let seed = await attemptSeedDerivation(next, this.props)
40 if (seed.getOrElse('') === '' && this.props.networkSeedCache) {
41 seed = Maybe.Just(this.props.networkSeedCache)
42 }
44 this.setState({
45 networkSeed: seed.getOrElse('')
46 })
47 }
49 handleNetworkSeedInput = (networkSeed) => {
50 this.setState({ networkSeed })
51 }
53 handleKeyfileChange = (keyfile) => {
54 this.setState({ keyfile })
55 }
57 render() {
58 const { pointCache } = this.props
59 const { pointCursor } = this.props
61 const { keyfile, networkSeed } = this.state
63 const point = pointCursor.matchWith({
64 Just: (pt) => pt.value,
65 Nothing: () => {
66 throw BRIDGE_ERROR.MISSING_POINT
67 }
68 })
70 const pointDetails =
71 point in pointCache
72 ? pointCache[point]
73 : (() => { throw BRIDGE_ERROR.MISSING_POINT })()
75 const revision = parseInt(pointDetails.keyRevisionNumber)
77 const crypub = pointDetails.encryptionKey
78 const sgnpub = pointDetails.authenticationKey
80 const { crypt, auth } = kg.deriveNetworkKeys(networkSeed)
82 const keysmatch =
83 crypub === addHexPrefix(crypt.public)
84 && sgnpub === addHexPrefix(auth.public)
86 const warning =
87 keyfile !== '' && keysmatch === false
88 ? <Row>
89 <Col>
90 <b>{ 'WARNING' }</b>{ ": derived key doesn't match Azimuth keys!" }
91 </Col>
92 </Row>
93 : <div />
95 const hexRegExp = /[0-9A-Fa-f]{64}/g
97 const download =
98 keyfile !== ''
99 ? <Button
100 onClick={
101 () => {
102 let blob = new Blob([keyfile], {type:"text/plain;charset=utf-8"});
103 saveAs(blob, `${ob.patp(point).slice(1)}-${revision}.key`)
104 }
105 }>
106 Download →
107 </Button>
108 : ''
110 let networkSeedWarning
112 if (this.state.networkSeed === '') {
113 networkSeedWarning = (
114 <P>
115 <b>Warning: </b>
116 { "No network seed detected. To generate a keyfile, you must reset your networking keys." }
117 </P>
118 )
119 }
121 return (
122 <Row>
123 <Col className={'col-md-8'}>
124 <H1>{ 'Generate keyfile' }</H1>
126 <P>
127 { "Generate a private key file for booting this point in Arvo." }
128 </P>
130 { networkSeedWarning }
132 <Button
133 className={'mt-8'}
134 color={'blue'}
135 disabled={this.state.networkSeed === ''}
136 onClick={
137 () => {
138 if (hexRegExp.test(networkSeed)) {
139 const keyfile = genKey(networkSeed, point, revision)
140 this.setState({ keyfile })
141 }
142 }
143 }
144 >
145 { 'Generate →' }
146 </Button>
148 { keyfile !== '' &&
149 <Code className="pb-5">
150 { keyfile }
151 </Code>
152 }
154 { warning }
156 { download }
158 </Col>
159 </Row>
160 )
161 }
165export default GenKeyfile