Skip to content

Commit

Permalink
Add frontend app linked with basic ui connected to backend api and ma…
Browse files Browse the repository at this point in the history
…intaining user session through public_key
  • Loading branch information
GrishonNganga committed May 5, 2022
1 parent 10e91bc commit 3d02857
Show file tree
Hide file tree
Showing 39 changed files with 29,806 additions and 6 deletions.
Binary file added __pycache__/art_factory.cpython-38.pyc
Binary file not shown.
Binary file added __pycache__/blockchain.cpython-38.pyc
Binary file not shown.
Binary file added __pycache__/ipfs.cpython-38.pyc
Binary file not shown.
53 changes: 53 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from art_factory import generate_unique_nft
from flask import Flask, request, make_response, jsonify, current_app
from flask_cors import CORS
from functools import wraps
from ipfs import upload
from blockchain import create_account
from datetime import datetime, timedelta
import jwt

# nft = generate_unique_nft()
# response = upload(nft)
# print(response)

app = Flask(__name__)
app.config["APP_SECRET"] = "KSiJ65bGtapUEQeg"
app.config["DEBUG"] = True
CORS(app, origins=["http://localhost:3000"], supports_credentials = True)


@app.route('/auth', methods=['POST'])
def signup():
post_data = request.get_json(force=True)
private_key = None
if post_data and "private_key" in post_data:
private_key = post_data["private_key"]
if private_key:
public_key = create_account(private_key = private_key)
response = {'status': 'success', 'message':'Account created successfully', "key": public_key}
response = make_response(jsonify(response), 200)
response.set_cookie("public_key", value=public_key, httponly=True, samesite="None", secure=True)
else:
public_key, private_key = create_account(private_key = private_key)
response = {'status': 'success', 'message':'Account loaded successfully', "key": {'public': public_key, 'private': private_key}}
response = make_response(jsonify(response), 200)
response.set_cookie("public_key", value=public_key, httponly=True, samesite="None", secure=True)
return response


@app.route('/refresh_session')
def refresh_token():
if request.cookies and "public_key" in request.cookies:
public_key = request.cookies.get("public_key")
response = {'status': 'success', 'message':'Admin signin successful','user': public_key}
response = make_response(jsonify(response), 200)
else:
response = {'status': 'error', 'message': 'Authentication failed'}
response = make_response(jsonify(response), 400)

return response


if __name__ == '__main__':
app.run()
5 changes: 2 additions & 3 deletions art_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
selected_image = {}
output_file = "generated_nfts.json"
base_dir = "layers"
output_dir = "output/"
files = os.listdir(f'./{base_dir}')
rarity_index = 0

Expand Down Expand Up @@ -49,7 +50,7 @@ def merge_layers(layers):
def save_image(image_name, image, layers):
generated_nfts = get_generated_nfts()
if image_name not in generated_nfts:
image.save(image_name)
image.save(output_dir + image_name)
generated_nfts[image_name] = layers
with open(output_file, "w") as f:
f.write(json.dumps(generated_nfts))
Expand All @@ -66,5 +67,3 @@ def generate_unique_nft():
return image
else:
return None

generate_unique_nft()
44 changes: 44 additions & 0 deletions blockchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import json
from web3 import Web3

network_url= "HTTP://127.0.0.1:7545"
web3 = Web3(Web3.HTTPProvider(network_url))
contract_address = ""
contract_abi = ''

def create_account(private_key = None):
if private_key:
acct = web3.eth.account.privateKeyToAccount(private_key)
print(acct)
return acct.address
else:
account = web3.eth.account.create()
return account.address, web3.toHex(account.privateKey)

def send_eth(to_account, amount):
from_account = web3.eth.accounts[0]

tx = {
"nonce": web3.eth.get_transaction_count(from_account),
"to": to_account,
"value": web3.toWei(amount, "ether"),
"gas": 2000000,
"gasPrice": web3.toWei(50, "gwei")
}

signed_tx = web3.eth.account.sign_transaction(tx, "ea34a23473fa895af810ec354474fef923686007242f510883a18d93028a04d6")

try:
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_response = web3.eth.get_transaction_receipt(tx_hash)

if tx_response and tx_response["status"] == 1:
return True, None
else:
return False, "Something wrong happened"

except Exception as e:
return False, e["message"]

def format_address(address):
return web3.toChecksumAddress(address)
23 changes: 23 additions & 0 deletions frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Empty file added frontend/README.md
Empty file.
Loading

0 comments on commit 3d02857

Please sign in to comment.