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

Expose hardened bit in Bip32ECKeyPair for other libraries #838

Closed
macalinao opened this Issue Jan 8, 2019 · 2 comments

Comments

Projects
None yet
3 participants
@macalinao
Copy link
Contributor

macalinao commented Jan 8, 2019

https://github.com/web3j/web3j/blob/master/crypto/src/main/java/org/web3j/crypto/Bip32ECKeyPair.java

Is there a reason why the hardened bit is package-private? It's needed to actually derive HD wallet keys.

@macalinao

This comment has been minimized.

Copy link
Contributor Author

macalinao commented Jan 8, 2019

For future readers that are trying to use web3j to generate bip44 hdwallets, here's my scala code to do so:

import java.security.SecureRandom
import abacus.server.util.StringUtils
import org.web3j.crypto._

object Wallets {

  val HardenedBit = 0x80000000
  // no idea why this is private.

  val random = new SecureRandom()

  def generateMnemonicAndPassphrase(): (String, String) = {
    val initialEntropy = new Array[Byte](16)
    random.nextBytes(initialEntropy)
    val mnemonic = MnemonicUtils.generateMnemonic(initialEntropy)
    val passphrase = StringUtils.secureAlphanumeric(32)
    (mnemonic, passphrase)
  }

  def generateMasterKey(
    mnemonic: String,
    passphrase: String,
  ): Bip32ECKeyPair = {
    val seed = MnemonicUtils.generateSeed(mnemonic, passphrase)
    Bip32ECKeyPair.generateKeyPair(seed)
  }

  def generateKeyCredentials(
    mnemonic: String,
    passphrase: String,
    index: Int,
  ): Credentials = {
    // ledger does things this way according to
    // https://github.com/ethereum/EIPs/issues/84#issuecomment-406324344
    val path =
      Array[Int](44 | HardenedBit, 60 | HardenedBit, index | HardenedBit, 0, 0)
    Credentials.create(
      Bip32ECKeyPair.deriveKeyPair(
        generateMasterKey(mnemonic, passphrase),
        path,
      ),
    )
  }

}
@MDhondt

This comment has been minimized.

Copy link
Contributor

MDhondt commented Jan 18, 2019

I too vote to make this HARDENED_BIT a public field.

In Java:

public ECKeyPair generateKeyPair(String mnemonic, String passphrase, String derivationPath) {
    int HARDENED_BIT = 0x80000000; // This should be statically importable from org.web3j.crypto.Bip32ECKeyPair

    Integer purpose, coinType, account, change, addressIndex; // parse derivationPath

    Bip32ECKeyPair masterKeypair = Bip32ECKeyPair.generateKeyPair(MnemonicUtils.generateSeed(mnemonic, passphrase));
    int[] path = {purpose | HARDENED_BIT, coinType | HARDENED_BIT, account | HARDENED_BIT, change, addressIndex};
    return Bip32ECKeyPair.deriveKeyPair(masterKeypair, path);
}

MDhondt added a commit to MDhondt/web3j that referenced this issue Jan 18, 2019

@snazha-blkio snazha-blkio added this to the 4.1.x milestone Jan 20, 2019

@snazha-blkio snazha-blkio self-assigned this Jan 20, 2019

@snazha-blkio snazha-blkio modified the milestones: 4.1.2, 4.1.x Mar 6, 2019

snazha-blkio added a commit that referenced this issue Mar 6, 2019

@snazha-blkio snazha-blkio modified the milestones: 4.1.x, 4.1.2 Mar 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.