Skip to content

Commit 179dc7a

Browse files
authored
Merge pull request #847 from tronprotocol/master
Master merge into develop
2 parents b6d26da + b2f3b98 commit 179dc7a

File tree

21 files changed

+1564
-103
lines changed

21 files changed

+1564
-103
lines changed

README.md

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ For more information on a specific command, just type the command on terminal wh
104104
| [GenerateSubAccount](#generate-sub-account) | [ClearWalletKeystore](#clear-wallet-keystore) | [ExportWalletKeystore](#export-import-wallet-keystore) |
105105
| [ImportWalletByKeystore](#export-import-wallet-keystore) | [ImportWalletByLedger](#import-wallet-by-ledger) | [LoginAll](#login-all) |
106106
| [Lock](#lock) | [Unlock](#unlock) | [ResetWallet](#reset-wallet) |
107-
| [CreateAccount](#create-account) | [SwitchWallet](#switch-wallet) |
107+
| [CreateAccount](#create-account) | [SwitchWallet](#switch-wallet) | [SwitchNetwork](#switch-network) |
108+
| [CurrentNetwork](#current-network) | [GasFreeInfo](#gas-free-info) | [GasFreeTransfer](#gas-free-transfer) |
109+
| [GasFreeTrace](#gas-free-trace) | | |
108110

109111

110112
Type any one of the listed commands, to display how-to tips.
@@ -1640,6 +1642,156 @@ password:
16401642
unlock successful !!!
16411643
```
16421644

1645+
## switch network
1646+
> SwitchNetwork
1647+
>This command allows for flexible network switching at any time. Unlocking can specify parameters in seconds.
1648+
>`switchnetwork local` will switch to the network configured in local config.conf.
1649+
1650+
Example:
1651+
```console
1652+
wallet> switchnetwork
1653+
Please select network:
1654+
1. MAIN
1655+
2. NILE
1656+
3. SHASTA
1657+
Enter numbers to select a network (1-3):1
1658+
Now, current network is : MAIN
1659+
SwitchNetwork successful !!!
1660+
```
1661+
```console
1662+
wallet> switchnetwork main
1663+
Now, current network is : MAIN
1664+
SwitchNetwork successful !!!
1665+
```
1666+
1667+
```console
1668+
wallet> switchnetwork empty localhost:50052
1669+
Now, current network is : CUSTOM
1670+
SwitchNetwork successful !!!
1671+
```
1672+
1673+
## current network
1674+
> CurrentNetwork
1675+
>View current network.
1676+
1677+
Example:
1678+
```console
1679+
wallet> currentnetwork
1680+
currentNetwork: NILE
1681+
```
1682+
1683+
```console
1684+
wallet> currentnetwork
1685+
current network: CUSTOM
1686+
fullNode: EMPTY, solidityNode: localhost:50052
1687+
```
1688+
1689+
## gas free info
1690+
> GasFreeInfo
1691+
>Get gasfree info of the current address.
1692+
1693+
Example:
1694+
```console
1695+
wallet> gasfreeinfo
1696+
balanceOf(address):70a08231
1697+
{
1698+
"gasFreeAddress":"TCtSt8fCkZcVdrGpaVHUr6P8EmdjysswMF",
1699+
"active":true,
1700+
"tokenBalance":998696000,
1701+
"activateFee":0,
1702+
"transferFee":2000,
1703+
"maxTransferValue":998694000
1704+
}
1705+
gasFreeInfo: successful !!
1706+
```
1707+
1708+
```console
1709+
wallet> gasfreeinfo TRvVXgqddDGYRMx3FWf2tpVxXQQXDZxJQe
1710+
balanceOf(address):70a08231
1711+
{
1712+
"gasFreeAddress":"TCtSt8fCkZcVdrGpaVHUr6P8EmdjysswMF",
1713+
"active":true,
1714+
"tokenBalance":998696000,
1715+
"activateFee":0,
1716+
"transferFee":2000,
1717+
"maxTransferValue":998694000
1718+
}
1719+
gasFreeInfo: successful !!
1720+
```
1721+
1722+
## gas free transfer
1723+
> GasFreeTransfer
1724+
>Transfer funds through gas-free.
1725+
1726+
Example:
1727+
```console
1728+
wallet> gasfreetransfer TEkj3ndMVEmFLYaFrATMwMjBRZ1EAZkucT 100000
1729+
1730+
GasFreeTransfer result: {
1731+
"code":200,
1732+
"data":{
1733+
"amount":100000,
1734+
"providerAddress":"TKtWbdzEq5ss9vTS9kwRhBp5mXmBfBns3E",
1735+
"apiKey":"",
1736+
"accountAddress":"TUUSMd58eC3fKx3fn7whxJyr1FR56tgaP8",
1737+
"signature":"",
1738+
"targetAddress":"TEkj3ndMVEmFLYaFrATMwMjBRZ1EAZkucT",
1739+
"maxFee":2000000,
1740+
"version":1,
1741+
"nonce":8,
1742+
"tokenAddress":"TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf",
1743+
"createdAt":1747909635678,
1744+
"expiredAt":1747909695000,
1745+
"estimatedTransferFee":2000,
1746+
"id":"6c3ff67e-0bf4-4c09-91ca-0c7c254b01a0",
1747+
"state":"WAITING",
1748+
"estimatedActivateFee":0,
1749+
"gasFreeAddress":"TNER12mMVWruqopsW9FQtKxCGfZcEtb3ER",
1750+
"updatedAt":1747909635678
1751+
}
1752+
}
1753+
GasFreeTransfer successful !!!
1754+
```
1755+
1756+
## gas free trace
1757+
> GasFreeTrace
1758+
>Query GasFreeTrace to obtain transfer details by using the transaction ID returned by GasFreeTransfer as the traceId.
1759+
1760+
Example:
1761+
```console
1762+
wallet> gasfreetrace 6c3ff67e-0bf4-4c09-91ca-0c7c254b01a0
1763+
GasFreeTrace result: {
1764+
"code":200,
1765+
"data":{
1766+
"amount":100000,
1767+
"providerAddress":"TKtWbdzEq5ss9vTS9kwRhBp5mXmBfBns3E",
1768+
"txnTotalCost":102000,
1769+
"accountAddress":"TUUSMd58eC3fKx3fn7whxJyr1FR56tgaP8",
1770+
"txnActivateFee":0,
1771+
"estimatedTotalCost":102000,
1772+
"targetAddress":"TEkj3ndMVEmFLYaFrATMwMjBRZ1EAZkucT",
1773+
"txnBlockTimestamp":1747909638000,
1774+
"txnTotalFee":2000,
1775+
"nonce":8,
1776+
"estimatedTotalFee":2000,
1777+
"tokenAddress":"TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf",
1778+
"txnHash":"858f9a00776163b1f8a34467b9c5727657f8971a9f4e9d492f0a247fac0384f9",
1779+
"txnBlockNum":57175988,
1780+
"createdAt":1747909635678,
1781+
"expiredAt":1747909695000,
1782+
"estimatedTransferFee":2000,
1783+
"txnState":"ON_CHAIN",
1784+
"id":"6c3ff67e-0bf4-4c09-91ca-0c7c254b01a0",
1785+
"state":"CONFIRMING",
1786+
"estimatedActivateFee":0,
1787+
"gasFreeAddress":"TNER12mMVWruqopsW9FQtKxCGfZcEtb3ER",
1788+
"txnTransferFee":2000,
1789+
"txnAmount":100000
1790+
}
1791+
}
1792+
GasFreeTrace: successful!!
1793+
```
1794+
16431795
## switch wallet
16441796
> SwitchWallet
16451797
>After logging in with the LoginAll command, you can switch wallets

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ dependencies {
9393
}
9494
implementation group: 'org.hid4java', name: 'hid4java', version: '0.8.0'
9595
//implementation 'javax.annotation:javax.annotation-api:1.3.2'
96+
implementation("com.squareup.okhttp3:okhttp:4.12.0")
97+
implementation("io.github.tronprotocol:trident:0.10.0") {
98+
exclude group: "com.google.guava", module: "guava"
99+
}
100+
96101
}
97102

98103
protobuf {

src/main/java/org/tron/common/crypto/ECKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public ECKey(SecureRandom secureRandom) {
179179
* <p>All private key operations will use the provider.
180180
*/
181181

182-
// isPrivateKey true 私钥 其他公钥
182+
// isPrivateKey true
183183
public ECKey(byte[] key, boolean isPrivateKey) {
184184
if (isPrivateKey) {
185185
BigInteger pk = new BigInteger(1, key);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.tron.common.enums;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
public enum NetType {
8+
MAIN(
9+
"https://api.trongrid.io",
10+
new Grpc("grpc.trongrid.io:50051", "grpc.trongrid.io:50052"),
11+
new GasFree(
12+
728126428L,
13+
"TFFAMQLZybALaLb4uxHA9RBE7pxhUAjF3U",
14+
"https://open.gasfree.io",
15+
"/tron")
16+
),
17+
NILE("https://nile.trongrid.io",
18+
new Grpc("grpc.nile.trongrid.io:50051", "grpc.nile.trongrid.io:50061"),
19+
new GasFree(
20+
3448148188L,
21+
"THQGuFzL87ZqhxkgqYEryRAd7gqFqL5rdc",
22+
"https://open-test.gasfree.io",
23+
"/nile")
24+
),
25+
SHASTA(
26+
"https://api.shasta.trongrid.io",
27+
new Grpc("grpc.shasta.trongrid.io:50051", "grpc.shasta.trongrid.io:50052"),
28+
new GasFree(
29+
2494104990L,
30+
"TSwCtDum13k1PodgNgTWx5be7k1c6eWaNP",
31+
"https://open-test.gasfree.io",
32+
"/shasta")
33+
),
34+
CUSTOM(null, null, null);
35+
36+
private final String http;
37+
private final Grpc grpc;
38+
private final GasFree gasFree;
39+
40+
NetType(String http, Grpc grpc, GasFree gasFree) {
41+
this.http = http;
42+
this.grpc = grpc;
43+
this.gasFree = gasFree;
44+
}
45+
46+
@Setter
47+
@Getter
48+
public static class Grpc {
49+
public Grpc(String fullNode, String solidityNode) {
50+
this.fullNode = fullNode;
51+
this.solidityNode = solidityNode;
52+
}
53+
54+
private String fullNode;
55+
private String solidityNode;
56+
}
57+
58+
@Setter
59+
@Getter
60+
public static class GasFree {
61+
private long chainId;
62+
private String verifyingContract;
63+
private String httpUrl;
64+
private String apiPrefix;
65+
66+
public GasFree(long chainId, String verifyingContract, String httpUrl, String apiPrefix) {
67+
this.chainId = chainId;
68+
this.verifyingContract = verifyingContract;
69+
this.httpUrl = httpUrl;
70+
this.apiPrefix = apiPrefix;
71+
}
72+
73+
74+
}
75+
}
76+
77+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.tron.common.utils;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
import java.util.regex.Pattern;
6+
7+
public class DomainValidator {
8+
private static final Pattern DOMAIN_PATTERN = Pattern.compile(
9+
"^((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\.)+" +
10+
"([A-Za-z]{2,63}|xn--[A-Za-z0-9]{1,59})$"
11+
);
12+
13+
// IPv4
14+
private static final Pattern IPV4_PATTERN = Pattern.compile(
15+
"^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)(\\.|$)){4}$"
16+
);
17+
18+
// IPv6
19+
public static boolean isIPv6(String input) {
20+
try {
21+
InetAddress address = InetAddress.getByName(input);
22+
return address.getHostAddress().contains(":");
23+
} catch (UnknownHostException e) {
24+
return false;
25+
}
26+
}
27+
28+
public static boolean isIPv4(String input) {
29+
return IPV4_PATTERN.matcher(input).matches();
30+
}
31+
32+
public static boolean isDomain(String input) {
33+
return DOMAIN_PATTERN.matcher(input).matches();
34+
}
35+
36+
public static boolean isDomainOrIP(String input) {
37+
if (input == null || input.isEmpty()) return false;
38+
if ("localhost".equalsIgnoreCase(input)) return true;
39+
return isIPv4(input) || isDomain(input);
40+
}
41+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.tron.common.utils;
2+
3+
import static org.apache.commons.lang3.StringUtils.EMPTY;
4+
import static org.tron.common.utils.Utils.greenBoldHighlight;
5+
6+
import java.io.IOException;
7+
import java.util.Map;
8+
import java.util.Optional;
9+
import java.util.concurrent.TimeUnit;
10+
import okhttp3.MediaType;
11+
import okhttp3.OkHttpClient;
12+
import okhttp3.Request;
13+
import okhttp3.RequestBody;
14+
import okhttp3.Response;
15+
import okhttp3.ResponseBody;
16+
import org.apache.commons.lang3.StringUtils;
17+
18+
public class HttpUtils {
19+
20+
private static final OkHttpClient client = new OkHttpClient.Builder()
21+
.connectTimeout(10, TimeUnit.SECONDS)
22+
.readTimeout(15, TimeUnit.SECONDS)
23+
.writeTimeout(15, TimeUnit.SECONDS)
24+
.build();
25+
26+
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
27+
28+
public static String get(String url, Map<String, String> headers) throws IOException {
29+
Request.Builder builder = new Request.Builder().url(url).get();
30+
if (headers != null) {
31+
headers.forEach(builder::addHeader);
32+
}
33+
Request request = builder.build();
34+
try (Response response = client.newCall(request).execute()) {
35+
return responseBodyToString(response);
36+
}
37+
}
38+
39+
public static String postJson(String url, String jsonBody, Map<String, String> headers) throws IOException {
40+
RequestBody body = RequestBody.create(jsonBody, JSON);
41+
Request.Builder builder = new Request.Builder().url(url).post(body);
42+
if (headers != null) {
43+
headers.forEach(builder::addHeader);
44+
}
45+
Request request = builder.build();
46+
try (Response response = client.newCall(request).execute()) {
47+
return responseBodyToString(response);
48+
}
49+
}
50+
51+
public static String postJson(String url, String jsonBody) throws IOException {
52+
return postJson(url, jsonBody, null);
53+
}
54+
55+
private static String responseBodyToString(Response response) throws IOException {
56+
if (!response.isSuccessful()) {
57+
String msg = response.message();
58+
if (StringUtils.isEmpty(msg) ) {
59+
msg = Optional.ofNullable(response.body()).map(body -> {
60+
try {
61+
return body.string();
62+
} catch (Exception e) {
63+
return EMPTY;
64+
}
65+
}).orElse(EMPTY);
66+
}
67+
if (response.code() == 502) {
68+
throw new IOException("Unexpected HTTP code " + response.code() + ": Bad Gateway.");
69+
}
70+
throw new IOException(
71+
"Unexpected HTTP code " + response.code() + ": " + msg + "\nAPI authentication failed, "
72+
+ "please check the " + greenBoldHighlight("apikey") + " and "
73+
+ greenBoldHighlight("apiSecret") + " configured in config.conf.");
74+
}
75+
ResponseBody body = response.body();
76+
return body != null ? body.string() : EMPTY;
77+
}
78+
}
79+
80+

0 commit comments

Comments
 (0)