buidl-bitcoin / buidl-python

python3 bitcoin library with no dependencies and extensive test coverage
https://pypi.org/project/buidl/
MIT License
83 stars 26 forks source link

fix: pass correct int to op_1negate up to op_16 in encode_num, ScriptPubKey should accept stream and raw kwargs #160

Closed katsucodes247 closed 7 months ago

katsucodes247 commented 7 months ago

PR opened as per https://github.com/buidl-bitcoin/buidl-python/issues/159 and fixes the described issue.

>>> TapScript.parse_hex('52935487')
OP_2 OP_ADD OP_4 OP_EQUAL 
>>> TapScript.parse_hex('52935487').raw_serialize().hex()
'52935487'

I've also changed the ScriptPubKey's parse method so it accepts both stream and raw kwargs as inherited Script class handles, otherwise we get an error and in case parse_hex() method is used:

Before:

>>> from io import BytesIO
>>> from buidl.taproot import TapScript
>>> TapScript.parse_hex('52935487')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "~/Documents/projects/bitcoin/buidl-python/buidl/script.py", line 116, in parse_hex
    return cls.parse(raw=bytes.fromhex(hex_str))
TypeError: ScriptPubKey.parse() got an unexpected keyword argument 'raw'

After:

>>> from io import BytesIO
>>> from buidl.taproot import TapScript
>>> TapScript.parse_hex('52935487')
OP_2 OP_ADD OP_4 OP_EQUAL 

Let me know if it's ok. I will also be adding some test for above test cases.

katsucodes247 commented 7 months ago

I'm closing this temporarily until I make some things a bit more clear.