This project aims to develop a system for Contract Management with verification using HMAC(Hash-based Message Authentication Code).
- Python
- Introduction
- How to Use
- main.py file
- Classes(Files)
- Class_Functions
- Steps to Run the Code
- Usage Example
This system consists of three main classes: CONTRACT, BLOCK, and BLOCKCHAIN.
CONTRACTrepresents a contract, storing its ID, amount, issuer ID, and user ID. It also handles key generation, challenge creation, HMAC creation, and verification.BLOCKrepresents a block in the blockchain, containing a list of transactions, index, timestamp, and previous block's hash.BLOCKCHAINmanages the blockchain, including creating blocks, adding transactions, verifying transactions, and ensuring the validity of the blockchain.
- Creating Contracts: Users can create contracts by providing contract ID, amount, issuer ID, and user ID. The system automatically generates a private key for each contract.
- Adding Transactions: Users can add transactions to the blockchain, verifying them using HMAC. Each block can contain up to a specified number of transactions (TRANSACTION_LIMIT).
- Verification: Users can verify transactions against their respective contracts using HMAC.
- Viewing Data: Users can print the entire blockchain, view transactions of a specific user, check the total number of contracts, and see the total number of unique users.
- Checking Validity: Users can check if the blockchain is valid.
Functions:
main(): The main function of the program which controls the flow of execution.BLOCKCHAIN(diff): A constructor for theBLOCKCHAINclass which initializes the blockchain with the specified parameter.verifyTransaction(tx, contract_id, user_id, issuer_id, amount): Verifies if a transaction is valid.addTransaction(tx): Adds a new transaction to the blockchain.printChain(): Prints the entire blockchain.viewUser(user_id): Prints all transactions related to a specific user.getNoOfContracts(): Prints the total no. of contracts and the sum of amounts across all these contracts.getTotalUsers(): Prints the total number of unique users.isChainValid(): Checks if the blockchain is valid.
Variables:
diff: Difficulty used for mining blocks.count: The number of transactions the user wants to add to the blockchain.i: A counter variable used in theforloop for adding transactions.tx: A vector ofCONTRACTobjects representing the transactions added to the blockchain.contract_id: Unique ID for a contract(string).amount: The amount of money being transferred in a contract(double).issuer_id: An identifier for every issuer of a contract(string).user_id: An identifier for every user in the blockchain(string).tx_temp: ACONTRACTobject used to store a transaction before it is added to the blockchain.control: An integer variable used to control the switch case in the program.
- CONTRACT: Represents a contract and provides methods for key generation, challenge creation, HMAC creation, and verification.
- BLOCK: Represents a block in the blockchain and calculates its hash.
- BLOCKCHAIN: Manages the blockchain, creates blocks, adds transactions, verifies transactions, and ensures blockchain validity.
__init__(self, c_id, amt, i_id, u_id): Constructor method for the CONTRACT class.make_key(self, c_id, amt, i_id, u_id): Generates a private key for a contract.generate_challenge(self): Generates a challenge for a transaction.set_rand_bit(self, bit): Sets a random bit for a transaction.create_hmac(self): Creates HMAC for a transaction.doSomeHash(self, data): Performs hashing for key generation.
__init__(self, idx, prevHash): Constructor method for the BLOCK class.calculateHash(self): Calculates hash for a block.
__init__(self, diff): Constructor method for the BLOCKCHAIN class.getNoOfContracts(self): Prints the total number of contracts present.getTotalUsers(self): Prints the total number of unique users.generate_basic_hash(self, data): Generates a basic hash for blockchain.calculateHash(self, block): Calculates hash for a block.mineBlock(self, newBlock, difficulty): Mines a block with a specified difficulty.createBlock(self): Creates a new block if the current block reaches the transaction limit.addTransaction(self, tx): Adds a transaction to the blockchain.verifyTransaction(self, tx, c_id, u_id, i_id, amount): Verifies a transaction using HMAC.isChainValid(self): Checks if the blockchain is valid.viewUser(self, user_id): Views transactions of a specific user.printChain(self): Prints the entire blockchain.
- Ensure you have Python installed on your system.
- Download the provided Python files to your local machine.
- Open a terminal or command prompt and navigate to the directory where the files are located.
- Run the main.py file by executing the command:
python main.py. - Follow the on-screen instructions to interact with the program.
# Example usage
blockchain = BLOCKCHAIN(4) # Initialize blockchain with difficulty 4
# Add transactions to the blockchain
contract_id = "1"
amount = 100.0
issuer_ID = "abc12"
user_id = "xyz24"