区块链基础:钱包设计

1、钱包设计

package com.blockchain.model;

import java.util.Map;
import com.blockchain.security.CryptoUtil;
import com.blockchain.security.RSACoder;

/** * 钱包:公钥、私钥、钱包地址 */
public class Wallet {

    /** * 公钥 */
    private String publicKey;
    /** * 私钥 */
    private String privateKey;
    /** * 钱包地址 */
    private String address=null;

    /** * 隐藏无参构造器 */
    private Wallet() {}

    /** * 只包含公钥的钱包,用来给其他节点使用,其他节点在转账时需要用到 * @param publicKey */
    public Wallet(String publicKey) {
        this.publicKey = publicKey;
        this.address= CryptoUtil.md5(hashPubKey(publicKey));
    }
    /** * 该构造器仅供静态方法createWallet()使用 * @param publicKey * @param privateKey */
    private Wallet(String publicKey, String privateKey) {
        this.publicKey = publicKey;
        this.privateKey = privateKey;
        this.address= CryptoUtil.md5(hashPubKey(publicKey));
    }

    /** * 生成一个新钱包,也就是生成新的密钥对 * @return */
    public static Wallet createWallet() {
        Map 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;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    /** * 获取钱包地址:由公钥生成钱包地址 * @return */
    public String getAddress() {
        //单例模式,不需要重复计算地址
        if(null==address){
            String publicKeyHash = hashPubKey(publicKey);
            return CryptoUtil.md5(publicKeyHash);
        }else{
            return address;
        }
    }
    /** * 静态方法:根据指定钱包公钥生成钱包地址 * @param publicKey * @return */
    public static String getAddress(String publicKey) {
        String publicKeyHash = hashPubKey(publicKey);
        return CryptoUtil.md5(publicKeyHash);
    }

    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;
    }

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

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

}

2、测试代码

package com.blockchian.model;

import org.junit.Before;
import org.junit.Test;
import com.blockchain.model.Wallet;

public class WalletTest {
    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testWallet() throws Exception {
        Wallet wallet=Wallet.createWallet();
        System.out.println("钱包地址:\n"+wallet.getAddress());
        System.out.println("钱包公钥:\n"+wallet.getPublicKey());
        System.out.println("钱包私钥:\n"+wallet.getPrivateKey());
    }
}

测试结果

钱包地址:
a2e2b8849c1d874d14884e914a8a0c20
钱包公钥:
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCA0ojSpUpztE4RzvzcYJYnLjYR5XHVkwxdAWeY
bYsZqaEJDLVDDGcaS0LCfVACbvswfHikm8XBML0umVTCAof6gHToAqrNziOuAzjP3Quu25iD13er
wOgoxT7DTNF99GZaFo2dgOS+IYFwqlyZ6yW9QKbAUm1a3PyGI6ZqxFjedwIDAQAB

钱包私钥:
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIDSiNKlSnO0ThHO/NxglicuNhHl
cdWTDF0BZ5htixmpoQkMtUMMZxpLQsJ9UAJu+zB8eKSbxcEwvS6ZVMICh/qAdOgCqs3OI64DOM/d
C67bmIPXd6vA6CjFPsNM0X30ZloWjZ2A5L4hgXCqXJnrJb1ApsBSbVrc/IYjpmrEWN53AgMBAAEC
gYBB1dbQ+GJdUXdYiJUXt1Dlgytk6ALOKJF38qDT+J/PJK7E5ja4pl0GxhG/PbWnGLrM47/wOB/P
hK4aY4g6gnqlIgx8L4gGlXtoGtOIuHC8tm+JAff2/puXZl7AGGBJggS2Z7WiUjcKoTxxj86Youc9
Ur7P8dH4b7hU/raMqmS96QJBANTa9Sv/ps/SEubFdy6UYSbWUxVH3Ff7DooiUmEuycMY9LM4XfAs
mxbR+oKOa/F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