karask / python-bitcoin-utils

Library to interact with the Bitcoin network. Ideal for low-level learning and experimenting.
MIT License
262 stars 99 forks source link

[Feature Request] implement magic method for `Script` #60

Closed PabloLION closed 4 months ago

PabloLION commented 4 months ago

The idea is to add a easier way to compare two Script instances of bitcoinutils.script instances:

say I have an addr: P2pkhAddress and the two Script instances below (by_script, by_to_script_pub_key) should be the same.

by_p2pkh = P2pkhAddress(addr.to_string()).to_script_pub_key()
by_script = Script(
    [
        "OP_DUP",
        "OP_HASH160",
        addr.to_hash160(),
        "OP_EQUALVERIFY",
        "OP_CHECKSIG",
    ]
)

But in the assertion, we'd have to use assert by_to_script_pub_key.to_bytes() == by_script.to_bytes(). Can we add a special method __eq__ to the Script class that compares their bytes?

I'm open to make a PR on this but I want to know if there are other concerns on that.

karask commented 4 months ago

I don't have any concerns with that. Please make a PR and I will add it.