java web3 区块链钱包“BTC,TRX,ETH”地址生成、助记词导入、keyStron文件生成等功能

资讯 2024-07-09 阅读:56 评论:0
一、前言I. Foreword#最近闲着没事做,然后看听到说什么BTC疯涨,这类的小道消息。# I haven't had much time to do lately, and then I've heard little news o...
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

一、前言

I. Foreword

#最近闲着没事做,然后看听到说什么BTC疯涨,这类的小道消息。

# I haven't had much time to do lately, and then I've heard little news of BTC going crazy, that kind of thing.

?哦豁~,这一下就让我觉得非常好奇,说,他们这些东西是怎样在链上存在的呢,然后他们那个一串串的地址又是啥,imtoken钱包又是怎么实现的呢。

{\cHFFFFFF}{\cH00FFFF} Oh, I'm curious at this point, saying, "How do they have these things on the chain, and what do they have on a string of addresses, and how do they get their imtoken wallets?"

根据以上疑问,我自己去研究,自己去做一个钱包可以不?PS:主要是这个问题简单点,那些什么在链上存在的我没研究透。

In the light of these questions, I'm going to do my own research, and I'm going to do my own wallet? PS: It's a simple question, and I don't know what's on the chain.

好的鸣人不说暗话,我们开始!

Good Naruto doesn't say the secret. Let's do it!

? ? ?

二、理解区块钱包

ii. Area of Understanding dollar packs

首先我们得知道传说中的imtoken,web3等钱包是什么东西,它实际上就是一个以地址为核心的一个软件

First of all, we need to know what's in the legend's wallets like mtoken, web3, which is actually a software with an address at its core.

ps:当然现在他们也有很多其他的功能,比如说新闻啊,杂七杂八的,我们暂时把他当作银行卡包用

Ps: Now, of course, they've got a lot of other functions, like news. We're using him as a bank card for the time being.

从而得知,我们需要做一个银行卡包,那么它里面都有些啥内容呢? ? ? ? ? 卡包最主要的就是银行卡啊,那么钱包的内容是啥呢?? ?ok !? ? 钱包的内容就是一个区块地址

♪ What's the contents of the wallet? ♪ OK! ♪ The contents of the wallet are a block address ♪

三、钱包地址

III. Wallet Addresses

问:钱包地址是啥呢?

Q: What's the wallet address?

答:“钱包地址是一串16位进制的字符串”

Answer: "The wallet address is a 16-digit string."

? ? ? ? “我都没接触过,区块是啥我都不清楚,我怎么做啊!“

♪ "I haven't touched anything. I don't know what a block is. How am I supposed to do that?"

? ? ? ? “这东西肯定很难,又是什么16进制,什么的,肯定还有很多算法,算了不看了”

♪ "This thing's gonna be hard, and it's gonna be a hex or something, and it's gonna have a lot of algorithms, forget it."

..............................................?

? ? ? ? ? ?好的,其实很简单,你想想,区块这东西听着很深奥,那么是不是已经有大佬帮我们开路了呢?然后java语言的生态在全世界来说是不是很广(说白了就是调包调用API),肯定有现成的包给你用的。

¶ well, it's easy, you think, the block sounds deep, so is there a big guy to help us out? Then the java language's ecology is wide around the world, and there's got to be a ready package for you.

四、具体实现

IV. REALIZATION

问:好的,那我们用java怎么去做。

Q: Okay, so what do we do with java?

答:看下面

Answer: Look at the bottom.

首先我们得引用几个包

First, we'll have to quote a few bags.

第一个:tron 也就是 trx需要用的核心包

First: Toron is the core package that trx needs.

GitHub:tron.tronprotocol

//org.tron.cli 包 在git上可以下得到,所以在本地引用即可

/org.tron.cl. package is available on git, so local references are available.

org.tron.cli

core

4.7.0

system

${project.basedir}/src/main/java/lib/wallet-cli.jar

第二个:web3J 主网包 ETH

Second: web3J Main Net Package ETH

//宝宝我用的是5.0.0 方法更全面,对比之前的4.8? ?4.0这些去掉了很多方法,然而优化了很多地方,具体可以去看看

