java使用bip39创建以太坊钱包

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.web3j.crypto.Bip39Wallet;
import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys;
import org.web3j.crypto.Wallet;
import org.web3j.crypto.WalletFile;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Numeric;

import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * ClassName:CeShi
 * Description:
 */
public class CeShi {
    private static final Logger logger = LogManager.getLogger(CeShi.class);

    public void firstOne() throws Exception {
        HttpService httpService = new HttpService("https://mainnet.infura.io/v3/节点地址");
        Web3j web3 = Web3j.build(httpService);
        Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().sendAsync().get();
        String clientVersion = web3ClientVersion.getWeb3ClientVersion();
        System.out.println(clientVersion);

        Bip39Wallet wallet = null;
        // 创建一个存放keystore的文件夹
        String path = "d:\\keystore";
        try {

            // 创建钱包
            wallet = WalletUtils.generateBip39Wallet("", new File(path));
        } catch (Exception e) {
            logger.info("创建钱包失败");
        }
        // 获取keystore的名字
        String keyStoreKey = wallet.getFilename();
        logger.info("keyStoreKey ================ " + keyStoreKey);
        // 获取助记词
        String mnemonic = wallet.getMnemonic();
        logger.info("mnemonic ======================== " + mnemonic);

//        byte[] seed = MnemonicUtils.generateSeed(mnemonic, "");
//        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
//        byte[] hash = messageDigest.digest(seed);
//        ECKeyPair privateKeyS = ECKeyPair.create(hash);

//        String s = WalletUtils.generateWalletFile("", privateKeyS, new File(path), true);
//        logger.info("new Bip39Wallet(walletFile,mnemonic) ================= " + new Bip39Wallet(s,mnemonic));

        // 使用密码和助记词让账户解锁
        Credentials credentials = WalletUtils.loadBip39Credentials("", wallet.getMnemonic());
        // 获取账户地址
        String address = credentials.getAddress();
        logger.info("address ================= " + address);
        // 获取公钥
        String publicKey = credentials.getEcKeyPair().getPublicKey().toString(16);
        logger.info("publicKey ==================== " + publicKey);
        // 获取私钥
        String privateKey = credentials.getEcKeyPair().getPrivateKey().toString(16);
        logger.info("privateKey ================== " + privateKey);

    }

    /**
     * generate a random group of mnemonics
     * 生成一组随机的助记词
     */
    public static String generateMnemonics() {
        StringBuilder sb = new StringBuilder();
        byte[] entropy = new byte[Words.TWELVE.byteLength()];
        new SecureRandom().nextBytes(entropy);

        new MnemonicGenerator(English.INSTANCE)
                .createMnemonic(entropy, sb::append);
        return sb.toString();
    }



    public static void main(String[] args) throws NoSuchAlgorithmException, CipherException {
        String s = generateMnemonics();
        logger.info("sssssssssss =============== " + s);
        //根据助记词和密码生成种子seed
        byte[] seed = new SeedCalculator().calculateSeed(generateMnemonics(), "");
        // 根据seed生成公钥、私钥等
        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        byte[] hash = messageDigest.digest(seed);
        ECKeyPair ecKeyPair = ECKeyPair.create(hash);
        Numeric.toHexStringWithPrefix(ecKeyPair.getPrivateKey());
        Numeric.toHexStringWithPrefix(ecKeyPair.getPublicKey());
        //根据公钥或者ECKeyPair获取钱包地址
        String address = Keys.getAddress(ecKeyPair);
        logger.info("address ================= "+ address);
        // 根据公钥 私钥 密码 得到 生成钱包文件keystore
        WalletFile walletFile = Wallet.createLight("", ecKeyPair);
        logger.info("walletFile =================== " + walletFile);
        logger.info("walletFile =================== " + walletFile.toString());
    }
}

你可能感兴趣的:(java使用bip39创建以太坊钱包)