BlockChain Tech
BlockChain Tech
Merkle Tree
Merkle tree is a tree data structure with leaf nodes and non leaf nodes. It also
known as Hash tree.
The reason behind it is it only stores the hashes in its nodes instead of data. In its
leaf nodes, it will
store the hash of the data. Non leaf nodes contain the hash of its children.
SOURCE CODE
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
this.merkleTree = buildMerkleTree(transactions);
try {
if (hex.length() == 1) {
hexString.append('0');
hexString.append(hex);
return hexString.toString();
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
private List<String> buildMerkleTree(List<String> transactions) {
int levelOffset = 0;
merkleTree.add(parentHash);
levelOffset += levelSize;
return merkleTree;
public List<String>getMerkleTree() {
return merkleTree;
transactions.add("Transaction 1");
transactions.add("Transaction 2");
transactions.add("Transaction 3");
transactions.add("Transaction 4");
System.out.println(hash);
}}}
OUTPUT
AIM : Creation of Block.
Blocks are data structures within the blockchain database, where transaction data
in a
transactions not yet validated by the network. Once the data are validated, the
block is closed. Then,
a new block is created for new transactions to be entered into and validated.
Blocks are created when miners or block validators successfully validate the
encrypted
SOURCE CODE
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
this.previousHash = previousHash;
this.data = data;
this.nonce = 0;
this.hash = calculateHash();
try {
if (hex.length() == 1) {
hexString.append('0');
hexString.append(hex);
return hexString.toString();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
nonce++;
hash = calculateHash();
return index;
return timestamp;
return previousHash;
}
public String getHash() {
return hash;
return data;
Block b=new
Block(1,"3a42c503953909637f78dd8c99b3b85ddde362415585afc11901bdefe834
9102","hai");
b.calculateHash();
b.mineBlock(1);
b.getIndex();
b.getTimestamp();
b.getPreviousHash();
b.getHash();
b.getData();
}}
OUTPUT :
AIM : Block chain Implementation Programming code.
Digital Signature is basically a function that takes a string as input and returns a
fixed-size
alphanumeric string. The output string is known as the Digital Signature or the
Hash of the input
message. The important point is that the function via which we obtain the Digital
Signature is
“irreversible” in that given an input string, it can compute the Hash. However,
given the Hash, it is
Hash1=hash(input1)
Hash2=hash(input2)
It is virtually impossible to compute input1 given the value of hash1. Similarly for
input2 and hash2.
It is virtually impossible to find distinct input1 and input2 such that hash1 = hash2.
SOURCE CODE:
import java.util.ArrayList;
import java.util.List;
public class Blockchain {
this.difficulty = difficulty;
createGenesisBlock();
genesisBlock.mineBlock(difficulty);
chain.add(genesisBlock);
newBlock.mineBlock(difficulty);
chain.add(newBlock);
if (!currentBlock.getHash().equals(currentBlock.calculateHash())) {
return false;
if (!previousBlock.getHash().equals(currentBlock.getPreviousHash())) {
return false;
}}
return true;
blockchain.addBlock(block1);
blockchain.addBlock(block2);
blockchain.addBlock(block3);
OUTPUT :
AIM : CreatingERC20 token.
An ERC20 token is a standard used for creating and issuing smart contracts on the
that people can invest in. ERC stands for "Ethereum request for comment," and
the ERC20 standard
SOURCE CODE
import java.util.HashMap;
import java.util.Map;
this.name = name;
this.symbol = symbol;
this.decimals = decimals;
System.out.println("Insufficient balance");
return;
System.out.println("Transfer successful");
return name; }
return symbol; }
return decimals; }
token.balances.put("Alice", 1000);
token.balances.put("Bob", 500);
token.balances.put("Charlie", 200);
}}
OUTPUT :
AIM: Java code to implement blockchain in Merkle Trees.
child nodes. The leaves can either be the data itself or a hash/signature of the
data.
Usages:
Merkle tree(Hash tree) is used to verify any kind of data stored, handled and
transferred in and
between computers.
Currently, the main use of Merkle tree is to make sure that data blocks received
from other peers in a
peer-to-peer network are received undamaged and unaltered, and even to check
that the other peers
SOURCE CODE
import java.util.ArrayList;
import java.util.List;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
class MerkleTree {
this.transactions = transactions;
this.root = buildTree();
nextLevel.add(hash);
level = nextLevel;
return level.get(0);
try {
if (hex.length() == 1)
hexString.append('0');
hexString.append(hex);
return hexString.toString();
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}}
return root;
}}
public Blockchain1() {
}
public void addBlock(List<String> transactions) {
blocks.add(merkleTree);
if (blockIndex>= 0 &&blockIndex<blocks.size()) {
return merkleTree.getRoot();
return null;
transactions1.add("Transaction 1");
transactions1.add("Transaction 2");
transactions1.add("Transaction 3");
blockchain.addBlock(transactions1);
transactions2.add("Transaction 4");
transactions2.add("Transaction 5");
blockchain.addBlock(transactions2);
String root1 = blockchain.getBlockRoot(0);
OUTPUT :
AIM: Java Code to implement Mining using block chain.
the modern technology that stores data in the form of block data connected
through cryptography
1991. It is a linked list where the nodes are the blocks in the Blockchain, and the
references are
hashes of the previous block in the chain. References are cryptographic hashes
when dealing with
link lists. The references are just basically objects. So every single node will store
another node
variable, and it will be the reference to the next node. In this case, the references
are cryptographic
hashes.
Blockchain uses hash pointers to reference the previous node in a long list. We
assign a hash to every
SOURCE CODE
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
class Block {
this.index = index;
this.timestamp = timestamp;
this.previousHash = previousHash;
this.transactions = transactions;
this.nonce = 0;
this.hash = calculateHash();
try {
if (hex.length() == 1)
hexString.append('0');
hexString.append(hex);
return hexString.toString();
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
nonce++;
hash = calculateHash();
}
public String getHash() {
return hash;
return previousHash;
}}
class Transaction {
this.from = from;
this.to = to;
this.amount = amount;
@Override
}}
class Blockchain {
this.difficulty = difficulty;
createGenesisBlock();
genesisBlock.mineBlock(difficulty);
chain.add(genesisBlock);
block.mineBlock(difficulty);
chain.add(block);
if (!currentBlock.getHash().equals(currentBlock.calculateHash()))
return false;
if (!currentBlock.getPreviousHash().equals(previousBlock.getHash()))
return false;
return true;
}}
transactions);
blockchain.addBlock(block1);
transactions2);
blockchain.addBlock(block2);
}}
OUTPUT :
AIM: Java Code to implement peer-to-peer using block chain
Earlier, we made a single blockchain. Now we’re going to make a set of them and
get them talking to
blocks from any nodes and eventually it gets to peer nodes so everyone agrees on
what the
blockchain looks like. There is one problem that comes up right away: Each node is
two services,
plus a MongoDB and a Kafka message bus that all need to talk to one another.
We’ll be working on
a node service that will allow the nodes to work with one another. This will get
input from two
places, a restful interface that allows you to add and list the nodes connected, and
a message bus
provided by Kafka that notifies the node service of changes in the local blockchain
that need to be
SOURCE CODE
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
class Block {
this.index = index;
this.timestamp = timestamp;
this.previousHash = previousHash;
this.transactions = transactions;
this.nonce = 0;
this.hash = calculateHash();
try {
if (hex.length() == 1)
hexString.append('0');
hexString.append(hex);
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
nonce++;
hash = calculateHash();
return index;
}
return timestamp;
return previousHash;
return hash;
return nonce;
public List<Transaction>getTransactions() {
return transactions;
}}
class Transaction {
this.from = from;
this.to = to;
this.amount = amount;
return from;
return to;
return amount;
@Override
}}
class Blockchain {
this.difficulty = difficulty;
createGenesisBlock();
genesisBlock.mineBlock(difficulty);
chain.add(genesisBlock);
block.mineBlock(difficulty);
chain.add(block);
if (!currentBlock.getHash().equals(currentBlock.calculateHash()))
return false;
if (!currentBlock.getPreviousHash().equals(previousBlock.getHash()))
return false;
}
return true;
public List<Block>getChain() {
return chain;
}}
class Node {
private List<Transaction>pendingTransactions;
this.blockchain = blockchain;
blockchain.getLastBlock().getIndex() + 1,
System.currentTimeMillis(),
blockchain.getLastBlock().getHash(),pendingTransactions);
blockchain.addBlock(newBlock);
pendingTransactions.clear();
}
pendingTransactions.add(transaction);
return blockchain;
public List<Transaction>getPendingTransactions() {
return pendingTransactions;
}}
node1.createTransaction(transaction1);
node1.minePendingTransactions();
node2.minePendingTransactions();
}}
OUTPUT :
AIM: creating a Crypto-currency Wallet.
2. Sign up for an account, buy the device or download the software needed.
One option is a software wallet or hot wallet that stores your crypto on an
internet-connected device
Another option to consider with added security is a cold wallet, a specialized piece
of hardware that
Custodial wallets, which leave your crypto in the control of a company you trust,
such as a crypto
SOURCE CODE
import java.security.*;
import java.security.spec.ECGenParameterSpec;
generateKeyPair();
try {
keyGen.initialize(ecSpec, random);
privateKey = keyPair.getPrivate();
publicKey = keyPair.getPublic();
} catch (Exception e) {
e.printStackTrace();
}}
}}
OUTPUT :