//Baby, I'm using the 5.0.0 method more comprehensive, compared to the 4.8?

org.web3j

core

5.0.0

第三个:bitcoinj 主网包 就是BTC的工具包

Third: bitcoinj's main web kit is the BTC toolkit.

//我这里用的是0.14.7 这个包里面方法很全面,阅读源码看起来也很清晰。而且推荐使用的也是这个

//I'm using 0.14.7. The package is comprehensive, and the reading code looks clear. And this is the one that's recommended.

org.bitcoinj

bitcoinj-core

0.14.7

com.google.guava

guava

好的以上就是具体需要用到的核心包,接下来看代码实现

All right, that's the core package that needs to be used. Let's see the code.

1.具体代码实现

1. Specific code realization

1.1公共方法

1.1 Public methods

? ? ? ? 下面是一些算法,就是16进制转字符串啊,这些东西,我们在生成钱包后返回的是一个10进制byte[]数组,所以我们需要转换成相应16进制的字符串,

♪ Here's some algorithms, that's a hexadecimal string, these things that we're returning with a 10-byte [] array of wallets, so we need to convert to the corresponding hexadecimal string. ♪

PS:下面代码复制过去没用,你需要去下载我的完整项目才行,最下面的GIT地址

PS: The code below is not working. You need to download my entire project. The bottom GIT address.

public static byte[] hexStringToByteArray(String s) {

int len=s.length();

byte[] data=new byte[len / 2];

for (int i=0; i < len; i +=2) {

data[i / 2]=(byte) ((Character.digit(s.charAt(i), 16) << 4)

+ Character.digit(s.charAt(i + 1), 16));

}

return data;

}

public static byte[] generateRandomBytes(int length) {

byte[] bytes=new byte[length];

new SecureRandom().nextBytes(bytes);

return bytes;

}

public static String bytesToHex(byte[] bytes) {

StringBuilder hexString=new StringBuilder();

for (byte b : bytes) {

String hex=Integer.toHexString(0xff & b);

if (hex.length()==1) {

hexString.append('0');

}

hexString.append(hex);

}

return hexString.toString();

}

1.2常量

1.2 Constant

? ? ? ? 首先我们还是要有一些概念的,BIP44_ZERO_PATH? 是啥,他是一个路径(看注释)

♪ First we're gonna have some ideas, BIP44-ZERO-PATH?

PS:常量解释看注释

PS: Constants explain comments

public static final ImmutableList BIP44_ZERO_PATH=ImmutableList.of(new ChildNumber(44, true), new ChildNumber(60, true),

ChildNumber.ZERO_HARDENED, ChildNumber.ZERO, ChildNumber.ZERO);

public static final String TRX="TRX";

public static final String ETH="ETH";

public static final String BTC="BTC";

public static final String PASSWORD="123456";

public static final String KEYSTORE_PATH=System.getProperty("user.dir")+"/keyStore";

1.3trx相关方法

1.3 Trx-related methods

public WalletModule createWallet(){

WalletModule module=new WalletModule();

byte[] entropy=WalletInitMethod.generateRandomBytes(16);//其实这个你就可以保存起来,因为是生成助记词很重要的一个熵,没办法逆推算,写入到keyStron种

Byte[ ] entropy=WalletInitMethod.generateRandomBytes (16); /In fact, you can save this because it's an entropy that's important to generate the word, and it can't be inverted and written to the KeyStron type.

String mnemonic=MnemonicUtils.generateMnemonic(entropy);

byte[] seed=MnemonicUtils.generateSeed(mnemonic, "");

// 从种子生成私钥

// Generate private keys from torrents

ECKey ecKey=ECKey.fromPrivate(seed);

// 获取公钥和地址

/ / Get Public Keys and Addresses

byte[] publicKeyBytes=ecKey.getPubKey();

String address=Base58.encode(ecKey.getAddress());

module.setAddress(address);

module.setPublicKey(WalletInitMethod.bytesToHex(publicKeyBytes));

module.setPrivateKey(WalletInitMethod.bytesToHex(ecKey.getPrivateKey()));

module.setMnemonic(Arrays.asList(mnemonic.split(" ")));

return module;

}

