* @throws {TypeError} Will throw an error if the RPC responds with an HTTP error.
*/
export function fetchBytecode(address: string, rpc: string): Promise<string | undefined>;
+/**
+ * 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.
+ * @returns {'solidity'|'vyper'|'unknown'} The smart contract language used to generate this smart contract.
+ */
+export function detectLanguage(bytecode: string): 'solidity' | 'vyper' | 'unknown';
/**
* Parse arbitrary EVM bytecode to extract the bytecode metadata from it.
* @param {string} bytecode Raw EVM bytecode in hex (including `0x` prefix).
}
logOutput(result, '✅ Successfully fetched runtime bytecode.', bytecode);
+ // Validate smart contract language used
+ const lang = detectLanguage(bytecode);
+ if (lang !== 'solidity') {
+ logOutput(result, '❌ Unsupported smart contract language.', `Language detected: ${lang}`);
+ return;
+ }
+
// Parse runtime bytecode
const cbor = extractCBOR(bytecode);
if (cbor.includes(NaN)) {
.then(obj => obj.result);
}
+/**
+ * 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.
+ * @returns {'solidity'|'vyper'|'unknown'} The smart contract language used to generate this smart contract.
+ */
+function detectLanguage(bytecode) {
+ const prefix = bytecode.replace('0x', '').substring(0, 10);
+
+ // https://github.com/banteg/erigon-kv/blob/5584ada83c75b244e611641d100ccc647a7f6791/examples/compilers.py#L17
+ if (prefix === '6060604052' || prefix === '6080604052') {
+ return 'solidity';
+ }
+ else if (prefix === '6004361015' || prefix === '341561000a') {
+ return 'vyper';
+ }
+ else {
+ return 'unknown';
+ }
+}
+
/**
* Parse arbitrary EVM bytecode to extract the bytecode metadata from it.
* @param {string} bytecode Raw EVM bytecode in hex (including `0x` prefix).
return response;
}
-export { fetchBytecode, extractCBOR, decodeCBOR, calculateCID, fetchCID };
+export { fetchBytecode, detectLanguage, extractCBOR, decodeCBOR, calculateCID, fetchCID };