diff --git a/README.md b/README.md new file mode 100644 index 0000000..997fb5f --- /dev/null +++ b/README.md @@ -0,0 +1,117 @@ +# Introduction +This repository contrain Assessment Submission of task given by HogoByte. + +## Blockchain Ledger +This is a simple implementation of a blockchain ledger in C++. It allows you to add transactions, create blocks, validate the ledger, and print the ledger. + +## Prerequisites +To compile and run this code, you need to have the following installed on your system: + +- C++ compiler (supporting C++11 or later) +- OpenSSL library + + +## Compilation +To compile the code, follow these steps: + +1. Clone the repository or download the source code files. +2. Open a terminal or command prompt. +3. Navigate to the directory where the source code files are located. +4. Run the following command to compile the code: + - g++ blockchain.cpp -o program -lssl -lcrypto (This command uses the g++ compiler, sets the C++ version to C++11, and links against the OpenSSL library.) + +## Execution +1. Open a terminal or command prompt. +2. If there are no compilation errors, an executable file named `program` will be generated in the same directory. + Navigate to the directory where the `program` executable is located. + - ./program + +## Usage +Here's a brief description of the available operations: + +1. **Add Transaction**: Allows you to add a transaction to the transaction pool. +2. **Add Block**: Creates a new block with the transactions in the transaction pool and adds it to the blockchain. +3. **Validate Ledger**: Checks the integrity and validity of the blockchain. +4. **Print Ledger**: Displays the entire blockchain with block details and transactions. +5. **Exit**: Quits the program. + +## Example +Starting Blockchain Ledger... + +Enter a number to perform an operation: +1: Add Transaction +2: Add Block +3: Validate Ledger +4: Print Ledger +5: Exit + +1 + +Enter Transaction: Abhishek to Rajat, 50 INR +Transaction Successfully added.. + +Enter a number to perform an operation: +1: Add Transaction +2: Add Block +3: Validate Ledger +4: Print Ledger +5: Exit + +1 + +Enter Transaction: Abhishek to Swapnil, 40 INR +Transaction Successfully added.. + +Enter a number to perform an operation: +1: Add Transaction +2: Add Block +3: Validate Ledger +4: Print Ledger +5: Exit + +2 + +Block successfully added to Blockchain... + +Enter a number to perform an operation: +1: Add Transaction +2: Add Block +3: Validate Ledger +4: Print Ledger +5: Exit + +4 + +Block Index: 1 +Block Hash: 2f64366f6d4433eaf48293313f1f82668afcee78f5f0394042dda4327a20cc8e +Previous Hash: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 +Transactions: + - Abhishek to Swapnil, 40 INR + - Abhishek to Rajat, 50 INR +------------------------ +Block Index: 0 +Block Hash: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 +Previous Hash: +Transactions: + +------------------------- +Enter a number to perform an operation: +1: Add Transaction +2: Add Block +3: Validate Ledger +4: Print Ledger +5: Exit + +3 + +Blockchain is valid. + +Enter a number to perform an operation: +1: Add Transaction +2: Add Block +3: Validate Ledger +4: Print Ledger +5: Exit + + + diff --git a/Solution/block.cpp b/Solution/block.cpp new file mode 100644 index 0000000..0c58724 --- /dev/null +++ b/Solution/block.cpp @@ -0,0 +1,3 @@ +#include "block.h" + +// No additional implementation required for the block structure diff --git a/Solution/block.h b/Solution/block.h new file mode 100644 index 0000000..8805451 --- /dev/null +++ b/Solution/block.h @@ -0,0 +1,17 @@ +#ifndef BLOCK_H +#define BLOCK_H + +#include +#include "transaction.h" + +struct Block { + int index; + std::string blockHash; + std::string previousHash; + TransactionNode* transactions; + Block* previousBlock; + + Block() : transactions(nullptr), previousBlock(nullptr) {} +}; + +#endif // BLOCK_H diff --git a/Solution/blockchain.cpp b/Solution/blockchain.cpp new file mode 100644 index 0000000..9bc775c --- /dev/null +++ b/Solution/blockchain.cpp @@ -0,0 +1,201 @@ +#include "blockchain.h" +#include +#include +#include +#include +#include + +using namespace std; + +Blockchain::Blockchain() { + + //Initialize the transactionPool to nullptr + transactionPool = nullptr; + + // Initialize the chain with a genesis block + chainHead = new Block; + chainHead->index = 0; + chainHead->blockHash = calculateHash(""); + chainHead->previousHash = ""; + chainHead->transactions = nullptr; +} + +Blockchain::~Blockchain() { + // Delete the blockchain chain + Block* currentBlock = chainHead; + while (currentBlock != nullptr) { + TransactionNode* currentTransaction = currentBlock->transactions; + while (currentTransaction != nullptr) { + TransactionNode* nextTransaction = currentTransaction->next; + delete currentTransaction; + currentTransaction = nextTransaction; + } + Block* nextBlock = currentBlock->previousBlock; + delete currentBlock; + currentBlock = nextBlock; + } +} + +void Blockchain::addBlock(TransactionNode* transactions) { + // Create a new block + Block* newBlock = new Block; + + // Concanate all transactions to stringBuilder and calculate Hash + TransactionNode* tempHead = transactions; + string transactionBuilder = ""; + while(tempHead != NULL) { + transactionBuilder += tempHead->transaction; + tempHead = tempHead->next; + } + string blockHash = calculateHash(transactionBuilder); + + // new block have it's own hash and previous block hash + newBlock->blockHash = blockHash; + newBlock->previousHash = chainHead->blockHash; + newBlock->transactions = transactions; + newBlock->index = chainHead->index + 1; + + // Update the chain head + newBlock->previousBlock = chainHead; + chainHead = newBlock; + + // Remove old transactions + transactionPool = nullptr; + cout<<"Block successfully added to Blockchain..."<next = transactionPool; + transactionPool = newTransaction; + cout<<"Transaction Successfully added.."<previousBlock != nullptr) { + //calculate hash of previous block + TransactionNode* transactionhead = currentBlock->previousBlock->transactions; + string transactions = ""; + while(transactionhead != NULL) { + transactions += transactionhead->transaction; + transactionhead = transactionhead->next; + } + + string prevBlockHash = calculateHash(transactions); + + if (currentBlock->previousHash != prevBlockHash) { + return false; // Invalid chain + } + currentBlock = currentBlock->previousBlock; + } + + return true; // return true if Valid chain +} + + + + +void Blockchain::printChain() const { + Block* currentBlock = chainHead; + while (currentBlock != nullptr) { + cout << "Block Index: " << currentBlock->index << endl; + cout << "Block Hash: " << currentBlock->blockHash << endl; + cout << "Previous Hash: " << currentBlock->previousHash << endl; + + cout << "Transactions:" << endl; + TransactionNode* currentTransaction = currentBlock->transactions; + while (currentTransaction != nullptr) { + cout << " - " << currentTransaction->transaction << endl; + currentTransaction = currentTransaction->next; + } + + cout << "------------------------" << endl; + cout<previousBlock; + } +} + + + +string Blockchain::calculateHash(const string& data) const { + EVP_MD_CTX* mdContext = EVP_MD_CTX_new(); + + EVP_DigestInit(mdContext, EVP_sha256()); + EVP_DigestUpdate(mdContext, data.c_str(), data.length()); + + unsigned char hash[EVP_MAX_MD_SIZE]; + unsigned int hashLength; + EVP_DigestFinal(mdContext, hash, &hashLength); + + EVP_MD_CTX_free(mdContext); + + stringstream ss; + ss << hex << setfill('0'); + for (unsigned int i = 0; i < hashLength; ++i) { + ss << setw(2) << static_cast(hash[i]); + } + + return ss.str(); +} + + + +//main function +int main() { + cout << "Starting Blockchain Ledger..." << endl; + Blockchain blockchain; + + while (true) { + int inputNumber; + cout << "Enter a number to perform an operation:" << endl; + cout << "1: Add Transaction" << endl; + cout << "2: Add Block" << endl; + cout << "3: Validate Ledger" << endl; + cout << "4: Print Ledger" << endl; + cout << "5: Exit" << endl; + cin >> inputNumber; + cout< +#include "block.h" +#include "transaction.h" + +class Blockchain { +private: + Block* chainHead; +public: + TransactionNode* transactionPool; + Blockchain(); + ~Blockchain(); + void addBlock(TransactionNode* transactions); + void addTransaction(const std::string& transaction); + bool validateChain() const; + void printChain() const; + +private: + std::string calculateHash(const std::string& data) const; +}; + +#endif // BLOCKCHAIN_H diff --git a/Solution/financial_transactions_security.md b/Solution/financial_transactions_security.md new file mode 100644 index 0000000..d6c2df5 --- /dev/null +++ b/Solution/financial_transactions_security.md @@ -0,0 +1,34 @@ +# Introduction +Blockchain technology has gained significant attention in recent years, particularly in the realm of financial transactions. This report aims to discuss the security implications of utilizing a blockchain for financial transactions. We will explore the key security considerations and benefits offered by blockchain technology in this context. + +## 1. Immutable Transaction History +One of the fundamental security benefits of a blockchain is its immutability. Once a transaction is added to the blockchain, it becomes extremely difficult to alter or tamper with. This property ensures the integrity of financial transactions, reducing the risk of fraudulent activities and unauthorized modifications. + +## 2. Distributed Consensus +Blockchain relies on distributed consensus mechanisms, such as proof-of-work or proof-of-stake, to validate and verify transactions. This decentralized approach eliminates the need for a central authority, reducing the vulnerability to single points of failure and potential attacks. Consensus mechanisms ensure that a majority of participants in the network agree on the validity of transactions, making it difficult for malicious actors to manipulate the system. + +## 3. Cryptographic Security +Blockchain utilizes strong cryptographic algorithms to secure transactions. Transactions are digitally signed, ensuring the authenticity and integrity of the involved parties. Cryptographic hashing algorithms, such as SHA-256, are used to create unique transaction identifiers, providing a high level of data integrity and preventing data manipulation. + +## 4. Transparency and Auditability +Blockchain's transparent nature allows all participants to view and verify transactions. This transparency enhances accountability and helps detect and prevent fraudulent activities. Additionally, the auditability of blockchain enables easy tracing and tracking of transactions, contributing to regulatory compliance and reducing the risk of money laundering and other illicit activities. + +## 5. Smart Contract Security +Smart contracts, executable code stored on the blockchain, enable the automation and enforcement of financial agreements. However, they introduce their own security considerations. Flaws in smart contracts can lead to vulnerabilities and potential exploits. Thorough code audits, formal verification techniques, and adherence to best practices are essential to mitigate these risks. + +## 6. Network Security +The security of a blockchain network depends on the underlying consensus mechanism and the robustness of its nodes. Proof-of-work mechanisms, such as those used in Bitcoin, require significant computational power to launch a successful attack. However, alternative consensus mechanisms, such as proof-of-stake or delegated proof-of-stake, introduce different security considerations that need to be carefully evaluated. + +## 7. External Security Threats +While blockchain technology itself provides strong security measures, it does not eliminate the risk of external threats. Users' private keys, used to sign transactions, must be securely stored to prevent unauthorized access. Malware, phishing attacks, and social engineering are potential risks that can compromise the security of blockchain-based financial transactions. Educating users about these risks and promoting secure practices is crucial. + +# Conclusion +Utilizing a blockchain for financial transactions offers several security advantages over traditional centralized systems. Its immutability, distributed consensus, cryptographic security, transparency, and auditability contribute to enhancing the integrity, accountability, and trustworthiness of financial transactions. However, it is important to acknowledge that blockchain technology is not immune to all security risks, and additional measures, such as secure key management and user awareness, are essential for a holistic security approach. + +By understanding the security implications and taking appropriate precautions, blockchain technology can play a pivotal role in revolutionizing the security and efficiency of financial transactions, leading to increased trust, reduced fraud, and enhanced financial inclusion. + +## References +- https://consensys.net/blockchain-use-cases/finance/ +- https://www.salesforce.com/eu/blog/2020/02/how-financial-services-are-implementing-blockchain-technology.html +- https://www.leewayhertz.com/10-use-cases-of-blockchain-in-finance/ + diff --git a/Solution/program b/Solution/program new file mode 100755 index 0000000..7888492 Binary files /dev/null and b/Solution/program differ diff --git a/Solution/transaction.cpp b/Solution/transaction.cpp new file mode 100644 index 0000000..69965fc --- /dev/null +++ b/Solution/transaction.cpp @@ -0,0 +1,3 @@ +#include "transaction.h" + +// No additional implementation required for the transaction structure diff --git a/Solution/transaction.h b/Solution/transaction.h new file mode 100644 index 0000000..ee2b401 --- /dev/null +++ b/Solution/transaction.h @@ -0,0 +1,13 @@ +#ifndef TRANSACTION_H +#define TRANSACTION_H + +#include + +struct TransactionNode { + std::string transaction; + TransactionNode* next; + + TransactionNode(const std::string& transaction) : transaction(transaction), next(nullptr) {} +}; + +#endif // TRANSACTION_H