on 915baeb help sign in
bridge/src/views/IssueChild.js
516 lines 13 kB history raw
1import Maybe from 'folktale/maybe'
2import React from 'react'
3import * as azimuth from 'azimuth-js'
4import * as ob from 'urbit-ob'
6import { Row, Col, H1, H3, P, Anchor, Warning } from '../components/Base'
7import { Button, ShowBlockie, ValidatedSigil } from '../components/Base'
8import { PointInput, AddressInput, InnerLabel } from '../components/Base'
10import StatelessTransaction from '../components/StatelessTransaction'
12import { ROUTE_NAMES } from '../lib/router'
13import { NETWORK_NAMES } from '../lib/network'
14import { BRIDGE_ERROR } from '../lib/error'
15import { getSpawnCandidate } from '../lib/child'
16import {
17 canDecodePatp,
18 sendSignedTransaction,
19 fromWei,
20 } from '../lib/txn'
22import {
23 ETH_ZERO_ADDR,
24 isValidAddress,
25 eqAddr,
26 addressFromSecp256k1Public
27} from '../lib/wallet'
29class IssueChild extends React.Component {
30 constructor(props) {
31 super(props)
34 const issuingPoint = props.pointCursor.matchWith({
35 Just: (pt) => parseInt(pt.value, 10),
36 Nothing: () => {
37 throw BRIDGE_ERROR.MISSING_POINT
38 }
39 })
41 const getCandidate = () => ob.patp(getSpawnCandidate(issuingPoint))
43 const suggestions = [
44 getCandidate(),
45 getCandidate(),
46 getCandidate(),
47 getCandidate()
48 ]
50 this.state = {
51 txError: Maybe.Nothing(),
52 receivingAddress: '',
53 issuingPoint: issuingPoint,
54 desiredPoint: '',
55 isAvailable: Maybe.Nothing(), // allow attempt when offline
56 userApproval: false,
57 nonce: '',
58 gasPrice: '5',
59 chainId: '',
60 gasLimit: '600000',
61 txn: Maybe.Nothing(),
62 stx: Maybe.Nothing(),
63 suggestions: suggestions,
64 }
66 this.handlePointInput = this.handlePointInput.bind(this)
67 this.handleAddressInput = this.handleAddressInput.bind(this)
68 this.handleConfirmAvailability = this.handleConfirmAvailability.bind(this)
69 // Transaction
70 this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this)
71 this.handleSubmit = this.handleSubmit.bind(this)
72 this.handleSetUserApproval = this.handleSetUserApproval.bind(this)
73 this.handleSetTxn = this.handleSetTxn.bind(this)
74 this.handleSetStx = this.handleSetStx.bind(this)
75 this.handleSetNonce = this.handleSetNonce.bind(this)
76 this.handleSetChainId = this.handleSetChainId.bind(this)
77 this.handleSetGasPrice = this.handleSetGasPrice.bind(this)
78 this.handleSetGasLimit = this.handleSetGasLimit.bind(this)
79 }
81 componentDidMount() {
82 const { props } = this
84 const addr = props.wallet.matchWith({
85 Just: wal => addressFromSecp256k1Public(wal.value.publicKey),
86 Nothing: () => {
87 throw BRIDGE_ERROR.MISSING_WALLET
88 }
89 })
91 props.web3.matchWith({
92 Nothing: () => {},
93 Just: (w3) => {
94 const validWeb3 = w3.value
96 const getTxMetadata = [
97 validWeb3.eth.getTransactionCount(addr),
98 validWeb3.eth.net.getId(),
99 validWeb3.eth.getGasPrice()
100 ];
102 Promise.all(getTxMetadata).then(r => {
103 const txMetadata = {
104 nonce: r[0],
105 chainId: r[1],
106 gasPrice: fromWei(r[2], 'gwei'),
107 };
109 this.setState({...txMetadata})
111 })
112 }
113 });
115 }
119 handleAddressInput = (receivingAddress) => {
120 this.setState({ receivingAddress })
121 this.handleClearTxn()
122 }
126 handlePointInput(desiredPoint) {
127 if (desiredPoint.length < 15) {
128 this.setState({
129 desiredPoint,
130 isAvailable: Maybe.Nothing()
131 })
132 this.handleClearTxn()
133 }
134 }
138 handleConfirmAvailability() {
139 this.confirmPointAvailability().then(r => {
140 this.setState({
141 isAvailable: r,
142 })
143 })
145 }
149 handleCreateUnsignedTxn() {
150 const txn = this.createUnsignedTxn()
151 this.setState({ txn })
152 }
155 handleSetUserApproval(){
156 const {state} = this
157 this.setState({ userApproval: !state.userApproval })
158 }
162 handleSetTxn(txn){
163 this.setState({ txn })
164 }
168 handleSetStx(stx){
169 this.setState({
170 stx,
171 userApproval: false,
172 })
173 }
177 handleSetNonce(nonce){
178 this.setState({ nonce })
179 this.handleClearStx()
180 }
184 handleSetChainId(chainId){
185 this.setState({ chainId })
186 this.handleClearStx()
187 }
191 handleSetGasPrice(gasPrice){
192 this.setState({ gasPrice })
193 this.handleClearStx()
194 }
198 handleSetGasLimit(gasLimit){
199 this.setState({ gasLimit })
200 this.handleClearStx()
201 }
205 handleClearStx() {
206 this.setState({
207 userApproval: false,
208 stx: Maybe.Nothing(),
209 })
210 }
214 handleClearTxn() {
215 this.setState({
216 userApproval: false,
217 txn: Maybe.Nothing(),
218 stx: Maybe.Nothing(),
219 })
220 }
224 handleClearTransaction() {
225 this.setState({
226 userApproval: false,
227 txn: Maybe.Nothing(),
228 stx: Maybe.Nothing(),
229 })
230 }
234 handleSubmit(){
235 const { props, state } = this
236 sendSignedTransaction(props.web3.value, state.stx)
237 .then(sent => {
238 props.setTxnCursor(sent)
239 props.popRoute()
240 props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION)
241 })
242 .catch(err => {
243 // Note that value.value is due to wrapped Maybe.Just + Result.Error
244 this.setState({ txError: Maybe.Just(err.value.value) })
245 })
246 }
250 async confirmPointAvailability() {
251 const { state, props } = this
253 if (canDecodePatp(state.desiredPoint) === false) return Maybe.Just(false)
255 const validContracts = props.contracts.matchWith({
256 Just: (cs) => cs.value,
257 Nothing: () => {
258 throw BRIDGE_ERROR.MISSING_CONTRACTS
259 }
260 })
262 const pointDec = ob.patp2dec(state.desiredPoint)
264 const owner = await azimuth.azimuth.getOwner(
265 validContracts,
266 pointDec
267 )
269 if (eqAddr(owner, ETH_ZERO_ADDR)) return Maybe.Just(true)
271 return Maybe.Just(false)
272 }
276 createUnsignedTxn() {
277 const { state, props } = this
279 if (isValidAddress(state.receivingAddress) === false) return Maybe.Nothing()
280 if (state.isAvailable === false) return Maybe.Nothing()
281 if (canDecodePatp(state.desiredPoint) === false) return Maybe.Nothing()
283 const validContracts = props.contracts.matchWith({
284 Just: (cs) => cs.value,
285 Nothing: () => {
286 throw BRIDGE_ERROR.MISSING_CONTRACTS
287 }
288 })
290 const pointDec = ob.patp2dec(state.desiredPoint)
292 const txn = azimuth.ecliptic.spawn(
293 validContracts,
294 pointDec,
295 state.receivingAddress
296 )
298 return Maybe.Just(txn)
299 }
303 buttonTriState() {
304 const a = this.state.isAvailable
305 if (Maybe.Nothing.hasInstance(a)) return 'blue'
306 if (a.value === false) return 'yellow'
307 if (a.value === true) return 'green'
308 }
312 buttonTriStateText() {
313 const a = this.state.isAvailable
314 if (Maybe.Nothing.hasInstance(a)) return 'Confirm Availablility'
315 if (a.value === false) return 'Point is Not Available'
316 if (a.value === true) return 'Available'
317 }
319 validatePoint = patp => {
320 const point = this.point
322 let vpatp = false
323 try {
324 vpatp = ob.isValidPatp(patp)
325 } catch(_) {
326 }
328 let vchild = false
329 try {
330 vchild = ob.sein(patp) === ob.patp(point)
331 } catch(_) {
332 }
334 return vpatp && vchild
335 }
338 render() {
339 const { props, state } = this
341 const validAddress = isValidAddress(state.receivingAddress)
342 const validPoint = canDecodePatp(state.desiredPoint)
344 const canGenerate = props.web3.matchWith({
345 Nothing: () => {
346 return validAddress === true &&
347 validPoint === true
348 },
349 Just: () => {
350 return validAddress === true &&
351 validPoint === true &&
352 state.isAvailable.value === true
353 }
354 })
356 const canSign = !Maybe.Nothing.hasInstance(state.txn)
357 const canApprove = !Maybe.Nothing.hasInstance(state.stx)
358 const canSend = !Maybe.Nothing.hasInstance(state.stx) && state.userApproval === true
360 const esvisible =
361 props.networkType === NETWORK_NAMES.ROPSTEN ||
362 props.networkType === NETWORK_NAMES.MAINNET
364 const esdomain =
365 props.networkType === NETWORK_NAMES.ROPSTEN
366 ? "ropsten.etherscan.io"
367 : "etherscan.io"
369 return (
370 <Row>
371 <Col>
372 <H1>
373 { 'Issue a Child From ' } <code>{ `${ob.patp(state.issuingPoint)}` }</code>
374 </H1>
377 <P>
378 {
379 `Please enter the point you would like to issue, and specify the
380 receiving Ethereum address. If you need to create an address, you
381 can also use Wallet Generator.`
382 }
383 </P>
385 <P>
386 {
387 `Your point can only issue children with particular names. Some
388 valid suggestions for `
389 }
390 {
391 <code>
392 { ob.patp(state.issuingPoint) }
393 </code>
394 }
395 {
396 ' are '
397 }
398 <code>
399 { state.suggestions[0] }
400 </code>
401 { ', ' }
402 <code>
403 { state.suggestions[1] }
404 </code>
405 { ', and ' }
406 <code>
407 { state.suggestions[2] }
408 </code>
409 { '.' }
410 </P>
412 <PointInput
413 prop-size='lg'
414 prop-format='innerLabel'
415 className={'mono mt-8'}
416 placeholder={ `e.g. ${state.suggestions[3]}` }
417 value={ state.desiredPoint }
418 onChange={ this.handlePointInput }>
419 <InnerLabel>{ 'Point to Issue' }</InnerLabel>
420 <ValidatedSigil
421 className={'tr-0 mt-05 mr-0 abs'}
422 patp={state.desiredPoint}
423 size={76}
424 margin={8}
425 validator={() => this.validatePoint(state.desiredPoint)} />
426 </PointInput>
428 <AddressInput
429 className='text-mono mt-8'
430 prop-size='lg'
431 prop-format='innerLabel'
432 placeholder={ `e.g. 0x84295d5e054d8cff5a22428b195f5a1615bd644f` }
433 value={ state.receivingAddress }
434 disabled={ !state.available }
435 onChange={ v => this.handleAddressInput(v) }>
436 <InnerLabel>{ 'Receiving Address' }</InnerLabel>
437 <ShowBlockie className={'mt-1'} address={state.receivingAddress} />
438 </AddressInput>
440 <Anchor
441 className={'mt-1'}
442 prop-size={'sm'}
443 prop-disabled={!isValidAddress(state.receivingAddress) || !esvisible}
444 target={'_blank'}
445 href={`https://${esdomain}/address/${state.receivingAddress}`}>
446 {'View on Etherscan ↗'}
447 </Anchor>
449 {
450 props.web3.matchWith({
451 Nothing: _ => <div />,
452 Just: _ =>
453 <Button
454 prop-size='lg wide'
455 prop-color={this.buttonTriState()}
456 className={'mt-8'}
457 disabled={!validPoint}
458 onClick={this.handleConfirmAvailability}>
459 {this.buttonTriStateText()}
460 </Button>
461 })
462 }
464 <StatelessTransaction
465 // Upper scope
466 web3={props.web3}
467 contracts={props.contracts}
468 wallet={props.wallet}
469 walletType={props.walletType}
470 walletHdPath={props.walletHdPath}
471 networkType={props.networkType}
472 // Tx
473 txn={state.txn}
474 stx={state.stx}
475 // Tx details
476 nonce={state.nonce}
477 gasPrice={state.gasPrice}
478 chainId={state.chainId}
479 gasLimit={state.gasLimit}
480 // Checks
481 userApproval={state.userApproval}
482 canGenerate={ canGenerate }
483 canSign={ canSign }
484 canApprove={ canApprove }
485 canSend={ canSend }
486 // Methods
487 createUnsignedTxn={this.handleCreateUnsignedTxn}
488 setUserApproval={this.handleSetUserApproval}
489 setTxn={this.handleSetTxn}
490 setStx={this.handleSetStx}
491 setNonce={this.handleSetNonce}
492 setChainId={this.handleSetChainId}
493 setGasPrice={this.handleSetGasPrice}
494 setGasLimit={this.handleSetGasLimit}
495 handleSubmit={this.handleSubmit} />
497 {
498 Maybe.Nothing.hasInstance(state.txError)
499 ? ''
500 : <Warning className={'mt-8'}>
501 <H3 style={{marginTop: 0, paddingTop: 0}}>
502 {
503 'There was an error sending your transaction.'
504 }
505 </H3>
506 { state.txError.value }
507 </Warning>
508 }
510 </Col>
511 </Row>
512 )
513 }
516export default IssueChild