<?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 Majd on Medium]]></title>
        <description><![CDATA[Stories by Majd on Medium]]></description>
        <link>https://medium.com/@turfa?source=rss-6c33f457ba27------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DmPW8kkV9Fidu7cw49HEOA.jpeg</url>
            <title>Stories by Majd on Medium</title>
            <link>https://medium.com/@turfa?source=rss-6c33f457ba27------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:45:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@turfa/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[Why Upgrading OpenZeppelin Smart Contracts from Version 4 to Version 5 is Unsafe]]></title>
            <link>https://medium.com/51nodes/why-upgrading-openzeppelin-smart-contracts-from-version-4-to-version-5-is-unsafe-e08be30efd8a?source=rss-6c33f457ba27------2</link>
            <guid isPermaLink="false">https://medium.com/p/e08be30efd8a</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[openzeppelin]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <dc:creator><![CDATA[Majd]]></dc:creator>
            <pubDate>Fri, 22 Dec 2023 14:15:37 GMT</pubDate>
            <atom:updated>2023-12-23T19:20:06.338Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*5WjQaHVQt16XB5Lr_2TeeQ.png" /><figcaption><a href="https://forum.openzeppelin.com/t/contract-upgrade-failed-because-variables-are-deleted-in-openzepplin-5-0/38341">unsafe upgradability note</a></figcaption></figure><p>OpenZeppelin is a well-known and trusted company in the blockchain world. It provides secure open-source smart contracts and libraries, which accelerate the blockchain development worldwide. For more than two years, developers from all over the world have been using OpenZeppelin’s <em>v4</em> contracts to build applications. The <em>v4</em> contracts were maintained, well-documented, and audited over a long period. However, On the 5th of October, a new journey began with the release the <em>v5</em> of OpenZeppelin contracts. In this article, we will explain - with an example - why it is not safe to upgrade existing deployments from OpenZeppelin Contract <em>v4</em> to <em>v5</em>.</p><h4>TLDR</h4><p>The <em>v5</em> of the contracts includes significant changes and improvements. Some contracts and libraries have been restructured, while others have been removed. However, the most crucial reason for the incompatibility is that the <em>v5</em> contracts of OpenZeppelin integrate a significant change to the storage layer. The contracts in <em>v5</em> adopt <strong>the namespaced storage layout</strong>, which is defined in <a href="https://eips.ethereum.org/EIPS/eip-7201"><strong>ERC-7201</strong></a>. This change implies that the new logic of <em>v5</em> contracts will query existing data and write new data in <strong>incorrect</strong> storage slots, making an upgrade from <em>v4</em> to <em>v5</em> not safe. Developers have several options to update an existing contract, such as reinitializing the contract, deploying it as a new proxy, or choosing to continue with updated v4 contracts. Each option has its own unique challenges and requirements.</p><h4>Prerequisites</h4><p>We consider the knowledge of upgradeable smart contracts and rudimentary knowledge of the Ethereum Virtual Machine (EVM) storage as a requirement for understanding this article. The following tutorials give a good overview about upgradability of smart contracts:</p><ul><li><a href="https://www.youtube.com/watch?v=JgSj7IiE4jA">https://www.youtube.com/watch?v=JgSj7IiE4jA</a></li><li><a href="https://www.youtube.com/watch?v=kWUDTZhxKZI">https://www.youtube.com/watch?v=kWUDTZhxKZI</a></li><li><a href="https://www.youtube.com/watch?v=YJZV9uiDbJI">https://www.youtube.com/watch?v=YJZV9uiDbJI</a></li></ul><p>For the EVM storage layer we recommend the following articles:</p><ul><li><a href="https://steveng.medium.com/ethereum-virtual-machine-storage-layout-beb9a72a07e9">https://steveng.medium.com/ethereum-virtual-machine-storage-layout-beb9a72a07e9</a></li><li><a href="https://www.adrianhetman.com/unboxing-evm-storage/">https://www.adrianhetman.com/unboxing-evm-storage/</a></li><li><a href="https://medium.com/@Knownsec_Blockchain_Lab/knowsec-blockchain-lab-depth-understanding-of-evm-storage-mechanism-and-security-issues-50509acea373">https://medium.com/@Knownsec_Blockchain_Lab/knowsec-blockchain-lab-depth-understanding-of-evm-storage-mechanism-and-security-issues-50509acea373</a></li></ul><h4>Comparing how state variables are stored in OpenZeppelin upgradeable contracts in v4 versus v5</h4><p>The Ethereum Virtual Machine (EVM) stores state variables in a key-value store with 256-bit keys and 256-bit values. These state variables are stored in a compact, continuous manner. Therefore, <strong>the order</strong> of the state variables in a smart contract determines their position in the storage.</p><p>OpenZeppelin <em>v4</em> upgradeable contracts primarily use the default approach for storage management. The storage slots for state variables are determined by their sequential order in the contract. Therefore, base contracts often include a ‘storage buffer’ — defined as <strong><em>uint256[49] __gap; </em></strong>— to reserve storage slots. This will allow future versions of that contract to use up those slots without affecting the storage layout of child contracts. This approach of storage handling complicates contract management and upgradeability by increasing the risk of mistakes and storage conflicts, where new upgrades may unintentionally overwrite existing storage slots.</p><p>To simplify upgradability and minimize storage conflicts, OpenZeppelin <em>v5</em> adopted the <strong>namespace storage layout,</strong> which has been proposed officially as <strong>ERC-7201</strong> on June 20, 2023. The ERC-7201 recommends using pseudorandom locations derived from a namespace as the basis for new storage trees in base contracts. The root slot for storage is calculated using a <a href="https://eips.ethereum.org/EIPS/eip-7201#formula">specific formula</a>. This approach does not require changes on the EVM level and can be implemented by grouping state variables within a struct and using assembly language to access specific storage slots directly, as shown in the example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*ZoQ8W_gLnUAdFo2yBFizbw.png" /><figcaption>Example of namespace-based storage locations in a smart contract following <a href="https://eips.ethereum.org/EIPS/eip-7201">ERC-7201</a></figcaption></figure><p>In the example smart contract <strong><em>x</em></strong> and <strong><em>y</em></strong> are state variables. However, they are not stored on the first and second slots in the storage as default. They will be stored in the storage tree under the given root <em>MAIN_STORAGE_LOCATION</em> = <em>0x183a…b500, </em>which is calculated using the suggested formula in ERC-7201.</p><p>When a <em>v4</em> upgradeable contract is deployed on the network, the state variables of the contract are initialized, stored, and managed using the default storage pattern based on their order in the contracts. However, if the implementation contract is upgraded using <em>v5</em> contracts, a critical issue occurs. The new <em>v5</em> contracts are designed to interact with storage in a different way, using the namespaced storage layout introduced in ERC-7201. As a result, these updated contracts will not recognize or access the original storage slots used by the old contract. This mismatch in storage access will lead to dangerous and incorrect states where variables may not reflect the intended values, and functions may behave unpredictably, leading to loss of funds or compromised security.</p><h4>Unsafe Upgradability Example</h4><p>The following <strong><em>ExampleV4</em></strong> contract uses OpenZeppelin <em>v4</em> and imports the following contracts <strong><em>Initializable</em></strong>, <strong><em>OwnableUpgradeable</em></strong>, and <strong><em>UUPSUpgradeable</em></strong>. It sets the value of <em>exampleStateVariable</em>, the address of the owner, the address of implementation contract, and the status of the contract to Initialized during the initialization process when it is deployed as proxy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*NEgIUdRNixj945TWaZH-vA.png" /><figcaption>example upgradable contract using v4 version</figcaption></figure><p>To check the storage keys of the state variables, we can deploy it as <a href="https://remix-ide.readthedocs.io/en/latest/run_proxy_contracts.html">proxy using Remix IDE</a> and then debug it using the <a href="https://remix-ide.readthedocs.io/en/latest/debugger.html">Debugger</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*maeTLLFwIIC7Osk7qFxpjQ.png" /><figcaption>the storage of the contract</figcaption></figure><p>As shown in the picture, slot <em>0x000…000</em> (slot 0) has the value <em>0x01 </em>marking the contract as initialized. At Slot <em>0x000….033</em> (Slot 51) the, address of the owner of the contract is stored. At <em>0x000…0c9 (</em>Slot 201) the value <strong>7</strong> of the <em>exampleStateVariable </em>is stored<em> </em>(<strong>Note</strong>: The variables are not sorted at slots 0, 1, 2 due to the buffer gaps between the contracts). At Slot <em>0x36089…82bbc</em> the address of the implementation contract stored (<strong>Note</strong>: The slot of the implementation contract address was already hardcoded in v4 in a similar way to the namespaced storage layout to prevent conflicts).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B4ubieJKckFVLtVfmvePVg.png" /><figcaption>map current slots to values in v4</figcaption></figure><p>For demonstration purposes, we will update the implementation contract to <strong>ExampleV5</strong> using OpenZeppelin <em>v5</em> contracts, without introducing new logic. To execute the upgrade, we must import the new <em>v5</em> contracts, make minor modifications to the original contract to align with the new structure, and then deploy it. Then, we invoke the upgradeTo function on the proxy contract, providing the address of the new implementation contract as an argument.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*I1PW5hI4syYiX4c5CfnYMQ.png" /><figcaption>Code of the upgradeable smart contract in OpenZeppelin version 5</figcaption></figure><p>After upgrading the contract we can call the two state variables in the proxy contract to check their values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*5IxkINGc3L8WWWkA9XJvTA.png" /><figcaption>the values of the state variables after the upgrade</figcaption></figure><p>The value of <em>exampleStateVariable</em> is now <strong>1</strong> and not <strong>7</strong> because the upgraded contract reads the value from the first storage slot, which was the same slot of the initialized status. Furthermore, the owner is the zero address <em>0x000…000</em> because the new implemented contract following <em>v5</em> of OpenZeppelin looks for the address of the owner at another storage slot. Inspecting the <em>v5</em> contract, we will find that the owner value is read from slot <em>0x9016…9300,</em> which is empty.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*GB6smNDzOj7aUVi5DT881w.png" /><figcaption>the <a href="https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v5.0.1/contracts/access/OwnableUpgradeable.sol">OwnableUpgradeable</a> v5 contract</figcaption></figure><p>The same issue applies to the <strong><em>Initialization</em></strong> status of the contract. It is read from slot <em>0xf0c5…6a00</em> and not from <em>0x000…000</em> as it was in <em>v4</em> contracts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/658/1*JUVL1OAgmGzZrif4gBf2Bg.png" /><figcaption>the <a href="https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/v5.0.1/contracts/proxy/utils/Initializable.sol">Initializable</a> v5 contract</figcaption></figure><p>This means our very simple updated contract has incorrect values, is not initialized, and has the zero address as Owner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bc1wVdNiPRpEsYdGTZjhXA.png" /><figcaption>compare current slots to values before and after upgrade shows the misplacing of state variables between old and new v5 contract</figcaption></figure><p>At this point, anyone would be able to initialize it again giving the address of owner to control the contract. Even if the original owner still managed to initialize the contract before anyone else the contract will stay in an unsafe state. To explain this we will re-initialize and debug the storage of the upgraded contract using Remix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*_Vka33pQg0Q8EubBqlq7jg.png" /><figcaption>The storage keys and values after the upgrade and re-initialization of the contract</figcaption></figure><p>As displayed on the above storage snapshot, the original (v4) storage slots at <em>0x000…0c9</em>, <em>0x000…033</em> are not empty. Therefore, new state variables added to the contract could unintentionally use these slots, creating invalid values.</p><h4>Going Forward with Existing Contracts</h4><p>For developers, finding the best way to update existing contracts is crucial. There are several options for updating deployed <em>v4 </em>upgradable contracts, some of these options are:</p><ul><li><strong>Reinitializing the Contract</strong>: This process involves clearing the storage and migrating the data within the contract. While this approach might be feasible for simple contracts, for complex contracts with dynamic data types such as arrays, strings, and mappings, the storage becomes overly complicated, making migration within the contract impractical, if not impossible.</li><li><strong>Deploying as a Proxy</strong>: Similar to the <a href="https://sergiomartinrubio.com/articles/how-to-release-new-versions-of-smart-contracts/#social-migration">Social Migration</a> approach for non-upgradable contracts, this method involves deploying the new contract as a new proxy with a new address, followed by migrating the data and users to this new contract. Depending on the contract type and existing data, this process can become complicated. Additionally, it requires users’ involvement.</li><li><strong>Updating using v4 Contracts</strong>: Likely the most advisable approach is to continue using v4 contracts. This method eliminates the need for data migration or storage restructuring. Developers need to maintain their existing OpenZeppelin v4 contracts, extending or updating only the necessary components for their specific needs when required.</li></ul><h4>Summary</h4><p>In Ethereum, upgradeable contracts are complicated due to potential storage conflicts. The introduction of a new storage layout aims to make the upgrade process easier and cleaner. However, old upgradable <em>v4</em> contracts will face issues with the new layout, as their state variables could be dislocated, leading to an invalid contract state. Therefore, a native upgrade from <em>v4</em> contracts to <em>v5</em> is considered as not safe and is not recommended, requiring careful selection of strategies for updating <em>v4</em> Upgradeable Contracts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e08be30efd8a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/51nodes/why-upgrading-openzeppelin-smart-contracts-from-version-4-to-version-5-is-unsafe-e08be30efd8a">Why Upgrading OpenZeppelin Smart Contracts from Version 4 to Version 5 is Unsafe</a> was originally published in <a href="https://medium.com/51nodes">51nodes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Indexing and Querying Revoked Verifiable Credentials]]></title>
            <link>https://medium.com/51nodes/indexing-and-querying-revoked-verifiable-credentials-e229dc2781d4?source=rss-6c33f457ba27------2</link>
            <guid isPermaLink="false">https://medium.com/p/e229dc2781d4</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[self-sovereign-identity]]></category>
            <category><![CDATA[web3]]></category>
            <category><![CDATA[verifiable-credentials]]></category>
            <category><![CDATA[the-graph]]></category>
            <dc:creator><![CDATA[Majd]]></dc:creator>
            <pubDate>Fri, 01 Jul 2022 08:35:50 GMT</pubDate>
            <atom:updated>2022-07-01T08:35:50.188Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cf_nHmZ_YMKWONZYpkYo5w.jpeg" /></figure><p>Due to its immutability and censorship resistance, the Blockchain seems to be a suitable place to manage identities and credentials. However, querying the data on the blockchain in a trusted and efficient way is one of the most challenging issues every Web3 developer has to face while developing a DApp. Therefore, this article describes a simple approach to revoke verifiable credentials and a decentralized and efficient way to index and query those revoked credentials using the <a href="https://thegraph.com/en/">Graph protocol</a>.</p><p>We consider the knowledge of Self-Sovereign Identity (SSI) and rudimentary knowledge of the <a href="https://github.com/decentralized-identity/ethr-did-resolver/blob/master/doc/did-method-spec.md">Ethr DID method</a> as a requirement for understanding this article.</p><h4>Ethr DID and Credentials</h4><p>The Ethr DID method uses Ethereum addresses as identifiers. By default, the DID of the issuer/subject does not require an on-chain registration and it is constructed from the Ethereum address and controlled by its private key. This feature minimizes the effort of integrating Identities in many on-chain use cases.</p><p>When an off-chain verifiable credential is created, the issuer entity must set the “<em>issuer</em>” property to its Ethr DID and the “<em>credentialSubject.id</em>” property to the Ethr DID of the subject. Example:</p><pre>&quot;verifiableCredential&quot;: {<br>    &quot;@context&quot;: [..],<br>    &quot;issuer&quot;: {<br>      &quot;id&quot;: &quot;<strong><em>did:ethr:0xf1232f840f3ad7d23fcdaa84d6c66dac24efb198</em></strong>&quot;<br>    },<br>    &quot;issuanceDate&quot;: &quot;2022-07-2T12:00:00.000Z&quot;,<br>    &quot;credentialSubject&quot;: {<br>      &quot;id&quot;: &quot;<strong>did:ethr:0x435df3eda57154cf8cf7926079881f2912f54db4</strong>&quot;,<br>      &quot;degree&quot;: &quot;Bachelor Of Arts&quot;<br>    },<br>    &quot;proof&quot;: { ... }<br>  }</pre><p>After that as usual the issued credential will be used by the subject to generate a verifiable presentation. The verifier will verify the presentation and accept or reject it depending on its business logic.</p><h4>Credential Revocation</h4><p>Verifiable Credentials are similar to certificates, some of them have an expiration date and some don’t. However, in both cases, the <em>issuer</em> and the <em>subject</em> must be able to revoke them. To enable this critical requirement, we need a tamper-proof revocation data registry that is always available and cannot be censored by any of the mentioned parties. This can be achieved using the Blockchain. Such a registry can be implemented as a simple smart contract that stores a reference of the credential and a reference of the entity who revoked it.</p><p>The revocation process:</p><ul><li>Get the hash value of the issued verifiable credential using the <em>keccak256</em> hash function after converting the credentials to bytes. This value will be used as the on-chain reference to the credential</li><li>Send a transaction to the revocation smart contract to call the revoke method revoke(bytes32 digest)giving the extracted hash value as input</li><li>The entity that revoked the credential is the address of the invoker and it can be matched with the issuer or subject of the credential based on their DIDs</li></ul><p>The additional important key that will help use indexing those revocations is that when a credential is revoked, the revocation smart contract triggers an event that includes the <em>keccak256 </em>hash of that credential and the address of the entity that revoked it.</p><p>In the Github <a href="https://github.com/51nodes/vc-revocation-graph">Repository</a>, we prepared a simple project that:</p><ul><li>Deploys a revocation smart contract that stores references to the credential and the entity that revoked it.</li><li>Uses the <em>did-jwt-vc</em> and <em>ethr-did</em> libraries to issue 3 off-chain verifiable credentials by 3 different issuers and revoke them on-chain</li></ul><p>Please check the<em> ./ssi-contracts </em>folder and follow the instructions in the <a href="https://github.com/51nodes/vc-revocation-graph/tree/main/ssi-contracts"><em>README</em></a> file. After following the steps you should have the address of the revocation registry smart contract deployed on the <em>Goerli </em>network<em>. </em>The 3 revoked transactions can be found on <a href="https://goerli.etherscan.io/">Etherscan</a> using the address of the deployed contract.</p><p><strong>Why do we need to index the revoked Credentials?</strong></p><p>In a verification scenario, the verifier gets a verifiable presentation that includes <em>1 to n </em>verifiable credentials from the subject. In addition to the normal validation process of the credentials, the verifier must also check the on-chain status of the credential at any time easily and efficiently. To do that the verifier could request the status of each credential by calling the methodrevoked(address issuer, bytes32 digest) in the revocation registry contract. However, this approach of getting the information is limited, costly, and not efficient for many reasons, some of which are:</p><ul><li>It requires knowledge of the Blockchain and Web3 technology</li><li>It is slow and limited because of the latency, number of requests, node capability</li><li>It requires running a blockchain node or relaying on a node provider</li><li>Duplicate requests for the same credentials are sent over and over again</li><li>It requires the verifier to be online all the time</li></ul><p>And this is when <strong>indexing</strong> comes into play to provide an easy, fast and simple way to iterate and query data. Indexing is especially needed in the Blockchain area where its underlying design makes it difficult to search for relevant data.</p><p><strong>But How can we index the Blockchain?</strong></p><p>One approach is iterating all transactions and checking if the transaction interacts with our revocation contract. This method is very slow and resource-intensive. The other approach which is used by most developers is to subscribe to events that are triggered by the target contract. This method is much easier, supported by tools and libraries like web3js, and is more resource-saving. Now we come to the second important question of <strong>who</strong> should do the indexing? There are some possible answers:</p><p>1. <strong>The verifier himself. </strong>But it is not a good solution because each verifier needs to build up and maintain its own logic and infrastructure. Furthermore, the server of the verifier that is responsible for the indexing is a Single-point-of-failure</p><p>2.<strong> External ingestion service </strong>that indexes the data on behalf of the verifier. Such a service could be very useful. However, it creates one of the most critical issues which is the lack of <strong><em>TRUST</em></strong>. Verifiers need to trust the ingestion service and relying on it eliminates the advantage of using the Blockchain</p><p>3. <strong>Decentralized indexing protocol</strong> like <strong>The Graph protocol</strong>, which enables decentralized applications to query complex data from the blockchain without having to develop and operate proprietary indexing servers or use a centralized service provider. This is the preferred solution in most use cases because:</p><ul><li>It is distributed and decentralized. Therefore it removes the Single-point-of-failure and the missing trust issue</li><li>It does not require advanced Web3 knowledge. Developers use only <strong><em>GraphQL</em></strong> requests to query the data from already implemented and indexed Subgraphs</li><li>The technology behind it has been designed specifically for the Blockchain. Therefore, it is quick and easy to query and filter</li><li>Cost-efficient because indexers are competing with each other to provide the best service for the lowest price</li><li>Can be used by many verifiers around the world with minimal latency because the nodes are distributed and public</li></ul><p>The creators of the Graph protocol recognized the indexing and querying problem at the end of 2017 and started working to solve it by defining and implementing a decentralized indexing protocol. The <a href="https://thegraph.com/docs/en/about/introduction/">introduction page</a> of the Graph protocol offers detailed information about the protocol and you can also join the free and very useful courses provided by <a href="https://thegraph.academy/courses/"><em>The Graph Academy</em></a><em>.</em></p><p><strong>Develop and Deploy a Revocation Subgraph</strong></p><p>A Subgraph defines which data should be indexed from a blockchain, and how it should be stored. In our simple example, the core data the verifier requires is the hash of the credential and who revoked it. To make things easier for verifiers we could also add the timestamp and block number.</p><p>To keep the article simple we will deploy the subgraph on a <a href="https://thegraph.com/docs/en/hosted-service/what-is-hosted-service/">hosted service</a>. Hosted service can be seen today as a good and easy place to start with the development of Subgraphs because it is free to use and reduces the deployment and testing complexity of Subgraphs. Developers can at any time migrate their Subgraphs to the decentralized network.</p><p>The following steps to develop and deploy the Subgraph are described in detail with all the related files in this <a href="https://github.com/51nodes/vc-revocation-graph/tree/main/credential-revocation-subgraph-files">repository</a> :</p><ul><li>First, initialize a new sample project using the <a href="https://github.com/graphprotocol/graph-cli"><em>graph cli</em></a></li><li>Then modify some of the configurations like adding a startBlock and checking the address of the contract</li><li>After that comes the main part of the development which is to define the entities and implement the <em>handle</em> method that will be executed when an event is emitted by a smart contract</li><li>In the end, build the project and deploy it on the <em>hosted service</em> using the <em>graph cli</em> and an access token</li></ul><p>When the Subgraph is deployed and synchronized we can query the indexed data using simple <strong><em>GraphQL </em></strong><em>requests.</em></p><p>Example: open https://api.thegraph.com/subgraphs/name/<strong>&lt;github-name&gt;</strong>/credential-revocation-graph in browser and run the following query to get the hashes of all credentials that are revoked by Issuer-A from ./ssi-contracts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7NSnQbUcFT_RW0c3sngmaw.png" /><figcaption>Query revoked credentials by an issuer</figcaption></figure><p>The results should contain only a single revoked credential unless you modified the scripts inside ./ssi-contract or used an already existing contract for this query. You can also compare the results with the logged data of the executed script in./ssi-contract</p><h4>Summary</h4><p>In this article, we described one of many approaches on how to revoke issued credentials. This outlined approach benefits from the Ethr DID characteristic and it depends on the Blockchain as an immutable registry accessible for all parties. We also talked about the likely best way to index and query revoked credentials without relying on centralized services. And at the end, we showed how to develop a simple Subgraph that could run on the Graph protocol decentralized indexing <a href="https://thegraph.com/explorer/">network</a> and index the required data.</p><p><a href="https://www.51nodes.io/">51nodes GmbH</a> is a provider of crypto-economy solutions based in Stuttgart, Germany.</p><p>51nodes supports companies and other organizations in realizing their Blockchain projects. 51nodes offers technical consulting and implementation with a focus on smart contracts, decentralized apps (DApps), integration of blockchain with industry applications, and tokenization of assets.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e229dc2781d4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/51nodes/indexing-and-querying-revoked-verifiable-credentials-e229dc2781d4">Indexing and Querying Revoked Verifiable Credentials</a> was originally published in <a href="https://medium.com/51nodes">51nodes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build A Minimal Quorum Network]]></title>
            <link>https://medium.com/51nodes/build-a-minimal-quorum-network-90c59ec22226?source=rss-6c33f457ba27------2</link>
            <guid isPermaLink="false">https://medium.com/p/90c59ec22226</guid>
            <category><![CDATA[private-blockchain]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[permissioned-blockchains]]></category>
            <category><![CDATA[quorum]]></category>
            <dc:creator><![CDATA[Majd]]></dc:creator>
            <pubDate>Sat, 25 Jan 2020 13:04:26 GMT</pubDate>
            <atom:updated>2020-01-25T13:04:26.079Z</atom:updated>
            <content:encoded><![CDATA[<h4>Setting up a minimal Quorum network with Raft consensus for developing using Docker</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0A1s-55W7if9fNt9nfd1jA.jpeg" /><figcaption>Image by <a href="https://pixabay.com/users/wolfgang_vogt-4661554/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2136505">Wolfgang Vogt</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=2136505">Pixabay</a></figcaption></figure><h3>The Aim of this Article</h3><p>The main aim of this article is to show how to set up and dockerize a simple Quorum permissioned network for developing. Our goal is to minimize the needed resources and time by the developer, by using only 3 nodes and the Raft algorithm. Besides that, we provide the full source code on our <a href="https://github.com/51nodes/quorum-local-raft-network">GitHub repository</a>.</p><p>To follow the setup steps you need to install <a href="https://docs.docker.com/v17.09/engine/installation/">Docker</a> and <a href="https://docs.docker.com/compose/install/">Docker Compose</a> (version 3.6). We consider rudimentary knowledge of Docker a requirement for appreciating this article.</p><h3>What is Quorum and what makes it important?</h3><p><a href="https://www.goquorum.com/">Quorum </a>is an open-sourced permissioned Blockchain Platform, which is built based on the <a href="https://ethereum.org/de/">Ethereum </a>technology. By combining the innovation of the Ethereum community and features that support enterprise needs, Quorum aims to achieve a trusted technology that puts blockchain to work for business.</p><p>Not only because of the support by JP Morgan and Microsoft, but also due to its useful features, Quorum can be considered one of the preferred Blockchain platforms for enterprises. The most known feature of Quorum is the enabling of private transactions. However, Quorum has more to offer like network and peer permissions management, better performance, developer support, and — maybe the most underrated feature — a pluggable consensus algorithm. At the time of writing, Quorum supports two major consensus mechanisms, <a href="https://github.com/ethereum/EIPs/issues/650">IBFT </a>(fault-tolerant consensus) and <a href="https://raft.github.io/">Raft </a>(crash-tolerant consensus). Even more consensus mechanisms will be implemented in future releases. IBFT and Raft lead to faster consensus and provide immediate transaction finality, which makes them a suitable choice for business’s needs.</p><h3>Problem statement</h3><p>As we tried to set up a local quorum network to test if our application is also compatible with Quorum, we started with the 7-nodes example provided on the Quorum repository on <a href="https://github.com/jpmorganchase/quorum-examples">GitHub</a>. That example provides two methods to run your local network, either you use <a href="https://www.vagrantup.com/">Vagrant </a>or <a href="https://www.docker.com/">Docker</a>. Docker was our choice because we already had more experience using it. The long troubleshooting list and time needed to set up the network using Vagrant made the decision even easier.</p><p>However, running that example with Docker also brought up some issues. The docker-compose file starts 15 Docker containers, 7 containers for the nodes, 7 containers for the transaction managers and 1 container for the example. All those containers must be run simultaneously, which results in a high demand of memory and CPU resources and may not suit a development environment.</p><p>To test that setup, run the example using Docker</p><pre>git clone <a href="https://github.com/jpmorganchase/quorum-examples">https://github.com/jpmorganchase/quorum-examples</a><br>cd quorum-examples<br>docker-compose up -d</pre><h3>The minimal setup</h3><p>So, let us start with implementing the docker-compose file:</p><p>First, we must set the configuration fragments, which will be used in all nodes. Those reusable fragments start with the x- character sequence and can be merged in the services using the <a href="https://yaml.org/type/merge.html">YAML merge Type</a> &lt;&lt;</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af2f15165545c83a0a6e20a16d338e90/href">https://medium.com/media/af2f15165545c83a0a6e20a16d338e90/href</a></iframe><p>At line 4 we define the Docker <em>image </em>that will be pulled from the public Docker Hub repository, at the time of writing this article the current version is 2.3.0. After that, lines 6 and 7 define the <em>exposed ports</em> 21000 for the node client and 50400 for the raft communication between the nodes. The node client in Quorum that will run and manage the node on the containers is <a href="https://geth.ethereum.org/docs/">Geth</a>, which is a GoLang implementation of the Ethereum protocol. At line 8, we set up a simple <em>healthcheck</em>, which checks the health of the container every 4 seconds by running a command inside it, on the given port 8545. Later when we start the containers we will notice the message (healthy/unhealthy) in the STATUS column, we can also get the container health with the command:</p><pre>docker inspect — format=’{{json .State.Health}}’ &lt;container-name&gt;</pre><p>At line 16 we have the <em>entrypoint, </em>below that are the scripts that will run after the container start, like first copying necessary resources (node files and genesis block file) from the host to the container, set some variables and also starting Geth at line 33. Notice that the following additional arguments are needed in addition to the default Geth configuration to enable Quorum specific features.</p><pre>--permissioned \<br>--rpcapi ..,..,..,..,..,quorum,raft\<br>--raft \ <br>--raftport 50400 \</pre><p>As we mentioned before, Quorum supports two consensus algorithms IBFT or Raft, Raft was our choice. According to the following performance evaluation in this <a href="https://arxiv.org/pdf/1809.03421.pdf">article</a>, Raft performs slightly better at higher input transaction rates, the block time can be set for less than 1 second, and also the minimal required number of Nodes is 3.</p><p>Now we need to define the Services :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b24dd55b6199544420e61c4e88e8da59/href">https://medium.com/media/b24dd55b6199544420e61c4e88e8da59/href</a></iframe><p>The code above shows only the service of the first container. As we see in line 3 we use the above-defined quorum configuration. At line 6 we make the first node available at port 22000 by mapping the port 8545 (see Geth argument: rpcport) to 22000. For persistent data, Docker data volumes are set at line 8 and 9. It is also important to set <em>PRIVATE_CONFIG</em> to <em>ignore</em> at line 11 because we are not using the privacy feature of Quorum (which needs the transaction manager component). Line 13 sets the Docker network on which the container will run, and that network is defined at line 16. The services of the second and third containers are similar. The complete version of the compose file can be found <a href="https://github.com/51nodes/quorum-local-raft-network">here</a>.</p><h3>Running the network</h3><p>At this point, we are done and have a local quorum network, which can be started by the following commands:</p><pre>git clone <a href="https://github.com/51nodes/quorum-local-raft-network.git">https://github.com/51nodes/quorum-local-raft-network.git</a><strong><br>cd </strong>quorum-local-raft-network<br>docker-compose up -d</pre><p>To check if the containers are healthy and running use the command:<br>docker ps</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YClw74ZOOg1x1kTRjy-U4A.png" /><figcaption>3 docker containers for 3 quorum nodes building a local quorum network</figcaption></figure><p>To use the console of the node client Geth of the first node:</p><pre>docker exec -it quorum-local-raft-network_node1_1 geth attach /qdata/dd/geth.ipc</pre><p>To test the network using Remix set the following environment variable before starting the containers:</p><pre>export QUORUM_GETH_ARGS=&quot;--rpccorsdomain https://remix.ethereum.org&quot;</pre><p>To connect remix to your local network, you need to change the environment under ‘Deploy and Transactions’ to Web3 Provider, and set the endpoint of one of the nodes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c4n2181EFxQuwerMDZB4nA.png" /><figcaption>connect remix to the external Endpoint</figcaption></figure><p><a href="https://www.51nodes.io/">51nodes GmbH</a> based in Stuttgart is a provider of crypto economy solutions.</p><p>51nodes supports companies and other organizations in realizing their Blockchain projects. 51nodes offers technical consulting and implementation with a focus on smart contracts, decentralized apps (DApps), integration of Blockchain with industry applications, and tokenization of assets.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90c59ec22226" width="1" height="1" alt=""><hr><p><a href="https://medium.com/51nodes/build-a-minimal-quorum-network-90c59ec22226">Build A Minimal Quorum Network</a> was originally published in <a href="https://medium.com/51nodes">51nodes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solidity Library for Array of Type Address]]></title>
            <link>https://medium.com/51nodes/solidity-library-for-array-of-type-address-e40c36784ab2?source=rss-6c33f457ba27------2</link>
            <guid isPermaLink="false">https://medium.com/p/e40c36784ab2</guid>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[web3]]></category>
            <dc:creator><![CDATA[Majd]]></dc:creator>
            <pubDate>Wed, 25 Sep 2019 11:22:50 GMT</pubDate>
            <atom:updated>2020-04-09T11:04:20.538Z</atom:updated>
            <content:encoded><![CDATA[<h4>The AddrArrayLib Library provides utility functions to work with address[] types</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OGkwflbIzfRf_Br7h3f46Q.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@freiburgermax?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Max Langelott</a> on <a href="https://unsplash.com/s/photos/library?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Solidity and Libraries</h3><p><a href="https://solidity.readthedocs.io/en/v0.5.0/">Solidity</a> is one of the high-level programming languages for implementing smart contracts on the Ethereum Network and any blockchain that supports the EVM (Ethereum virtual machine). Its JavaScript syntax with C-style data types makes it the most preferred and supported language in the ecosystem.</p><p>Because of the immutability of the blockchain, it is extremely important to develop smart contracts with no bugs. Therefore, for the development of Ethereum smart contracts developers should always try to reuse and rely on code that already has been reviewed, tested and used by many developers, for example by using Solidity libraries.</p><p>In Solidity, libraries are similar to contracts, they implement the logic of operations and could be deployed once to be used later by a non-limited number of other smart contracts without the need to deploy the same logic again and again. This has the benefit of saving gas for the deployment. However, the fact that multiple contracts depend on the same piece of code could have bad consequences if that piece of code has a vulnerability. So it is very critical and you should be very cautious at choosing the libraries to use in your smart contract. The benefits of libraries are not just limited to reusability, they are also useful to make your code more maintainable, readable and even upgradeable to some degree.</p><h3>Using Libraries in Solidity</h3><p>Libraries are like contracts, and we use the library keyword to create them in solidity. However, libraries have no storage, cannot hold any Ether and cannot inherit nor be inherited. There are also two different types of libraries, libraries which have external functions are called <strong>linked libraries, </strong>they should be deployed first and<strong> </strong>their addresses should be added to the bytecode of the contract by the deployment process. Libraries which only have internal functions are called <strong>embedded libraries</strong>. They are deployed with the contract, so their bytecode becomes part of the contract bytecode and is also reachable with jump command. More information about linked and embedded libraries can be found in this <a href="https://medium.com/coinmonks/all-you-should-know-about-libraries-in-solidity-dd8bc953eae7">article</a>.</p><p>A simple example is the following SafeMath Library, which has one function to add two numbers of type uint256 and prevent the possible overflow bug.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5b8a094371e6f44b95d9ff7dbbb4cad/href">https://medium.com/media/a5b8a094371e6f44b95d9ff7dbbb4cad/href</a></iframe><p>To using the library, we first attach the <strong>uint256 </strong>type to it by using the directive</p><pre><strong>using</strong> SafeMath <strong>for</strong> uint256</pre><p>then any variable of type uint256 can call the function add() use a special <strong>DELEGATECALL</strong>, where the sender and value are not changed and will be just propagated from the parent scope to the child scope, also the storage will still refer to the calling contract.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a184e53c740d15e600d3e60051bc70e/href">https://medium.com/media/3a184e53c740d15e600d3e60051bc70e/href</a></iframe><p>As we mentioned before libraries have no storage, however they can modify the storage of the linked smart contract by adding the keyword <strong>storage</strong> to the first argument in the function and using <strong>struct</strong> to encapsulate the state variable in the library as it is shown in the modified implementation of the SafeMath library. We will use it in the implementation of the address array library.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2db274e037b3cf0735c3e01932d39b46/href">https://medium.com/media/2db274e037b3cf0735c3e01932d39b46/href</a></iframe><h3>Why the need for an Array of addresses?</h3><p>Address is a special data type in Solidity that holds a 20-byte value, and provides built-in functions like transfer and balance. Since every account and smart contract is represented through a unique address, working with addresses is needed in almost every smart contract project.</p><p>A simple iterating through a map in solidity is not possible, therefore there are many scenarios where developers must save addresses in an array, especially when it comes to on-chain interactions between contracts where the addresses cannot be given within the transaction data. Recently we worked on a project where we needed to store and manage addresses of investors on-chain in an array. After not finding any existing library for free, public usage, we implemented our own library for managing an array of addresses.</p><h3>Example Implementation</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25af2b81becfb61ff5e2954c72baf9bb/href">https://medium.com/media/25af2b81becfb61ff5e2954c72baf9bb/href</a></iframe><p>In the above library code, we have the <strong>Addresses</strong> struct which contains an array of type address and the functions that are usually needed to manage an array such as:</p><ul><li>pushAddress: add an address to the list, only if it does not exist yet. Allowing duplicate addresses is not preferred, because it will make the array bigger, the operations more complicated, and increase the used gas. Therefore, we assume that a simple special treatment for such addresses in the contract code is more efficient.</li><li>removeAddress: remove an address from the list and return true in case the address has been removed. The function will return false if the address does not exist. The method will find the element, swap it with the last element, and then delete it.</li><li>exists: return true if the address exists in the array.</li><li>size: return the size of the array.</li><li>getAddressAtIndex: get the address at the given index.</li></ul><p>In all functions, we pass the <strong>Addresses </strong>argument using the keyword<strong> </strong>as storage, so the EVM passes it by reference from the storage of the smart contract instead of creating a copy of it in memory.</p><p>Now we will create an example contract that uses this library to manage addresses of users, who can mint a Token.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89f11da368f92ebe9fcffe286fce26d9/href">https://medium.com/media/89f11da368f92ebe9fcffe286fce26d9/href</a></iframe><p>In this contract we used two embedded libraries, the first one is the embedded SafeMath library based on the implementation of <a href="https://openzeppelin.com/">Openzeppelin</a><a href="https://openzeppelin.com/,">,</a> which provides the logic of addition but does not change the state of the contract directly, and the second library the AddrArrayLib we defined before and imported via its .sol file.</p><p>We create a private variable of the struct type <strong>AddrArrayLib.Addresses</strong>, which represents the state variable of the contract and stores the addresses. After that we have a constructor and three contract functions:</p><ul><li>The <strong>addMinter</strong> function, which takes in the address of the new minter and calls the <strong>trustedMinters.pushAddress </strong>function passing in the struct instance from storage.</li><li>The <strong>removeMinter </strong>function .</li><li><strong>mintToken</strong> which checks if the <strong>msg.sender </strong>exists in the array of trusted minters by calling <strong>trustedMinters.exists(msg.sender)</strong> and after successful verification mints the token.</li></ul><p>The Repository including the library, TokenContract and test instructions can be found on Github under this <a href="https://github.com/51nodes/address-library-sample">link</a>.</p><h3>Summary and Future work</h3><p>In this article we briefly explained libraries, why are they used and how to use them. Besides, we introduced an implementation of a library to manage addresses in an array, which we consider an often needed building block of smart contracts.</p><p>In future work we will focus on testing, improving the current functions and implementing new functions. Furthermore, we will analyze the gas costs and compare a linked version of the library with an embedded one. Finally, after getting the library reviewed by many experienced developers, it will be deployed on the Ethereum blockchain and be available to be used in new projects.</p><p><a href="https://www.51nodes.io/">51nodes GmbH</a> based in Stuttgart is a provider of crypto economy solutions.</p><p>51nodes supports companies and other organizations in realizing their Blockchain projects. 51nodes offers technical consulting and implementation with a focus on smart contracts, decentralized apps (DApps), integration of Blockchain with industry applications, and tokenization of assets.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e40c36784ab2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/51nodes/solidity-library-for-array-of-type-address-e40c36784ab2">Solidity Library for Array of Type Address</a> was originally published in <a href="https://medium.com/51nodes">51nodes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>