From: Ardis Lu Date: Sat, 7 Oct 2023 03:57:35 +0000 (-0700) Subject: Support Vyper in extractCBOR for future use X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=2b3a3f1a596b0cbfdf223fe08ca38b3804667ab3;p=solc-metadata Support Vyper in extractCBOR for future use --- diff --git a/index.js b/index.js index 0f26a7b..865677b 100644 --- a/index.js +++ b/index.js @@ -17,12 +17,13 @@ async function fetchBytecode(address, rpc) { .then(obj => obj.result); } -function extractCBOR(bytecode) { +function extractCBOR(bytecode, lang = 'solidity') { // 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); // Derive the CBOR by counting backwards from the end (minus the last two bytes) - const cbor = bytecode.substring(bytecode.length - 4 - cborLen * 2, bytecode.length - 4); + // IMPORTANT: Vyper INCLUDES the last two bytes in the byte length, unlike Solidity! + const cbor = bytecode.substring(bytecode.length - (lang === 'solidity' ? 4 : 0) - cborLen * 2, bytecode.length - 4); // Convert to a byte array for easier use in subsequent processing const byteArray = cbor.match(/.{2}/g).map(v => parseInt(v, 16));