Java language analyzes the principle of block chain wallet generation

Java language analyzes the principle of block chain wallet generation:

1. The technical principle of blockchain wallet implementation is roughly as follows:

The wallet mnemonic generates a seed, the seed germinates, and the fruit is the private key. The private key derives the public key, and the excerpt of the public key data becomes the wallet address. At the same time, the wallet provides the Key Store, which is also a file encrypted by the private key in order to cooperate with the normal password and facilitate the user's wallet.

Public key: It is a concept in cryptography. Obtained by an algorithm, the algorithm is to obtain a pair of secret keys: public key and private key. The public key belongs to asymmetric encryption and is the public part of the key pair.

Private key: It is a concept in cryptography. Obtained through an algorithm, the algorithm is to obtain a pair of secret keys: public key and private key, the private key is the non-public part of the secret key pair, and the holder of the private key is the holder of the digital currency.

Wallet address: It is similar to the card number of each bank card. A person can apply for multiple bank cards with his ID card in the bank. Similarly, he can also have multiple wallet addresses. The wallet address is converted from the public key in the pair of keys, so a wallet address can only correspond to one private key.

The birth of the wallet mnemonic is that the private key is too difficult to remember, but the security of the wallet must be ensured. Under normal circumstances, the mnemonic consists of some words. As long as you remember these words and enter them in the wallet in order, you can also open the wallet.

Bitcoin wallet generation principle reference

2. Wallet generates pseudo code:

package com.blockchain.model;

import java.util.Map;

import com.blockchain.security.CryptoUtil;
import com.blockchain.security.RSACoder;

/**
 * 钱包
 * @author nandao
 */
public class Wallet {
	/**
	 * 公钥
	 */
	private String publicKey;
	/**
	 * 私钥
	 */
	private String privateKey;

	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}

	public Wallet() {
    }
	
	/**
	 * 只包含公钥的钱包,用来给其他节点使用,其他节点在转账时需要用到
	 * @param publicKey
	 */
	public Wallet(String publicKey) {
		this.publicKey = publicKey;
	}
	
	public Wallet(String publicKey, String privateKey) {
		this.publicKey = publicKey;
		this.privateKey = privateKey;
	}
	
	public static Wallet generateWallet() {
		Map<String, Object> initKey;
		try {
			// 本地生成公私钥对
			initKey = RSACoder.initKey();
			String publicKey = RSACoder.getPublicKey(initKey);
			String privateKey = RSACoder.getPrivateKey(initKey);
			return new Wallet(publicKey, privateKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取钱包地址
	 * @return
	 */
	public String getAddress() {
		String publicKeyHash = hashPubKey(publicKey);
		return CryptoUtil.MD5(publicKeyHash);   
	}

	/**
	 * 根据钱包公钥生成钱包地址
	 * @param publicKey
	 * @return
	 */
	public static String getAddress(String publicKey) {
		String publicKeyHash = hashPubKey(publicKey);
		return CryptoUtil.MD5(publicKeyHash);
	}

	/**
	 * 获取钱包公钥hash
	 * @return
	 */
	public String getHashPubKey() {
		return CryptoUtil.SHA256(publicKey);
	}

	/**
	 * 生成钱包公钥hash
	 * @param publicKey
	 * @return
	 */
	public static String hashPubKey(String publicKey) {
		return CryptoUtil.SHA256(publicKey);
	}

}

Tool class for generating wallet addresses:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * 加密工具类
 * @author nandao
 */
public class CryptoUtil {
	private CryptoUtil() {
	}

	public static String SHA256(String str) {
		MessageDigest messageDigest;
		String encodeStr = "";
		try {
			messageDigest = MessageDigest.getInstance("SHA-256");
			messageDigest.update(str.getBytes("UTF-8"));
			encodeStr = byte2Hex(messageDigest.digest());
		} catch (Exception e) {
			System.out.println("getSHA256 is error" + e.getMessage());
		}
		return encodeStr;
	}

	public static String MD5(String str) {  
        try {  
            StringBuffer buffer = new StringBuffer();  
            char[] chars = new char[]{'0','1','2','3',  
                    '4','5','6','7','8','9','A','B','C','D','E','F'};  
            byte [] bytes = str.getBytes();  
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");  
            byte[] targ = messageDigest.digest(bytes);  
            for(byte b:targ) {  
                buffer.append(chars[(b>>4)&0x0F]);  
                buffer.append(chars[b&0x0F]);  
            }  
            return buffer.toString();  
        } catch (NoSuchAlgorithmException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return null;  
    } 

	public static String UUID() {
		return UUID.randomUUID().toString().replaceAll("\\-", "");
	}

	private static String byte2Hex(byte[] bytes) {
		StringBuilder builder = new StringBuilder();
		String temp;
		for (int i = 0; i < bytes.length; i++) {
			temp = Integer.toHexString(bytes[i] & 0xFF);
			if (temp.length() == 1) {
				builder.append("0");
			}
			builder.append(temp);
		}
		return builder.toString();
	}
	
}

Test code to get wallet address:

	@Test
	public void testGenWallet() throws Exception {
		//获取钱包
		Wallet wallet = Wallet.generateWallet();
		//获取钱包地址
		String walletAddress = Wallet.getAddress(wallet.getPublicKey());
		System.out.println("地址钱包:"+ walletAddress);
		System.out.println(JSON.toJSON(wallet));
	}

At this point, the wallet address generation scheme is shared.

Guess you like

Origin blog.csdn.net/nandao158/article/details/125932332