BCH-常见问题总结

区块链开发

Posted by 松下百合子 on June 27, 2019

初学BCH当做笔记记之。

文章转载自简书

elementUI 在el-row 或者 el-col 上使用@click失效

参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import org.bitcoinj.params.MainNetParams;

import com.alibaba.druid.util.Base64;
import com.bun.bip.base58.Base58;
import com.bun.bip.bip32.ExtendedKey;
import com.bun.bip.bip32.ExtendedPrivateKey;
import com.bun.bip.bip32.ExtendedPublicKey;
import com.bun.bip.bip32.HdKey;
import com.bun.bip.bip32.Network;
import com.bun.bip.bip32.WIF;
import com.bun.bip.bip32.networks.Bitcoin;
import com.bun.bip.bip32.networks.Litecoin;
import com.bun.bip.bip39.MnemonicGenerator;
import com.bun.bip.bip39.SeedCalculator;
import com.bun.bip.bip39.Words;
import com.bun.bip.bip39.wordlists.English;
import com.bun.bip.bip44.AddressIndex;
import com.bun.bip.bip44.BIP44;
import com.bun.bip.hashing.Hash160;
import com.bun.bip.utils.Hex;

import de.tobibrandt.MoneyNetwork;
import de.tobibrandt.bitcoincash.BitcoinCashAddressFormatter;
import de.tobibrandt.bitcoincash.BitcoinCashAddressType;

public class PrivateKeyTest {
	
	public static void main(String[] args) throws Exception {
		
		//while(true) {
	
		StringBuilder stringbuilder = new StringBuilder();
		byte[] entropy = new byte[Words.TWELVE.byteLength()];
		new SecureRandom().nextBytes(entropy);
		new MnemonicGenerator(English.INSTANCE)
		    .createMnemonic(entropy, stringbuilder::append);
		
		//String mnemonic =stringbuilder.toString();
		//String mnemonic ="nest ketchup duty during zone video weekend infant trophy stock huge vacant";
		
		String mnemonic = "another boy aware plastic injury glass ship brass once vast vault real";
		
		System.out.println(mnemonic);
		
        byte[] seed1 = new SeedCalculator()
        		.calculateSeed(mnemonic, "");//passphrase
        
		ExtendedPrivateKey key = ExtendedPrivateKey.fromSeed(seed1, Bitcoin.MAIN_NET);
		ExtendedPrivateKey child = key.derive("m/44'/0'/0'/0");//m/44'/0'/0'/1      1NWiVuRkF5MDy91fLbvX8KEuAWWZEpnRop    2N57QDHfkM1Uw2VT54a1AAy1gYUZfTh6urk m/44'/0'/0'/0
		//ExtendedPrivateKey child  = key;
		ExtendedPublicKey childPub = child.neuter();

		String childpk1 = Base58.base58Encode(child.extendedKeyByteArray());
		System.out.println(childpk1);
		
		HdKey child0Prk = child.cKDpriv(0).getHdKey();
		String test70 = Hex.toHexString(child0Prk.getKey());
		//child0Prk.getKey().
		//System.out.println("test70--------"+test70);
		System.out.println(test70);
		
		String test80 = Base58.base58Encode(child0Prk.getKey());
		System.out.println(test80);
		
		//String prKey = Base58.base58Encode(getPrivate(child0Prk.getKey()));
		//System.out.println(prKey);
		
		//00bca36f57d4af227b36c54d295f98e5ab4fab1d808ecc5c71594a297c007834
		//String test90 = Base64.byteArrayToBase64(child0Prk.getKey());
		//System.out.println(test90);
		//child.cKDpub(0)
		
		String normalPub = child.cKDpub(0).p2pkhAddress();
		
		
		System.out.println(normalPub);
		String xPub = child.cKDpub(0).p2shAddress();
		System.out.println(xPub);
		
		String keyPri2 = WIF.getWif(child0Prk,Bitcoin.MAIN_NET);
		System.out.println(keyPri2);
		
		System.out.println("==================LTC==================");
		
		
		
		ExtendedPrivateKey ltcKey = ExtendedPrivateKey.fromSeed(seed1,Litecoin.MAIN_NET);
		ExtendedPrivateKey LtcChild = ltcKey.derive("m/44'/2'/0'/0");
		HdKey ltcChild0Prk = LtcChild.cKDpriv(0).getHdKey();
		
		String ltcChildpk1 = Base58.base58Encode(LtcChild.extendedKeyByteArray());
		System.out.println(ltcChildpk1);
		
		String ltcTest70 = Hex.toHexString(ltcChild0Prk.getKey());
		//child0Prk.getKey().
		//System.out.println("test70--------"+test70);
		System.out.println(ltcTest70);
		
		String ltcTest80 = Base58.base58Encode(ltcChild0Prk.getKey());
		System.out.println("ltcTest80++="+ltcTest80);
		
		String ltcNormalPub = LtcChild.cKDpub(0).p2pkhAddress();
		
		System.out.println(ltcNormalPub);
		
		String ltcKeyPri = WIF.getWif(ltcChild0Prk,Litecoin.MAIN_NET);
		System.out.println(ltcKeyPri);
		
		
		
		System.out.println("==================BCH==================");
		
		ExtendedPrivateKey bchKey = ExtendedPrivateKey.fromSeed(seed1,Bitcoin.MAIN_NET);
		ExtendedPrivateKey bchChild = bchKey.derive("m/44'/145'/0'/0");
		HdKey bchChild0Prk = bchChild.cKDpriv(0).getHdKey();
		
		String bchChildpk1 = Base58.base58Encode(bchChild.extendedKeyByteArray());
		System.out.println(bchChildpk1);
		
		String bchTest70 = Hex.toHexString(bchChild0Prk.getKey());
		System.out.println(bchTest70);
		
		
		
		
		
		
		
		
		String bchTest80 = Base58.base58Encode(bchChild0Prk.getKey());
		System.out.println("bchTest80++="+bchTest80);
		
		String bchNormalPub = bchChild.cKDpub(0).p2pkhAddress();
		
		
		
		System.out.println(bchNormalPub);
		
		
		 
		
		
		
		String bchKeyPri = WIF.getWif(bchChild0Prk,Bitcoin.MAIN_NET);
		System.out.println(bchKeyPri);
		
		NetworkParameters main = MainNetParams.get();
		Address addr = Address.fromBase58(main, bchNormalPub);
		System.out.print("\n");
	    byte[] hash = addr.getHash160();
	    
//	    byte[] sha256hash160 = Utils.sha256hash160(Base58.base58Decode(bchNormalPub));
		byte[] test = bchChild.cKDpub(0).p2pkhAddress().getBytes();
		
	    String cashAddress = BitcoinCashAddressFormatter.toCashAddress(BitcoinCashAddressType.P2PKH, hash,
				MoneyNetwork.MAIN);
		String cashAddresstest = BitcoinCashAddressFormatter.toCashAddress(BitcoinCashAddressType.P2PKH, test,
				MoneyNetwork.MAIN);
		
		//child0Prk.getKey().
		//System.out.println("test70--------"+test70);
		System.out.println("=========cashAddress:"+cashAddress);
		System.out.println("=========cashAddresstest:"+cashAddresstest);
		
		
	}
	