public WalletModule createWalletByMnemonic(String mnemonic){

WalletModule module=new WalletModule();

byte[] seed=MnemonicUtils.generateSeed(mnemonic, "");

// 从种子生成私钥

// Generate private keys from torrents

ECKey ecKey=ECKey.fromPrivate(seed);

// 获取公钥和地址

/ / Get Public Keys and Addresses

byte[] publicKeyBytes=ecKey.getPubKey();

String address=Base58.encode(ecKey.getAddress());

module.setAddress(address);

module.setPublicKey(WalletInitMethod.bytesToHex(publicKeyBytes));

module.setPrivateKey(WalletInitMethod.bytesToHex(ecKey.getPrivateKey()));

module.setMnemonic(Arrays.asList(mnemonic.split(" ")));

return module;

}

public WalletModule createWalletByPrivateKey(String privateKey){

WalletModule module=new WalletModule();

// 将私钥转换为字节数组

/ / Convert private keys to bytes

byte[] privateKeyBytes=WalletInitMethod.hexStringToByteArray(privateKey);

// 使用私钥生成 ECKey 对象

/ / Use private key to generate ECKey objects

ECKey ecKey=ECKey.fromPrivate(privateKeyBytes);

// 获取公钥和地址

/ / Get Public Keys and Addresses

byte[] publicKeyBytes=ecKey.getPubKey();

String address=Base58.encode(ecKey.getAddress());

module.setAddress(address);

module.setPublicKey(WalletInitMethod.bytesToHex(publicKeyBytes));

module.setPrivateKey(WalletInitMethod.bytesToHex(ecKey.getPrivateKey()));

return module;

}

1.4eth相关方法

1.4eth-related methodology

public WalletModule createWallet() throws CipherException, IOException {

WalletModule module=new WalletModule();

Bip39Wallet walletEthereum=WalletInitMethod.createWalletEthereum();

String mnemonic=walletEthereum.getMnemonic();

Credentials credentials=WalletUtils.loadBip39Credentials(Constants.PASSWORD, mnemonic);

ECKeyPair ecKeyPair=credentials.getEcKeyPair();

module.setMnemonic(Arrays.asList(walletEthereum.getMnemonic().split(",")));

module.setKeyStoreFileName(walletEthereum.getFilename());

module.setPublicKey(ecKeyPair.getPublicKey().toString(16));

module.setPrivateKey(ecKeyPair.getPrivateKey().toString(16));

module.setAddress(credentials.getAddress());

return module;

}

public static WalletModule createWalletByMnemonic(String mnemonic) {

WalletModule module=new WalletModule();

Credentials credentials=WalletUtils.loadBip39Credentials(Constants.PASSWORD, mnemonic);

ECKeyPair ecKeyPair=credentials.getEcKeyPair();

module.setPublicKey(ecKeyPair.getPublicKey().toString(16));

module.setPrivateKey(ecKeyPair.getPrivateKey().toString(16));

module.setAddress(credentials.getAddress());

return module;

}

public static WalletModule createWalletByPrivateKey(String privateKey) {

WalletModule module=new WalletModule();

Credentials credentials=Credentials.create(privateKey);

ECKeyPair ecKeyPair=credentials.getEcKeyPair();

module.setPublicKey(ecKeyPair.getPublicKey().toString(16));

module.setPrivateKey(ecKeyPair.getPrivateKey().toString(16));

module.setAddress(credentials.getAddress());

return module;

}

public static WalletModule createWalletByKeyStore(String password, String file_Path) throws CipherException, IOException {

WalletModule module=new WalletModule();

//用loadCredentials 方法就行

// with the laadCredentials method

Credentials credentials=WalletUtils.loadCredentials(password, file_Path);

ECKeyPair ecKeyPair=credentials.getEcKeyPair();

module.setPublicKey(ecKeyPair.getPublicKey().toString(16));

module.setPrivateKey(ecKeyPair.getPrivateKey().toString(16));

module.setAddress(credentials.getAddress());

return module;

}

1.5btc相关方法

1.5btc related methodology

