Skip to content

Commit f224b44

Browse files
committed
feature(cmd): add Address This Command
1 parent 1bdfd5d commit f224b44

File tree

8 files changed

+373
-1
lines changed

8 files changed

+373
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.tron.core.dao;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class AddressEntry {
9+
private String name;
10+
private String address;
11+
private String note;
12+
13+
public AddressEntry(String name, String address, String note) {
14+
this.name = name;
15+
this.address = address;
16+
this.note = note;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return String.format("%-15s %-35s %s", name, address, note == null ? "" : note);
22+
}
23+
24+
public String toFileString() {
25+
return String.join(",", name, address, note == null ? "" : note);
26+
}
27+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package org.tron.core.service;
2+
3+
import java.util.List;
4+
import java.util.Scanner;
5+
import org.tron.core.dao.AddressEntry;
6+
7+
public class AddressBookInteractive {
8+
private final AddressBookService addressBook;
9+
private final Scanner scanner = new Scanner(System.in);
10+
11+
public AddressBookInteractive(AddressBookService addressBook) {
12+
this.addressBook = addressBook;
13+
}
14+
15+
/** Interactive add **/
16+
public void addAddress() {
17+
System.out.print("Enter name: ");
18+
String name = scanner.nextLine().trim();
19+
if (name.isEmpty()) {
20+
System.out.println("Error: Name cannot be empty");
21+
return;
22+
}
23+
24+
System.out.print("Enter address: ");
25+
String address = scanner.nextLine().trim();
26+
if (address.isEmpty()) {
27+
System.out.println("Error: Address cannot be empty");
28+
return;
29+
}
30+
31+
System.out.print("Enter note (optional): ");
32+
String note = scanner.nextLine().trim();
33+
34+
addressBook.add(name, address, note);
35+
}
36+
37+
/** Interactive edit **/
38+
public void editAddress() {
39+
List<AddressEntry> list = addressBook.getEntries();
40+
if (list.isEmpty()) {
41+
System.out.println("Address book is empty");
42+
return;
43+
}
44+
45+
System.out.println("Select the address to edit:");
46+
for (int i = 0; i < list.size(); i++) {
47+
AddressEntry e = list.get(i);
48+
System.out.printf("%d. %s (%s) - %s%n", i + 1, e.getName(), e.getAddress(), e.getNote());
49+
}
50+
51+
System.out.print("Enter number (0 to cancel): ");
52+
String input = scanner.nextLine().trim();
53+
if (input.equals("0")) {
54+
System.out.println("Canceled");
55+
return;
56+
}
57+
58+
int index;
59+
try {
60+
index = Integer.parseInt(input) - 1;
61+
} catch (NumberFormatException e) {
62+
System.out.println("Error: Invalid input");
63+
return;
64+
}
65+
66+
if (index < 0 || index >= list.size()) {
67+
System.out.println("Error: Number out of range");
68+
return;
69+
}
70+
71+
AddressEntry entry = list.get(index);
72+
73+
System.out.printf("Current name: %s%nNew name (press Enter to keep): ", entry.getName());
74+
String newName = scanner.nextLine().trim();
75+
if (newName.isEmpty()) newName = entry.getName();
76+
77+
System.out.printf("Current address: %s%nNew address (press Enter to keep): ", entry.getAddress());
78+
String newAddress = scanner.nextLine().trim();
79+
if (newAddress.isEmpty()) newAddress = entry.getAddress();
80+
81+
System.out.printf("Current note: %s%nNew note (press Enter to keep): ", entry.getNote());
82+
String newNote = scanner.nextLine().trim();
83+
if (newNote.isEmpty()) newNote = entry.getNote();
84+
85+
// Delete old entry and add updated one
86+
addressBook.delete(entry.getName());
87+
addressBook.add(newName, newAddress, newNote);
88+
89+
System.out.println("✅ Address updated: " + newName);
90+
}
91+
92+
/** Interactive delete **/
93+
public void deleteAddress() {
94+
List<AddressEntry> list = addressBook.getEntries();
95+
if (list.isEmpty()) {
96+
System.out.println("Address book is empty");
97+
return;
98+
}
99+
100+
System.out.println("Current address book:");
101+
for (int i = 0; i < list.size(); i++) {
102+
AddressEntry e = list.get(i);
103+
System.out.printf("%d. %s (%s) - %s%n", i + 1, e.getName(), e.getAddress(), e.getNote());
104+
}
105+
106+
System.out.print("Enter number to delete (0 to cancel): ");
107+
String input = scanner.nextLine().trim();
108+
if (input.equals("0")) {
109+
System.out.println("Canceled");
110+
return;
111+
}
112+
113+
int index;
114+
try {
115+
index = Integer.parseInt(input) - 1;
116+
} catch (NumberFormatException e) {
117+
System.out.println("Error: Invalid input");
118+
return;
119+
}
120+
121+
if (index < 0 || index >= list.size()) {
122+
System.out.println("Error: Number out of range");
123+
return;
124+
}
125+
126+
AddressEntry entry = list.get(index);
127+
System.out.printf("Confirm delete [%s] (%s)? (y/N): ", entry.getName(), entry.getAddress());
128+
String confirm = scanner.nextLine().trim();
129+
if (!confirm.equalsIgnoreCase("y")) {
130+
System.out.println("Delete canceled");
131+
return;
132+
}
133+
134+
addressBook.delete(entry.getName());
135+
System.out.println("🗑️ Deleted: " + entry.getName());
136+
}
137+
}
138+
139+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package org.tron.core.service;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
import org.tron.core.dao.AddressEntry;
6+
7+
public class AddressBookService {
8+
private static final String DATA_DIR = "wallet_data";
9+
private static final String STORAGE_FILE = DATA_DIR + File.separator + "address_book.txt";
10+
private final File file;
11+
private final List<AddressEntry> entries = new ArrayList<>();
12+
13+
public AddressBookService() {
14+
this.file = new File(STORAGE_FILE);
15+
load();
16+
}
17+
18+
private void load() {
19+
entries.clear();
20+
if (!file.exists()) return;
21+
22+
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
23+
String line;
24+
while ((line = br.readLine()) != null) {
25+
if (line.trim().isEmpty()) continue;
26+
String[] parts = line.split(",", 3); // 最多分3段
27+
String name = parts.length > 0 ? parts[0].trim() : "";
28+
String address = parts.length > 1 ? parts[1].trim() : "";
29+
String note = parts.length > 2 ? parts[2].trim() : "";
30+
entries.add(new AddressEntry(name, address, note));
31+
}
32+
} catch (IOException e) {
33+
System.err.println("load address book failed:" + e.getMessage());
34+
}
35+
}
36+
37+
private void save() {
38+
try {
39+
file.getParentFile().mkdirs();
40+
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
41+
for (AddressEntry e : entries) {
42+
bw.write(e.toFileString());
43+
bw.newLine();
44+
}
45+
}
46+
} catch (IOException e) {
47+
System.err.println("save address book failed:" + e.getMessage());
48+
}
49+
}
50+
51+
public void add(String name, String address, String note) {
52+
if (findByName(name) != null) {
53+
System.out.println("Name already exists:" + name);
54+
return;
55+
}
56+
entries.add(new AddressEntry(name, address, note));
57+
save();
58+
System.out.println("Address added:" + name);
59+
}
60+
61+
public void edit(String name, String newAddress, String newNote) {
62+
AddressEntry entry = findByName(name);
63+
if (entry == null) {
64+
System.out.println("Name not found:" + name);
65+
return;
66+
}
67+
if (newAddress != null && !newAddress.isEmpty()) entry.setAddress(newAddress);
68+
if (newNote != null && !newNote.isEmpty()) entry.setNote(newNote);
69+
save();
70+
System.out.println("Updated address:" + name);
71+
}
72+
73+
public void delete(String name) {
74+
boolean removed = entries.removeIf(e -> e.getName().equalsIgnoreCase(name));
75+
if (removed) {
76+
save();
77+
System.out.println("Deleted:" + name);
78+
} else {
79+
System.out.println("Not found:" + name);
80+
}
81+
}
82+
83+
public void list() {
84+
if (entries.isEmpty()) {
85+
System.out.println("The address book is empty.");
86+
return;
87+
}
88+
System.out.printf("%-15s %-35s %s%n", "Name", "Address", "Note");
89+
System.out.println("--------------------------------------------------------------------------");
90+
entries.forEach(System.out::println);
91+
}
92+
93+
public AddressEntry findByName(String name) {
94+
return entries.stream()
95+
.filter(e -> e.getName().equalsIgnoreCase(name))
96+
.findFirst()
97+
.orElse(null);
98+
}
99+
100+
public List<AddressEntry> getEntries() {
101+
return entries;
102+
}
103+
}
104+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.tron.core.viewer;
2+
3+
import java.util.Scanner;
4+
import org.tron.core.manager.TxHistoryManager;
5+
import org.tron.core.service.AddressBookInteractive;
6+
import org.tron.core.service.AddressBookService;
7+
8+
public class AddressBookView {
9+
private final Scanner scanner;
10+
AddressBookService addressBook = new AddressBookService();
11+
AddressBookInteractive interactive = new AddressBookInteractive(addressBook);
12+
13+
public AddressBookView() {
14+
this.scanner = new Scanner(System.in);
15+
}
16+
17+
public void viewAddressBook() {
18+
while (true) {
19+
printMainMenu();
20+
String command = scanner.nextLine().trim().toLowerCase();
21+
22+
switch (command) {
23+
case "1": interactive.addAddress(); break;
24+
case "2": interactive.editAddress(); break;
25+
case "3": interactive.deleteAddress(); break;
26+
case "4": addressBook.list(); return;
27+
default: System.out.println("Invalid command");
28+
}
29+
}
30+
}
31+
32+
private void printMainMenu() {
33+
System.out.println("\nMAIN MENU:");
34+
System.out.println("1. addAddress");
35+
System.out.println("2. editAddress");
36+
System.out.println("3. delAddress");
37+
System.out.println("4. getAddressBook");
38+
System.out.print("Select option: ");
39+
}
40+
}

