0

I use web3j to create my ethereum wallet.the Code just like down

import org.web3j.crypto.Bip39Wallet;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.WalletUtils;

wallet = WalletUtils.generateBip39Wallet(password, new File(keystorePath));
// keystore's file name
String keystoreFileName = wallet.getFilename();
// my mnemonic
String mnemonic = wallet.getMnemonic();

I can use this code get my address

Credentials credentials = WalletUtils.loadBip39Credentials(password, mnemonic);
String address = credentials.getAddress();

I can import my wallet by this:

Credentials credentials = WalletUtils.loadBip39Credentials(password, mnemonic);

but in this way I need password & mnemonic,How I import or recovery my wallet by mnemonic without password,because some wallet app just like metamask or imtoken , they don't need the old password that I create my wallet and the can reset a new password.

In other words,recovery or import the wallet just need mnemonic,how I do it by web3j

Is ereryone can tell me how to do it by web3j.thank u very much.

2 Answers2

0

You can simply pass an empty string as password:

// generate
wallet = WalletUtils.generateBip39Wallet("", tempDir);

// restore using WalletUtils
Credentials credentials = WalletUtils.loadBip39Credentials("", wallet.getMnemonic());

// or using constructor (and empty dummy file)
File tempFile = Files.createTempFile(null, null).toFile();
wallet = new Bip39Wallet(tempFile.getName(), wallet.getMnemonic());
Yohanes Gultom
  • 3,782
  • 2
  • 25
  • 38
0

You need to use the bip44 encryption protocol to generate mnemonics and recover wallets. Because imtoken and metamask wallets operate based on the bip44 protocol. If you use the bip39 protocol, the result will be different from that of imtoken and metamask. Refer to the following example code

Credentials credentials = Bip44WalletUtils.loadBip44Credentials("", "you mnemonic");
String address = credentials.getAddress();
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
James
  • 1
  • 1