Repository navigation
Expand file tree
/
Copy pathWalletClient.java
More file actions
235 lines (206 loc) · 6.37 KB
/
Copy pathWalletClient.java
File metadata and controls
235 lines (206 loc) · 6.37 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.tron.core;
import com.google.protobuf.ByteString;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.util.encoders.Hex;
import org.tron.common.crypto.ECKey;
import org.tron.common.crypto.Hash;
import org.tron.common.crypto.SymmEncoder;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.FileUtil;
import org.tron.common.utils.Utils;
import org.tron.protos.core.TronTransaction.Transaction;
public class WalletClient {
private static final Logger logger = LoggerFactory.getLogger("WalletClient");
private static final String FilePath = "Wallet";
private ECKey ecKey = null;
private boolean loginState = false;
/**
* Creates a new WalletClient with a random ECKey or no ECKey.
*/
public WalletClient(boolean genEcKey) {
if (genEcKey) {
this.ecKey = new ECKey(Utils.getRandom());
}
}
// Create Wallet with a pritKey
public WalletClient(String priKey) {
ECKey temKey = null;
try {
BigInteger priK = new BigInteger(priKey, 16);
temKey = ECKey.fromPrivate(priK);
} catch (Exception ex) {
ex.printStackTrace();
}
this.ecKey = temKey;
}
public static byte[] getPassWord(String password) {
if (password == null || "".equals(password)) {
return null;
}
byte[] pwd;
pwd = Hash.sha256(password.getBytes());
pwd = Hash.sha256(pwd);
pwd = Arrays.copyOfRange(pwd, 0, 16);
return pwd;
}
public static byte[] getEncKey(String password) {
if (password == null || "".equals(password)) {
return null;
}
byte[] encKey;
encKey = Hash.sha256(password.getBytes());
encKey = Arrays.copyOfRange(encKey, 0, 16);
return encKey;
}
public static boolean checkPassWord(String password) {
if (password == null || "".equals(password)) {
return false;
}
byte[] pwd = getPassWord(password);
String pwdAsc = ByteArray.toHexString(pwd);
String pwdInstore = loadPassword();
return pwdAsc.equals(pwdInstore);
}
public static boolean passwordValid(String password) {
if (password == null || "".equals(password)) {
return false;
}
if (password.length() < 6) {
return false;
}
//Other rule;
return true;
}
public boolean login(String password) {
if (password == null || "".equals(password)) {
return false;
}
loginState = checkPassWord(password);
return loginState;
}
public boolean isLoginState() {
return loginState;
}
public void logout() {
loginState = false;
}
/**
* Get a Wallet from storage
*/
public static WalletClient GetWalletByStorage(String password) {
String priKeyEnced = loadPriKey();
if (priKeyEnced == null) {
return null;
}
//dec priKey
byte[] priKeyAscEnced = priKeyEnced.getBytes();
byte[] priKeyHexEnced = Hex.decode(priKeyAscEnced);
byte[] aesKey = getEncKey(password);
byte[] priKeyHexPlain = SymmEncoder.AES128EcbDec(priKeyHexEnced, aesKey);
String priKeyPlain = Hex.toHexString(priKeyHexPlain);
return new WalletClient(priKeyPlain);
}
/**
* Creates a Wallet with an existing ECKey.
*/
public WalletClient(final ECKey ecKey) {
this.ecKey = ecKey;
}
public ECKey getEcKey() {
return ecKey;
}
public byte[] getAddress() {
return ecKey.getAddress();
}
/**
* Get a Wallet from storage
*/
public static WalletClient GetWalletByStorageIgnorPrivKey() {
try {
String pubKey = loadPubKey(); //04 PubKey[128]
byte[] pubKeyAsc = pubKey.getBytes();
byte[] pubKeyHex = Hex.decode(pubKeyAsc);
ECKey eccKey = ECKey.fromPublicOnly(pubKeyHex);
return new WalletClient(eccKey);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static String getAddressByStorage() {
try {
String pubKey = loadPubKey(); //04 PubKey[128]
byte[] pubKeyAsc = pubKey.getBytes();
byte[] pubKeyHex = Hex.decode(pubKeyAsc);
ECKey eccKey = ECKey.fromPublicOnly(pubKeyHex);
return ByteArray.toHexString(eccKey.getAddress());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public void store(String password) {
if (ecKey == null || ecKey.getPrivKey() == null) {
logger.warn("store wallet fail, PrivKey is null !!");
}
byte[] pwd = getPassWord(password);
String pwdAsc = ByteArray.toHexString(pwd);
byte[] privKeyPlain = ecKey.getPrivKeyBytes();
//encrypted by password
byte[] aseKey = getEncKey(password);
byte[] privKeyEnced = SymmEncoder.AES128EcbEnc(privKeyPlain, aseKey);
String privKeyStr = ByteArray.toHexString(privKeyEnced);
byte[] pubKeyBytes = ecKey.getPubKey();
String pubKeyStr = ByteArray.toHexString(pubKeyBytes);
// SAVE PASSWORD
FileUtil.saveData(FilePath, pwdAsc, false);//ofset:0 len:32
// SAVE PUBKEY
FileUtil.saveData(FilePath, pubKeyStr, true);//ofset:32 len:130
// SAVE PRIKEY
FileUtil.saveData(FilePath, privKeyStr, true);
}
private static String loadPassword() {
char[] buf = new char[0x100];
int len = FileUtil.readData(FilePath, buf);
if (len != 226) {
return null;
}
return String.valueOf(buf, 0, 32);
}
public static String loadPubKey() {
char[] buf = new char[0x100];
int len = FileUtil.readData(FilePath, buf);
if (len != 226) {
return null;
}
return String.valueOf(buf, 32, 130);
}
private static String loadPriKey() {
char[] buf = new char[0x100];
int len = FileUtil.readData(FilePath, buf);
if (len != 226) {
return null;
}
return String.valueOf(buf, 162, 64);
}
public long getBalance() {
byte[] address;
if (this.ecKey == null) {
} else {
address = getAddress();
}
//getBalance(address);//call rpc
return 0;
}
public Transaction signTransaction(Transaction tx, List<ByteString> pubKeyHashList) {
if (this.ecKey == null || this.ecKey.getPrivKey() == null) {
logger.error("ERROR: Can't sign,there is no private key !!");
return null;
}
return TransactionUtils.sign(tx, this.ecKey, pubKeyHashList);
}
}