src/main/java/org/tron/walletcli/Client.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public class Client {
194194
"SwitchNetwork",
195195
"SwitchWallet",
196196
"TransferAsset",
197+
"TransferUSDT",
197198
"TriggerConstantContract",
198199
"TriggerContract",
199200
"UnDelegateResource",
@@ -3423,6 +3424,10 @@ private void run() {
34233424
help(parameters);
34243425
break;
34253426
}
3427+
case "addressbook": {
3428+
addressBook();
3429+
break;
3430+
}
34263431
case "registerwallet": {
34273432
registerWallet();
34283433
break;
@@ -3575,6 +3580,10 @@ private void run() {
35753580
sendCoin(parameters);
35763581
break;
35773582
}
3583+
case "transferUSDT": {
3584+
transferUSDT(parameters);
3585+
break;
3586+
}
35783587
case "transferasset": {
35793588
transferAsset(parameters);
35803589
break;
@@ -3949,6 +3958,41 @@ private void run() {
39493958
}
39503959
}
39513960

3961+
private void transferUSDT(String[] parameters) {
3962+
if (parameters == null || (parameters.length != 2 && parameters.length != 3)) {
3963+
System.out.println("TransferUSDT needs 2 parameters like following: ");
3964+
System.out.println("TransferUSDT [OwnerAddress] ToAddress Amount");
3965+
return;
3966+
}
3967+
3968+
int index = 0;
3969+
byte[] ownerAddress = null;
3970+
if (parameters.length == 3) {
3971+
ownerAddress = WalletApi.decodeFromBase58Check(parameters[index++]);
3972+
if (ownerAddress == null) {
3973+
System.out.println("Invalid OwnerAddress.");
3974+
return;
3975+
}
3976+
}
3977+
3978+
String base58ToAddress = parameters[index++];
3979+
byte[] toAddress = WalletApi.decodeFromBase58Check(base58ToAddress);
3980+
if (toAddress == null) {
3981+
System.out.println("Invalid toAddress.");
3982+
return;
3983+
}
3984+
3985+
String amountStr = parameters[index++];
3986+
long amount = Long.parseLong(amountStr);
3987+
3988+
// boolean result = walletApiWrapper.transferUSDT(ownerAddress, toAddress, amount);
3989+
// if (result) {
3990+
// System.out.println("Transfer " + amount + " to " + base58ToAddress + " " + successfulHighlight() + " !!");
3991+
// } else {
3992+
// System.out.println("Transfer " + amount + " to " + base58ToAddress + " " + failedHighlight() + " !!");
3993+
// }
3994+
}
3995+
39523996
private void viewBackupRecords(String[] parameters) {
39533997
if (parameters.length > 0) {
39543998
System.out.println("viewBackupRecords needs no parameters like the following: ");
@@ -3967,6 +4011,11 @@ private void viewTransactionHistory(String[] parameters) {
39674011
walletApiWrapper.viewTransactionHistory();
39684012
}
39694013

4014+
4015+
private void addressBook() {
4016+
walletApiWrapper.addressBook();
4017+
}
4018+
39704019
private void modifyWalletName(String[] parameters) throws IOException {
39714020
if (parameters.length != 1) {
39724021
System.out.println("ModifyWalletName needs 1 parameter like the following: ");

0 commit comments

Comments
 (0)