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

Refactor key derivation #1309

Merged
merged 17 commits into from
Feb 16, 2023
Merged

Refactor key derivation #1309

merged 17 commits into from
Feb 16, 2023

Conversation

dikel
Copy link
Contributor

@dikel dikel commented Jan 24, 2023

Description:
This PR refactors the derivation of private keys:

  • Adds toStandard[Ed25519|ECDSAsecp256k1]PrivateKey() which use the correct derivation path
  • Deprecates Mnemonic.toPrivateKey() and PrivateKey.fromMnemonic()
  • Adds support for ECDSA secp256k1 derivation

Related issue(s):

Fixes #1277, Fixes #1263

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
@codecov
Copy link

codecov bot commented Jan 24, 2023

Codecov Report

Base: 74.40% // Head: 74.52% // Increases project coverage by +0.11% 🎉

Coverage data is based on head (5c156be) compared to base (b910e63).
Patch coverage: 92.42% of modified lines in pull request are covered.

Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #1309      +/-   ##
=============================================
+ Coverage      74.40%   74.52%   +0.11%     
- Complexity      2875     2892      +17     
=============================================
  Files            184      185       +1     
  Lines          11391    11435      +44     
  Branches        1121     1123       +2     
=============================================
+ Hits            8476     8522      +46     
+ Misses          2255     2253       -2     
  Partials         660      660              
Impacted Files Coverage Δ
...ava/com/hedera/hashgraph/sdk/utils/Bip32Utils.java 50.00% <50.00%> (ø)
...java/com/hedera/hashgraph/sdk/PrivateKeyECDSA.java 86.41% <91.66%> (+6.02%) ⬆️
...c/main/java/com/hedera/hashgraph/sdk/Mnemonic.java 90.74% <100.00%> (+3.30%) ⬆️
...main/java/com/hedera/hashgraph/sdk/PrivateKey.java 90.69% <100.00%> (-0.61%) ⬇️
...va/com/hedera/hashgraph/sdk/PrivateKeyED25519.java 88.00% <100.00%> (+1.63%) ⬆️
.../main/java/com/hedera/hashgraph/sdk/AccountId.java 72.27% <0.00%> (-1.99%) ⬇️

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.

Copy link

@nikola-genov nikola-genov left a comment

Choose a reason for hiding this comment

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

Generally looks good to me. One minor improvement could be to use the isDerivable() instead of explicit 'chainCode == null' check.

Signed-off-by: dikel <dikelito@tutamail.com>
@dikel dikel requested a review from litt3 January 25, 2023 10:04
Signed-off-by: dikel <dikelito@tutamail.com>
Copy link

@nikola-genov nikola-genov left a comment

Choose a reason for hiding this comment

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

Looks good. Minor improvement would be to extract 0x80000000 as a constant.

dikel added 3 commits February 6, 2023 09:38
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
Copy link

@litt3 litt3 left a comment

Choose a reason for hiding this comment

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

Overall LGTM. FYI - I didn't have a chance to verify the mathy parts of this PR (though of course the unit tests testify to correct math). Other reviewers more involved in this SDK should verify separately that the logic makes sense

@@ -554,4 +542,48 @@ private byte[] wordsToLegacyEntropy2() throws BadMnemonicException {

return entropy;
}

Copy link

Choose a reason for hiding this comment

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

IMO it would make sense to have versions of these toStandard functions with default passphrase and index

  • toStandard(passphrase) (using index 0)
  • toStandard() (using no passphrase and index 0)
  • toStandard(index) (using no passphrase) << this one might be overkill, though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We thought about having multiple versions of the toStandard methods but we decided it's better to have only one, because it cannot be done in the other SDKs. JS and Go don't support overloading of methods.

Copy link

Choose a reason for hiding this comment

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

I've fine with that logic, but this really needs to be folded into the standard proposal.

For example, I know the C++ SDK effort has already implemented the proposal as it currently exists, with default parameters.

@@ -19,6 +19,8 @@
*/
package com.hedera.hashgraph.sdk;

import com.hedera.hashgraph.sdk.utils.Bip32Utils;
import org.bouncycastle.util.encoders.Hex;
Copy link

Choose a reason for hiding this comment

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

Since this is the first implementation of the new standard, I think it would make sense to include a new set of standard "hedera" test vectors. The list I had come up with is detailed in the Tests section of the standards issue, bullet #2

In addition to defining new test vectors, this would also involve renaming test variables MNEMONIC3_STRING, MNEMONIC_LEGACY_STRING, MNEMONIC_STRING, and MNEMONIC_PRIVATE_KEY, so that it is clear what these actually are.

Copy link
Contributor Author

@dikel dikel Feb 7, 2023

Choose a reason for hiding this comment

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

We've already added the test vectors (toStandardED25519PrivateKey and toStandardECDSAsecp256k1PrivateKey methods in MnemonicTest. I've changed the variable names to be clearer.

Copy link

Choose a reason for hiding this comment

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

It's important that these particular test vectors are tracked somewhere so that all SDKs use the same ones. That will ensure that the Standard mnemonic->key functions are consistent across SDKs

dikel added 2 commits February 7, 2023 13:16
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
Signed-off-by: dikel <dikelito@tutamail.com>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 8 Code Smells

93.8% 93.8% Coverage
0.0% 0.0% Duplication

@dikel dikel merged commit ab8632c into develop Feb 16, 2023
@dikel dikel deleted the refactor-key-from-mnemonic-logic branch February 16, 2023 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants