Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Ajay Kumar Block Chain/block.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "block.h"

Block::Block(std::string _preBlockHash,std::list<Transaction> _transactionList)
{
this->preBlockHash = _preBlockHash;
this->transactionList = _transactionList;
this->blockHash= generateHash();
}

std::string Block::generateHash()
{
std::string message = preBlockHash;

// Fetching transaction data
for(Transaction transaction: transactionList)
{
message += transaction.toString();
}

// Hashing the message

std::hash<std::string> hasher;

return std::to_string(hasher(message));
}

std::string Block::getHash()
{
return (std::string)this->blockHash;
}

std::string Block::getPreHash()
{
return this->preBlockHash;
}

std::list<Transaction> Block::getTransactionList()
{
return this->transactionList;
}
30 changes: 30 additions & 0 deletions Ajay Kumar Block Chain/block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef BLOCK_H
#define BLOCK_H
#include <iostream>
// #include <openssl/sha.h>
#include <list>
#include <iomanip>
#include "transaction.h"

class Block
{
private:
std::string blockHash;
std::string preBlockHash;
std::list<Transaction> transactionList; // linked list representation

public:

// Constructor
Block(std::string _preBlockHash,std::list<Transaction> _transactionList);

// SHA256 Hashing
std::string generateHash();

// Getter methods
std::string getHash();
std::string getPreHash();
std::list<Transaction> getTransactionList();
};

#endif
83 changes: 83 additions & 0 deletions Ajay Kumar Block Chain/blockchain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "blockchain.h"

void BlockChain::createGenisisBlock()
{
// Genesis block dummy transaction data
std::list<Transaction> transactionList =
{
// senderId, receiverId, amount
Transaction("Genisis", "Genisis", 0)
};

// previous block hash, current transactions
chain.push_back(Block("0", transactionList));
}

BlockChain::BlockChain()
{
createGenisisBlock();
}

void BlockChain::addBlock(std::list<Transaction> transaction)
{
chain.push_back(Block(getLatestBlock().getHash(), transaction));
}

std::vector<Block> BlockChain::getChain()
{
return chain;
}

Block BlockChain::getLatestBlock()
{
return chain[chain.size()-1];
}

int BlockChain::isChainValid()
{
if(chain[0].getHash() != chain[0].generateHash())
{
return 0;
}

for(int i=1; i<chain.size(); i++)
{
if(chain[i].getHash() != chain[i].generateHash() ||
chain[i-1].getHash() != chain[i].getPreHash())
{
return -i;
}
}
return 1;
}

void BlockChain::printChain()
{
for(int i=0; i<chain.size(); i++)
{
Block block = chain[i];
std::cout<<"\n\nBlock ################################################\n";
std::cout<<"Transactions:-\n--------------\n";

for(Transaction t: block.getTransactionList()){
std::cout<<t.toString()<<std::endl;
}

std::cout<<"---------------------------------------\n";
std::cout<<"Block Hash Stored: "<<block.getHash()<<std::endl;
std::cout<<"Previous Hash: "<<block.getPreHash()<<"\n\n";
}
}

void BlockChain::validationReport()
{
int index = isChainValid();
switch(index)
{
case 1: std::cout<<"Chain is Valid\n";
break;
case 0: std::cout<<"Genesis Block is Tampered!\n";
break;
default: std::cout<<"Block "<<abs(index)<<" is Tampered!\n";
}
}
36 changes: 36 additions & 0 deletions Ajay Kumar Block Chain/blockchain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef BLOCKCHAIN_H
#define BLOCKCHAIN_H

#include <iostream>
#include <vector>
#include <list>
#include "block.h"
#include "transaction.h"

class BlockChain{

private:

std::vector<Block> chain;
void createGenisisBlock();

public:
// constructor
BlockChain();

// new block insertion
void addBlock(std::list<Transaction> transaction);

// getters
std::vector<Block> getChain();
Block getLatestBlock();

// validation
int isChainValid();
void validationReport();

// printing
void printChain();
};

#endif
61 changes: 61 additions & 0 deletions Ajay Kumar Block Chain/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include<iostream>
#include<list>
#include "transaction.h"
#include "block.h"
#include "blockchain.h"

int main(){

BlockChain blockChain;

// New list of transactions
std::list<Transaction> transaction1=
{
Transaction("User1","User2",2000),
Transaction("User2","User3",1500),
Transaction("User3","User4",2700)
};

// Add new Block
blockChain.addBlock(transaction1);

// another list
std::list<Transaction> transaction2=
{
Transaction("User1","User2",4000),
Transaction("User2","User3",8600),
Transaction("User3","User4",7300)
};
blockChain.addBlock(transaction2);

std::list<Transaction> transaction3=
{
Transaction("User4","User5",8000),
Transaction("User4","User6",2500),
Transaction("User4","User7",6300)
};

std::list<Transaction> transaction4=
{
Transaction("User1","User2",6000),
Transaction("User2","User3",3500),
Transaction("User3","User4",4500)
};

std::list<Transaction> transaction5=
{
Transaction("User1","User2",1000),
Transaction("User2","User3",9000),
Transaction("User3","User4",2300)
};

// adding blocks
blockChain.addBlock(transaction3);
blockChain.addBlock(transaction4);
blockChain.addBlock(transaction5);

blockChain.printChain();
blockChain.validationReport();

return 0;
}
Binary file added Ajay Kumar Block Chain/main.exe
Binary file not shown.
17 changes: 17 additions & 0 deletions Ajay Kumar Block Chain/transaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "transaction.h"

