本文主要介绍在Java工程中如何生成ETH钱包的助记词、私钥、地址。

一、在之前创建的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>

二、创建一个Wallet.java类

1、定义一个path路径常量

    /**
* 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.nextBytes(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、写一个main方法调用  createWallet 方法。

  public static void main(String[] args) throws Exception {
//创建钱包
createWallet();
}

4、执行main方法 查看输出的钱包信息

助记词:
[usage, universe, dress, client, gold, wealth, short, diet, tourist, leave, roof, conduct]

地址:
0x1f3220b4651503bfe53bb96997fa547c4bc0fdaa

私钥:
0xa91f672a0c2094d20f3beeb4f47e2ce6c356e9081c74d7e3cd261f1bdc279a0b

公钥:
462aa97d90c49eaa4ec98c44a64c44606049be5d168c5461c8ed49762fa130d7b3201b336bac26e871c04f5aa84a0d0c868ffd0e04ad2b11d01561d6bb99eb1

5、验证

将上述生成的私钥或助记词在任意支持ETH的钱包中导入,可以看到地址与我们生成的一致,至此说明成功!

附:

完整代码

package com.bizzan.test;

import java.security.SecureRandom;
import java.util.List; import org.bitcoinj.crypto.ChildNumber;
import org.bitcoinj.crypto.DeterministicHierarchy;
import org.bitcoinj.crypto.DeterministicKey;
import org.bitcoinj.crypto.HDKeyDerivation;
import org.bitcoinj.crypto.MnemonicCode;
import org.bitcoinj.crypto.MnemonicException;
import org.bitcoinj.wallet.DeterministicSeed;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Keys; import com.google.common.collect.ImmutableList; public class Wallet {
/**
* 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); public static void main(String[] args) throws Exception {
//创建钱包
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.nextBytes(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));
} }

币严(BIZZAN.COM)数字货币交易平台官方网址:

https://www.bizzan.com

[币严BIZZAN区块链]Java生成ETH钱包助记词、私钥、地址的更多相关文章

  1. [币严BIZZAN区块链]数字货币交易所钱包对接之比特币(BTC)

    在币严BIZZAN开发数字货币交易所的过程中,一共有两大难点,一个是高速撮合交易引擎,另一个是钱包对接,这两者是我们团队以前没有接触过的.这个系列的文章主要介绍数字货币交易所钱包对接实现技术.第一个要 ...

  2. [转]HD钱包的助记词与密钥生成原理

    本文转自:https://blog.csdn.net/opassf/article/details/79978047 区块链相关的话题持续发酵之时,应该不少人知道加密货币钱包,钱包是普通用户与加密货币 ...

  3. 1.16. BIP39协议:使用助记词生成确定性钱包

    以太坊系统学习教程: https://www.netkiller.cn/blockchain/bip39.html 1.16. BIP39协议:使用助记词生成确定性钱包 BIP:39 层:应用层 标题 ...

  4. 程序员的自我救赎---12.2.3: 虚拟币交易平台(区块链) 下 【C#与以太坊通讯】

    <前言> (一) Winner2.0 框架基础分析 (二)PLSQL报表系统 (三)SSO单点登录 (四) 短信中心与消息中心 (五)钱包系统 (六)GPU支付中心 (七)权限系统 (八) ...

  5. 以太坊区块链Java(EthereumJ)学习笔记:概述

    本系列文章介绍以太坊区块链基于Java语言的解决方案.通过介绍EthereumJ定义的主要模块和Class,希望为大家学习和使用EthereumJ提供一些帮助. 整体架构 以太坊的Java解决方案主要 ...

  6. 区块链——java实现

    简述 本文主要的内容试一次关于区块链的作业,本次作业中有很多地方和实际的区块链不符合,比如hash,本文实现的区块链只是用了区块本身的hash并没去区分,头部和数据部分.仅供参考学习. 介绍 内容有点 ...

  7. 以太坊 生成助记词和infuru插件

    https://iancoleman.io/bip39/ https://infura.io google faucet : https://faucet.rinkeby.io/ 登录google账号 ...

  8. iFace安全专家揭秘:存放在区块链钱包中的比特币,其实已经早就不属于你……

    自MoreToken钱包跑路之后,2019年3月以来陆续多个钱包.交易所跑路,造成了大量用户账户被盗,仅MoreToken钱包用户损失总价值就达12.2亿人民币,用户损失惨重.为什么这么多钱包.交易所 ...

  9. 14、创建/恢复ETH钱包身份

    借助网上的一段描述: 若以银行账户为类比,这 5 个词分别对应内容如下: 地址=银行卡号密码=银行卡密码私钥=银行卡号+银行卡密码助记词=银行卡号+银行卡密码Keystore+密码=银行卡号+银行卡密 ...

随机推荐

  1. 一个自动递增生成目录和文件的cop文件类

    package com.hudong.util.orther; import java.io.File; import java.io.FileInputStream; import java.io. ...

  2. mailx发邮件报错Error initializing NSS: Unknown error -8015. . . . message not sent.处理

    前提:在配置zabbix3.0监控发送邮件告警时zabbix界面显示邮件以送达,但是QQ邮箱却没有收到邮件,再shell命令行测试发邮件QQ邮箱又是可以收到的,在别人的提醒下用zabbix用户执行发送 ...

  3. 使用 Hexo 在 GitHub 上建立博客 · Utopia's Daily Note

    使用 Hexo 在 GitHub 上建立博客 # 写在前面 其实我在一月份的就开始写了三篇博客文章,你没有看错,只是写了三篇,然后,就没有然后了.我还在其中一篇文章中写着,不知道自己能够坚持多久.事实 ...

  4. Protobuf 简介及简单应用

    Protobuf 是 protocol buffers 的缩写. 根据官网的说法, protocol buffers 与平台无关, 与语言无关, 实现数据序列化的一种手段. 正如名字一样, proto ...

  5. 软件测试价值观-SMBT新理念

    软件测试价值观-SMBT新理念 作者:张元礼 http://blog.csdn.net/vincetest 近年来有不少软件测试同行不少有些困惑-软件测试人员的价值在哪里?我们怎么才能做好软件测试?怎 ...

  6. nginx 502排错

    线上一台机器(该论坛所在机器)近期频繁出现502,每100次访问就会出现10次,这频率也太高了.于是开始了我的502排查之旅 ps aux |grep -c php 结果为200 netstat -a ...

  7. Mac 常见命令行

    1. unrar解压rar文件 1.1 安装命令:brew install unrar 1.2 解压文件:unrar x test.rar 2. 创建文件夹:mkdir 文件夹名 3. 删除文件夹: ...

  8. Android系统研究资料收集---站在前人的肩膀上

    Android系统研究资料收集---站在前人的肩膀上 针对Android系统研究任务,收集高价值资料在本页更新 AuthBlog:秋城https://www.cnblogs.com/houser032 ...

  9. unittest实战(一):用例框架

    import unittest class forTest0(unittest.TestCase): @classmethod def setUpClass(cls) -> None: prin ...

  10. [React技术内幕] setState的秘密

    对于大多数的React开发者,setState可能是最常用的API之一.React作为View层,通过改变data从而引发UI的更新.React不像Vue这种MVVM库,直接修改data并不能视图的改 ...