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

Add __eq__ and __hash__ methods for LedgerAccount #51

Merged
merged 1 commit into from
Nov 16, 2023

Conversation

moisses89
Copy link
Contributor

Closes #50
This PR defines __eq__ and __hash__ methods on LedgerAccount class to makes possible that instances of LedgerAccount with the same path and address were evaluated as equals.

In [2]: LedgerAccount("44'/60'/0'/0/10", 0x85bC4dc18e2Dde71294EA1c87CF0824A4c04489D) == LedgerAccount("44'/60'/0'/0/10", 0x85bC4dc18e2Dde71294EA1c87CF0824A4c04489D)
Out[2]: True

Copy link
Owner

@mikeshultz mikeshultz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR! This is useful.

Linting is failing though. Can you please run python setup.py lint to take care of it? After that I'll probably merge after some brief testing.

@@ -180,6 +180,13 @@ def __init__(self, path, address):
def __repr__(self):
return f"<ledgereth.objects.LedgerAccount {self.address}>"

def __eq__(self, other):
if isinstance(other, LedgerAccount):
return self.path == other.path and self.address == other.address
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Why compare path here? The odds of an address collision with a different path are all but impossible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you when LedgerAccount is calculated from Ledger, but the class itself let you create different instances with the same address and different path, this instances shouldn't be equal between them.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you run into this IRL? If so, under what circumstances?

Not asking for any changes, just trying to understand this so I don't introduce a regression unintentionally down the road.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't faced this scenario in real life. As far as I know, this could occur only if two ledgers are connected simultaneously, which isn't possible with the current implementation or someone manually initializes the class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, two ledgers shouldn't return the same address. I think the unique situation where this could happen is instantiating manually.

@moisses89
Copy link
Contributor Author

Can you please run python setup.py lint to take care of it? After that I'll probably merge after some brief testing.

Done :)

@mikeshultz
Copy link
Owner

Thanks for your contribution. I'll probably have a release tonight.

@mikeshultz mikeshultz merged commit 96ec953 into mikeshultz:master Nov 16, 2023
5 checks passed
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

Successfully merging this pull request may close these issues.

Instances of LedgerAccount with the same attributes are currently evaluated as different
2 participants