Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GREEN-33 - add input validation for eth_getBalance #3

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -702,7 +702,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 @@ -967,7 +982,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 @@ -2443,7 +2459,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
Loading