Browse Source

Refactor types to recieve a more generic Account instead

gre-patch-1
Juan Cortes Ross 6 years ago
parent
commit
344ad5cea9
No known key found for this signature in database GPG Key ID: 34A99C03E9455EB8
  1. 8
      src/bridge/EthereumJSBridge.js
  2. 8
      src/bridge/LibcoreBridge.js
  3. 22
      src/bridge/RippleJSBridge.js
  4. 2
      src/bridge/makeMockBridge.js
  5. 4
      src/bridge/types.js
  6. 12
      src/components/modals/Send/fields/RecipientField.js
  7. 2
      src/components/modals/Send/steps/01-step-amount.js

8
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<Transaction> = {
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),

8
src/bridge/LibcoreBridge.js

@ -60,15 +60,15 @@ const EditAdvancedOptions = ({ onChange, value }: EditProps<Transaction>) => (
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)

22
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<Transaction> = {
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),

2
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: () => ({

4
src/bridge/types.js

@ -52,8 +52,8 @@ export interface WalletBridge<Transaction> {
// 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<boolean>;
getRecipientWarning(currency: Currency, recipient: string, source?: string): Promise<?Error>;
isRecipientValid(account: Account, recipient: string): Promise<boolean>;
getRecipientWarning(account: Account, recipient: string): Promise<?Error>;
// Related to send funds:

12
src/components/modals/Send/fields/RecipientField.js

@ -53,12 +53,8 @@ class RecipientField<Transaction> 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<Transaction> 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

2
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

Loading…
Cancel
Save