我的比特币钱包网站的Python代码似乎找不到我的wallet.dat比特币核心文件?

问题描述 投票:0回答:1

我正在尝试创建一个比特币钱包网站,到目前为止我已经编写了一些Python代码。

我已经下载了bitcoin core并在目录中创建了一个钱包: C:\Users\MylesUsername\AppData\Roaming\Bitcoin\wallets est2 在其中我可以看到我的 wallet.dat 文件,但是当我尝试运行我的 python 代码时,我收到错误消息:

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. 

所以我的Python代码似乎找不到wallet.dat文件?我也不知道为什么一开始就提到了bitcoinlib目录?

这是迄今为止我的比特币钱包网站的Python代码:

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)

我检查了我的比特币核心软件,它似乎运行良好并且已完成所有内容的下载。

我已将 bitcoin.conf 文件添加到我的比特币核心文件夹中,该文件允许 rpc。

我已确保钱包未加密

python bitcoin bitcoinlib
1个回答
0
投票

使用bitcoinlib,您不必使用wallet.dat 文件作为钱包函数的参数。 根据文档,该函数采用名称作为参数来创建钱包对象。

文档中的示例:

wif = 'xprv9s21ZrQH143K3cxbMVswDTYgAc9CeXABQjCD9zmXCpXw4MxN93LanEARbBmV3utHZS9Db4FX1C1RbC5KSNAjQ5WNJ1dDBJ34PjfiSgRvS8x'
if wallet_delete_if_exists('bitcoinlib_legacy_wallet_test', force=True):
    w = Wallet.create('bitcoinlib_legacy_wallet_test', wif)

如果您想导入在 bitcoincore 应用程序中创建的当前钱包,您可以在使用库创建的钱包对象上使用

import_key
方法

© www.soinside.com 2019 - 2024. All rights reserved.