Part One: Developing Your Own Blockchain

own blockchain

Welcome to the first part of our series on developing your own blockchain in Java using Tomcat and Servlet. Creating a blockchain from scratch is a fascinating journey into the realms of decentralized technology and cryptographic security. Whether you are an experienced developer or a curious beginner, building a blockchain helps you understand the fundamental principles powering cryptocurrencies like Bitcoin and Ethereum. In this guide, we focus on the essential elements and classes you’ll need for your Java-based blockchain project.

Before we delve into the Java classes required for developing a blockchain, I recommend checking out this comprehensive step-by-step guide on how to create a cryptocurrency and on steemit. It offers valuable insights and a solid foundation to start your journey.

Key Java Classes for Your Own Blockchain

To develop a own blockchain, you need to implement several key classes that handle various aspects of the blockchain’s functionality. Here is a brief overview of each class and its role in the system:

1. Block

The Block class represents a single block in the blockchain. Each block contains a list of transactions, a timestamp, a reference to the previous block, and a unique hash. Here’s a simple structure for the Block class:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

public class Block implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private int nonce;
    private long timeStamp;
    private String hash;
    private String previousHash;
    private ArrayList<TransactionNode> transactions = new ArrayList<>();

    // Constructor and other methods
}

2. Blockchain

The Blockchain class manages the chain of blocks. It includes methods for adding new blocks, validating the chain, and ensuring that each block links correctly to the previous one.

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Blockchain implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<Block> blockchain;
    private Map<String, TransactionOutput> UTXOs;

    // Methods for adding blocks, validating the chain, etc.
}

3. BlockchainFileOperation

This class handles the reading and writing of blockchain data to and from files, ensuring the persistence of the blockchain.

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.StringTokenizer;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class BlockchainFileOperation {
    private String directory = "c:/tmp";

    // Methods for saving and loading the blockchain from a file
}

4. Constants

The Constants class holds important constants such as the difficulty level for mining, the reward for mining a block, and other configuration parameters.

public class Constants {
    public static final int DIFFICULTY = 4;
    public static final double MINER_REWARD = 10;
    public static final String GENESIS_PREV_HASH = "0000000000000000000000000000000000000000000000000000000000000000";
    public static final double FEE = 0.01;

    private Constants() {
        // Private constructor to prevent instantiation
    }
}

5. CryptographyHelper

This utility class provides methods for cryptographic operations like generating hashes and digital signatures.

import java.security.MessageDigest;

public class CryptographyHelper {
    public static String generateHash(String data) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(data.getBytes("UTF-8"));

            StringBuffer hexadecimalString = new StringBuffer();

            for (int i = 0; i < hash.length; i++) {
                String hexadecimal = Integer.toHexString(0xff & hash[i]);
                if (hexadecimal.length == 1) hexadecimalString.append('0');
                hexadecimalString.append(hexadecimal);
            }

            return hexadecimalString.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Other methods for generating key pairs, signing data, etc.
}

6. KeyPairHero

This class manages the generation and handling of public-private key pairs used in transactions.

import java.io.Serializable;
import com.starkbank.ellipticcurve.PrivateKey;
import com.starkbank.ellipticcurve.PublicKey;

public class KeyPairHero implements Serializable {
    private static final long serialVersionUID = 1L;
    private PrivateKey privateKey = new PrivateKey();
    private PublicKey publicKey = privateKey.publicKey();

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(PrivateKey privateKey) {
        this.privateKey = privateKey;
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }
}

7. Miner

The Miner class is responsible for mining new blocks by solving the cryptographic puzzle defined by the difficulty level.

public class Miner {
    private double reward;

    public double getReward() {
        return this.reward;
    }

    public void mine(Block block, Blockchain blockchain) {
        // Proof of Work (PoW)
        while (!isGoldenHash(block)) {
            block.incrementNonce();
            block.generateHash();
        }

        System.out.println(block + " hash just mined...");
        System.out.println("Hash is: " + block.getHash());

        // Broadcasting it to the network
        blockchain.addBlock(block);
        reward += Constants.MINER_REWARD;
    }

