Skip to content

Commit 6c9fab2

Browse files
BarbatosBarbatos
authored andcommitted
fix(plugins): handle duplicate-address keystores and add import warning
- KeystoreUpdate: findKeystoreByAddress now detects multiple keystores with the same address and returns an error with file list, instead of silently picking one nondeterministically. - KeystoreImport: scan directory before writing and print WARNING if a keystore for the same address already exists. Import still proceeds (legitimate use case) but user is made aware. - KeystoreUpdate: strip UTF-8 BOM from password file before parsing. - KeystoreUpdate: add comment clarifying old password skips validation. - KeystoreList: add version != 3 check to filter non-keystore JSON.
1 parent 1492a49 commit 6c9fab2

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

plugins/src/main/java/common/org/tron/plugins/KeystoreImport.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ public Integer call() {
7676
+ " for the selected algorithm.");
7777
return 1;
7878
}
79+
String address = Credentials.create(keyPair).getAddress();
80+
warnIfAddressExists(keystoreDir, address);
7981
String fileName = WalletUtils.generateWalletFile(password, keyPair, keystoreDir, true);
8082
KeystoreCliUtils.setOwnerOnly(new File(keystoreDir, fileName));
81-
82-
String address = Credentials.create(keyPair).getAddress();
8383
if (json) {
8484
KeystoreCliUtils.printJson(KeystoreCliUtils.jsonMap(
8585
"address", address, "file", fileName));
@@ -136,4 +136,29 @@ private String readPrivateKey() throws IOException {
136136
private boolean isValidPrivateKey(String key) {
137137
return !StringUtils.isEmpty(key) && HEX_PATTERN.matcher(key).matches();
138138
}
139+
140+
private void warnIfAddressExists(File dir, String address) {
141+
if (!dir.exists() || !dir.isDirectory()) {
142+
return;
143+
}
144+
File[] files = dir.listFiles((d, name) -> name.endsWith(".json"));
145+
if (files == null) {
146+
return;
147+
}
148+
com.fasterxml.jackson.databind.ObjectMapper mapper =
149+
KeystoreCliUtils.mapper();
150+
for (File file : files) {
151+
try {
152+
org.tron.keystore.WalletFile wf =
153+
mapper.readValue(file, org.tron.keystore.WalletFile.class);
154+
if (address.equals(wf.getAddress())) {
155+
System.err.println("WARNING: keystore for address "
156+
+ address + " already exists: " + file.getName());
157+
return;
158+
}
159+
} catch (Exception e) {
160+
// Skip invalid files
161+
}
162+
}
163+
}
139164
}

plugins/src/main/java/common/org/tron/plugins/KeystoreUpdate.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,26 @@ private File findKeystoreByAddress(String targetAddress) {
183183
if (files == null) {
184184
return null;
185185
}
186+
java.util.List<File> matches = new java.util.ArrayList<>();
186187
for (File file : files) {
187188
try {
188189
WalletFile wf = MAPPER.readValue(file, WalletFile.class);
189190
if (targetAddress.equals(wf.getAddress())) {
190-
return file;
191+
matches.add(file);
191192
}
192193
} catch (Exception e) {
193194
// Skip invalid files
194195
}
195196
}
196-
return null;
197+
if (matches.size() > 1) {
198+
System.err.println("Multiple keystores found for address "
199+
+ targetAddress + ":");
200+
for (File m : matches) {
201+
System.err.println(" " + m.getName());
202+
}
203+
System.err.println("Please remove duplicates and retry.");
204+
return null;
205+
}
206+
return matches.isEmpty() ? null : matches.get(0);
197207
}
198208
}

0 commit comments

Comments
 (0)