building flask app

This commit is contained in:
0ceanSlim 2023-12-19 16:59:39 -05:00
parent 93e0e4c67e
commit b6b061b945
3 changed files with 92 additions and 35 deletions

View File

@ -1,7 +1,23 @@
from flask import Flask, request, jsonify
from flask import Flask, request, jsonify, render_template
from ..src import create_wallet, generate_seed
app = Flask(__name__)
@app.route("/")
def home():
return "hello world"
@app.route('/')
def show_wallet_form():
return render_template('index.html')
@app.route('/create_wallet', methods=['POST'])
def handle_create_wallet():
wallet_name = request.form['walletName']
result = create_wallet(wallet_name)
return jsonify({'message': result})
@app.route('/generate_seed', methods=['POST'])
def handle_generate_seed():
seed_length = request.form['seedLength']
result = generate_seed(seed_length)
return jsonify({'seed': result})
if __name__ == "__main__":
app.run(debug=True)

49
app/templates/index.html Normal file
View File

@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Wallet</title>
<!-- Add your CSS stylesheets here if any -->
<style>
/* Add your styles here */
</style>
</head>
<body>
<h1>Create a New Wallet</h1>
<form id="walletForm">
<label for="walletName">Wallet Name:</label>
<input type="text" id="walletName" name="walletName" required><br><br>
<label for="seedLength">Choose Seed Phrase Length:</label>
<select id="seedLength" name="seedLength">
<option value="12">12 Words</option>
<option value="24">24 Words</option>
</select><br><br>
<button type="submit">Create Wallet</button>
</form>
<div id="result"></div>
<!-- Add your JavaScript code here -->
<script>
document.getElementById('walletForm').addEventListener('submit', function(event) {
event.preventDefault();
const form = event.target;
const formData = new FormData(form);
fetch('/create_wallet', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
document.getElementById('result').innerHTML = data.message;
})
.catch(error => {
console.error('Error:', error);
});
});
</script>
</body>
</html>

View File

@ -1,43 +1,35 @@
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
from util.rpcHandler import read_rpc_config, create_rpc_connection
from util.rpcHandler import read_rpc_config
from mnemonic import Mnemonic
# Read the RPC configuration from the configuration file
rpc_host, rpc_port, rpc_user, rpc_password = read_rpc_config()
def create_wallet(wallet_name):
rpc_host, rpc_port, rpc_user, rpc_password = read_rpc_config()
# Prompt the user for the wallet name
wallet_name = input("Enter the wallet name to create: ")
try:
rpc_connection = AuthServiceProxy(
f"http://{rpc_user}:{rpc_password}@{rpc_host}:{rpc_port}"
)
try:
rpc_connection = AuthServiceProxy(
f"http://{rpc_user}:{rpc_password}@{rpc_host}:{rpc_port}"
)
wallet_info = rpc_connection.listwallets()
# Check if the wallet already exists
wallet_info = rpc_connection.listwallets()
if wallet_name not in wallet_info:
rpc_connection.createwallet(wallet_name)
return f"Wallet '{wallet_name}' created successfully."
else:
return f"Wallet '{wallet_name}' already exists."
except JSONRPCException as json_exception:
return "A JSON RPC Exception occurred: " + str(json_exception)
except Exception as general_exception:
return "An Exception occurred: " + str(general_exception)
# If the wallet doesn't exist, create it
if wallet_name not in wallet_info:
rpc_connection.createwallet(wallet_name)
print(f"Wallet '{wallet_name}' created successfully.")
# Prompt the user for seed phrase length preference
seed_length = input("Choose seed phrase length (12 or 24 words): ")
def generate_seed(seed_length):
try:
if seed_length == '12' or seed_length == '24':
entropy_bits = 128 if seed_length == '12' else 256
mnemonic = Mnemonic("english")
seed_words = mnemonic.generate(entropy_bits)
print("Write down these seed words:")
print(seed_words)
return seed_words
else:
print("Invalid choice. Please choose 12 or 24.")
# You might want to save or display these seed words securely for the user
else:
print(f"Wallet '{wallet_name}' already exists.")
except JSONRPCException as json_exception:
print("A JSON RPC Exception occurred: " + str(json_exception))
except Exception as general_exception:
print("An Exception occurred: " + str(general_exception))
return "Invalid choice. Please choose 12 or 24."
except Exception as e:
return "An error occurred while generating the seed phrase: " + str(e)