-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.java
159 lines (134 loc) · 6.41 KB
/
Program.java
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
/*
* Copyright 2017 Bepal
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that
* the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package pro.bepal;
import com.google.common.collect.ImmutableList;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.LegacyAddress;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.crypto.*;
import org.bitcoinj.params.MainNetParams;
import org.testng.annotations.Test;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
/**
* bip-0039 implement
* <span>https://github.com/Bepal/bips/blob/master/bip-0039.mediawiki</span>
* bip-0044 implement
* <span>https://github.com/Bepal/bips/blob/master/bip-0044.mediawiki</span>
*
* @author bepal
*/
public class Program {
/**
* Chinese text typically does not use any spaces as word separators.
* For the sake of uniformity, we propose to use normal ASCII spaces (0x20) to separate words as per standard.
*
* <span>https://github.com/Bepal/bips/blob/master/bip-0039/chinese_simplified.txt</span>
*/
private static final String BIP39_CHINESE_RESOURCE_NAME = "mnemonic/wordlist/zh_cn.txt";
private static final String BIP39_CHINESE_SHA256 = "bfd683b91db88609fabad8968c7efe4bf69606bf5a49ac4a4ba5e355955670cb";
/**
* DefaultWords English text
*
* <span>https://github.com/Bepal/bips/blob/master/bip-0039/english.txt</span>
*/
private static final String BIP39_ENGLISH_RESOURCE_NAME = "mnemonic/wordlist/english.txt";
/**
* create master private key with mnemonicCode
*
* @return
*/
private DeterministicKey createMasterPrivateKey() {
//private key seed
byte[] seed = null;
try {
//load mnemonic code,use chinese
InputStream stream = MnemonicCode.class.getResourceAsStream(BIP39_CHINESE_RESOURCE_NAME);
if (stream == null) {
throw new FileNotFoundException(BIP39_CHINESE_RESOURCE_NAME);
}
MnemonicCode mnemonicCode = new MnemonicCode(stream, BIP39_CHINESE_SHA256);
SecureRandom secureRandom = new SecureRandom();
byte[] randomData = secureRandom.random();
//random generate mnemonic code
List<String> seedCode = mnemonicCode.toMnemonic(randomData);
//print mnemonic code
int size = seedCode.size();
for (int i = 0; i < size; i++) {
System.out.println("index:" + i + ",code:" + seedCode.get(i));
}
seed = MnemonicCode.toSeed(seedCode, "");
} catch (Exception e) {
e.printStackTrace();
System.err.println("create seed fail");
}
if (seed == null || seed.length == 0) {
System.err.println("seed is null");
}
//create master private key
DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(seed);
return masterPrivateKey;
}
@Test
public void testCreateMasterPrivateKey() {
DeterministicKey masterPrivateKey = createMasterPrivateKey();
System.out.println(masterPrivateKey.getPrivKey());
}
/**
* create bitcoin address from root private key
* implement bip-0044
*
* @param rootKey root private key
* @param count address count
*/
private void createBitcoinAddress(DeterministicKey rootKey, Integer count) {
DeterministicHierarchy rootHierarchy = new DeterministicHierarchy(rootKey);
//implement bip-0044 bitcoin path m / 44' / 0' / 0'
ImmutableList<ChildNumber> thisRootPath = ImmutableList.of(new ChildNumber(44, true),
new ChildNumber(0, true), ChildNumber.ZERO_HARDENED);
DeterministicHierarchy hierarchy = new DeterministicHierarchy(rootHierarchy.get(thisRootPath, false, true));
//bitcoin network parameters
NetworkParameters parameters = MainNetParams.get();
//create external address and internal address
for (int i = 0; i < count; i++) {
ECKey ecKey = hierarchy.get(ImmutableList.of(ChildNumber.ZERO, new ChildNumber(i)), true, true);
System.out.printf("external address %d: %s\n", i + 1, LegacyAddress.fromPubKeyHash(parameters, ecKey.getPubKeyHash()).toBase58());
}
for (int i = 0; i < count; i++) {
ECKey ecKey = hierarchy.get(ImmutableList.of(ChildNumber.ONE, new ChildNumber(i)), true, true);
System.out.printf("internal address %d: %s\n", i + 1, LegacyAddress.fromPubKeyHash(parameters, ecKey.getPubKeyHash()).toBase58());
}
}
@Test
public void testCreateBitcoinAddress() {
// uncomment this four line code , use you own mnemonic code
// String[] words = new String[]{"a","a","a","a","a","a","a","a","a","a","a","a"};
// List<String> seedCode = Arrays.asList(words);
// byte[] seed = MnemonicCode.toSeed(seedCode, "");
// DeterministicKey rootKey = HDKeyDerivation.createMasterPrivateKey(seed);
DeterministicKey rootKey = createMasterPrivateKey();
createBitcoinAddress(rootKey, 10);
}
}