From 20630aa7353ea48cf0fa5d7e53b9c1cb43ab69b7 Mon Sep 17 00:00:00 2001 From: Ana G <40721795+Bitcoinera@users.noreply.github.com> Date: Tue, 14 Jan 2020 02:34:09 +0100 Subject: [PATCH] Ana/handle "address not from this network" error (#263) * add check address function for all queries * apply suggestions --- lib/source/cosmosV0-source.js | 17 +++++++++++++++++ lib/source/cosmosV2-source.js | 2 ++ lib/source/regenV0-source.js | 1 + 3 files changed, 20 insertions(+) diff --git a/lib/source/cosmosV0-source.js b/lib/source/cosmosV0-source.js index 6016cd3090..aa15faf81a 100644 --- a/lib/source/cosmosV0-source.js +++ b/lib/source/cosmosV0-source.js @@ -11,6 +11,7 @@ class CosmosV0API extends RESTDataSource { this.baseURL = network.api_url this.initialize({}) this.networkId = network.id + this.networkTitle = network.title this.delegatorBech32Prefix = network.bech32_prefix this.validatorConsensusBech32Prefix = `${network.bech32_prefix}valcons` @@ -55,6 +56,14 @@ class CosmosV0API extends RESTDataSource { return involvedAddresses } + checkAddress(address) { + if (!address.startsWith(this.delegatorBech32Prefix)) { + throw new UserInputError( + `The address you entered doesn't belong to the ${this.networkTitle} network` + ) + } + } + async getTransactionsByHeight(height) { const txs = await this.get(`txs?tx.height=${height}`) return Array.isArray(txs) @@ -248,6 +257,7 @@ class CosmosV0API extends RESTDataSource { } async getDelegatorVote({ proposalId, address }) { + this.checkAddress(address) const response = await this.query(`gov/proposals/${proposalId}/votes`) const votes = response || [] const vote = votes.find(({ voter }) => voter === address) || {} @@ -275,6 +285,7 @@ class CosmosV0API extends RESTDataSource { } async getBalancesFromAddress(address) { + this.checkAddress(address) const response = await this.query(`bank/balances/${address}`) const balances = response || [] return balances.map(this.reducers.coinReducer) @@ -288,6 +299,7 @@ class CosmosV0API extends RESTDataSource { } async getDelegationsForDelegatorAddress(address, validatorsDictionary) { + this.checkAddress(address) let delegations = (await this.query(`staking/delegators/${address}/delegations`)) || [] @@ -309,6 +321,7 @@ class CosmosV0API extends RESTDataSource { } async getUndelegationsForDelegatorAddress(address, validatorsDictionary) { + this.checkAddress(address) let undelegations = (await this.query( `staking/delegators/${address}/unbonding_delegations` @@ -339,6 +352,7 @@ class CosmosV0API extends RESTDataSource { } async getDelegationForValidator(delegatorAddress, validator) { + this.checkAddress(delegatorAddress) const operatorAddress = validator.operatorAddress const delegation = await this.query( `staking/delegators/${delegatorAddress}/delegations/${operatorAddress}` @@ -366,6 +380,7 @@ class CosmosV0API extends RESTDataSource { } async getRewards(delegatorAddress, validatorsDictionary, delegations = null) { + this.checkAddress(delegatorAddress) if (!delegations) { delegations = await this.getDelegationsForDelegatorAddress( delegatorAddress, @@ -389,6 +404,7 @@ class CosmosV0API extends RESTDataSource { } async getOverview(delegatorAddress, validatorsDictionary) { + this.checkAddress(delegatorAddress) const [ balances, delegations, @@ -421,6 +437,7 @@ class CosmosV0API extends RESTDataSource { } async getTransactions(address) { + this.checkAddress(address) const pagination = `&limit=${1000000000}` const txs = await Promise.all([ diff --git a/lib/source/cosmosV2-source.js b/lib/source/cosmosV2-source.js index f0c3e2a4c3..22f30c4618 100644 --- a/lib/source/cosmosV2-source.js +++ b/lib/source/cosmosV2-source.js @@ -17,6 +17,7 @@ class CosmosV2API extends CosmosV0API { } async getTransactions(address) { + this.checkAddress(address) const pagination = `&limit=${1000000000}` const txs = await Promise.all([ @@ -74,6 +75,7 @@ class CosmosV2API extends CosmosV0API { } async getRewards(delegatorAddress, validatorsDictionary) { + this.checkAddress(delegatorAddress) const result = await this.query( `distribution/delegators/${delegatorAddress}/rewards` ) diff --git a/lib/source/regenV0-source.js b/lib/source/regenV0-source.js index e2fd17f32b..9867758238 100644 --- a/lib/source/regenV0-source.js +++ b/lib/source/regenV0-source.js @@ -23,6 +23,7 @@ class RegenV0API extends CosmosV0API { } async getTransactions(address) { + this.checkAddress(address) const pagination = `&limit=${1000000000}` const txs = await Promise.all([