From 344ad5cea980c1830e98335789e0929e44d6f875 Mon Sep 17 00:00:00 2001 From: Juan Cortes Ross Date: Tue, 5 Feb 2019 12:16:00 +0100 Subject: [PATCH] Refactor types to recieve a more generic Account instead --- src/bridge/EthereumJSBridge.js | 8 +++---- src/bridge/LibcoreBridge.js | 8 +++---- src/bridge/RippleJSBridge.js | 22 ++++++++----------- src/bridge/makeMockBridge.js | 2 +- src/bridge/types.js | 4 ++-- .../modals/Send/fields/RecipientField.js | 12 +++------- .../modals/Send/steps/01-step-amount.js | 2 +- 7 files changed, 24 insertions(+), 34 deletions(-) diff --git a/src/bridge/EthereumJSBridge.js b/src/bridge/EthereumJSBridge.js index 145d6524..f7948a46 100644 --- a/src/bridge/EthereumJSBridge.js +++ b/src/bridge/EthereumJSBridge.js @@ -129,7 +129,7 @@ function isRecipientValid(currency, recipient) { } // Returns a warning if we detect a non-eip address -function getRecipientWarning(currency, recipient) { +function getRecipientWarning(account, recipient) { if (!recipient.match(/^0x[0-9a-fA-F]{40}$/)) return null const slice = recipient.substr(2) const isFullUpper = slice === slice.toUpperCase() @@ -420,9 +420,9 @@ const EthereumBridge: WalletBridge = { pullMoreOperations: () => Promise.resolve(a => a), // NOT IMPLEMENTED - isRecipientValid: (currency, recipient) => Promise.resolve(isRecipientValid(currency, recipient)), - getRecipientWarning: (currency, recipient) => - Promise.resolve(getRecipientWarning(currency, recipient)), + isRecipientValid: (account, recipient) => Promise.resolve(isRecipientValid(account, recipient)), + getRecipientWarning: (account, recipient) => + Promise.resolve(getRecipientWarning(account, recipient)), createTransaction: () => ({ amount: BigNumber(0), diff --git a/src/bridge/LibcoreBridge.js b/src/bridge/LibcoreBridge.js index e9d2ce8b..e6fb6d40 100644 --- a/src/bridge/LibcoreBridge.js +++ b/src/bridge/LibcoreBridge.js @@ -60,15 +60,15 @@ const EditAdvancedOptions = ({ onChange, value }: EditProps) => ( const recipientValidLRU = LRU({ max: 100 }) -const isRecipientValid = (currency, recipient) => { - const key = `${currency.id}_${recipient}` +const isRecipientValid = (account, recipient) => { + const key = `${account.currency.id}_${recipient}` let promise = recipientValidLRU.get(key) if (promise) return promise if (!recipient) return Promise.resolve(false) promise = libcoreValidAddress .send({ address: recipient, - currencyId: currency.id, + currencyId: account.currency.id, }) .toPromise() recipientValidLRU.set(key, promise) @@ -83,7 +83,7 @@ const getFeesKey = (a, t) => }` const getFees = async (a, transaction) => { - const isValid = await isRecipientValid(a.currency, transaction.recipient) + const isValid = await isRecipientValid(a, transaction.recipient) if (!isValid) return null const key = getFeesKey(a, transaction) let promise = feesLRU.get(key) diff --git a/src/bridge/RippleJSBridge.js b/src/bridge/RippleJSBridge.js index 0ec547cb..a6d16ef6 100644 --- a/src/bridge/RippleJSBridge.js +++ b/src/bridge/RippleJSBridge.js @@ -137,21 +137,18 @@ async function signAndBroadcast({ a, t, deviceId, isCancelled, onSigned, onOpera } } -function isRecipientValid(recipient, source) { - if (source === recipient) { - return false - } - +function isRecipientValid(account, recipient) { try { bs58check.decode(recipient) - return true + + return !(account && account.freshAddress === recipient) } catch (e) { return false } } -function getRecipientWarning(recipient, source) { - if (source === recipient) { +function getRecipientWarning(account, recipient) { + if (account.freshAddress === recipient) { return new InvalidAddressBecauseDestinationIsAlsoSource() } return null @@ -282,7 +279,7 @@ const getServerInfo = (map => endpointConfig => { })({}) const recipientIsNew = async (endpointConfig, recipient) => { - if (!isRecipientValid(recipient)) return false + if (!isRecipientValid(null, recipient)) return false const api = apiForEndpointConfig(RippleAPI, endpointConfig) try { await api.connect() @@ -517,10 +514,9 @@ const RippleJSBridge: WalletBridge = { pullMoreOperations: () => Promise.resolve(a => a), // FIXME not implemented - isRecipientValid: (currency, recipient, source) => - Promise.resolve(isRecipientValid(recipient, source)), - getRecipientWarning: (currency, recipient, source) => - Promise.resolve(getRecipientWarning(recipient, source)), + isRecipientValid: (account, recipient) => Promise.resolve(isRecipientValid(account, recipient)), + getRecipientWarning: (account, recipient) => + Promise.resolve(getRecipientWarning(account, recipient)), createTransaction: () => ({ amount: BigNumber(0), diff --git a/src/bridge/makeMockBridge.js b/src/bridge/makeMockBridge.js index 93080000..169bbd4d 100644 --- a/src/bridge/makeMockBridge.js +++ b/src/bridge/makeMockBridge.js @@ -128,7 +128,7 @@ function makeMockBridge(opts?: Opts): WalletBridge<*> { } }, - isRecipientValid: (currency, recipient) => Promise.resolve(recipient.length > 0), + isRecipientValid: (account, recipient) => Promise.resolve(recipient.length > 0), getRecipientWarning: () => Promise.resolve(null), createTransaction: () => ({ diff --git a/src/bridge/types.js b/src/bridge/types.js index 5b7ddd00..64835064 100644 --- a/src/bridge/types.js +++ b/src/bridge/types.js @@ -52,8 +52,8 @@ export interface WalletBridge { // count is user's desired number of ops to pull (but implementation can decide to ignore it or not) pullMoreOperations(initialAccount: Account, count: number): Promise<(Account) => Account>; - isRecipientValid(currency: Currency, recipient: string, source?: string): Promise; - getRecipientWarning(currency: Currency, recipient: string, source?: string): Promise; + isRecipientValid(account: Account, recipient: string): Promise; + getRecipientWarning(account: Account, recipient: string): Promise; // Related to send funds: diff --git a/src/components/modals/Send/fields/RecipientField.js b/src/components/modals/Send/fields/RecipientField.js index 639d7fe6..558061fb 100644 --- a/src/components/modals/Send/fields/RecipientField.js +++ b/src/components/modals/Send/fields/RecipientField.js @@ -53,12 +53,8 @@ class RecipientField extends Component< const { account, bridge, transaction } = this.props const syncId = ++this.syncId const recipient = bridge.getTransactionRecipient(account, transaction) - const isValid = await bridge.isRecipientValid(account.currency, recipient, account.freshAddress) - const warning = await bridge.getRecipientWarning( - account.currency, - recipient, - account.freshAddress, - ) + const isValid = await bridge.isRecipientValid(account, recipient) + const warning = await bridge.getRecipientWarning(account, recipient) if (syncId !== this.syncId) return if (this.isUnmounted) return this.setState({ isValid, warning }) @@ -73,9 +69,7 @@ class RecipientField extends Component< if (amount) { t = bridge.editTransactionAmount(account, t, amount) } - const warning = fromQRCode - ? await bridge.getRecipientWarning(account.currency, recipient) - : null + const warning = fromQRCode ? await bridge.getRecipientWarning(account, recipient) : null if (this.isUnmounted) return false if (warning) { // clear the input if field has warning AND has a warning diff --git a/src/components/modals/Send/steps/01-step-amount.js b/src/components/modals/Send/steps/01-step-amount.js index dddbb3e3..fd810e9c 100644 --- a/src/components/modals/Send/steps/01-step-amount.js +++ b/src/components/modals/Send/steps/01-step-amount.js @@ -133,7 +133,7 @@ export class StepAmountFooter extends PureComponent< const totalSpent = await bridge.getTotalSpent(account, transaction) if (syncId !== this.syncId) return const isRecipientValid = await bridge.isRecipientValid( - account.currency, + account, bridge.getTransactionRecipient(account, transaction), ) if (syncId !== this.syncId) return