]> git.ardis.lu - solc-metadata/commitdiff
Add check for smart contract language
authorArdis Lu <ardisglu@gmail.com>
Wed, 3 Jan 2024 04:49:22 +0000 (20:49 -0800)
committerArdis Lu <ardisglu@gmail.com>
Wed, 3 Jan 2024 04:49:22 +0000 (20:49 -0800)
index.d.ts
index.html
index.js

index 6a8482558ad35d9c2736f370aed07134d7feb816..8d1257c0941d4bba925db6d7597657a6323f0a0e 100644 (file)
@@ -6,6 +6,13 @@
  * @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).
index e9c56d9e48871342a921f31615e61dc537d65904..bcbec42c59d8fa4cb04cc4c078ff71a3b902a4da 100644 (file)
       }
       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)) {
index d8e1fa2556dc36fd98ca0084281f9a1773efca28..03ecd5bd49ba8104ef4d9df8aff806e07e6abe37 100644 (file)
--- a/index.js
+++ b/index.js
@@ -24,6 +24,27 @@ async function fetchBytecode(address, rpc) {
     .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).
@@ -211,4 +232,4 @@ function fetchCID(cid, ipfs) {
   return response;
 }
 
-export { fetchBytecode, extractCBOR, decodeCBOR, calculateCID, fetchCID };
+export { fetchBytecode, detectLanguage, extractCBOR, decodeCBOR, calculateCID, fetchCID };
This page took 0.045706 seconds and 4 git commands to generate.