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

Fixing inconsistency between generated entropy value type and the expected HDWallet.entropy value type #101

Merged
merged 17 commits into from Oct 19, 2022

Conversation

daehan-koreapool
Copy link
Contributor

mnemonic Python package generates an entropy value in bytearray type.
However, HDWallet.entropy class is expecting a string value and it is used as a string throughout the entire bip32.py module codebase.

This merge request explicitly converts generated entropy value as a string value to be consistent with the original implementer's intention.

@daehan-koreapool daehan-koreapool marked this pull request as draft October 17, 2022 06:59
@codecov-commenter
Copy link

codecov-commenter commented Oct 17, 2022

Codecov Report

Base: 85.60% // Head: 86.00% // Increases project coverage by +0.40% 🎉

Coverage data is based on head (899d3d9) compared to base (195c2fb).
Patch coverage: 95.65% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #101      +/-   ##
==========================================
+ Coverage   85.60%   86.00%   +0.40%     
==========================================
  Files          22       24       +2     
  Lines        2445     2630     +185     
  Branches      498      517      +19     
==========================================
+ Hits         2093     2262     +169     
- Misses        258      270      +12     
- Partials       94       98       +4     
Impacted Files Coverage Δ
pycardano/crypto/bip32.py 91.30% <95.65%> (ø)
pycardano/crypto/__init__.py 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

…efault value for passphrase parameter

REFACTOR. pulling out duplicate seed creating logic into a class method

ADD. adding test_is_entropy() testcase against meumonic package generated entropy value
@daehan-koreapool
Copy link
Contributor Author

Since crypto/bip32.py is being implemented and maintained by PyCardano project contributers, I think this module should be included in the coverage report in my opinion.

What do you think @cffls ?

@cffls
Copy link
Collaborator

cffls commented Oct 17, 2022

Since crypto/bip32.py is being implemented and maintained by PyCardano project contributers, I think this module should be included in the coverage report in my opinion.

What do you think @cffls ?

Thank you for raising this PR! Yes, I think it makes sense to include this file to coverage. Please feel free to change .coveragerc.

@daehan-koreapool daehan-koreapool marked this pull request as ready for review October 17, 2022 20:46
@daehan-koreapool
Copy link
Contributor Author

Major Updates

  • Ensuring Mnemonic package generated entropy value is stored as a string representation
  • Pull out seed generating method for reusability
  • Setting from_entropy() passphrase parameter as a string value by default
  • Add more testcases ported from Emurgo serialization library to ensure both Mainnet and Testnet addresses are covered
  • Add tests around entropy-based class methods to ensure refactored codes are functional

Minor Updates

  • Replace print statements with logging statements
  • Update Makefile, gitignore, and coveragerc

Copy link
Collaborator

@cffls cffls left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for contributing to this project! @henryyuanheng-wang Henry, could you please take a look at this?

@daehan-koreapool
Copy link
Contributor Author

Hi @henryyuanheng-wang ,

I have a question regarding test_payment_address_12_reward2() test case.
For this test, I see that you are explicitly specifying private=False for the last two derivation path indexes.
From my experiment, setting this to private=True results in the same public key generation.
Would you be able to clarify on why this results in the same public key?

I've also just pushed another commit which can be rolled back if you wish to.
This commit is to enhance HDWallet creation UX to support simpler chaining of HDWallet derivation steps which is similar to other HDWallet library API design.

@henryyuanheng-wang
Copy link
Contributor

Hi @henryyuanheng-wang ,

I have a question regarding test_payment_address_12_reward2() test case. For this test, I see that you are explicitly specifying private=False for the last two derivation path indexes. From my experiment, setting this to private=True results in the same public key generation. Would you be able to clarify on why this results in the same public key?

I've also just pushed another commit which can be rolled back if you wish to. This commit is to enhance HDWallet creation UX to support simpler chaining of HDWallet derivation steps which is similar to other HDWallet library API design.

Hi @daehan-koreapool , thanks for catching the inconsistencies and for making some great improvements! Everything looks pretty good. I like how you simplified the derivation step so a new HDWallet instance is created without needing to pass a parent HDWallet (was a bit redundant in hindsight). One question regarding the function name change from derive_from_index to derive - can you point me to the other popular implementation you were referring to? I was referencing some of the naming conventions from python-hdwallet repo but would love to see some other implementations.

To answer your question regarding public keys derived either private or public is the same - I think this is expected behavior and beauty of HDWallet is that with public key from account you can derive the same public address as if you had the private key (you can refer to the diagram here under HD Sequential wallets section and a HDWallet reading I found really useful here). Let me know if it makes sense!

@daehan-koreapool
Copy link
Contributor Author

Hi @henryyuanheng-wang,

Hi @daehan-koreapool , thanks for catching the inconsistencies and for making some great improvements! Everything looks pretty good. I like how you simplified the derivation step so a new HDWallet instance is created without needing to pass a parent HDWallet (was a bit redundant in hindsight). One question regarding the function name change from derive_from_index to derive - can you point me to the other popular implementation you were referring to? I was referencing some of the naming conventions from python-hdwallet repo but would love to see some other implementations.

It's pretty cool to know that derived public keys are identical for both private and public derivation methods.
Thanks for the clarification.

To answer your question regarding public keys derived either private or public is the same - I think this is expected behavior and beauty of HDWallet is that with public key from account you can derive the same public address as if you had the private key (you can refer to the diagram here under HD Sequential wallets section and a HDWallet reading I found really useful here). Let me know if it makes sense!

Regarding the derive() naming update, I referred to cardano-serialization-lib rust implementation.
This API design seem very simple and intuitive to use, so here's where I got the idea of returning a new HDWallet each time a derive() method is called for easier chaining operations.

Lucid js library also follows similar derive(index) pattern but this could be because Lucid uses cardano-serialization-lib wasm module under the hood.

@daehan-koreapool
Copy link
Contributor Author

I think the coverage test is failing since we are now including bip32.py module in the coverage report.
I'll add more testcases try to increase the test coverage.

@daehan-koreapool
Copy link
Contributor Author

Added a bit more refactoring commits as well.

  • Pulled out frequently used list as a constant set. This could be set as a tuple if we want immutability, but since we use "contains" check more often, thought a set data structure is more suitable
  • Cleaned up nested statements

@cffls cffls merged commit af63f0f into Python-Cardano:main Oct 19, 2022
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.

None yet

4 participants