]> git.ardis.lu - solc-metadata/commitdiff
Fetch source code in addition to JSON metadata
authorArdis Lu <ardisglu@gmail.com>
Tue, 31 Oct 2023 05:59:45 +0000 (22:59 -0700)
committerArdis Lu <ardisglu@gmail.com>
Tue, 31 Oct 2023 05:59:45 +0000 (22:59 -0700)
index.html
index.js

index e0fead77803711a729280dcd58d4b65bda5023b8..86310ba4520f17d909b4f8074bbbe88293f1747e 100644 (file)
       const metadata = decodeCBOR(cbor);
       logOutput(result, '✅ Successfully decoded CBOR metadata.', JSON.stringify(metadata, null, 2));
 
+      // Fetch JSON metadata
+      let json;
       if (metadata.bzzr0 !== undefined) {
         const hex = [...metadata.bzzr0].map(b => b.toString(16).padStart(2, '0')).join('');
         logOutput(result, '❌ Unable to fetch JSON metadata because the smart contract uses a Swarm hash.', `bzzr://${hex}`);
         const { cidV0, cidV1 } = calculateCID(metadata);
         logOutput(result, '✅ Successfully calculated IPFS CID.', `IPFS CIDv0: ${cidV0}\nIPFS CIDv1: ${cidV1}`);
         try {
-          const json = await fetchJSONMetadata(cidV1, ipfs);
+          json = await fetchCID(cidV1, ipfs).then(r => r.json());
           logOutput(result, '✅ Successfully fetched JSON metadata.', JSON.stringify(json, null, 2));
         }
         catch {
           logOutput(result, '❌ Failed to fetch JSON metadata from IPFS.', `IPFS node: ${ipfs}`);
         }
       }
+
+      // Fetch source code
+      if (json?.sources !== undefined) {
+        for (const [file, source] of Object.entries(json.sources)) {
+          try {
+            const cidV0 = source.urls[1].substring(11);
+            const code = await fetchCID(cidV0, ipfs).then(r => r.text());
+            logOutput(result, `✅ Successfully fetched source code for "${file}" from IPFS.`, code);
+          }
+          catch {
+            logOutput(result, `❌ Failed to fetch source code for "${file}" from IPFS.`, `IPFS node: ${ipfs}`);
+          }
+        }
+      }
     }
 
     document.querySelector('form').addEventListener('submit', async e => {
index 865677b405dcb1b2e3377666d83ad29ffe995a1e..118ffa8581802e3129882efc41d3acc2941a8016 100644 (file)
--- a/index.js
+++ b/index.js
@@ -155,7 +155,7 @@ function calculateCID(metadata) {
   return { solcVersion, cidV0, cidV1 };
 }
 
-function fetchJSONMetadata(cid, ipfs) {
+function fetchCID(cid, ipfs) {
   let response;
   if (cid[0] === 'Q') { // v0
     response = fetch(`${ipfs.origin}/ipfs/${cid}${ipfs.search}`);
@@ -168,7 +168,7 @@ function fetchJSONMetadata(cid, ipfs) {
       response = fetch(`${ipfs.protocol}//${cid}.ipfs.${ipfs.host}${ipfs.pathname}${ipfs.search}`);
     }
   }
-  return response.then(r => r.json());
+  return response;
 }
 
-export { fetchBytecode, extractCBOR, decodeCBOR, calculateCID, fetchJSONMetadata };
+export { fetchBytecode, extractCBOR, decodeCBOR, calculateCID, fetchCID };
This page took 0.033978 seconds and 4 git commands to generate.