Skip to content

Commit

Permalink
Merge pull request #1107 from poanetwork/issue#1042
Browse files Browse the repository at this point in the history
(Fix) check address of Proxy smart-contract for validity
  • Loading branch information
vbaranov committed Aug 27, 2018
2 parents ecf5a70 + 6490964 commit b9abc09
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 25 deletions.
12 changes: 7 additions & 5 deletions src/components/contribute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import {
isCrowdSaleFull,
getUserBalanceByStore,
getUserBalance,
getCurrentTierInfoCustom
getCurrentTierInfoCustom,
getExecID,
getAddr
} from '../crowdsale/utils'
import {
countDecimalPlaces,
getExecID,
getAddr,
getNetworkID,
toast,
toBigNumber,
Expand Down Expand Up @@ -120,10 +120,12 @@ export class Contribute extends React.Component {
const { gasPriceStore, generalStore, crowdsaleStore, contractStore } = this.props
const crowdsaleExecID = getExecID()
contractStore.setContractProperty('crowdsale', 'execID', crowdsaleExecID)
const crowdsaleAddr = getAddr()

try {
await this.validateEnvironment(crowdsaleExecID, crowdsaleAddr)
await getCrowdsaleAssets(generalStore.networkID)
const crowdsaleAddr = await getAddr()

await this.validateEnvironment(crowdsaleExecID, crowdsaleAddr)

let strategy
if (crowdsaleExecID) {
Expand Down
20 changes: 11 additions & 9 deletions src/components/crowdsale/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ import {
getCrowdsaleStrategy,
getCrowdsaleStrategyByName
} from '../../utils/blockchainHelpers'
import { getContractStoreProperty, getCrowdsaleData, getTokenData, initializeAccumulativeData } from './utils'
import { getExecID, getAddr, getNetworkID, toBigNumber, isExecIDValid, isAddressValid } from '../../utils/utils'
import {
getExecID,
getAddr,
getContractStoreProperty,
getCrowdsaleData,
getTokenData,
initializeAccumulativeData
} from './utils'
import { getNetworkID, toBigNumber, isExecIDValid } from '../../utils/utils'
import { getCrowdsaleAssets } from '../../stores/utils'
import { StepNavigation } from '../Common/StepNavigation'
import { NAVIGATION_STEPS } from '../../utils/constants'
import { Loader } from '../Common/Loader'
import { CrowdsaleConfig } from '../Common/config'
import { inject, observer } from 'mobx-react'
import { invalidCrowdsaleExecIDAlert, invalidNetworkIDAlert, invalidCrowdsaleProxyAlert } from '../../utils/alerts'
import { invalidCrowdsaleExecIDAlert, invalidNetworkIDAlert } from '../../utils/alerts'
import logdown from 'logdown'

const logger = logdown('TW:crowdsale')
Expand Down Expand Up @@ -71,18 +78,13 @@ export class Crowdsale extends React.Component {

try {
await getCrowdsaleAssets(generalStore.networkID)
const crowdsaleAddr = getAddr()
const crowdsaleAddr = await getAddr()

if (!isExecIDValid(contractStore.crowdsale.execID) && contractStore.crowdsale.execID) {
invalidCrowdsaleExecIDAlert()
return Promise.reject('invalid exec-id')
}

if (!isAddressValid(crowdsaleAddr) && !contractStore.crowdsale.execID) {
invalidCrowdsaleProxyAlert()
return Promise.reject('invalid proxy addr')
}

let strategy
if (contractStore.crowdsale.execID) {
strategy = await getCrowdsaleStrategy(contractStore.crowdsale.execID)
Expand Down
65 changes: 62 additions & 3 deletions src/components/crowdsale/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { noContractAlert } from '../../utils/alerts'
import { attachToSpecificCrowdsaleContract } from '../../utils/blockchainHelpers'
import { invalidCrowdsaleProxyAlert, noContractAlert } from '../../utils/alerts'
import {
attachToSpecificCrowdsaleContract,
attachToSpecificCrowdsaleContractByAddr,
getCrowdsaleStrategyByName
} from '../../utils/blockchainHelpers'
import { contractStore, crowdsalePageStore, tokenStore, web3Store, crowdsaleStore } from '../../stores'
import { toJS } from 'mobx'
import { removeTrailingNUL, toBigNumber } from '../../utils/utils'
import { _getAddr, _getExecID, removeTrailingNUL, toBigNumber } from '../../utils/utils'
import { BigNumber } from 'bignumber.js'
import logdown from 'logdown'
import { CrowdsaleConfig } from '../Common/config'

const logger = logdown('TW:crowdsale:utils')

Expand Down Expand Up @@ -812,3 +817,57 @@ export const getInitializeDataFromContractStore = async () => {
isMintedCappedCrowdsale: crowdsaleStore.isMintedCappedCrowdsale
}
}

export const getExecID = () => {
return CrowdsaleConfig.crowdsaleContractURL || _getExecID()
}

export const getAddr = async () => {
try {
let address

// First: add the contract config if exist
if (CrowdsaleConfig && CrowdsaleConfig.crowdsaleContractURL) {
address = CrowdsaleConfig.crowdsaleContractURL
} else {
address = _getAddr()
}

// Second: get the addr and validate if is a valid address
if (!address) {
return sendAddressError(`There is no addr`)
}

// Exist a race condition between clear the storage an initialize data from contract, so ...
const proxyContract = await attachToSpecificCrowdsaleContractByAddr(address, contractStore.MintedCappedProxy.abi)
const appName = await proxyContract.methods.app_name().call()
const strategy = await getCrowdsaleStrategyByName(appName)
crowdsaleStore.setProperty('strategy', strategy)
contractStore.setContractProperty(crowdsaleStore.proxyName, 'addr', address)

// Third: validate if the app exec id exist
const { methods } = await getInitializeDataFromContractStore()
const { app_exec_id, getAdmin } = methods
const appExecIdVar = await app_exec_id().call()

if (!appExecIdVar) {
return sendAddressError(`There is no app exec id`)
}

// Four: validate that exec id that exec-id of this Proxy smart-contract exists in Auth-os registry
let ownerAccount = await getAdmin().call()
if (!ownerAccount) {
return sendAddressError(`There is no owner account `)
}

return address
} catch (e) {
sendAddressError(e)
}
}

export const sendAddressError = message => {
logger.log('Error getting address', message)
invalidCrowdsaleProxyAlert()
return Promise.reject(message)
}
8 changes: 0 additions & 8 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@ export const _getExecID = () => {
return isExecIDValid(execID) ? execID : null
}

export const getExecID = () => {
return CrowdsaleConfig.crowdsaleContractURL || _getExecID()
}

export const _getAddr = () => {
const addr = getQueryVariableAddr()
logger.log('getAddr:', addr)
return isAddressValid(addr) ? addr : null
}

export const getAddr = () => {
return CrowdsaleConfig.crowdsaleContractURL || _getAddr()
}

export const getQueryVariableAddr = () => {
return getQueryVariable('addr')
}
Expand Down

0 comments on commit b9abc09

Please sign in to comment.