-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Bitcoin): Add "Verify Message" modal to validate the signature o…
…f a Bitcoin message. fix #WEB4-500
- Loading branch information
Empowerful
committed
Dec 10, 2018
1 parent
0d96e1c
commit 525574d
Showing
8 changed files
with
254 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/blockchain-wallet-v4-frontend/src/modals/Btc/VerifyMessage/core.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import BitcoinMessage from 'bitcoinjs-message' | ||
|
||
export const showResult = ({ address, message, signature }) => | ||
Boolean(address && message && signature) | ||
|
||
export const verifySignature = ({ address, message, signature }) => { | ||
// Exceptions are thrown if the signature is malformed or doesn't match the | ||
// address. | ||
try { | ||
return BitcoinMessage.verify(message, address, signature) | ||
} catch (exception) { | ||
return false | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/blockchain-wallet-v4-frontend/src/modals/Btc/VerifyMessage/core.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as core from './core' | ||
|
||
describe(`showResult`, () => { | ||
it(`none`, () => { | ||
expect( | ||
core.showResult({ address: ``, message: ``, signature: `` }) | ||
).toEqual(false) | ||
}) | ||
|
||
it(`some`, () => { | ||
expect( | ||
core.showResult({ | ||
address: `address`, | ||
message: `message`, | ||
signature: `` | ||
}) | ||
).toEqual(false) | ||
}) | ||
|
||
it(`all`, () => { | ||
expect( | ||
core.showResult({ | ||
address: `address`, | ||
message: `message`, | ||
signature: `signature` | ||
}) | ||
).toEqual(true) | ||
}) | ||
}) | ||
|
||
describe(`verifySignature`, () => { | ||
it(`happy`, () => { | ||
expect( | ||
core.verifySignature({ | ||
address: `15WJg3bnKkuzeLxCKUGCKjRM42d3LgWg9u`, | ||
message: `Fun is nuf spelled backwards!`, | ||
signature: `HxB1FMYnYLda3SDhFVXpfyUfxfGOUOuCkrY0rwxIFaXzWgv/y7Wlijd9C/2t6ydoVOpi4v5XedgvDDOb8a6ZCfo=` | ||
}) | ||
).toEqual(true) | ||
}) | ||
|
||
it(`bad signature`, () => { | ||
expect( | ||
core.verifySignature({ | ||
address: `15WJg3bnKkuzeLxCKUGCKjRM42d3LgWg9u`, | ||
message: `Fun is nuf spelled backwards!`, | ||
signature: `bad signature` | ||
}) | ||
).toEqual(false) | ||
}) | ||
}) |
142 changes: 142 additions & 0 deletions
142
packages/blockchain-wallet-v4-frontend/src/modals/Btc/VerifyMessage/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Verify the signature of a message signed with a Bitcoin address. | ||
|
||
import { | ||
Banner, | ||
Button, | ||
Modal, | ||
ModalHeader, | ||
ModalBody, | ||
ModalFooter, | ||
TooltipIcon, | ||
TooltipHost | ||
} from 'blockchain-info-components' | ||
|
||
import { FormItem, FormLabel, TextArea, TextBox } from 'components/Form' | ||
import * as core from './core' | ||
import modalEnhancer from 'providers/ModalEnhancer' | ||
import React from 'react' | ||
import { FormattedMessage } from 'react-intl' | ||
import { validBitcoinAddress } from 'services/FormHelper' | ||
|
||
const item = (label, input) => ( | ||
<FormItem style={{ marginBottom: `1rem` }}> | ||
<FormLabel> | ||
<div style={{ marginBottom: `0.5rem` }}>{label}</div> | ||
{input} | ||
</FormLabel> | ||
</FormItem> | ||
) | ||
|
||
const ItemAddress = ({ address, network, onChange }) => | ||
item( | ||
<FormattedMessage | ||
id='modals.verifyMessage.address' | ||
defaultMessage='Bitcoin Address:' | ||
/>, | ||
<TextBox | ||
input={{ | ||
onChange, | ||
name: 'address' | ||
}} | ||
meta={{ | ||
error: validBitcoinAddress(address, null, { network }), | ||
touched: address !== `` | ||
}} | ||
/> | ||
) | ||
|
||
const ItemMessage = ({ onChange }) => | ||
item( | ||
<FormattedMessage | ||
id='modals.verifyMessage.message' | ||
defaultMessage='Message:' | ||
/>, | ||
<TextArea | ||
input={{ | ||
name: 'message', | ||
onChange | ||
}} | ||
meta={{}} | ||
/> | ||
) | ||
|
||
const ItemSignature = ({ onChange }) => | ||
item( | ||
<FormattedMessage | ||
id='modals.verifyMessage.signature' | ||
defaultMessage='Signature:' | ||
/>, | ||
<TextArea | ||
input={{ | ||
name: 'signature', | ||
onChange | ||
}} | ||
meta={{}} | ||
/> | ||
) | ||
|
||
class VerifyMessage extends React.PureComponent { | ||
constructor (props) { | ||
super(props) | ||
this.state = { address: ``, message: ``, signature: `` } | ||
} | ||
|
||
render () { | ||
const { close, network } = this.props | ||
|
||
const onChange = ({ target: { name, value } }) => { | ||
this.setState({ [name]: value }) | ||
} | ||
|
||
return ( | ||
<Modal> | ||
<ModalHeader onClose={close}> | ||
<FormattedMessage | ||
id='modals.verifyMessage.title' | ||
defaultMessage='Verify Message' | ||
/> | ||
<TooltipHost id='verifyMessage'> | ||
<TooltipIcon name='question-in-circle' /> | ||
</TooltipHost> | ||
</ModalHeader> | ||
<ModalBody> | ||
<ItemAddress | ||
address={this.state.address} | ||
network={network} | ||
onChange={onChange} | ||
/> | ||
<ItemMessage onChange={onChange} /> | ||
<ItemSignature onChange={onChange} /> | ||
<div | ||
style={{ | ||
visibility: core.showResult(this.state) ? `visible` : `hidden` | ||
}} | ||
> | ||
{core.verifySignature(this.state) ? ( | ||
<Banner type='success'> | ||
<FormattedMessage | ||
id='modals.verifyMessage.success' | ||
defaultMessage='The message has a valid signature from the address.' | ||
/> | ||
</Banner> | ||
) : ( | ||
<Banner type='caution'> | ||
<FormattedMessage | ||
id='modals.verifyMessage.failure' | ||
defaultMessage='The signature does not match the message.' | ||
/> | ||
</Banner> | ||
)} | ||
</div> | ||
</ModalBody> | ||
<ModalFooter align='right'> | ||
<Button onClick={close} nature='primary'> | ||
<FormattedMessage id='close' defaultMessage='Close' /> | ||
</Button> | ||
</ModalFooter> | ||
</Modal> | ||
) | ||
} | ||
} | ||
|
||
export default modalEnhancer('VerifyMessage')(VerifyMessage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters