From 4bc25cb23d555e5a63e9c4f0118eec4ddf23f004 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Wed, 2 Oct 2024 15:14:18 +0530 Subject: [PATCH 1/3] Sumcheck: contractHandler.js --- packages/actions/src/Contract/contractHandler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/actions/src/Contract/contractHandler.js b/packages/actions/src/Contract/contractHandler.js index 6278997b35..f13e804d2e 100644 --- a/packages/actions/src/Contract/contractHandler.js +++ b/packages/actions/src/Contract/contractHandler.js @@ -52,7 +52,7 @@ export const contractHandler = const precompile = params.to && vm.evm.getPrecompile(createAddress(params.to)) if (!params.deployedBytecode && !params.code && contract && contract?.length === 0 && !precompile) { client.logger.debug( - { contract, precompile, to: params.to }, + { contract, precompile, to: params.to.toString() }, 'contractHandler: No contract bytecode nor precompile was found at specified `to` address. Unable to execute contract call.', ) return maybeThrowOnFail(params.throwOnFail ?? throwOnFailDefault, { @@ -62,7 +62,7 @@ export const contractHandler = { _tag: 'InvalidRequestError', name: 'InvalidRequestError', - message: `Contract at address ${params.to} does not exist`, + message: `Contract at address ${params.to.toString()} does not exist`, }, ], }) From c8409f85607d48aca9067eaa425a59ba54dd500b Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Thu, 10 Oct 2024 11:26:59 +0530 Subject: [PATCH 2/3] update: checksum --- .../actions/src/Contract/contractHandler.js | 5 ++- .../actions/src/utils/toChecksumAddress.js | 41 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 packages/actions/src/utils/toChecksumAddress.js diff --git a/packages/actions/src/Contract/contractHandler.js b/packages/actions/src/Contract/contractHandler.js index f13e804d2e..59956fbe17 100644 --- a/packages/actions/src/Contract/contractHandler.js +++ b/packages/actions/src/Contract/contractHandler.js @@ -1,6 +1,7 @@ import { createAddress } from '@tevm/address' import { DecodeFunctionDataError, InvalidRequestError, RevertError } from '@tevm/errors' import { decodeErrorResult, decodeFunctionResult, encodeFunctionData, isHex } from '@tevm/utils' +import { toChecksumAddress } from '../utils/toChecksumAddress.js' import { callHandler } from '../Call/callHandler.js' import { maybeThrowOnFail } from '../internal/maybeThrowOnFail.js' import { validateContractParams } from './validateContractParams.js' @@ -52,7 +53,7 @@ export const contractHandler = const precompile = params.to && vm.evm.getPrecompile(createAddress(params.to)) if (!params.deployedBytecode && !params.code && contract && contract?.length === 0 && !precompile) { client.logger.debug( - { contract, precompile, to: params.to.toString() }, + { contract, precompile, to: toChecksumAddress(params.to) }, 'contractHandler: No contract bytecode nor precompile was found at specified `to` address. Unable to execute contract call.', ) return maybeThrowOnFail(params.throwOnFail ?? throwOnFailDefault, { @@ -62,7 +63,7 @@ export const contractHandler = { _tag: 'InvalidRequestError', name: 'InvalidRequestError', - message: `Contract at address ${params.to.toString()} does not exist`, + message: `Contract at address ${toChecksumAddress(params.to)} does not exist`, }, ], }) diff --git a/packages/actions/src/utils/toChecksumAddress.js b/packages/actions/src/utils/toChecksumAddress.js new file mode 100644 index 0000000000..ec27bb79aa --- /dev/null +++ b/packages/actions/src/utils/toChecksumAddress.js @@ -0,0 +1,41 @@ +import { keccak256,stringToHex} from '@tevm/utils' + +/** + * @param {string} address + * @returns String + */ +export const toChecksumAddress = (/** @type {string} */ address) => { + + if (!address) { + throw new Error('Invalid address provided'); + } + + + let addressLower=address.toLowerCase().replace(/^0x/, ''); + if(!addressLower){ + addressLower='0x'; + } + + // Hash the lowercase address using Keccak-256 + let hash = keccak256(stringToHex(addressLower)); + + +// Iterate over each character of the address and the corresponding hash +let checksumAddress = '0x'; +for (let i = 0; i < addressLower.length; i++) { + if(hash[i]){ + + // @ts-ignore + if (parseInt(hash[i], 16) >= 8) { + // @ts-ignore + checksumAddress += addressLower[i].toUpperCase(); + } else { + checksumAddress += addressLower[i]; + } + } +} + +return checksumAddress; + + +} From df6a9f6883f1401d0c629ff7e2537b437259a2e9 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Fri, 11 Oct 2024 01:35:49 +0530 Subject: [PATCH 3/3] resolved commits --- .../actions/src/utils/toChecksumAddress.js | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/actions/src/utils/toChecksumAddress.js b/packages/actions/src/utils/toChecksumAddress.js index ec27bb79aa..a8aadb7dd3 100644 --- a/packages/actions/src/utils/toChecksumAddress.js +++ b/packages/actions/src/utils/toChecksumAddress.js @@ -6,8 +6,8 @@ import { keccak256,stringToHex} from '@tevm/utils' */ export const toChecksumAddress = (/** @type {string} */ address) => { - if (!address) { - throw new Error('Invalid address provided'); + if (typeof address !== 'string' || !/^(0x)?[0-9a-fA-F]{40}$/.test(address)) { + throw new Error('Invalid Ethereum address format'); } @@ -20,20 +20,21 @@ export const toChecksumAddress = (/** @type {string} */ address) => { let hash = keccak256(stringToHex(addressLower)); -// Iterate over each character of the address and the corresponding hash -let checksumAddress = '0x'; -for (let i = 0; i < addressLower.length; i++) { - if(hash[i]){ +const checksumAddress = '0x' + Array.from(addressLower) + .map((char, i) => { + const charCode = char.charCodeAt(0); + // @ts-ignore + const shouldUpperCase = Number.parseInt(hash[i], 16) > 7; - // @ts-ignore - if (parseInt(hash[i], 16) >= 8) { - // @ts-ignore - checksumAddress += addressLower[i].toUpperCase(); + if (charCode >= 48 && charCode <= 57) { // '0'-'9' + return char; // Numbers remain unchanged + } else if (charCode >= 97 && charCode <= 102) { // 'a'-'f' + return shouldUpperCase ? char.toUpperCase() : char; } else { - checksumAddress += addressLower[i]; + throw new Error(`Invalid character ${char} in address.`); } - } -} + }) + .join(''); return checksumAddress;