Blog | G5 Cyber Security

Blockchain: Bypass Censorship

TL;DR

Use decentralised blockchain technology to store and access information that is resistant to censorship. This guide covers using IPFS with a blockchain for content addressing, creating a simple smart contract for registration, and accessing the data through a web interface.

1. Understanding the Problem & Solution

Traditional internet systems rely on central authorities (servers, ISPs) who can block or filter information. Blockchain offers a way to store data across many computers, making it very difficult for anyone to control or censor it. IPFS (InterPlanetary File System) is a peer-to-peer protocol for storing files in a distributed manner. Combining IPFS with a blockchain allows you to create permanent, censorship-resistant content addresses.

2. Setting up Your Environment

  1. Install Node.js and npm: You’ll need these to run JavaScript code. Download from nodejs.org
  2. Install Truffle: A development framework for Ethereum smart contracts. Open your terminal and run:
    npm install -g truffle
  3. Install Ganache: A personal blockchain for testing. Download from trufflesuite.com/ganache
  4. Install IPFS: Download and install the IPFS desktop application from ipfs.io/#downloads. Make sure the ipfs daemon is running in your terminal (ipfs daemon).

3. Creating a Simple Smart Contract

This contract will allow users to register their IPFS content hashes on the blockchain.

  1. Create a new Truffle project: In your terminal, run:
    truffle init
  2. Write the smart contract (contracts/ContentRegistry.sol):
    pragma solidity ^0.8.0;
    
    contract ContentRegistry {
        address public owner;
        mapping(address => string) public contentHashes;
    
        constructor() {
            owner = msg.sender;
        }
    
        function registerContent(string memory _hash) public {
            contentHashes[msg.sender] = _hash;
        }
    
        function getContentHash(address _addr) public view returns (string memory) {
            return contentHashes[_addr];
        }
    }
    
  3. Compile the contract: In your terminal, run:
    truffle compile

4. Deploying the Smart Contract

  1. Start Ganache: Open the Ganache application. It will provide you with development blockchain accounts and a network address (usually http://127.0.0.1:7545).
  2. Migrate the contract: Edit truffle-config.js to point to your Ganache network. Then, run:
    truffle migrate

5. Storing Content on IPFS

Upload the content you want to store to IPFS.

  1. Add your file: Open your terminal and run:
    ipfs add /path/to/your/file

    This will return a long hash (e.g., Qm...). This is the content identifier.

6. Registering Content Hash on Blockchain

Register the IPFS hash with your smart contract.

  1. Connect to the blockchain: Use a JavaScript library like Web3.js or Ethers.js in your frontend application.
  2. Call the registerContent function: Send a transaction to the deployed smart contract, passing the IPFS hash as an argument.
    // Example using Web3.js (simplified)
    const contentRegistry = new web3.eth.Contract(abi, address);
    contentRegistry.methods.registerContent(ipfsHash).send({ from: account });
    

7. Accessing Content

Retrieve the IPFS hash from the blockchain and access the content.

  1. Call the getContentHash function: Query the smart contract to get the IPFS hash associated with a specific address.
    // Example using Web3.js (simplified)
    const ipfsHash = await contentRegistry.methods.getContentHash(account).call();
    
  2. Fetch content from IPFS: Use the retrieved IPFS hash to download the content directly from the IPFS network.
    ipfs cat ${ipfsHash} // using ipfs command line tool
    

8. Building a Web Interface

Create a simple web page with forms to upload files, register hashes and display content.

Important Considerations

Exit mobile version