    private boolean isGoldenHash(Block block) {
        String leadingZeros = new String(new char[Constants.DIFFICULTY]).replace('
public class Miner {
    private double reward;

    public double getReward() {
        return this.reward;
    }

    public void mine(Block block, Blockchain blockchain) {
        // Proof of Work (PoW)
        while (!isGoldenHash(block)) {
            block.incrementNonce();
            block.generateHash();
        }

        System.out.println(block + " hash just mined...");
        System.out.println("Hash is: " + block.getHash());

        // Broadcasting it to the network
        blockchain.addBlock(block);
        reward += Constants.MINER_REWARD;
    }

    private boolean isGoldenHash(Block block) {
        String leadingZeros = new String(new char[Constants.DIFFICULTY]).replace('
public class Miner {
private double reward;
public double getReward() {
return this.reward;
}
public void mine(Block block, Blockchain blockchain) {
// Proof of Work (PoW)
while (!isGoldenHash(block)) {
block.incrementNonce();
block.generateHash();
}
System.out.println(block + " hash just mined...");
System.out.println("Hash is: " + block.getHash());
// Broadcasting it to the network
blockchain.addBlock(block);
reward += Constants.MINER_REWARD;
}
private boolean isGoldenHash(Block block) {
String leadingZeros = new String(new char[Constants.DIFFICULTY]).replace('\0', '0');
return block.getHash().substring(0, Constants.DIFFICULTY).equals(leadingZeros);
}
}
', '0'); return block.getHash().substring(0, Constants.DIFFICULTY).equals(leadingZeros); } }
', '0'); return block.getHash().substring(0, Constants.DIFFICULTY).equals(leadingZeros); } }

8. ReceiveBlockServlet

This servlet class handles the receipt of new blocks from other nodes in the network, ensuring the blockchain stays updated across all participants.

import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet("/receiveBlock")
public class ReceiveBlockServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Blockchain blockchain;
    private ArrayList<TransactionNode> unconfirmedTransactions = new ArrayList<>();
    private Wallet wallet = new Wallet();
}

Part One: Developing Your Own Blockchain

Welcome to the first part of our series on developing your own blockchain in Java using Tomcat and Servlet. Creating a blockchain from scratch is a fascinating journey into the realms of decentralized technology and cryptographic security. Whether you are an experienced developer or a curious beginner, building a blockchain helps you understand the fundamental principles powering cryptocurrencies like Bitcoin and Ethereum. In this guide, we focus on the essential elements and classes you’ll need for your Java-based blockchain project.

Before we delve into the Java classes required for developing a blockchain, I recommend checking out this comprehensive step-by-step guide on how to create a cryptocurrency. It offers valuable insights and a solid foundation to start your journey.

Key Java Classes for Your Blockchain

To develop a blockchain, you need to implement several key classes that handle various aspects of the blockchain’s functionality. Here is a brief overview of each class and its role in the system:

1. Block

The Block class represents a single block in the blockchain. Each block contains a list of transactions, a timestamp, a reference to the previous block, and a unique hash. Here’s a simple structure for the Block class:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

public class Block implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private int nonce;
    private long timeStamp;
    private String hash;
    private String previousHash;
    private ArrayList<TransactionNode> transactions = new ArrayList<>();

    // Constructor and other methods
}

2. Blockchain

The Blockchain class manages the chain of blocks. It includes methods for adding new blocks, validating the chain, and ensuring that each block links correctly to the previous one.

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Blockchain implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<Block> blockchain;
    private Map<String, TransactionOutput> UTXOs;

    // Methods for adding blocks, validating the chain, etc.
}

3. BlockchainFileOperation

This class handles the reading and writing of blockchain data to and from files, ensuring the persistence of the blockchain.

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.StringTokenizer;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class BlockchainFileOperation {
    private String directory = "c:/tmp";

    // Methods for saving and loading the blockchain from a file
}

4. Constants

The Constants class holds important constants such as the difficulty level for mining, the reward for mining a block, and other configuration parameters.

public class Constants {
    public static final int DIFFICULTY = 4;
    public static final double MINER_REWARD = 10;
    public static final String GENESIS_PREV_HASH = "0000000000000000000000000000000000000000000000000000000000000000";
    public static final double FEE = 0.01;

    private Constants() {
        // Private constructor to prevent instantiation
    }
}

5. CryptographyHelper

This utility class provides methods for cryptographic operations like generating hashes and digital signatures.

import java.security.MessageDigest;

public class CryptographyHelper {
    public static String generateHash(String data) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(data.getBytes("UTF-8"));

            StringBuffer hexadecimalString = new StringBuffer();

            for (int i = 0; i < hash.length; i++) {
                String hexadecimal = Integer.toHexString(0xff & hash[i]);
                if (hexadecimal.length == 1) hexadecimalString.append('0');
                hexadecimalString.append(hexadecimal);
            }

            return hexadecimalString.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // Other methods for generating key pairs, signing data, etc.
}

6. KeyPairHero

This class manages the generation and handling of public-private key pairs used in transactions.

import java.io.Serializable;
import com.starkbank.ellipticcurve.PrivateKey;
import com.starkbank.ellipticcurve.PublicKey;

public class KeyPairHero implements Serializable {
    private static final long serialVersionUID = 1L;
    private PrivateKey privateKey = new PrivateKey();
    private PublicKey publicKey = privateKey.publicKey();

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(PrivateKey privateKey) {
        this.privateKey = privateKey;
    }

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }
}

7. Miner of the own blockchain

The Miner class is responsible for mining new blocks by solving the cryptographic puzzle defined by the difficulty level on the own blockchain.

public class Miner {
    private double reward;

    public double getReward() {
        return this.reward;
    }

    public void mine(Block block, Blockchain blockchain) {
        // Proof of Work (PoW)
        while (!isGoldenHash(block)) {
            block.incrementNonce();
            block.generateHash();
        }

        System.out.println(block + " hash just mined...");
        System.out.println("Hash is: " + block.getHash());

        // Broadcasting it to the network
        blockchain.addBlock(block);
        reward += Constants.MINER_REWARD;
    }

    private boolean isGoldenHash(Block block) {
        String leadingZeros = new String(new char[Constants.DIFFICULTY]).replace('
public class Miner {
    private double reward;

    public double getReward() {
        return this.reward;
    }

    public void mine(Block block, Blockchain blockchain) {
        // Proof of Work (PoW)
        while (!isGoldenHash(block)) {
            block.incrementNonce();
            block.generateHash();
        }

        System.out.println(block + " hash just mined...");
        System.out.println("Hash is: " + block.getHash());

        // Broadcasting it to the network
        blockchain.addBlock(block);
        reward += Constants.MINER_REWARD;
    }

    private boolean isGoldenHash(Block block) {
        String leadingZeros = new String(new char[Constants.DIFFICULTY]).replace('
public class Miner {
private double reward;
public double getReward() {
return this.reward;
}
public void mine(Block block, Blockchain blockchain) {
// Proof of Work (PoW)
while (!isGoldenHash(block)) {
block.incrementNonce();
block.generateHash();
}
System.out.println(block + " hash just mined...");
System.out.println("Hash is: " + block.getHash());
// Broadcasting it to the network
blockchain.addBlock(block);
reward += Constants.MINER_REWARD;
}
private boolean isGoldenHash(Block block) {
String leadingZeros = new String(new char[Constants.DIFFICULTY]).replace('\0', '0');
return block.getHash().substring(0, Constants.DIFFICULTY).equals(leadingZeros);
}
}
', '0'); return block.getHash().substring(0, Constants.DIFFICULTY).equals(leadingZeros); } }
', '0'); return block.getHash().substring(0, Constants.DIFFICULTY).equals(leadingZeros); } }

8. ReceiveBlockServlet on the own blockchain

This servlet class handles the receipt of new blocks from other nodes in the network, ensuring the blockchain stays updated across all participants of the own blockchain.

import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet("/receiveBlock")
public class ReceiveBlockServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private Blockchain blockchain;
    private ArrayList<TransactionNode> unconfirmedTransactions = new ArrayList<>();
    private Wallet wallet = new Wallet();
}

9. Sha256Helper

A utility class dedicated to SHA-256 hashing, which is crucial for creating block hashes.

import java.security.MessageDigest;

public class Sha256Helper {
    public static String generateHash(String data) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(data.getBytes("UTF-8"));

            StringBuffer hexadecimalString = new StringBuffer();

            for (int i = 0; i < hash.length; i++) {
                String hexadecimal = Integer.toHexString(0xff & hash[i]);
                if (hexadecimal.length == 1) hexadecimalString.append("0");
                hexadecimalString.append(hexadecimal);
            }

            return hexadecimalString.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

10. TransactionInput

This class represents an input in a transaction, referencing a previous transaction output.

import java.io.Serializable;

public class TransactionInput implements Serializable {
    private static final long serialVersionUID = 1L;
    private String transactionOutputId;
    private TransactionOutput UTXO;
    private double amount;
    private String sendTo;

    // Getters, setters, and other methods
}

11. TransactionNode

A class that defines the structure of a transaction node within the network.

import java.io.Serializable;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;

public class TransactionNode implements Serializable {
    private static final long serialVersionUID = 1L;
    private String transactionId;
    private PrivateKey senderPrivateKey;
    private PublicKey sender;
    private PublicKey receiver;
    private String receiverString;
    private String senderString;
    private double amount;
    private Signature signature;
    private String time;
    private TransactionInput inputs;
    private TransactionOutput outputs;

    // Getters, setters, and other methods
}

12. TransactionOutput

This class represents the output of a transaction, specifying the recipient and the amount transferred.

import java.io.Serializable;
import java.security.PublicKey;

public class TransactionOutput implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String parentTransactionId;
    private PublicKey receiver;
    private String receiverString;
    private String senderString;

    // Getters, setters, and other methods
}

13. TransactionServlet

A servlet for handling incoming transactions from clients and processing them within the blockchain.

import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet("/transaction")
public class TransactionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ArrayList unconfirmedTransactions = new ArrayList<>()

Disclaimer