public WalletModule createWallet(){

WalletModule module=new WalletModule();

try {

List list=WalletInitMethod.createMnemonic();

byte[] seed=MnemonicCode.INSTANCE.toEntropy(list);

DeterministicSeed deterministicSeed=new DeterministicSeed(list,seed,null,new Date().getTime());

Wallet wallet=Wallet.fromSeed(NetworkParameters.fromID("org.bitcoin.production"), deterministicSeed);//主网,你也可以选择测试网

Wallet wallet = Wallet.fromSeed (Network Parameters.fromID), deterministicSeed;// master network, you can also choose the test network

module.setAddress(wallet.currentReceiveAddress().toString());

wallet.getIssuedReceiveKeys();

//ps:这里为什么要做一个这样的处理,是因为BitcoinJ 库会从比特币网络中下载区块并同步交易历史,

/ps: Why do we do this here because BitcoinJ library downloads blocks from the Bitcoin network and synchronizes the trade history?

// 这个过程可能需要一些时间,特别是在刚开始使用钱包时,或者你可以添加一个监听器addChangeEventListener 也行

// This process may take some time, especially when you start using the wallet, or you can add a listening device atdChangeEventListener.

while (wallet.getIssuedReceiveKeys().isEmpty()){

Thread.sleep(2000);//直接等待五秒

Thread.sleep (2000);//directly waiting for five seconds

}

ECKey ecKey=wallet.getIssuedReceiveKeys().get(0);

module.setMnemonic(list);

module.setPrivateKey(ecKey.getPrivateKeyAsHex());

module.setPublicKey(ecKey.getPublicKeyAsHex());

} catch (MnemonicException.MnemonicWordException | MnemonicException.MnemonicChecksumException |

MnemonicException.MnemonicLengthException | InterruptedException e) {

throw new RuntimeException(e);

}

return module;

}

public WalletModule createWalletByMnemonic(List mnemonic){

WalletModule module=new WalletModule();

try {

List list=mnemonic;

byte[] seed=MnemonicCode.INSTANCE.toEntropy(list);

DeterministicSeed deterministicSeed=new DeterministicSeed(list,seed,null,new Date().getTime());

Wallet wallet=Wallet.fromSeed(NetworkParameters.fromID("org.bitcoin.production"), deterministicSeed);//主网,你也可以选择测试网

Wallet wallet = Wallet.fromSeed (Network Parameters.fromID), deterministicSeed;// master network, you can also choose the test network

module.setAddress(wallet.currentReceiveAddress().toString());

wallet.getIssuedReceiveKeys();

//ps:这里为什么要做一个这样的处理,是因为BitcoinJ 库会从比特币网络中下载区块并同步交易历史,

/ps: Why do we do this here because BitcoinJ library downloads blocks from the Bitcoin network and synchronizes the trade history?

// 这个过程可能需要一些时间,特别是在刚开始使用钱包时,或者你可以添加一个监听器addChangeEventListener 也行

// This process may take some time, especially when you start using the wallet, or you can add a listening device atdChangeEventListener.

while (wallet.getIssuedReceiveKeys().isEmpty()){

Thread.sleep(2000);//直接等待五秒

Thread.sleep (2000);//directly waiting for five seconds

}

ECKey ecKey=wallet.getIssuedReceiveKeys().get(0);

module.setMnemonic(list);

module.setPrivateKey(ecKey.getPrivateKeyAsHex());

module.setPublicKey(ecKey.getPublicKeyAsHex());

} catch (MnemonicException.MnemonicWordException | MnemonicException.MnemonicChecksumException |

MnemonicException.MnemonicLengthException | InterruptedException e) {

throw new RuntimeException(e);

}

return module;

}

public WalletModule createWalletByPrivate(String privateKey){

WalletModule module=new WalletModule();

byte[] bytes=hexStringToByteArray(privateKey);

ECKey ecKey=ECKey.fromPrivate(bytes);

MainNetParams mainNetParams=MainNetParams.get();

String address=ecKey.toAddress(mainNetParams).toString();

module.setAddress(address);

module.setPrivateKey(ecKey.getPrivateKeyAsHex());

module.setPublicKey(ecKey.getPublicKeyAsHex());

return module;

}

五.实现的结果

V. Results achieved

这是生成的keyStron:

This is the generation of KeyStron:

{

"address": "ab2d749ae5ceb1de1dfc41a7b875627982b8fcef",

"id": "473b287f-fe5a-41e4-aa97-4a91b76156dc",

"version": 3,

"crypto": {

"cipher": "aes-128-ctr",

"ciphertext": "30e30fb4584ab5f85b105e7b5a5d60f45ecedbe347e768d09de8bb75c0018be5",

"cipherparams": {

"iv": "ebc6f4d1d84886c39ed3b89be4774580"

},

"kdf": "scrypt",

"kdfparams": {

"dklen": 32,

"n": 4096,

"p": 6,

"r": 8,

"salt": "72177e219fabf5555ac21f989fc9e25e10b4a2136d9498443970efdd8e69b176"

},

"mac": "e58af76dac43694732ab53905c2d8e9b0b8760d23f4921ccd55d287f67130f1c"

}

}

PS:本来想写一个test类的,但是我太懒了,所以用main方法代替了,对了源码我放在git上了,你们需要的就去下载,打包就能用,

PS: I was going to write a test class, but I was too lazy, so I replaced it with a main, and I put the source code on the git, so you can download it and pack it.

6重要信息!!

Six messages!

不过你们去下载的要给我点击Star啊! 要不然我做鬼都托梦来骂你!

But you're going to hit Star on the download, or I'm going to be dreaming about you!

我的git地址

My git address.

:GitHub - lxxGOd/BTC-ETH-TRX-

最后:我哥哥镇楼

Finally: My brother's town building.

好文链接

Good text link

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: 

美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
文字格式和图片示例

注册有任何问题请添加 微信:MVIP619 拉你进入群

弹窗与图片大小一致 文章转载注明

分享:

扫一扫在手机阅读、分享本文

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
热门文章
  • 区块链社区有哪些?区块链社区是什么?

    区块链社区有哪些?区块链社区是什么?
    展开全文...
  • 0.00006694个比特币等于多少人民币/美金

    0.00006694个比特币等于多少人民币/美金
    0.00006694比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00006694比特币等于4.53424784美元/32.5436 16人民币。比特币(BTC)美元(USDT)人民币(CNY)0.000066944.53424784【比特币密码】32.82795436 16比特币对人民币的最新汇率为:490408.64 CNY(1比特币=490408.64人民币)(1美元=7.24人民币)(0.00006694USDT=0.0004846456 CNY)汇率更新时...
  • 0.00015693个比特币等于多少人民币/美金

    0.00015693个比特币等于多少人民币/美金
    0.000 15693比特币等于多少人民币?根据比特币对人民币的最新汇率,0.000 15693比特币等于10.6 1678529美元/76.86554996人民币。比特币(BTC)【比特币价格翻倍】美元(USDT)人民币(CNY)0.000/克洛克-0/5693【数字货币矿机】10.6 167852976.8655254996比特币对人民币的最新汇率为:489,807.72 CNY(1比特币= 489,807.72人民币)(1美元=7.24人民币)(0.00015693 U...
  • 0.00003374个比特币等于多少人民币/美金

    0.00003374个比特币等于多少人民币/美金
    0.00003374比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00003374比特币等于2.2826 1222美元/16.5261124728人民币。比特币(BTC)美元(USDT)人民币(CNY)0.00003374克洛克-0/22216.5261124728比特币对人民币的最新汇率为:489807.72 CNY(1比特币=489807.72人民币)(1美元=7.24人民币)(0.00003374USDT=0.0002442776 CNY)。汇率更新于2024...
  • 2018-5-31币圈简报

    2018-5-31币圈简报
    一、要闻资讯类I. KEY INFORMATION CATEGORY1、央视:数字货币在京揭牌成立!中国或为此突变!1. View: Digital currency is established in Kyoto! China or mutated for it!中国数字货币来了˂a href="https://mp.weixin.qq.com/s/69fxKCf8GKSCscS4lT8WCA" Target="_blank"rel="noformlow"" Chinese...
标签列表