Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Different privateKey generated by Mnemonic comparing to other libraries #919

Closed
lhtriet opened this issue Apr 25, 2019 · 7 comments · Fixed by #1259
Closed

Different privateKey generated by Mnemonic comparing to other libraries #919

lhtriet opened this issue Apr 25, 2019 · 7 comments · Fixed by #1259
Labels
awaiting-pr Waiting for PR

Comments

@lhtriet
Copy link

lhtriet commented Apr 25, 2019

To get privateKey from Mnemonic, I use: ECKeyPair.create(sha256(seed))

But it creates a different privateKey from the same mnemonic using TrustKeystore in iOS (TrustKeystore generates the correct privateKey comparing to https://iancoleman.io/bip39/)

I did some research and figure out that I need to do something with the configuration of how mnemonic is used. Can you please guide me. Thank you!

@iikirilov
Copy link
Contributor

Workaround - I would appreciate a PR in web3j to encapsulate this.

Bip32ECKeyPair masterKeypair = Bip32ECKeyPair.generateKeyPair(MnemonicUtils.generateSeed("XXXXXXX", null));
int[] path = {44 | HARDENED_BIT, 60 | HARDENED_BIT, 0 | HARDENED_BIT, 0,0};
Bip32ECKeyPair  x = Bip32ECKeyPair.deriveKeyPair(masterKeypair, path);
Credentials credentials = Credentials.create(x);
System.out.println(credentials.getAddress());

@iikirilov
Copy link
Contributor

Didn't mean to close it

@iikirilov iikirilov reopened this Apr 26, 2019
@razzbee
Copy link

razzbee commented May 18, 2019

I get the reason why, other wallets providers (mostly those who uses javascript libaray ) uses a different path compared to web3j, from my digging and observation, web3j borrowed it implementation from bitcoinj so below is the path difference :

web3j & bitcoinj -> // m/44'/60'/0'/0 (as commented in org/web3j/crypto/Bip44WalletUtils.java)
metamask, blockchain.com & other javascript based libs -> "m/44'/60'/0'/0/0"

How the test was conducted :

const ethers = require('ethers');

const mnemonic = "some phrase"
const path = "m/44'/60'/0'/0/0"
const wallet = ethers.Wallet.fromMnemonic(mnemonic,path);

console.log(wallet)

to reproduce the result install ethers (yarn add ethers)

@razzbee
Copy link

razzbee commented May 18, 2019

Well, I have solved it, if you want to support metamask and other wallet, use this :

    // "m/44'/60'/0'/0/0"
    val path = intArrayOf(44 or HARDENED_BIT, 60 or HARDENED_BIT, 0 or HARDENED_BIT, 0, 0)

     val mnemonic = "some key phrase"

      val seed = MnemonicUtils.generateSeed(mnemonic,"")

        val masterKeyPair = Bip32ECKeyPair.generateKeyPair(seed)

        val bip44Keypair = Bip32ECKeyPair.deriveKeyPair(masterKeyPair, path)

        val credentials = Credentials.create(bip44Keypair)

        println(credentials.address)

@iikirilov
Copy link
Contributor

Reopening because I think this can be made a lot easier to configure in web3j.

I think a PR should be made to change the default behavior of web3j to be consistent with other ethereum libraries.

It should also be super easy to configure for other use-cases.

@stale
Copy link

stale bot commented Aug 19, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale no activity for 21 days label Aug 19, 2019
@iikirilov iikirilov removed the stale no activity for 21 days label Aug 20, 2019
mawenpeng pushed a commit to mawenpeng/web3j that referenced this issue Sep 10, 2020
@conor10
Copy link
Contributor

conor10 commented Nov 12, 2020

This issue has been mentioned on Web3 Labs Community. There might be relevant details there:

https://community.web3labs.com/t/web3j-v4-8-0-released/112/1

xaviarias pushed a commit that referenced this issue Nov 12, 2020
* Fix #919. Gen consistent privateKey with others.

* Fix format issues.

* Fix format issues with spotlessJavaApply

Co-authored-by: mawenpeng <wp.ma@muheda.com>
rach-id pushed a commit to rach-id/web3j that referenced this issue Dec 10, 2021
…ibs (hyperledger-web3j#1259)

* Fix hyperledger-web3j#919. Gen consistent privateKey with others.

* Fix format issues.

* Fix format issues with spotlessJavaApply

Co-authored-by: mawenpeng <wp.ma@muheda.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
awaiting-pr Waiting for PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants