-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Milestone
Comments
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,
),
)
}
} |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The text was updated successfully, but these errors were encountered: