返回介绍

11.12. 钱包

发布于 2023-06-19 14:14:32 字数 4517 浏览 0 评论 0 收藏 0

11.12. 钱包

11.12.1. 创建钱包

			
package cn.netkiller.ethereum.wallet;

import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import org.web3j.crypto.CipherException;
import org.web3j.crypto.WalletUtils;

public class WalletMain {

	public void createWallet() throws NoSuchAlgorithmException, NoSuchProviderException,
			InvalidAlgorithmParameterException, CipherException, IOException {

		File file = new File("/tmp/ethereum/keystore");
		String password = "passw0rd";
		String fileName = WalletUtils.generateFullNewWalletFile(password, file);
		System.out.println(fileName);

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WalletMain wallet = new WalletMain();
		try {
			wallet.createWallet();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
				
			
			

运行结果

			
neo@MacBook-Pro ~ % mkdir -p /tmp/ethereum/keystore
neo@MacBook-Pro ~ % ll /tmp/ethereum/keystore
total 8
-rw-r--r--  1 neo  wheel   491B Feb  4 18:30 UTC--2018-02-04T10-30-58.476000000Z--75d01e920d6e018445dae504058ce4d968fd2a58.json

neo@MacBook-Pro ~ % cat /tmp/ethereum/keystore/UTC--2018-02-04T10-30-58.476000000Z--75d01e920d6e018445dae504058ce4d968fd2a58.json 
{"address":"75d01e920d6e018445dae504058ce4d968fd2a58","id":"80700448-69bc-475a-aaf9-f2b836f17b13","version":3,"crypto":{"cipher":"aes-128-ctr","ciphertext":"fe86f5dbd61d15d092f9d6870e70bff7ed99a7925703ea71eef23669c8e3ec62","cipherparams":{"iv":"d058819ab660cd062080b405591ba143"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"f69c535137b08667dbac53b8001313f5b43f81fce67a5d0e94b518c97d212d14"},"mac":"c247e34760bc838c3a4c8b2da286ccc6acec244bbc13fc6cc9ce28e88a7319d5"}}
			
			

11.12.2. 从钱包取出账号

			
package cn.netkiller.ethereum.wallet;

import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import org.web3j.crypto.CipherException;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;

public class WalletMain {

	public void walletAddress() throws IOException, CipherException {

		File file = new File(
				"/tmp/ethereum/keystore/UTC--2018-02-04T10-43-27.339000000Z--7cab470df532710d13078c5cdc0812a27f70cf51.json");
		String password = "passw0rd";
		Credentials credentials = WalletUtils.loadCredentials(password, file);
		System.out.println(credentials.getAddress());

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WalletMain wallet = new WalletMain();
		try {
			wallet.walletAddress();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
			
			

运行结果

			
0x7cab470df532710d13078c5cdc0812a27f70cf51				
			
			

11.12.3. 生成助记词钱包

			
		String keystore = WalletUtils.getDefaultKeyDirectory();
		System.out.println("生成keystore文件的默认目录:" + keystore);
		// 通过密码及keystore目录生成钱包
		Bip39Wallet wallet = WalletUtils.generateBip39Wallet("yourpassword", new File(keystore));
		// keystore文件名
		System.out.println(wallet.getFilename());
		// 12个单词的助记词
		System.out.println(wallet.getMnemonic());			
			
			

11.12.4. 随机产生助记词

			
package cn.netkiller.example.ethereum.mnemonic;

import java.security.SecureRandom;
import org.web3j.crypto.MnemonicUtils;

public class MnemonicUtilsTest {

	public MnemonicUtilsTest() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {

		// TODO Auto-generated method stub
		byte[] initialEntropy = new byte[16];
		SecureRandom secureRandom = new SecureRandom();
		secureRandom.nextBytes(initialEntropy);

		String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy);
		System.out.println(mnemonic);

	}

}
			
			
			

11.12.5. 导入 BIP39 钱包

			
		Credentials credentials = WalletUtils.loadBip39Credentials("password", "spoon crisp length scrub train scrap initial inherit airport that answer tornado");
		// 钱包地址
		System.out.println(credentials.getAddress());
		// 公钥16进制字符串表示
		System.out.println(credentials.getEcKeyPair().getPublicKey().toString(16));
		// 私钥16进制字符串表示
		System.out.println(credentials.getEcKeyPair().getPrivateKey().toString(16));			
			
			

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文