forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeystoreFactory.java
More file actions
executable file
·165 lines (149 loc) · 4.83 KB
/
KeystoreFactory.java
File metadata and controls
executable file
·165 lines (149 loc) · 4.83 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
package org.tron.program;
import com.beust.jcommander.JCommander;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Utils;
import org.tron.core.Constant;
import org.tron.core.config.args.Args;
import org.tron.keystore.CipherException;
import org.tron.keystore.Credentials;
import org.tron.keystore.WalletUtils;
public class KeystoreFactory {
private static final Logger logger = LoggerFactory.getLogger("KeystoreFactory");
private static final String FilePath = "Wallet";
private boolean priKeyValid(String priKey) {
if (StringUtils.isEmpty(priKey)) {
logger.warn("Warning: PrivateKey is empty !!");
return false;
}
if (priKey.length() != 64) {
logger.warn("Warning: PrivateKey length need 64 but " + priKey.length() + " !!");
return false;
}
//Other rule;
return true;
}
private void genKeystore() throws CipherException, IOException {
String password = WalletUtils.inputPassword2Twice();
ECKey eCkey = new ECKey(Utils.random);
File file = new File(FilePath);
if (!file.exists()) {
if (!file.mkdir()) {
throw new IOException("Make directory faild!");
}
} else {
if (!file.isDirectory()) {
if (file.delete()) {
if (!file.mkdir()) {
throw new IOException("Make directory faild!");
}
} else {
throw new IOException("File is exists and can not delete!");
}
}
}
String fileName = WalletUtils.generateWalletFile(password, eCkey, file, true);
System.out.println("Gen a keystore its name " + fileName);
Credentials credentials = WalletUtils.loadCredentials(password, new File(file, fileName));
System.out.println("Your address is " + credentials.getAddress());
}
private void importPrivatekey() throws CipherException, IOException {
Scanner in = new Scanner(System.in);
String privateKey;
System.out.println("Please input private key.");
while (true) {
String input = in.nextLine().trim();
privateKey = input.split("\\s+")[0];
if (priKeyValid(privateKey)) {
break;
}
System.out.println("Invalid private key, please input again.");
}
String password = WalletUtils.inputPassword2Twice();
ECKey eCkey = ECKey.fromPrivate(ByteArray.fromHexString(privateKey));
File file = new File(FilePath);
if (!file.exists()) {
if (!file.mkdir()) {
throw new IOException("Make directory faild!");
}
} else {
if (!file.isDirectory()) {
if (file.delete()) {
if (!file.mkdir()) {
throw new IOException("Make directory faild!");
}
} else {
throw new IOException("File is exists and can not delete!");
}
}
}
String fileName = WalletUtils.generateWalletFile(password, eCkey, file, true);
System.out.println("Gen a keystore its name " + fileName);
Credentials credentials = WalletUtils.loadCredentials(password, new File(file, fileName));
System.out.println("Your address is " + credentials.getAddress());
}
private void help() {
System.out.println("You can enter the following command: ");
System.out.println("GenKeystore");
System.out.println("ImportPrivatekey");
System.out.println("Exit or Quit");
System.out.println("Input any one of then, you will get more tips.");
}
private void run() {
Scanner in = new Scanner(System.in);
help();
while (in.hasNextLine()) {
try {
String cmdLine = in.nextLine().trim();
String[] cmdArray = cmdLine.split("\\s+");
// split on trim() string will always return at the minimum: [""]
String cmd = cmdArray[0];
if ("".equals(cmd)) {
continue;
}
String cmdLowerCase = cmd.toLowerCase();
switch (cmdLowerCase) {
case "help": {
help();
break;
}
case "genkeystore": {
genKeystore();
break;
}
case "importprivatekey": {
importPrivatekey();
break;
}
case "exit":
case "quit": {
System.out.println("Exit !!!");
in.close();
return;
}
default: {
System.out.println("Invalid cmd: " + cmd);
help();
}
}
} catch (Exception e) {
logger.error(e.getMessage());
}
}
}
public static void main(String[] args) {
Args.setParam(args, Constant.TESTNET_CONF);
KeystoreFactory cli = new KeystoreFactory();
JCommander.newBuilder()
.addObject(cli)
.build()
.parse(args);
cli.run();
}
}