Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue inside 'as_dict()' in transactions.py #329

Closed
hiddenvs opened this issue Aug 7, 2023 · 5 comments
Closed

Issue inside 'as_dict()' in transactions.py #329

hiddenvs opened this issue Aug 7, 2023 · 5 comments

Comments

@hiddenvs
Copy link

hiddenvs commented Aug 7, 2023

I am trying to use bitcoinlib to parse transactions and found problem inside transactions.py file
in function as_dict(), where by return of public_key is this error:

/lib/python3.10/site-packages/bitcoinlib/transactions.py", line 1346, in as_dict
'public_key': self.public_key.hex(),

AttributeError: 'str' object has no attribute 'hex'

I think it should be without .hex(), but I am not 100% sure about.

@hiddenvs
Copy link
Author

hiddenvs commented Aug 7, 2023

bitcoinlib 0.6.11
python 3.10.12
ubuntu 22.04 (but same on 20.04 or 18.04)

@hiddenvs
Copy link
Author

hiddenvs commented Aug 7, 2023

def as_dict(self):
"""
Get transaction output information in json format

     :return dict: Json with amount, locking script, public key, public key hash and address
     """

     return {
         'value': self.value,
         'script': self.lock_script.hex(),
         'script_type': self.script_type,
         'public_key': self.public_key.hex(),               <--- is no .hex() is there, it's working, but I am not sure, if it's working correctly
         'public_hash': self.public_hash.hex(),
         'address': self.address,
         'output_n': self.output_n,
         'spent': self.spent,
         'spending_txid': self.spending_txid,
         'spending_index_n': self.spending_index_n,
     }

@mccwdev
Copy link
Member

mccwdev commented Aug 8, 2023

The problem is not with the hex() method, the self.public_key should be always in bytes format.
Could you maybe post the code which leads to this error?

@Lucshine
Copy link

The problem is not with the hex() method, the self.public_key should be always in bytes format. Could you maybe post the code which leads to this error?

When the input of the transaction is P2PK, transactions.py#L1193 will be entered and the public_hex in the script is assigned to self.public_key. The value type here is str type. This problem can be solved by adding a to_bytes function on the right side of the equation.
The code to reproduce this error is as follows:

from bitcoinlib.transactions import Transaction

t = Transaction.parse_hex('01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0146ffffffff0100f2052a01000000434104e18f7afbe4721580e81e8414fc8c24d7cfacf254bb5c7b949450c3e997c2dc1242487a8169507b631eb3771f2b425483fb13102c4eb5d858eef260fe70fbfae0ac00000000')
t.as_dict()

@mccwdev
Copy link
Member

mccwdev commented Oct 23, 2023

Thanks, fixed in commit ae354c9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@hiddenvs @mccwdev @Lucshine and others