22import os
33
44from flask import Flask , request , jsonify , render_template , redirect , url_for
5+ from flask_sqlalchemy import SQLAlchemy
56from web3 import Web3 , HTTPProvider
67import json
78from sqlalchemy import create_engine
89from sqlalchemy .orm import scoped_session , sessionmaker
910
10-
1111app = Flask (__name__ )
1212app .config ["TEMPLATES_AUTO_RELOAD" ] = True
1313
1414# Connect to the Ethereum network using Infura
15- w3 = Web3 (Web3 .HTTPProvider ('https://mainnet.infura.io/v3/YOUR_PROJECT_ID ' ))
15+ w3 = Web3 (Web3 .HTTPProvider ('https://mainnet.infura.io/v3/' ))
1616
1717# Load the contract ABI from a JSON file
1818with open ('contract_abi.json' , 'r' ) as f :
1919 contract_abi = json .load (f )
2020
2121# Connect to the contract
22- contract_address = '0x6b175474e89094c44da98b954eedeac495271d0f'
2322contract = w3 .eth .contract (address = contract_address , abi = contract_abi )
2423
2524# Connect to the database
2625engine = create_engine (os .getenv ("DATABASE_URL" ))
2726db = scoped_session (sessionmaker (bind = engine ))
2827
28+ web3auth = web3auth (provider_uri = "" , contract_address = "" )
29+
30+ #Landing page
31+ @app .route ('/' )
32+ def landing_page ():
33+ print ("Here landing page will be shown in html" )
34+
35+
36+ #Endpoint for new user
37+ @app .route ('/login' )
38+ def signin ():
39+ address = request .json .get (address )
40+ if web3 .is_signed_in (address ):
41+ return jsonify ({'error : user already signed in' }), 400
42+
43+ user_id = web3auth .is_sign_in (address )
44+ return jsonify ({'user_id' :user_id }), 200
45+
46+ #Endpoint for new registration
47+ @app .route ('/register' )
48+ def register ():
49+ address = request .json .get ('address' )
50+ username = request .json .get ('username' )
51+
52+ if web3auth .is_signed_in (address ):
53+ return jsonify ({'error' : 'User already signed in' }), 400
54+
55+ user_id = web3auth .register (address , username )
56+ return jsonify ({'user_id' : user_id }), 200
57+
58+ #View details of a product by ID
59+ @app .route ("/products/<string:product_hash>" , methods = ['GET' ])
60+ def get_product (product_hash ):
61+ product_id = str (uuid .uuid5 (uuid .NAMESPACE_URL , product_hash ))
62+ product = contract .functions .getItemDetais (product_id ).call ()
63+
64+ return jsonify ({'product' :product })
65+
66+ # Endpoint to view details of a specific user identified by hash
67+ @app .route ('/users/<string:user_hash>' , methods = ['GET' ])
68+ def get_user (user_hash ):
69+ # Query the smart contract to get the details of the specified user
70+ user_id = str (uuid .uuid5 (uuid .NAMESPACE_URL , user_hash ))
71+ user = contract .functions .getUserDetails (user_id ).call ()
72+
73+ # Return the user details as a JSON response
74+ return jsonify ({'user' : user })
75+
76+
2977
78+ if __name__ == "__main__" :
79+ # gunicorn --bind 0.0.0.0:8000 app:app ---> use this to run in cmd
80+ app .run (debug = True )
0 commit comments