Exp-5 200
Exp-5 200
CO Outcome
CO1 Describe the basic concepts of Blockchain and Distributed Ledger Technology.
2. https://ideausher.com/blog/how-to-make-a-private-blockchain/
3. https://youtu.be/mYlHT9bB6OE
4. https://www.smashingmagazine.com/2020/02/cryptocurrency-blockchain-node-js/
Abstract:
Centralized ledger: A centralized ledger is a database administered and controlled by a single entity, such as a business, government body, or
financial institution. All data is stored in a single location in a centralized ledger system, and access to that data is controlled by authorized individuals
or institutions.
Cryptocurrency: Cryptocurrency, sometimes called crypto-currency or crypto, is any form of currency that exists digitally or virtually and uses
cryptography to secure transactions. Cryptocurrencies don't have a central issuing or regulating authority, instead using a decentralized system to
Types of cryptocurrencies:
1. Binance Coin (BNB): Binance Coin is native to Binance, the world’s largest cryptocurrency exchange as of 2024. Transaction fees for
this exchange are reduced for users who choose to pay in BNB. This has encouraged the adoption of Binance Coin, making it one of the
largest crypto coins in the market. To ensure its value remains stable, Binance destroys or “burns” a fixed percentage of the coins in
circulation.
2. Tether (USDT): Tether is a type of stablecoin, designed to have a less-volatile price by being linked to an external asset. In this case, each
coin is backed by an equivalent number of US dollars, which keeps it from experiencing the same kind of pricing volatility that other
cryptocurrencies face. There is however, some debate about whether it truly is fully backed by the dollar.
3. Solana (SOL): SOL is the native coin of the Solana platform, which functions on a blockchain system, just like Ethereum and Bitcoin.
Solana’s network can perform a whopping 50,000 transactions per second, making this platform especially attractive to investors looking
to trade quickly.
Related Theory: -
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 1
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
Coins: A coin is any cryptocurrency that uses its own independent blockchain. For example, Bitcoin is considered a “coin” because it runs on its own
What Are Coins Used for? When Bitcoin was created, it was envisioned as a replacement for traditional fiat currencies. Along with other crypto
coins, it was designed to work in the same ways as paper money and metal coins, meaning it can be used for many of the things normally used with US
Storing value
Exchanging for other currencies
Paying for goods and services
Transferring to others
Tokens: Like coins, tokens are also digital assets that can be bought and sold. However, tokens are a non-native asset, meaning that they use another
blockchain’s infrastructure. These include Tether, which is hosted on the Ethereum blockchain, and others, including Chainlink, Uniswap, and
Polygon.
What Are Tokens Used For? Most crypto tokens are designed to be used within a blockchain project or decentralized app (dapp). Unlike crypto
coins, tokens aren’t mined; they are created and distributed by the project developer. Once tokens are in the hands of purchasers, they can be used in
countless ways.
UTXO Model: An unspent transaction output (UTXO) is the amount of digital currency that remains after a cryptocurrency transaction. You can think
of it as the change you receive after buying an item, but it is not a lower denomination of the currency—it is a transaction output in the database
Geth (go-ethereum): It is a Go implementation of Ethereum - a gateway into the decentralized web. Geth has been a core part of Ethereum since the
very beginning. Geth was one of the original Ethereum implementations making it the most battle-hardened and tested client. Geth is an Ethereum
execution client meaning it handles transactions, deployment and execution of smart contracts and contains an embedded computer known as the
Ethereum Virtual Machine. Running Geth alongside a consensus client turns a computer into an Ethereum node.
Implementation:
import hashlib
import json
import random
import string
import rsa
import base64
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 2
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
def hash_data(data):
return hashlib.sha256(data.encode('utf-8')).hexdigest()
def generate_transaction_id():
try:
return True
except:
return False
# Transaction class
class Transaction:
self.transaction_id = transaction_id
self.sender = sender
self.receiver = receiver
self.value = value
self.signature = signature
def to_dict(self):
return {
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 3
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
'transaction_id': self.transaction_id,
'sender': self.sender,
'receiver': self.receiver,
'value': self.value,
# Block class
class Block:
self.index = index
self.transactions = transactions
self.previous_hash = previous_hash
self.nonce = 0
self.hash = self.compute_hash()
def compute_hash(self):
block_dict = self.__dict__.copy()
return hash_data(block_string)
self.nonce += 1
self.hash = self.compute_hash()
# Blockchain class
class Blockchain:
def __init__(self):
self.chain = []
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 4
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
self.transaction_cost = 5
self.create_genesis_block()
def create_genesis_block(self):
self.chain.append(genesis_block)
previous_block = self.chain[-1]
self.chain.append(new_block)
self.chain[i].hash = self.chain[i].compute_hash()
def get_chain(self):
chain_data = []
block_dict = block.__dict__.copy()
chain_data.append(block_dict)
return chain_data
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 5
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
self.utxos[transaction.sender] = 100
self.utxos[transaction.receiver] = 100
sender_utxos = self.utxos[transaction.sender]
self.utxos[transaction.receiver] += transaction.value
return True
return False
if self.update_utxos(transaction):
self.chain[-1].transactions.append(transaction)
self.update_chain_hashes(len(self.chain) - 1)
return True
return False
def get_utxos(self):
return self.utxos
app = Flask(__name__)
blockchain = Blockchain()
@app.route('/')
def index():
@app.route('/add_transaction', methods=['POST'])
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 6
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
def add_transaction():
block_index = int(request.form['block_index'])
transaction_id = generate_transaction_id()
sender = request.form['sender']
receiver = request.form['receiver']
value = float(request.form['value'])
# RSA key generation for signing (this would be done securely in production)
if blockchain.add_transaction(new_transaction, private_key):
return jsonify(success=True)
else:
else:
@app.route('/create_block', methods=['POST'])
def create_block():
blockchain.add_block([])
return jsonify(success=True)
@app.route('/get_chain')
def get_chain():
return jsonify(chain=blockchain.get_chain())
@app.route('/get_utxos')
def get_utxos():
return jsonify(utxos=blockchain.get_utxos())
@app.route('/get_balances')
def get_balances():
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 7
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
return jsonify(balances=blockchain.get_utxos())
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blockchain Simulation</title>
<style>
body {
background-color: #f8f9fa;
padding-top: 2rem;
.block {
background-color: #ffffff;
border-radius: 0.5rem;
margin-bottom: 1.5rem;
.block:hover {
transform: translateY(-5px);
.block-header {
background-color: #007bff;
color: white;
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 8
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
padding: 0.75rem;
border-top-left-radius: 0.5rem;
border-top-right-radius: 0.5rem;
.block-content {
padding: 1rem;
.transaction-list {
max-height: 200px;
overflow-y: auto;
.hash-value {
word-break: break-all;
font-size: 0.8rem;
.balances {
background-color: #28a745;
color: white;
padding: 1rem;
border-radius: 0.5rem;
margin-bottom: 1.5rem;
.input-group {
margin-bottom: 1rem;
.btn-primary, .btn-success {
margin-top: 1rem;
</style>
</head>
<body>
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 9
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
<div class="container">
<h3>User Balances</h3>
<div id="balance-list"></div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
function createBlock() {
.then(data => {
if (data.success) {
renderBlockchain();
renderBalances();
});
function addTransaction(blockIndex) {
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 10
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
fetch('/add_transaction', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `block_index=${blockIndex}&sender=${sender}&receiver=${receiver}&value=${value}`
})
.then(data => {
if (data.success) {
renderBlockchain();
renderBalances();
} else {
});
function renderBlockchain() {
fetch('/get_chain')
.then(data => {
blockchain = data.chain;
blockchainContainer.innerHTML = '';
blockElement.innerHTML = `
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 11
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
<div class="block">
<div class="block-header">
</div>
<div class="block-content">
<p><strong>Nonce:</strong> ${block.nonce}</p>
<h5>Transactions</h5>
${block.transactions.map((tx) => `
<div class="mb-2">
</div>
`).join('')}
</div>
${blockIndex > 0 ? `
</div>
Transaction</button>
` : ''}
</div>
</div>
`;
blockchainContainer.appendChild(blockElement);
});
});
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 12
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
function renderBalances() {
fetch('/get_balances')
.then(data => {
balanceListContainer.innerHTML = '';
});
document.addEventListener('DOMContentLoaded', () => {
renderBlockchain();
renderBalances();
});
</script>
</body>
</html>
Screenshots:
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 13
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 14
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 15
K. J. Somaiya College of Engineering, Mumbai-77
Department of Computer Engineering
Conclusion:
Successfully implemented a private block of cryptocurrency by ensuring each new block is connected to the previous block, forming a secure and
continuous cryptocurrency along with the UTXO model and the transaction fees deducting from the sender side.
Department of Computer Engineering BCT Sem VII – July - Nov 2024 Page - 16