I'm trying to create a bitcoin wallet website and I've written some python code so far.
I have downloaded bitcoin core and have created a wallet in the directory: C:\Users\MylesUsername\AppData\Roaming\Bitcoin\wallets\test2 In which I can see my wallet.dat file but when I try to run my python code I get the error message:
Traceback (most recent call last):
File "C:\Users\MylesUsername\Desktop\app.py", line 11, in <module>
wallet = Wallet(wallet_name)
File "C:\Users\MylesUsername\AppData\Local\Programs\Python\Python39\lib\site-packages\bitcoinlib\wallets.py", line 1408, in __init__
raise WalletError("Wallet '%s' not found, please specify correct wallet ID or name." % wallet)
bitcoinlib.wallets.WalletError: Wallet 'C:\Users\MylesUsername\AppData\Roaming\Bitcoin\wallets\test2\wallet.dat' not found, please specify correct wallet ID or name.
so it seems my python code can't find the wallet.dat file? I also don't know why it's mentioning the bitcoinlib directory at the start?
This is the python code for my bitcoin wallet website so far:
from flask import Flask, render_template, request, redirect, url_for, session
from bitcoinlib.wallets import Wallet
app = Flask(__name__)
app.secret_key = 'your_secret_key' # Change this to a secret key
# Provide the name or ID of the wallet
wallet_name = 'C:\\Users\\MylesUsername\\AppData\\Roaming\\Bitcoin\\wallets\\test2\\wallet.dat' # Change this to your actual wallet name or ID
# Create a Wallet object
wallet = Wallet(wallet_name)
# User data (this should be stored securely in a database in a production environment)
users = {
'user1': {
'password': 'password1',
'address': wallet.get_key().address # Generate a Bitcoin address for the user
}
}
# Check if the user is logged in
def is_user_logged_in():
return 'username' in session
# Create a root route
@app.route('/')
def home():
if is_user_logged_in():
return redirect(url_for('dashboard'))
return "Welcome to the Bitcoin Flask app!"
# Create a login page
@app.route('/login', methods=['GET', 'POST'])
def login():
if is_user_logged_in():
return redirect(url_for('dashboard'))
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if username in users and users[username]['password'] == password:
session['username'] = username # Store the username in the session
return redirect(url_for('dashboard'))
else:
return render_template('login.html', error='Invalid username or password.')
return render_template('login.html')
# Create a dashboard page
@app.route('/dashboard')
def dashboard():
if not is_user_logged_in():
return redirect(url_for('login'))
username = session['username']
address = users[username]['address']
balance = wallet.balance()
return render_template('dashboard.html', username=username, address=address, balance=balance)
# Create a send Bitcoin page
@app.route('/send', methods=['GET', 'POST'])
def send():
if not is_user_logged_in():
return redirect(url_for('login'))
if request.method == 'POST':
to_address = request.form['to_address']
amount = float(request.form['amount'])
# Send the Bitcoin
txid = wallet.send_to(to_address, amount, fee='normal')
return redirect(url_for('dashboard'))
return render_template('send.html')
# Create a reset password page
@app.route('/reset_password', methods=['GET', 'POST'])
def reset_password():
if not is_user_logged_in():
return redirect(url_for('login'))
username = session['username']
if request.method == 'POST':
new_password = request.form['new_password']
if len(new_password) < 8:
return render_template('reset_password.html', error='New password must be at least 8 characters long.')
users[username]['password'] = new_password
return redirect(url_for('dashboard'))
return render_template('reset_password.html')
# Create a create account page (only for demonstration, not a production-ready solution)
@app.route('/create_account', methods=['GET', 'POST'])
def create_account():
if is_user_logged_in():
return redirect(url_for('dashboard'))
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if len(username) < 4 or len(password) < 8:
return render_template('create_account.html', error='Username must be at least 4 characters and password at least 8 characters.')
if username in users:
return render_template('create_account.html', error='Username already taken')
users[username] = {
'password': password,
'address': wallet.get_key().address
}
session['username'] = username
return redirect(url_for('dashboard'))
return render_template('create_account.html')
# Logout
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(debug=True)
I've check my bitcoin core software and it seems to be running fine and has completed downloading everything.
I've added a bitcoin.conf file to my bitcoin core folder which allows rpc.
I've made sure the wallet isn't encrypted
Using bitcoinlib you don't have to use the wallet.dat file as a parameter for the wallet function. According to the documentation the function takes a name as parameter to create the wallet object.
Example from the documentation:
If you want to import the current wallet that you have created in bitcoincore application you can use the
import_key
method on the wallet object created with the library.