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
HDNodeWallet derivePath not working properly #4551
Comments
same problem |
Appears the deviation paths have been different since 6.0.0. We may have to revert to 5.7.2 if you want to derive accounts with the BIP44. As you can see from these tests, the base account from wallet is correct, but if you derive an account they are not correct, and the base account doesn't match derived account 0. Test with 5.7.2: function test() { const wallet = ethers.Wallet.fromMnemonic(seedPhrase) const path1 = const metaMaskAccount1 = '0x904c2b17cdf69198eed7004E6b4C99e4C1DdB930' console.log('Base Wallet Address:', wallet.address) test()` Results: Test with >=6.0.0 const wallet = Wallet.fromPhrase(seedPhrase) const metaMaskAccount1 = '0x904c2b17cdf69198eed7004E6b4C99e4C1DdB930' console.log('Base Wallet Address:', wallet.address) Results: |
Alright I found out what is causing it, and found a temp work around as well. So when you call derivePath, it will call that wallet instances "deriveChild" function. Once a wallet is instantiated, it will hold on to its deviation path with the class prop called "path". So when you call derivePath(), the class will go call deriveChild for each part of the path. What happens is it appends that class prop "path" when making the deviated path which isn't right and which is why it ends up like "m/44'/60'/0'/0/0/44'/60'/0'/0/1". This is why it works fine when first making the wallet as "path" is an empty string. It goes deeper and not sure the full intention of the code so I will leave it at that, as this is more than adding a line of code to fix. But for a work around, do not use "deviatePath()", use the static functions. Here is where the problem is in hdwallet.ts ` /**
}` Workaround: `const { ethers } = require("ethers") function test() { const wallet = ethers.HDNodeWallet.fromPhrase(seedPhrase) const path1 = const metaMaskAccount1 = '0x904c2b17cdf69198eed7004E6b4C99e4C1DdB930' console.log('Wallet Address:', wallet.address) test()` Results:
|
Hello thank you mate very good explanation |
We found a work around but the issue you brought up is still a problem if you can reopen it. |
I want to keep this open to further investigate. I’ll close this again if it is deemed not an issue. There are also two functions |
So, the above code should have thrown an error. I'm adding that now: the I'm adding a constraint that it ensures this is the case, so the code in the OP would throw. As an aside, for those that wish to compute a large number of child nodes, this should be about 5 times faster, as it keeps a reference to an intermediate node, so those calculations do not need to be replicated: const wallet = ethers.HDNodeWallet.fromMnemonic(mnemonic, "m/44'/60'/0'/0");
const wallet1 = wallet.derivePath("0");
console.log(wallet1);
const wallet2 = wallet.derivePath("1");
console.log(wallet2);
// Or in a for loop:
for (let i = 0; i < 10; i++) {
console.log(wallet.deriveChild(i));
} |
These changes were published in v6.11.1. Thanks! :) |
Ethers Version
^6.9.2
Search Terms
HDNodeWallet, DerivePath
Describe the Problem
I am trying to derivePath from an HDNodeWallet, but the provided path is not the same path when the wallet is generated
for example i am trying to generate a wallet using Mnemonic and a path
input path="m/44'/60'/0'/0/1"
output path="m/44'/60'/0'/0/1/44'/60'/0'/0/1"
Maybe i have a bad understanding of HDWallet but in my mind they should be the same path
Code Snippet
Contract ABI
No response
Errors
No response
Environment
node.js (v12 or newer)
Environment (Other)
No response
The text was updated successfully, but these errors were encountered: