-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathWalletUtils.java
More file actions
166 lines (140 loc) · 5.49 KB
/
WalletUtils.java
File metadata and controls
166 lines (140 loc) · 5.49 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
package org.tron.keystore;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
import org.tron.common.crypto.SignInterface;
import org.tron.common.crypto.SignUtils;
import org.tron.common.utils.Utils;
import org.tron.core.exception.CipherException;
/**
* Utility functions for working with Wallet files.
*/
public class WalletUtils {
private static final ObjectMapper objectMapper = new ObjectMapper();
static {
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public static String generateFullNewWalletFile(String password, File destinationDirectory,
boolean ecKey)
throws NoSuchAlgorithmException, NoSuchProviderException,
InvalidAlgorithmParameterException, CipherException, IOException {
return generateNewWalletFile(password, destinationDirectory, true, ecKey);
}
public static String generateLightNewWalletFile(String password, File destinationDirectory,
boolean ecKey)
throws NoSuchAlgorithmException, NoSuchProviderException,
InvalidAlgorithmParameterException, CipherException, IOException {
return generateNewWalletFile(password, destinationDirectory, false, ecKey);
}
public static String generateNewWalletFile(
String password, File destinationDirectory, boolean useFullScrypt, boolean ecKey)
throws CipherException, IOException, InvalidAlgorithmParameterException,
NoSuchAlgorithmException, NoSuchProviderException {
SignInterface ecKeyPair = SignUtils.getGeneratedRandomSign(Utils.getRandom(), ecKey);
return generateWalletFile(password, ecKeyPair, destinationDirectory, useFullScrypt);
}
public static String generateWalletFile(
String password, SignInterface ecKeyPair, File destinationDirectory, boolean useFullScrypt)
throws CipherException, IOException {
WalletFile walletFile;
if (useFullScrypt) {
walletFile = Wallet.createStandard(password, ecKeyPair);
} else {
walletFile = Wallet.createLight(password, ecKeyPair);
}
String fileName = getWalletFileName(walletFile);
File destination = new File(destinationDirectory, fileName);
objectMapper.writeValue(destination, walletFile);
return fileName;
}
public static Credentials loadCredentials(String password, File source, boolean ecKey)
throws IOException, CipherException {
WalletFile walletFile = objectMapper.readValue(source, WalletFile.class);
return Credentials.create(Wallet.decrypt(password, walletFile, ecKey));
}
public static String getWalletFileName(WalletFile walletFile) {
DateTimeFormatter format = DateTimeFormatter.ofPattern(
"'UTC--'yyyy-MM-dd'T'HH-mm-ss.nVV'--'");
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
return now.format(format) + walletFile.getAddress() + ".json";
}
public static String getDefaultKeyDirectory() {
return getDefaultKeyDirectory(System.getProperty("os.name"));
}
static String getDefaultKeyDirectory(String osName1) {
String osName = osName1.toLowerCase();
if (osName.startsWith("mac")) {
return String.format(
"%s%sLibrary%sEthereum", System.getProperty("user.home"), File.separator,
File.separator);
} else if (osName.startsWith("win")) {
return String.format("%s%sEthereum", System.getenv("APPDATA"), File.separator);
} else {
return String.format("%s%s.ethereum", System.getProperty("user.home"), File.separator);
}
}
public static String getTestnetKeyDirectory() {
return String.format(
"%s%stestnet%skeystore", getDefaultKeyDirectory(), File.separator, File.separator);
}
public static String getMainnetKeyDirectory() {
return String.format("%s%skeystore", getDefaultKeyDirectory(), File.separator);
}
public static boolean passwordValid(String password) {
if (StringUtils.isEmpty(password)) {
return false;
}
if (password.length() < 6) {
return false;
}
//Other rule;
return true;
}
public static String inputPassword() {
Scanner in = null;
String password;
Console cons = System.console();
if (cons == null) {
in = new Scanner(System.in);
}
while (true) {
if (cons != null) {
char[] pwd = cons.readPassword("password: ");
password = String.valueOf(pwd);
} else {
String input = in.nextLine().trim();
password = input.split("\\s+")[0];
}
if (passwordValid(password)) {
return password;
}
System.out.println("Invalid password, please input again.");
}
}
public static String inputPassword2Twice() {
String password0;
while (true) {
System.out.println("Please input password.");
password0 = inputPassword();
System.out.println("Please input password again.");
String password1 = inputPassword();
if (password0.equals(password1)) {
break;
}
System.out.println("Two passwords do not match, please input again.");
}
return password0;
}
}