From: Ardis Lu Date: Wed, 25 Feb 2026 07:24:54 +0000 (-0800) Subject: Standardize making `0x` optional in hex string inputs X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=362fe74b4b3428ec37f1f0429ca2eaa4641c65ef;p=solc-metadata Standardize making `0x` optional in hex string inputs --- diff --git a/index.js b/index.js index 3da587d..4a382a6 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ /** * Wrapper around the `fetch` API to simplify making an `eth_getCode` JSON-RPC call. - * @param {string} address Hex address of the smart contract to retrieve the bytecode for (including `0x` prefix). + * @param {string} address Hex address of the smart contract to retrieve the bytecode for. * @param {string} rpc URL for a node that supports the standard Ethereum JSON-RPC method `eth_getCode`. * @returns {Promise} `Promise` that will resolve to the `string` bytecode for the address, or `undefined` if the address is malformed. * @throws {TypeError} Will throw an error if the RPC responds with an HTTP error. @@ -15,7 +15,7 @@ async function fetchBytecode(address, rpc) { id: '1', method: 'eth_getCode', params: [ - address, + `0x${address.replace('0x', '')}`, 'latest' ] }) @@ -27,7 +27,7 @@ async function fetchBytecode(address, rpc) { /** * Determine the smart contract language used to produce a smart contract by inspecting the initial bytes * of the runtime bytecode. - * @param {string} bytecode Runtime bytecode of a smart contract. + * @param {string} bytecode Runtime bytecode of a smart contract in hex. * @returns {'solidity'|'vyper'|'unknown'} The smart contract language used to generate this smart contract. */ function detectLanguage(bytecode) { @@ -47,12 +47,14 @@ function detectLanguage(bytecode) { /** * Parse arbitrary EVM bytecode to extract the bytecode metadata from it. - * @param {string} bytecode Raw EVM bytecode in hex (including `0x` prefix). + * @param {string} bytecode Raw EVM bytecode in hex. * @param {'solidity'|'vyper'} [lang=solidity] Smart contract language used to compile this bytecode. * @returns {Array} Byte array of the CBOR-encoded bytecode metadata. * @throws {TypeError} Will throw an error if the bytecode is malformed. */ function extractCBOR(bytecode, lang = 'solidity') { + bytecode = bytecode.replace('0x', ''); + // The length of the CBOR will always be stored as the last two bytes of the runtime bytecode const cborLen = parseInt(bytecode.slice(-4), 16);