Skip to content

Commit

Permalink
add input validation for eth_getBalance (#3)
Browse files Browse the repository at this point in the history
passes smoke
  • Loading branch information
tonititi authored Apr 24, 2024
1 parent 7b2011c commit 230afb2
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ export type DetailedTxStatus = {
from: string
injected: boolean
accepted:
| TxStatusCode.BAD_TX
| TxStatusCode.SUCCESS
| TxStatusCode.BUSY
| TxStatusCode.OTHER_FAILURE
| boolean
| TxStatusCode.BAD_TX
| TxStatusCode.SUCCESS
| TxStatusCode.BUSY
| TxStatusCode.OTHER_FAILURE
| boolean
reason: string
timestamp: string
nodeUrl?: string
Expand Down Expand Up @@ -172,16 +172,16 @@ type Tx = readableTransaction & {

type TxParam =
| {
readableReceipt: Tx
txHash?: string
transactionType?: string | number
}
readableReceipt: Tx
txHash?: string
transactionType?: string | number
}
| {
wrappedEVMAccount: {
readableReceipt: Tx
txHash: string
}
wrappedEVMAccount: {
readableReceipt: Tx
txHash: string
}
}

function extractTransactionObject(
bigTransaction: TxParam,
Expand Down Expand Up @@ -705,7 +705,22 @@ function trimInjectRejection(message: string): string {
return 'ECONNREFUSED'
} else return message
}

async function validateBlockNumberInput(blockNumberInput: string) {
const { blockNumber } = await getCurrentBlockInfo()
if (blockNumberInput === 'latest') {
return blockNumber
}
if (blockNumberInput === 'earliest') {
return '0x0'
}
if (!isHex(blockNumberInput)) {
return undefined
}
if (parseInt(blockNumberInput, 16) > parseInt(blockNumber, 16)) {
return blockNumber
}
return blockNumberInput
}
export const methods = {
web3_clientVersion: async function (args: RequestParamsLike, callback: JSONRPCCallbackTypePlain) {
const api_name = 'web3_clientVersion'
Expand Down Expand Up @@ -970,7 +985,8 @@ export const methods = {
countFailedResponse(api_name, 'Invalid address')
return
}

// validate input blockNumber that support text such 'latest', 'earliest' ...
blockNumber = await validateBlockNumberInput(blockNumber)
let balance
try {
balance = await serviceValidator.getBalance(address, blockNumber)
Expand Down Expand Up @@ -2446,7 +2462,7 @@ export const methods = {
}
const explorerUrl = config.explorerUrl
res = await axios.get(`${explorerUrl}/api/transaction?txHash=${txHash}`)
/* prettier-ignore */ if (verbose) console.log('url', `${explorerUrl}/api/transaction?txHash=${txHash}`,'res', JSON.stringify(res.data))
/* prettier-ignore */ if (verbose) console.log('url', `${explorerUrl}/api/transaction?txHash=${txHash}`, 'res', JSON.stringify(res.data))

result = res.data.transactions ? res.data.transactions[0] : null
}
Expand Down

0 comments on commit 230afb2

Please sign in to comment.