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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gzip
|
| 2 |
+
import blockcypher
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
+
import os
|
| 5 |
+
import hashlib
|
| 6 |
+
import binascii
|
| 7 |
+
import uuid
|
| 8 |
+
import datetime
|
| 9 |
+
import base64
|
| 10 |
+
from enum import Enum
|
| 11 |
+
from cryptography.hazmat.primitives import hashes, serialization
|
| 12 |
+
from cryptography.hazmat.primitives.asymmetric import ec
|
| 13 |
+
from cryptography.hazmat.backends import default_backend
|
| 14 |
+
from web3 import Web3
|
| 15 |
+
from solcx import compile_source
|
| 16 |
+
|
| 17 |
+
app = Flask(__name__)
|
| 18 |
+
|
| 19 |
+
# Blockchain and Smart Contract setup
|
| 20 |
+
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))
|
| 21 |
+
w3.eth.default_account = w3.eth.accounts[0]
|
| 22 |
+
|
| 23 |
+
# Solidity source code
|
| 24 |
+
contract_source_code = '''
|
| 25 |
+
// SPDX-License-Identifier: MIT
|
| 26 |
+
pragma solidity ^0.8.0;
|
| 27 |
+
|
| 28 |
+
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
| 29 |
+
|
| 30 |
+
contract JBO3Token is ERC20 {
|
| 31 |
+
constructor(uint256 initialSupply) ERC20("JBO3Token", "JBO3") {
|
| 32 |
+
_mint(msg.sender, initialSupply);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
function mint(address to, uint256 amount) public {
|
| 36 |
+
_mint(to, amount);
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
'''
|
| 40 |
+
|
| 41 |
+
# Compile the contract
|
| 42 |
+
compiled_sol = compile_source(contract_source_code)
|
| 43 |
+
contract_interface = compiled_sol['<stdin>:JBO3Token']
|
| 44 |
+
|
| 45 |
+
# Deploy the contract
|
| 46 |
+
JBO3 = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
|
| 47 |
+
tx_hash = JBO3.constructor(1000000).transact()
|
| 48 |
+
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
|
| 49 |
+
contract_address = tx_receipt.contractAddress
|
| 50 |
+
jbo3_contract = w3.eth.contract(address=contract_address, abi=contract_interface['abi'])
|
| 51 |
+
|
| 52 |
+
class Lap(Enum):
|
| 53 |
+
PUBLIC_KEY = 1
|
| 54 |
+
PRIVATE_KEY = 2
|
| 55 |
+
ECDSA = 3
|
| 56 |
+
|
| 57 |
+
class CompressionAlgorithm(Enum):
|
| 58 |
+
NONE = 1
|
| 59 |
+
GZIP = 2
|
| 60 |
+
|
| 61 |
+
class StorageTimeline(Enum):
|
| 62 |
+
PAST = 1
|
| 63 |
+
PRESENT = 2
|
| 64 |
+
FUTURE = 3
|
| 65 |
+
|
| 66 |
+
class Blockchain:
|
| 67 |
+
def store_data(self, data):
|
| 68 |
+
try:
|
| 69 |
+
data_str = str(data)
|
| 70 |
+
data_hash = hashlib.sha3_256(data_str.encode()).hexdigest()
|
| 71 |
+
tx_hash = blockcypher.embed_data_to_blockchain(data_hash, coin_symbol='btc-testnet')
|
| 72 |
+
return "Storing data in the blockchain: " + tx_hash
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return "Error while storing data in the blockchain: " + str(e)
|
| 75 |
+
|
| 76 |
+
class SmartCard:
|
| 77 |
+
def store_data(self, data):
|
| 78 |
+
try:
|
| 79 |
+
return "Storing data on the smart card: " + str(data)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return "Error while storing data on the smart card: " + str(e)
|
| 82 |
+
|
| 83 |
+
def compress_data(data, algorithm):
|
| 84 |
+
if algorithm == CompressionAlgorithm.GZIP:
|
| 85 |
+
compressed_data = gzip.compress(data)
|
| 86 |
+
return compressed_data
|
| 87 |
+
else:
|
| 88 |
+
return data
|
| 89 |
+
|
| 90 |
+
@app.route('/process_files', methods=['POST'])
|
| 91 |
+
def process_files():
|
| 92 |
+
file_paths = request.json.get('file_paths')
|
| 93 |
+
compression_algorithm = CompressionAlgorithm(request.json.get('compression_algorithm'))
|
| 94 |
+
storage_timeline = StorageTimeline(request.json.get('storage_timeline'))
|
| 95 |
+
blockchain = Blockchain()
|
| 96 |
+
smart_card = SmartCard()
|
| 97 |
+
results = []
|
| 98 |
+
|
| 99 |
+
for path in file_paths:
|
| 100 |
+
try:
|
| 101 |
+
with open(path, 'rb') as file:
|
| 102 |
+
data = file.read()
|
| 103 |
+
except FileNotFoundError:
|
| 104 |
+
results.append(f"Error: File {path} not found.")
|
| 105 |
+
continue
|
| 106 |
+
except Exception as e:
|
| 107 |
+
results.append(f"Error reading file {path}: {str(e)}")
|
| 108 |
+
continue
|
| 109 |
+
|
| 110 |
+
compressed_data = compress_data(data, compression_algorithm)
|
| 111 |
+
|
| 112 |
+
for lap in Lap:
|
| 113 |
+
dk = hashlib.pbkdf2_hmac('sha256', compressed_data, b'salt', 100000)
|
| 114 |
+
hashed_data = binascii.hexlify(dk).decode()
|
| 115 |
+
new_uuid = str(uuid.uuid4())
|
| 116 |
+
timestamp = datetime.datetime.now()
|
| 117 |
+
serialized_data = base64.b64encode(compressed_data)
|
| 118 |
+
bitmap = list(map(int, bin(int.from_bytes(data, byteorder='big'))[2:]))
|
| 119 |
+
smart_store = {'uuid': new_uuid, 'timestamp': timestamp, 'hash': hashed_data, 'bitmap': bitmap}
|
| 120 |
+
|
| 121 |
+
if lap == Lap.PUBLIC_KEY:
|
| 122 |
+
private_key = ec.generate_private_key(ec.SECP256K1(), default_backend())
|
| 123 |
+
public_key = private_key.public_key()
|
| 124 |
+
pem = public_key.public_bytes(
|
| 125 |
+
encoding=serialization.Encoding.PEM,
|
| 126 |
+
format=serialization.PublicFormat.SubjectPublicKeyInfo
|
| 127 |
+
)
|
| 128 |
+
smart_store['public_key'] = pem
|
| 129 |
+
elif lap == Lap.PRIVATE_KEY:
|
| 130 |
+
pem = private_key.private_bytes(
|
| 131 |
+
encoding=serialization.Encoding.PEM,
|
| 132 |
+
format=serialization.PrivateFormat.PKCS8,
|
| 133 |
+
encryption_algorithm=serialization.NoEncryption()
|
| 134 |
+
)
|
| 135 |
+
smart_store['private_key'] = pem
|
| 136 |
+
elif lap == Lap.ECDSA:
|
| 137 |
+
signature = private_key.sign(compressed_data, ec.ECDSA(hashes.SHA256()))
|
| 138 |
+
smart_store['signature'] = signature
|
| 139 |
+
|
| 140 |
+
if storage_timeline == StorageTimeline.PAST:
|
| 141 |
+
result = blockchain.store_data(smart_store)
|
| 142 |
+
elif storage_timeline == StorageTimeline.PRESENT:
|
| 143 |
+
result = smart_card.store_data(smart_store)
|
| 144 |
+
elif storage_timeline == StorageTimeline.FUTURE:
|
| 145 |
+
result = "Storing data in the future"
|
| 146 |
+
|
| 147 |
+
results.append(result)
|
| 148 |
+
|
| 149 |
+
return jsonify(results)
|
| 150 |
+
|
| 151 |
+
@app.route('/mint', methods=['POST'])
|
| 152 |
+
def mint_tokens():
|
| 153 |
+
to = request.json.get('to')
|
| 154 |
+
amount = request.json.get('amount')
|
| 155 |
+
tx_hash = jbo3_contract.functions.mint(to, amount).transact()
|
| 156 |
+
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
|
| 157 |
+
return jsonify({'transactionHash': tx_hash.hex(), 'receipt': receipt})
|
| 158 |
+
|
| 159 |
+
@app.route('/transfer', methods=['POST'])
|
| 160 |
+
def transfer_tokens():
|
| 161 |
+
from_address = request.json.get('from')
|
| 162 |
+
to = request.json.get('to')
|
| 163 |
+
amount = request.json.get('amount')
|
| 164 |
+
tx_hash = jbo3_contract.functions.transfer(to, amount).transact({'from': from_address})
|
| 165 |
+
receipt = w3.eth.waitForTransactionReceipt(tx_hash)
|
| 166 |
+
return jsonify({'transactionHash': tx_hash.hex(), 'receipt': receipt})
|
| 167 |
+
|
| 168 |
+
@app.route('/balance', methods=['GET'])
|
| 169 |
+
def check_balance():
|
| 170 |
+
address = request.args.get('address')
|
| 171 |
+
balance = jbo3_contract.functions.balanceOf(address).call()
|
| 172 |
+
return jsonify({'balance': balance})
|
| 173 |
+
|
| 174 |
+
if __name__ == '__main__':
|
| 175 |
+
app.run(debug=True)
|