手把手教你使用Java生成助记词、私钥、地址|Java区块链钱包生成助记词、地址

10 篇文章 0 订阅

一、引入依赖

在spring boot 项目中的 pom.xml文件中加入需要的依赖

<dependency>
	<groupId>org.bitcoinj</groupId>
	<artifactId>bitcoinj-core</artifactId>
	<version>0.14.7</version>
</dependency>

<!-- 后面讲解签名转账交易 -->
<dependency>
	<groupId>org.web3j</groupId>
	<artifactId>core</artifactId>
	<version>3.6.0</version>
</dependency>

二、创建工具类

1、定义常量

    /**
     * path路径
     */
    private final static ImmutableList<ChildNumber> BIP44_ETH_ACCOUNT_ZERO_PATH =
            ImmutableList.of(new ChildNumber(44, true), new ChildNumber(60, true),
                    ChildNumber.ZERO_HARDENED, ChildNumber.ZERO);

2、创建 createWallet 方法

/**
     * 创建钱包
     * @throws MnemonicException.MnemonicLengthException
     */
    private static void createWallet()  throws MnemonicException.MnemonicLengthException {
        SecureRandom secureRandom = new SecureRandom();
        byte[] entropy = new byte[DeterministicSeed.DEFAULT_SEED_ENTROPY_BITS / 8];
        secureRandom.engineNextBytes(entropy);

        //生成12位助记词
        List<String>  str = MnemonicCode.INSTANCE.toMnemonic(entropy);

        //使用助记词生成钱包种子
        byte[] seed = MnemonicCode.toSeed(str, "");
        DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(seed);
        DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(masterPrivateKey);
        DeterministicKey deterministicKey = deterministicHierarchy
                .deriveChild(BIP44_ETH_ACCOUNT_ZERO_PATH, false, true, new ChildNumber(0));
        byte[] bytes = deterministicKey.getPrivKeyBytes();
        ECKeyPair keyPair = ECKeyPair.create(bytes);
        //通过公钥生成钱包地址
        String address = Keys.getAddress(keyPair.getPublicKey());

        System.out.println();
        System.out.println("助记词:");
        System.out.println(str);
        System.out.println();
        System.out.println("地址:");
        System.out.println("0x"+address);
        System.out.println();
        System.out.println("私钥:");
        System.out.println("0x"+keyPair.getPrivateKey().toString(16));
        System.out.println();
        System.out.println("公钥:");
        System.out.println(keyPair.getPublicKey().toString(16));
    }

3、调用结果

助记词:
[length, shaft, sponsor, ankle, during, bullet, coil, sketch, connect, another, stock, digital]

地址:
0x4c78c36e376eb1d701b415eaffc15c29c2170981

私钥:
0x47ae411da5eb66894aa0dae8ca888128205cc7cb23b38675904fda5545c6b2d5

公钥:
bed6ad054f6bacfa1a042a33bb89b51bdfedd883d24558cc3563ebf0cb25cc274fb4bf299f3684a8eb36f4b6c9b5b8add3b1bd6fc1b1178269995bf935d8becb

4、私钥 导入到 MetaMask 钱包验证 。

可见恢复的地址和我们生成地址一样。在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在BSC链上生成助记和私链,可以使用Java编写程序来实现。下面是一个简单的示例代码: ```java import org.web3j.crypto.Credentials; import org.web3j.crypto.MnemonicUtils; 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 java.util.Arrays; import java.util.List; public class BSCChainDemo { public static void main(String[] args) throws Exception { // 初始化连接BSC节点 Web3j web3j = Web3j.build(new HttpService("https://bsc-dataseed.binance.org/")); // 获取节点版本信息 Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send(); String clientVersion = web3ClientVersion.getWeb3ClientVersion(); System.out.println("BSC Node Version: " + clientVersion); // 生成助记 byte[] initialEntropy = new byte[16]; List<String> mnemonicWords = MnemonicUtils.generateMnemonic(initialEntropy); String mnemonic = String.join(" ", mnemonicWords); System.out.println("Mnemonic: " + mnemonic); // 通过助记生成私钥 byte[] seed = MnemonicUtils.generateSeed(mnemonic, ""); Credentials credentials = Credentials.create(seed); String privateKey = credentials.getEcKeyPair().getPrivateKey().toString(16); System.out.println("Private Key: " + privateKey); // 生成钱包文件 String walletFileName = WalletUtils.generateFullNewWalletFile("", WalletUtils.getTestnetKeyDirectory()); System.out.println("Wallet File Name: " + walletFileName); } } ``` 在这个示例代码中,我们使用了Web3j库来连接BSC节点,并且使用MnemonicUtils库来生成助记私钥使用Credentials库来创建凭证对象,并且使用WalletUtils库来生成钱包文件。你可以根据自己的需要,来修改这个示例代码以满足不同的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zgsdzczh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值