属性错误:“LocalAccount”对象没有属性“privateKey”

问题描述 投票:0回答:1
def register(update: Update, context: CallbackContext):
    """Register the user and generate a ETH address."""
    user_id = update.message.from_user.id
    if update.message.chat.type != 'private':
        update.message.reply_text('This command can only be used in a private chat.')
        return
    # Check if user is already registered
    cursor.execute('SELECT * FROM users WHERE user_id = %s', (user_id,))
    result = cursor.fetchone()
    if result is not None:
        update.message.reply_text('You are already registered.')
        return
    # Generate new ETH address and private key
    account = w3.eth.account.create()
    address = account.address
    private_key = account.privateKey.hex()
    # Check if address is valid
    if not Web3.isAddress(address):
        update.message.reply_text('Error: Invalid ETH address generated. Please try again.')
        return
    # Insert new address and private key into database
    cursor.execute('INSERT INTO addresses (user_id, address, private_key) VALUES (%s, %s, %s)', (user_id, address, private_key))
    db.commit()
    # Insert new user into database
    username = update.message.from_user.username
    cursor.execute('INSERT INTO users (user_id, username) VALUES (%s, %s)', (user_id, username))
    db.commit()
    # Send confirmation message
    message = f'You have been registered with the following ETH address:\n\n{address}\n\nPlease use this address to deposit ETH or ERC20 tokens to your account.'
    context.bot.send_message(chat_id=user_id, text=message)

我该如何解决这个问题?

尝试修复此问题,但仍然无法修复,我们收到错误:

AttributeError: 'LocalAccount' object has no attribute 'privateKey'

python ubuntu private-key cryptocurrency wallet
1个回答
0
投票

试试这个

def register(update: Update, context: CallbackContext):
    """Register the user and generate an ETH address."""
    user_id = update.message.from_user.id
    if update.message.chat.type != 'private':
        update.message.reply_text('This command can only be used in a private chat.')
        return
    # Check if the user is already registered
    cursor.execute('SELECT * FROM users WHERE user_id = %s', (user_id,))
    result = cursor.fetchone()
    if result is not None:
        update.message.reply_text('You are already registered.')
        return
    # Generate a new ETH address and private key
    account = w3.eth.account.create()
    address = account.address
    private_key = account.privateKey.hex()  # Corrected attribute name
    # Check if the address is valid
    if not Web3.isAddress(address):
        update.message.reply_text('Error: Invalid ETH address generated. Please try again.')
        return
    # Insert a new address and private key into the database
    cursor.execute('INSERT INTO addresses (user_id, address, private_key) VALUES (%s, %s, %s)', (user_id, address, private_key))
    db.commit()
    # Insert a new user into the database
    username = update.message.from_user.username
    cursor.execute('INSERT INTO users (user_id, username) VALUES (%s, %s)', (user_id, username))
    db.commit()
    # Send a confirmation message
    message = f'You have been registered with the following ETH address:\n\n{address}\n\nPlease use this address to deposit ETH or ERC20 tokens to your account.'
    context.bot.send_message(chat_id=user_id, text=message)
© www.soinside.com 2019 - 2024. All rights reserved.