From bc9765d99b115b8dd21895a7d16d97995dc920a8 Mon Sep 17 00:00:00 2001 From: Daniel Ternyak Date: Sat, 24 Jun 2017 01:08:33 -0500 Subject: [PATCH 1/3] Redux: Add SWAP_RECEIVING_ADDRESS action creators/reducers. --- common/actions/swap.js | 10 +++++++++- common/actions/swapConstants.js | 1 + common/reducers/swap.js | 11 +++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/common/actions/swap.js b/common/actions/swap.js index cdb3b1e1..79c37555 100644 --- a/common/actions/swap.js +++ b/common/actions/swap.js @@ -4,7 +4,8 @@ import { SWAP_ORIGIN_AMOUNT, SWAP_ORIGIN_KIND, SWAP_UPDATE_BITY_RATES, - SWAP_PART_ONE_COMPLETE + SWAP_PART_ONE_COMPLETE, + SWAP_RECEIVING_ADDRESS } from './swapConstants'; export const originKindSwap = value => { @@ -48,3 +49,10 @@ export const partOneCompleteSwap = (value: boolean) => { value }; }; + +export const receivingAddressSwap = value => { + return { + type: SWAP_RECEIVING_ADDRESS, + value + }; +}; diff --git a/common/actions/swapConstants.js b/common/actions/swapConstants.js index fe4cff5c..ecf0fb4a 100644 --- a/common/actions/swapConstants.js +++ b/common/actions/swapConstants.js @@ -4,3 +4,4 @@ export const SWAP_ORIGIN_AMOUNT = 'SWAP_ORIGIN_AMOUNT'; export const SWAP_DESTINATION_AMOUNT = 'SWAP_DESTINATION_AMOUNT'; export const SWAP_UPDATE_BITY_RATES = 'SWAP_UPDATE_BITY_RATES'; export const SWAP_PART_ONE_COMPLETE = 'SWAP_PART_ONE_COMPLETE'; +export const SWAP_RECEIVING_ADDRESS = 'SWAP_RECEIVING_ADDRESS'; diff --git a/common/reducers/swap.js b/common/reducers/swap.js index 06fbb2b9..3de1b101 100644 --- a/common/reducers/swap.js +++ b/common/reducers/swap.js @@ -4,7 +4,8 @@ import { SWAP_ORIGIN_AMOUNT, SWAP_ORIGIN_KIND, SWAP_UPDATE_BITY_RATES, - SWAP_PART_ONE_COMPLETE + SWAP_PART_ONE_COMPLETE, + SWAP_RECEIVING_ADDRESS } from 'actions/swapConstants'; import { combineAndUpper } from 'api/bity'; @@ -22,7 +23,8 @@ const initialState = { element => element !== 'REP' ), partOneComplete: false, - bityRates: {} + bityRates: {}, + receivingAddress: '' }; const buildDestinationAmount = ( @@ -101,6 +103,11 @@ export function swap(state = initialState, action) { ...state, partOneComplete: action.value }; + case SWAP_RECEIVING_ADDRESS: + return { + ...state, + receivingAddress: action.value + }; default: return state; } From c77f8430c53842f1dc979655a8bc4e8a246f3c31 Mon Sep 17 00:00:00 2001 From: Daniel Ternyak Date: Sat, 24 Jun 2017 01:08:57 -0500 Subject: [PATCH 2/3] Create Validator class and add deps to package.json --- common/libs/validator.js | 16 ++++++++++++++++ package.json | 2 ++ 2 files changed, 18 insertions(+) create mode 100644 common/libs/validator.js diff --git a/common/libs/validator.js b/common/libs/validator.js new file mode 100644 index 00000000..c930f47a --- /dev/null +++ b/common/libs/validator.js @@ -0,0 +1,16 @@ +import WalletAddressValidator from 'wallet-address-validator'; +import ethUtil from 'ethereumjs-util'; + +export default class Validator { + isValidETHAddress = function(address) { + if (address && address === '0x0000000000000000000000000000000000000000') + return false; + if (address) { + return ethUtil.isValidAddress(address); + } + return false; + }; + isValidBTCAddress = function(address) { + return WalletAddressValidator.validate(address, 'BTC'); + }; +} diff --git a/package.json b/package.json index ee4c86d0..c18c6355 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "description": "MyEtherWallet v4", "dependencies": { "axios": "^0.16.2", + "ethereumjs-util": "^5.1.2", "lodash": "^4.17.4", "prop-types": "^15.5.8", "react": "^15.4.2", @@ -17,6 +18,7 @@ "redux-logger": "^3.0.1", "redux-saga": "^0.15.3", "store2": "^2.5.0", + "wallet-address-validator": "^0.1.0", "whatwg-fetch": "^2.0.2" }, "devDependencies": { From 28415cd0e8bbdd5c4584e7e04ce5c1f48cd3b79d Mon Sep 17 00:00:00 2001 From: Daniel Ternyak Date: Sat, 24 Jun 2017 01:09:44 -0500 Subject: [PATCH 3/3] Consume new receivingAddressSwap action creator and receivingAddress state, validate receiving address input using Validator class. --- .../Tabs/Swap/components/yourReceiving.js | 35 ++++++++++++++++--- common/containers/Tabs/Swap/index.js | 17 +++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/common/containers/Tabs/Swap/components/yourReceiving.js b/common/containers/Tabs/Swap/components/yourReceiving.js index 584f0f9c..cde16274 100644 --- a/common/containers/Tabs/Swap/components/yourReceiving.js +++ b/common/containers/Tabs/Swap/components/yourReceiving.js @@ -1,18 +1,39 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { DONATION_ADDRESSES_MAP } from 'config/data'; +import Validator from 'libs/validator'; export default class YourReceiving extends Component { constructor(props) { super(props); + this.validator = new Validator(); + this.state = { + validAddress: false + }; } static propTypes = { - destinationKind: PropTypes.string.isRequired + destinationKind: PropTypes.string.isRequired, + receivingAddressSwap: PropTypes.func.isRequired, + receivingAddress: PropTypes.string + }; + + onChangeReceivingAddress = event => { + const value = event.target.value; + this.props.receivingAddressSwap(value); + let validAddress; + // TODO - find better pattern here once currencies move beyond BTC, ETH, REP + if (this.props.destinationKind === 'BTC') { + validAddress = this.validator.isValidBTCAddress(value); + } else { + validAddress = this.validator.isValidETHAddress(value); + } + this.setState({ validAddress }); }; render() { - const { destinationKind } = this.props; + const { destinationKind, receivingAddress } = this.props; + const { validAddress } = this.state; return (
@@ -23,16 +44,20 @@ export default class YourReceiving extends Component { ({destinationKind})
- +
diff --git a/common/containers/Tabs/Swap/index.js b/common/containers/Tabs/Swap/index.js index a97cfef0..9e875332 100644 --- a/common/containers/Tabs/Swap/index.js +++ b/common/containers/Tabs/Swap/index.js @@ -25,12 +25,14 @@ class Swap extends Component { destinationKind: PropTypes.string, destinationKindOptions: PropTypes.array, originKindOptions: PropTypes.array, + receivingAddress: PropTypes.string, originKindSwap: PropTypes.func, destinationKindSwap: PropTypes.func, originAmountSwap: PropTypes.func, destinationAmountSwap: PropTypes.func, updateBityRatesSwap: PropTypes.func, - partOneCompleteSwap: PropTypes.func + partOneCompleteSwap: PropTypes.func, + receivingAddressSwap: PropTypes.func }; componentDidMount() { @@ -62,7 +64,9 @@ class Swap extends Component { originAmountSwap, destinationAmountSwap, partOneComplete, - partOneCompleteSwap + partOneCompleteSwap, + receivingAddressSwap, + receivingAddress } = this.props; let wantToSwapMyProps = { @@ -87,6 +91,12 @@ class Swap extends Component { destinationKind }; + let yourReceivingProps = { + destinationKind, + receivingAddressSwap, + receivingAddress + }; + return (
@@ -99,7 +109,7 @@ class Swap extends Component { {partOneComplete &&
- +
}
@@ -110,6 +120,7 @@ class Swap extends Component { function mapStateToProps(state) { return { + receivingAddress: state.swap.receivingAddress, partOneComplete: state.swap.partOneComplete, originAmount: state.swap.originAmount, destinationAmount: state.swap.destinationAmount,