Page MenuHomePhabricator

[electrum] use an IntEnum for script types
ClosedPublic

Authored by PiRK on Aug 30 2023, 14:45.

Details

Reviewers
Fabien
Group Reviewers
Restricted Project
Commits
rABCa14979413bd6: [electrum] use an IntEnum for script types
Summary

This simplifies the code and makes it more typesafe.

The plan is to use this enum in the TxInput class and eventually in coin dicts to replace the "p2pkh", "p2sh", "p2pk" and "coinbase" strings (utxo["type"]).

Depends on D14434

Test Plan

python test_runner.py

I also ran the new test_privkey_from_WIF_privkey before this commit, to check that the behavior didn't change.

Run the application (electrum/electrum-abc), send a transaction to be sure the transaction construction/serialization/deserialization still works.

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

PiRK requested review of this revision.Aug 30 2023, 14:45

A short IntEnum demo:

$ python
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from enum import IntEnum
>>>
>>> class ScriptType(IntEnum):
...     p2pkh = 0
...     p2sh = 5
...
>>>
>>> ScriptType.p2pkh
<ScriptType.p2pkh: 0>
>>>
>>> ScriptType.p2pkh == 0
True
>>> ScriptType.p2pkh.name
'p2pkh'
>>>
>>> ScriptType(5)
<ScriptType.p2sh: 5>
>>>
>>> ScriptType(5) == ScriptType.p2sh
True
>>>
>>> ScriptType["p2sh"]
<ScriptType.p2sh: 5>
>>>
>>> ScriptType["p2sh"] == ScriptType.p2sh
True
>>>
>>> issubclass(ScriptType, int)
True
>>>
>>> isinstance(ScriptType.p2sh, int)
True
>>>
PiRK planned changes to this revision.Aug 30 2023, 14:53
PiRK edited the test plan for this revision. (Show Details)

fix privkey_from_WIF_privkey in address.py, add a test case to cover this function

name.lower() is no longer necessary now that I commited to keeping the enum names lowercase

This revision is now accepted and ready to land.Aug 31 2023, 08:55
This revision was automatically updated to reflect the committed changes.