-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create blockchain_certificate_verification.py
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...ork_university/blockchain_certificate_verification/blockchain_certificate_verification.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import hashlib | ||
import json | ||
from web3 import Web3 | ||
|
||
# Set up blockchain connection | ||
w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/YOUR_PROJECT_ID')) | ||
|
||
# Define certificate verification function | ||
def verify_certificate(certificate_data): | ||
# Hash certificate data | ||
certificate_hash = hashlib.sha256(json.dumps(certificate_data).encode()).hexdigest() | ||
|
||
# Check if certificate exists on blockchain | ||
if w3.eth.get_transaction_count(certificate_hash) > 0: | ||
return True | ||
else: | ||
return False | ||
|
||
# Define smart contract functions | ||
def deploy_certificate_smart_contract(): | ||
# Compile and deploy smart contract | ||
contract = w3.eth.compile_source('smart_contracts/CertificateVerification.sol') | ||
tx_hash = w3.eth.send_transaction({'from': '0x...', 'gas': 200000, 'gasPrice': w3.eth.gas_price, 'data': contract['bytecode']}) | ||
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash) | ||
contract_address = tx_receipt['contractAddress'] | ||
return contract_address | ||
|
||
def issue_certificate(student_id, course_id): | ||
# Issue certificate using smart contract | ||
contract_address = deploy_certificate_smart_contract() | ||
contract = w3.eth.contract(address=contract_address, abi=contract['abi']) | ||
tx_hash = contract.functions.issueCertificate(student_id, course_id).transact({'from': '0x...', 'gas': 200000, 'gasPrice': |