	/*public static byte[] getPrivate(byte[] p)
    {
		//byte[] p = priv.toByteArray();
		if (p.length != 32) {
			byte[] tmp = new byte[32];
			System.arraycopy(p, Math.max(0, p.length - 32), tmp, Math.max(0, 32 - p.length), Math.min(32, p.length));
			p = tmp;
		}
		return p;
    }
	
	private static byte[] getWIFBytes(byte[] k,boolean flag) throws Exception {
           // byte[] k = getPrivate();
            if (flag) {
                byte[] encoded = new byte[k.length + 6];
                byte[] ek = new byte[k.length + 2];
                ek[0] = (byte) 0x80;
                System.arraycopy(k, 0, ek, 1, k.length);
                ek[k.length + 1] = 0x01;
                byte[] hash = Hash160.ripemd160(ek);
                System.arraycopy(ek, 0, encoded, 0, ek.length);
               // System.arraycopy(hash, 0, encoded, ek.length, 4);
                return encoded;
            } else {
                byte[] encoded = new byte[k.length + 5];
                byte[] ek = new byte[k.length + 1];
                ek[0] = (byte) 0x80;
                System.arraycopy(k, 0, ek, 1, k.length);
                byte[] hash = Hash160.hash160(ek);
                System.arraycopy(ek, 0, encoded, 0, ek.length);
                System.arraycopy(hash, 0, encoded, ek.length, 4);
                return encoded;
            }
    }
	
	
	
	private static final byte[] xprv = new byte[] { 0x04, (byte) 0x88, (byte) 0xAD, (byte) 0xE4 };
*/
}