Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
refactor: extract sign/verify grpc interactions into actions
Browse files Browse the repository at this point in the history
  • Loading branch information
korhaliv committed Aug 7, 2019
1 parent 758f9ce commit 1c4ead1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import React, { useState, useRef } from 'react'
import PropTypes from 'prop-types'
import { FormattedMessage, injectIntl, intlShape } from 'react-intl'
import { Box, Flex } from 'rebass'
import { grpc } from 'workers'
import { Bar, CopyBox, TextArea, Text, Form, Button } from 'components/UI'
import messages from './messages'

const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
const ProfilePaneNodeInfo = ({ intl, signMessage, showNotification, ...rest }) => {
const [sig, setSig] = useState(null)

const formApiRef = useRef(null)
Expand All @@ -17,7 +16,7 @@ const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
try {
const { current: formApi } = formApiRef
const message = formApi.getValue('message')
const { signature } = await grpc.services.Lightning.signMessage({ msg: Buffer.from(message) })
const { signature } = await signMessage(message)
setSig(signature)
} catch (e) {
setSig(intl.formatMessage({ ...messages.sign_error }))
Expand Down Expand Up @@ -45,10 +44,10 @@ const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
word-break: break-all;
`}
description={intl.formatMessage({ ...messages.sign_message_desc })}
field="message"
isRequired
label={intl.formatMessage({ ...messages.sign_message_label })}
{...rest}
field="message"
onChange={() => setSig(null)}
spellCheck="false"
/>
<Button alignSelf="flex-end" mt={3} type="submit" variant="normal">
Expand All @@ -62,7 +61,7 @@ const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
<Bar />

<CopyBox
hint={intl.formatMessage({ ...messages.sign_message_action })}
hint={intl.formatMessage({ ...messages.copy_signature })}
my={3}
onCopy={notifyOfCopy}
value={sig}
Expand All @@ -78,6 +77,7 @@ const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
ProfilePaneNodeInfo.propTypes = {
intl: intlShape.isRequired,
showNotification: PropTypes.func.isRequired,
signMessage: PropTypes.func.isRequired,
}

export default injectIntl(ProfilePaneNodeInfo)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useState, useRef } from 'react'
import PropTypes from 'prop-types'
import { FormattedMessage, injectIntl, intlShape } from 'react-intl'
import { Box, Flex } from 'rebass'
import { grpc } from 'workers'
import { Bar, CopyBox, TextArea, Text, Form, Button, Input, Message } from 'components/UI'
import messages from './messages'

Expand All @@ -18,7 +17,7 @@ VerificationStatus.propTypes = {
isValid: PropTypes.bool,
}

const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
const ProfilePaneNodeInfo = ({ intl, verifyMessage, showNotification, ...rest }) => {
const [info, setInfo] = useState(null)
const formApiRef = useRef(null)

Expand All @@ -32,10 +31,7 @@ const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
const { current: formApi } = formApiRef
const message = formApi.getValue('message')
const signature = formApi.getValue('signature')
const { valid, pubkey } = await grpc.services.Lightning.verifyMessage({
msg: Buffer.from(message),
signature: signature,
})
const { valid, pubkey } = await verifyMessage(message, signature)
setInfo({ valid, pubkey })
} catch (e) {
setInfo({ valid: false })
Expand All @@ -62,15 +58,14 @@ const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
<TextArea
css={`
word-break: break-all;
height: 90px;
`}
description={intl.formatMessage({ ...messages.verify_message_desc })}
field="message"
isRequired
label={intl.formatMessage({ ...messages.verify_message_label })}
onChange={reset}
{...rest}
field="message"
mb={3}
mt={2}
onChange={reset}
spellCheck="false"
/>
<Input
Expand All @@ -87,7 +82,7 @@ const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {

{info && (
<>
<Text fontWeight="normal" mt={5}>
<Text fontWeight="normal" mt={4}>
<FormattedMessage {...messages.verification} />
</Text>
<Bar />
Expand All @@ -111,6 +106,7 @@ const ProfilePaneNodeInfo = ({ intl, showNotification, ...rest }) => {
ProfilePaneNodeInfo.propTypes = {
intl: intlShape.isRequired,
showNotification: PropTypes.func.isRequired,
verifyMessage: PropTypes.func.isRequired,
}

export default injectIntl(ProfilePaneNodeInfo)
2 changes: 2 additions & 0 deletions renderer/containers/Profile/ProfilePaneSignMessage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { connect } from 'react-redux'
import { showNotification } from 'reducers/notification'
import { signMessage } from 'reducers/lnd'
import ProfilePaneSignMessage from 'components/Profile/ProfilePaneSignMessage'

const mapDispatchToProps = {
showNotification,
signMessage,
}

export default connect(
Expand Down
2 changes: 2 additions & 0 deletions renderer/containers/Profile/ProfilePaneVerifyMessage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { connect } from 'react-redux'
import { showNotification } from 'reducers/notification'
import { verifyMessage } from 'reducers/lnd'
import ProfilePaneVerifyMessage from 'components/Profile/ProfilePaneVerifyMessage'

const mapDispatchToProps = {
showNotification,
verifyMessage,
}

export default connect(
Expand Down
24 changes: 24 additions & 0 deletions renderer/reducers/lnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,30 @@ export const fetchSeedSuccess = cipher_seed_mnemonic => dispatch => {
dispatch(stopLnd())
}

/**
* signMessage - Sign a message with a node's private key.
*
* @param {string} message Message to sign
* @returns {Function} Thunk
*/
export const signMessage = message => () => {
return grpc.services.Lightning.signMessage({ msg: Buffer.from(message) })
}

/**
* verifyMessage - Verify `signature` over the given `message`.
*
* @param {string} message Message to verify
* @param {string} signature Signature
* @returns {Function} Thunk
*/
export const verifyMessage = (message, signature) => () => {
return grpc.services.Lightning.verifyMessage({
msg: Buffer.from(message),
signature: signature,
})
}

/**
* fetchSeedError - Fetch seed error callback.
*
Expand Down

0 comments on commit 1c4ead1

Please sign in to comment.