Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tkn backend #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
54 changes: 54 additions & 0 deletions backend/backend_app/gate_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from flask import Flask, request, jsonify
from web3 import Web3
import json
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

app = Flask(__name__)

# Connect to School Backend
SCHOOL_BACKEND_URL = "http://localhost:5001"

# Connect to local Ethereum blockchain (e.g., Ganache)
w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))

# Load the contract ABI and address
with open("ZKACStorageABI.json") as f:
contract_abi = json.load(f)
contract_address = "YOUR_CONTRACT_ADDRESS"
contract = w3.eth.contract(address=contract_address, abi=contract_abi)

# Gate’s private key for decrypting user data
gate_private_key = RSA.import_key(open("gate_private_key.pem").read())

def verify_proof(a, s, y, p, g):
# Generate the challenge
e = int(SHA256.new((str(a) + str(y)).encode()).hexdigest(), 16)
# Check if g^s % p == a * y^e % p
return pow(g, s, p) == (a * pow(y, e, p)) % p

@app.route('/process_user_entry', methods=['POST'])
def process_user_entry():
data = request.json
encrypted_data = bytes.fromhex(data['encrypted_data'])

# Decrypt data using the gate's private key
cipher = PKCS1_OAEP.new(gate_private_key)
decrypted_data = cipher.decrypt(encrypted_data).decode().split(',')
a, s, y, ID_inv = map(int, decrypted_data)

# Verify the proof
g, p = 2, 23 # Example values for generator and prime
if verify_proof(a, s, y, p, g):
# Call school backend to validate and invalidate public key
response = requests.post(f"{SCHOOL_BACKEND_URL}/invalidate_public_key", json={
"public_key": y,
"invalidation_id": ID_inv
})
return response.json()
else:
return jsonify({"status": "proof_invalid"}), 400

if __name__ == '__main__':
app.run(port=5002)
79 changes: 79 additions & 0 deletions backend/backend_app/school_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from flask import Flask, request, jsonify
from web3 import Web3
import json
import os
app = Flask(__name__)

# Connect to Ganache or Ethereum node

# Connect to the Ethereum network
w3 = Web3(Web3.HTTPProvider('https://eth-sepolia.g.alchemy.com/v2/3LAfFWhjVk6bCEvhaSN-QK14ifGQclgp'))

# Load the contract ABI and address
abi_path = os.path.join(os.path.dirname(__file__), '../smart_contract/zkac_storage_abi.json')
with open(abi_path) as f:
contract_abi = json.load(f)
contract_address = "0xeeccb8dc5c0f0a1d4aefc7aab5cb6dda4c73fbe6"
contract = w3.eth.contract(address=contract_address, abi=contract_abi)

# School address
manager_address = "0xb3ff7fa3992e9325cf47d2af0aca4ba1155b35c7"
private_key = "3b7041f06f948636c3ccc48dcae30b9fd60ac509d5bb71d5386a7448a86122ab"

@app.route('/invalidate_public_key', methods=['POST'])
def invalidate_public_key():
data = request.json
public_key = data['public_key']
invalidation_id = data['invalidation_id']

# Check if public key is valid
is_valid = contract.functions.isPublicKeyValid(public_key).call()
if not is_valid:
return jsonify({"status": "public_key_invalid"}), 400

# Invalidate the public key on blockchain
txn = contract.functions.invalidatePublicKey(public_key).buildTransaction({
'from': manager_address,
'nonce': w3.eth.get_transaction_count(manager_address),
'gas': 2000000,
'gasPrice': w3.toWei('20', 'gwei')
})
signed_txn = w3.eth.account.sign_transaction(txn, private_key=private_key)
txn_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

return jsonify({
"status": "public_key_invalidated",
"txn_hash": txn_hash.hex(),
"public_key": public_key,
"invalidation_id": invalidation_id
})

@app.route('/update_public_key', methods=['POST'])
def update_public_key():
data = request.json
new_public_key = data['new_public_key']
old_public_key = data['old_public_key']
invalidation_id = data['invalidation_id']
signature = data['signature']

# Verify the signature with the old public key
# (Assume we have a function to verify signature)

# Update public key on the blockchain
txn = contract.functions.updatePublicKey(new_public_key).buildTransaction({
'from': manager_address,
'nonce': w3.eth.get_transaction_count(manager_address),
'gas': 2000000,
'gasPrice': w3.toWei('20', 'gwei')
})
signed_txn = w3.eth.account.sign_transaction(txn, private_key=private_key)
txn_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

return jsonify({
"status": "public_key_updated",
"txn_hash": txn_hash.hex(),
"new_public_key": new_public_key
})

if __name__ == '__main__':
app.run(port=5001)
242 changes: 242 additions & 0 deletions backend/smart_contract/zkac_storage_abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
[
{
"inputs": [
{
"internalType": "uint256",
"name": "_publicKey",
"type": "uint256"
}
],
"name": "invalidatePublicKey",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256[]",
"name": "_publicKeys",
"type": "uint256[]"
}
],
"name": "invalidatePublicKeyBatch",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "smartGate",
"type": "address"
},
{
"indexed": false,
"internalType": "bytes",
"name": "invalidationID",
"type": "bytes"
}
],
"name": "InvalidationIDSent",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "_smartGate",
"type": "address"
},
{
"internalType": "bool",
"name": "_add",
"type": "bool"
}
],
"name": "manageSmartGate",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "_smartGates",
"type": "address[]"
},
{
"internalType": "bool",
"name": "_add",
"type": "bool"
}
],
"name": "manageSmartGateBatch",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "publicKey",
"type": "uint256"
},
{
"indexed": false,
"internalType": "address",
"name": "smartGate",
"type": "address"
}
],
"name": "PublicKeyInvalidated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "newPublicKey",
"type": "uint256"
}
],
"name": "PublicKeyUpdated",
"type": "event"
},
{
"inputs": [
{
"internalType": "bytes",
"name": "enc_invalidationID",
"type": "bytes"
}
],
"name": "sendInvalidationID",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes[]",
"name": "enc_invalidationIDs",
"type": "bytes[]"
}
],
"name": "sendInvalidationIDBatch",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_newPublicKey",
"type": "uint256"
}
],
"name": "updatePublicKey",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256[]",
"name": "_newPublicKeys",
"type": "uint256[]"
}
],
"name": "updatePublicKeyBatch",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_publicKey",
"type": "uint256"
}
],
"name": "isPublicKeyValid",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "manager",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "publicKeyValid",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "smartGates",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
}
]