<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Fabijan Corak on Medium]]></title>
        <description><![CDATA[Stories by Fabijan Corak on Medium]]></description>
        <link>https://medium.com/@fabijan.corak?source=rss-832aa4918397------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*dM6LxY9Bdz8lalfI.jpg</url>
            <title>Stories by Fabijan Corak on Medium</title>
            <link>https://medium.com/@fabijan.corak?source=rss-832aa4918397------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 07:45:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fabijan.corak/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Smart Contracts Can Be Automatically Verified]]></title>
            <link>https://news.shardlabs.io/how-smart-contracts-can-be-automatically-verified-28ee1c5cf941?source=rss-832aa4918397------2</link>
            <guid isPermaLink="false">https://medium.com/p/28ee1c5cf941</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[verification]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[blockchain-technology]]></category>
            <category><![CDATA[sourcify]]></category>
            <dc:creator><![CDATA[Fabijan Corak]]></dc:creator>
            <pubDate>Fri, 29 Jan 2021 11:49:47 GMT</pubDate>
            <atom:updated>2021-02-01T13:27:57.749Z</atom:updated>
            <content:encoded><![CDATA[<h3>TL;DR</h3><p>By publishing metadata and source files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*ja8N6PZtvI7Luwlo3T206Q.png" /><figcaption>Monitor tracks new contracts, obtains sources and sends them to the verifier for recompilation and publishing.</figcaption></figure><h3>Introduction</h3><p>Once developed and deployed, smart contracts exist on the blockchain in the form of <strong>non-human-readable</strong> bytecode. So if you want to sign transactions that use this bytecode, you have to blindly trust whoever created it. Or at least you had to before tools like <a href="https://github.com/ethereum/sourcify">Sourcify</a> came into play.</p><p>The idea is to maintain a public repository where anyone can specify a bytecode address and see the <strong>human-readable</strong> sourcecode that generated it. If after deployment you publish your source to a distributed file system, Sourcify will automatically catch it and make a public link between the address of your bytecode and the content of your source files. The process of checking that the source matches the deployed bytecode is called <strong>verification</strong>. The Sourcify component that does this automatically is called <strong>Monitor</strong>.</p><h3>Code</h3><p>The emphasis of this article will not be so much on the implementation. If this is what you are looking for, head down to the Conclusion to find a GitHub link. However, for the sake of better illustration, a JS line might drop by every now and then, so just assume that an initial</p><pre>const web3 = new (require(&quot;web3&quot;))(process.env.WEB3_PROVIDER);<br>const ethers = require(&quot;ethers&quot;);</pre><p>has been executed.</p><h3>Sourcify</h3><p><a href="https://github.com/ethereum/sourcify">Sourcify</a> — a portmanteau of <em>source-verify</em> — is an open-source project that allows anyone to run a source verification server that:</p><ol><li>takes files and an address</li><li>recompiles the files</li><li>matches the recompiled bytecode to the bytecode at the provided address</li><li>puts the received files to a <a href="https://repo.sourcify.dev/">public repository</a></li></ol><p>This article will focus on step 1, that is — how to <strong>obtain the data</strong> necessary for the rest of the procedure. More precisely, it will analyze how this can be done <strong>automatically</strong>, i.e. without users directly interacting with Sourcify. The instructions for obtaining the data are — one might say — hidden within the blocks of the blockchain.</p><h3>How do we fetch the blocks?</h3><p>New blocks are periodically added to the blockchain. To keep track of new smart contracts, we have to <strong>listen to newly arriving blocks</strong>. This can be done by:</p><ul><li>periodically polling a JSON-RPC server over HTTPS</li><li>keeping a connection to a JSON-RPC server over WebSocket</li></ul><p>If you are not running your own <a href="https://github.com/ethereum/sourcify/issues">node</a>, these two options can be achieved via third parties such as <a href="https://infura.io/">Infura</a> and <a href="https://www.alchemyapi.io/">Alchemy</a>. The JSON-RPC information is mantained <a href="https://chainid.network/chains.json">here</a>.</p><pre>const n = await web3.eth.getBlockNumber();<br>const block = await web3.eth.getBlock(n, true);</pre><h3>How do we know which transactions to extract from the blocks?</h3><p>Every block contains transaction hashes. Every transaction hash identifies a transaction. Every transaction specifies a sender and a recipient. If there is <strong>no recipient</strong>, the transaction was used to create a <strong>new smart contract</strong>. This is the only kind of transaction that we take into consideration.</p><pre>for (const tx of block.transactions) {<br>    if (!tx.to) {<br>        const address = ethers.utils.getContractAddress(tx);<br>        const bytecode = await web3.eth.getCode(address);<br>    }<br>}</pre><h3>How do we know from where to obtain the metadata?</h3><p>The end of the bytecode basically contains metadata metadata. This is not a typo, but actually means that this is where the compilation metadata is stored. More precisely, based on the settings used during compilation, this part of bytecode might contain an <a href="https://ipfs.io/">IPFS</a> or a <a href="https://swarm-gateways.net/">Swarm</a> address from which it can be obtained. <a href="https://docs.soliditylang.org/en/v0.8.1/metadata.html#encoding-of-the-metadata-hash-in-the-bytecode">Here</a> you can read more about the last bytes of the bytecode and how to extract these CBOR-encoded pieces of information.</p><p>It is <strong>important</strong> to mention that while the bytecode might contain an IPFS address/hash, it does <strong>not necessarily</strong> mean that the resource can be obtained from that address. This happens because the IPFS hash is <strong>precalculated</strong> from the metadata content regardless of actual IPFS publication. So to enable automatic contract verification, the metadata has to be <strong>published</strong>!</p><h3>How do we know from where to obtain the source files?</h3><p>A metadata file holds a list of all sources used for bytecode generation (i.e. compilation). Every source file specifies:</p><ul><li>compilation path (the file name on the developer’s system)</li><li>content hash (<em>keccak256</em>)</li><li>the places from which it can be fetched (such as IPFS or Swarm)</li></ul><pre>...</pre><pre>},<br>&quot;sources&quot;: {<br>  &quot;browser/1_Storage.sol&quot;: {<br>   &quot;keccak256&quot;: &quot;0xaedc7086ad85039072...5d03740fea748ea3f88e&quot;,<br>   &quot;urls&quot;: [<br>    &quot;bzz-raw://4bc331951c2595132...0222c6bb2d45ff45334ee83a7&quot;,<br>    &quot;dweb:/ipfs/QmWb1NQ6Pw8ZLMFX8uDjM...eT9iP2TvWisPhjN3U2&quot;<br>   ]<br>  }<br>},<br>...</pre><p>The same important note from the previous paragraph can be applied here, with the suggestion being: to enable automatic contract verification, the source files have to be <strong>published</strong>!</p><p>The metadata file also holds other important values necessary for a correct recompilation:</p><ul><li>compilation target (the main contract and its source file)</li><li>compiler version</li><li>optimization specifications</li></ul><h3>Source fetching</h3><p>The options for source fetching are similar to those of block fetching. You can either:</p><ul><li>run your own IPFS or Swarm node</li><li>rely on a gateway, such as <a href="https://ipfs.infura.io:5001/api/v0/cat">the Infura gateway</a> or <a href="https://swarm-gateways.net/">the Swarm gateway</a></li></ul><p>Once fetched, the sources are ready to be recompiled relying on the aforementioned compiler specifications. The required compiler version can be obtained <a href="https://github.com/ethereum/solc-bin">here</a>.</p><h3>Conclusion</h3><p>As foreseen in the TL;DR paragraph, the key to answering the titular question and having your smart contract automatically verified is <strong>publishing your metadata and source files</strong>. This way they can be automatically fetched, analyzed, recompiled and stored into <a href="https://repo.sourcify.dev/">this repository</a>.</p><p>Some important details were not mentioned in this analysis and are left as research material to the reader:</p><ul><li>Which library to use for blockchain interaction (<em>web3</em>, <em>ethers</em>, etc.)?</li><li>How exactly to decode the metadata metadata?</li><li>Why third-party blockchain data providers ruin decentralization?</li><li>If polling, how often it should be done?</li><li>If a resource is not available, when to stop trying to fetch it?</li></ul><p>The current Sourcify Monitor implementation can be found <a href="https://github.com/ethereum/sourcify/blob/master/src/monitor/monitor.ts">here</a>. If you spot space for enhancement, feel free to <a href="https://github.com/ethereum/sourcify/issues">leave an issue</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28ee1c5cf941" width="1" height="1" alt=""><hr><p><a href="https://news.shardlabs.io/how-smart-contracts-can-be-automatically-verified-28ee1c5cf941">How Smart Contracts Can Be Automatically Verified</a> was originally published in <a href="https://news.shardlabs.io/">Shard Labs News</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>