import React from 'react' import Maybe from 'folktale/maybe' import * as azimuth from 'azimuth-js' import * as ob from 'urbit-ob' import { Row, Col, H1, P, Warning, H3 } from '../components/Base' import { RequiredInput, InnerLabel } from '../components/Base' import StatelessTransaction from '../components/StatelessTransaction' import { BRIDGE_ERROR } from '../lib/error' import { ROUTE_NAMES } from '../lib/router' import { attemptSeedDerivation } from '../lib/keys' import * as kg from '../../node_modules/urbit-key-generation/dist/index' import { sendSignedTransaction, fromWei, } from '../lib/txn' import { addressFromSecp256k1Public, addHexPrefix } from '../lib/wallet' class SetKeys extends React.Component { constructor(props) { super(props) const point = props.pointCursor.matchWith({ Just: (shp) => shp.value, Nothing: () => { throw BRIDGE_ERROR.MISSING_POINT } }) this.state = { auth: '', encr: '', networkSeed: '', point: point, cryptoSuiteVersion: 1, continuity: false, txn: Maybe.Nothing(), txError: Maybe.Nothing(), userApproval: false, nonce: '', gasPrice: '5', chainId: '', gasLimit: '600000', stx: Maybe.Nothing(), } this.handleNetworkSeedInput = this.handleNetworkSeedInput.bind(this) // Transaction this.handleCreateUnsignedTxn = this.handleCreateUnsignedTxn.bind(this) this.handleSubmit = this.handleSubmit.bind(this) this.handleSetUserApproval = this.handleSetUserApproval.bind(this) this.handleSetTxn = this.handleSetTxn.bind(this) this.handleSetStx = this.handleSetStx.bind(this) this.handleSetNonce = this.handleSetNonce.bind(this) this.handleSetChainId = this.handleSetChainId.bind(this) this.handleSetGasPrice = this.handleSetGasPrice.bind(this) this.handleSetGasLimit = this.handleSetGasLimit.bind(this) } componentDidMount() { const { props } = this this.deriveSeed() const addr = props.wallet.matchWith({ Just: wal => addressFromSecp256k1Public(wal.value.publicKey), Nothing: () => { throw BRIDGE_ERROR.MISSING_WALLET } }) props.web3.matchWith({ Nothing: () => {}, Just: (w3) => { const validWeb3 = w3.value const getTxMetadata = [ validWeb3.eth.getTransactionCount(addr), validWeb3.eth.net.getId(), validWeb3.eth.getGasPrice() ]; Promise.all(getTxMetadata).then(r => { const txMetadata = { nonce: r[0], chainId: r[1], gasPrice: fromWei(r[2], 'gwei'), }; this.setState({...txMetadata}) }) } }); } async deriveSeed() { const next = true const seed = await attemptSeedDerivation(next, this.props) this.setState({ networkSeed: seed.getOrElse('') }) } handleNetworkSeedInput(networkSeed) { this.setState({ networkSeed }) } handleCreateUnsignedTxn() { const txn = this.createUnsignedTxn() this.setState({ txn }) } handleSetUserApproval(){ const {state} = this this.setState({ userApproval: !state.userApproval }) } handleSetTxn(txn){ this.setState({ txn }) } handleSetStx(stx){ this.setState({ stx, userApproval: false, }) } handleSetNonce(nonce){ this.setState({ nonce }) this.handleClearStx() } handleSetChainId(chainId){ this.setState({ chainId }) this.handleClearStx() } handleSetGasPrice(gasPrice){ this.setState({ gasPrice }) this.handleClearStx() } handleSetGasLimit(gasLimit){ this.setState({ gasLimit }) this.handleClearStx() } handleClearStx() { this.setState({ userApproval: false, stx: Maybe.Nothing(), }) } handleClearTxn() { this.setState({ userApproval: false, txn: Maybe.Nothing(), stx: Maybe.Nothing(), }) } handleClearTransaction() { this.setState({ userApproval: false, txn: Maybe.Nothing(), stx: Maybe.Nothing(), }) } handleSubmit(){ const { props, state } = this sendSignedTransaction(props.web3.value, state.stx) .then(sent => { props.setTxnHashCursor(sent) props.popRoute() props.pushRoute(ROUTE_NAMES.SENT_TRANSACTION) }) .catch(err => { this.setState({ txError: err.map(val => val.merge()) }) }) } createUnsignedTxn() { const { state, props } = this const validContracts = props.contracts.matchWith({ Just: (cs) => cs.value, Nothing: () => { throw BRIDGE_ERROR.MISSING_CONTRACTS } }) const validPoint = props.pointCursor.matchWith({ Just: (shp) => shp.value, Nothing: () => { throw BRIDGE_ERROR.MISSING_POINT } }) const hexRegExp = /[0-9A-Fa-f]{64}/g if (hexRegExp.test(state.networkSeed)) { // derive network keys const pair = kg.deriveNetworkKeys(state.networkSeed) const pencr = addHexPrefix(pair.crypt.public) const pauth = addHexPrefix(pair.auth.public) const txn = azimuth.ecliptic.configureKeys( validContracts, validPoint, pencr, pauth, 1, false ) return Maybe.Just(txn) } return Maybe.Nothing() } render() { const { props, state } = this const canGenerate = state.networkSeed.length === 64 && state.networkSeed.length === 64 const canSign = Maybe.Just.hasInstance(state.txn) const canApprove = Maybe.Just.hasInstance(state.stx) const canSend = Maybe.Just.hasInstance(state.stx) && state.userApproval === true const pointDetails = state.point in props.pointCache ? props.pointCache[state.point] : (() => { throw BRIDGE_ERROR.MISSING_POINT })() return (

{ 'Set Network Keys For ' } { `${ob.patp(state.point)}` }

{ `Please enter a network seed for generating and setting your public network authentication and encryption keys. Your network seed must be a 32-byte-long hexadecimal string.` }

{ `If you've authenticated with a master ticket or management proxy mnemonic, a seed will be generated for you automatically.` }

{ pointDetails.keyRevisionNumber === '0' ?

{'Warning'}

{ 'Once these keys have been set, your point is considered ' + "'linked'. This operation cannot be undone." }
:
} { 'Network seed' } { Maybe.Nothing.hasInstance(state.txError) ? '' :

{ 'There was an error sending your transaction.' }

{ state.txError.value }
} ) } } export default SetKeys