Transaction::Transaction(){}

Transaction::Transaction(std::string senderKey, std::string receiverKey, double amount)
{
this->senderKey = senderKey;
this->receiverKey = receiverKey;
this->amount = amount;
this->timestamp = time(nullptr);
}

std::string Transaction::toString()
{
return senderKey +" "+ receiverKey+" "+
std::to_string(amount)+" "+ std::to_string(timestamp);
}
26 changes: 26 additions & 0 deletions Ajay Kumar Block Chain/transaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef TRANSACTION_H
#define TRANSACTION_H

#include<string>
#include<iostream>
#include<ctime>

class Transaction
{
private:
std::string senderKey;
std::string receiverKey;
double amount;
time_t timestamp;

public:

// Constructors
Transaction();
Transaction(std::string _senderKey, std::string _receiverKey, double _amount);

// convert transaction data to string (for hashing purpose)
std::string toString();
};

#endif
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Block Chain
A blockchain is a decentralized and immutable digital ledger that records transactions across multiple computers. It ensures transparency, security, and trust in financial transactions by linking blocks of data together in a sequential and cryptographically secure manner.

# Instructions to compile and run the code

This project is written in C++ and requires Mingw C/C++ compiler to compile and run the code.

## Installation

1. Downlode and install MinGW compiler from the internet
2. Add `C:\MinGW\bin` path to the system environment variable `PATH`

## Building and Running the Project

1. Open the command prompt (CMD) or terminal.
2. Navigate to the project folder using the `cd` command.
For Example: `cd /d "D:\Ajay Kumar Block Chain"`
3. To compile the code, run the following command:`g++ -o main main.cpp block.cpp blockChain.cpp transaction.cpp`
4. Once the compilation is successful, you can run the project by executing the generated executable `main` file.
27 changes: 27 additions & 0 deletions Security Implication-Report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Security Implications of Using Blockchain for Financial Transaction

Blockchain technology has gained significant attention for its potential to revolutionize financial transactions. However, it is essential to consider the security implications associated with its implementation. This report explores key security considerations when utilizing blockchain for financial transactions.

## Data Integrity and Immutability

One of the fundamental security benefits of blockchain technology is its ability to ensure data integrity and immutability. Once a transaction is recorded on the blockchain, it becomes nearly impossible to modify or tamper with the data. This feature mitigates the risk of fraudulent activities, unauthorized alterations, and data tampering, providing a reliable and trustworthy record of financial transactions. Furthermore, the provided code incorporates the use of timestamps for each transaction, thereby enhancing the uniqueness of the resulting hash values.

## Distributed Consensus Mechanisms

Blockchain networks rely on distributed consensus mechanisms to validate and confirm transactions. Consensus algorithms, such as Proof of Work (PoW) or Proof of Stake (PoS), ensure agreement among network participants on the validity of transactions. However, it's crucial to evaluate the security implications of the chosen consensus mechanism, as vulnerabilities or attacks targeting the consensus algorithm can compromise the integrity of financial transactions.

## Smart Contract Security

Smart contracts automate and enforce the terms of financial transactions on the blockchain. While they offer efficiency and transparency, they can introduce security risks. Vulnerabilities in smart contract code can lead to financial losses or unauthorized access. Thorough code audits, comprehensive testing, and adherence to secure coding practices are necessary to mitigate these risks and ensure the integrity of financial transactions.

## Privacy and Confidentiality

Privacy considerations arise when using blockchain for financial transactions. While blockchain provides transparency by design, protecting sensitive financial information is crucial. Techniques like encryption and zero-knowledge proofs can enhance privacy; however, finding the right balance between transparency and confidentiality remains a challenge. Organizations must carefully evaluate privacy requirements and implement appropriate measures to safeguard sensitive financial data.

## Regulatory Compliance

Integrating blockchain into financial transactions raises regulatory and compliance challenges. Compliance with regulations like anti-money laundering (AML) and know-your-customer (KYC) can be complex in a decentralized environment. Ensuring compliance while maintaining privacy and security requires collaboration with regulatory bodies, development of industry standards, and implementation of robust monitoring and reporting mechanisms.

# Conclusion

In conclusion, leveraging blockchain for financial transactions offers numerous security advantages. However, it is essential to address the potential security implications in areas such as data integrity, consensus mechanisms, smart contract security, privacy, and regulatory compliance. By implementing appropriate security measures and best practices, organizations can enhance the security and trustworthiness of financial transactions in the blockchain ecosystem.