Skip to content

Commit 9e916d7

Browse files
authored
trim 0x prefix of the address (#416)
1 parent 091c8ec commit 9e916d7

File tree

6 files changed

+43
-16
lines changed

6 files changed

+43
-16
lines changed

sdk-core/src/main/java/org/fisco/bcos/sdk/utils/Hex.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@
1616
import java.io.ByteArrayOutputStream;
1717
import java.io.IOException;
1818
import java.io.OutputStream;
19+
import java.util.Objects;
1920
import org.fisco.bcos.sdk.utils.exceptions.DecoderException;
2021
import org.fisco.bcos.sdk.utils.exceptions.EncoderException;
2122

2223
/** Utility class for converting hex data to bytes and back again. */
2324
public class Hex {
2425
private static final HexEncoder encoder = new HexEncoder();
2526

27+
public static String trimPrefix(String data) {
28+
if (Objects.nonNull(data) && data.startsWith("0x")) {
29+
return data.substring(2);
30+
}
31+
return data;
32+
}
33+
2634
public static String toHexString(byte[] data) {
2735
return toHexString(data, 0, data.length);
2836
}

sdk-service/src/main/java/org/fisco/bcos/sdk/BcosSDK.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public ConfigOption getConfig() {
3333
return config;
3434
}
3535

36-
// public org.fisco.bcos.sdk.jni.BcosSDK getJniBcosSdk() {
37-
// return jniBcosSdk;
38-
// }
39-
//
40-
// public void setJniBcosSdk(org.fisco.bcos.sdk.jni.BcosSDK jniBcosSdk) {
41-
// this.jniBcosSdk = jniBcosSdk;
42-
// }
36+
// public org.fisco.bcos.sdk.jni.BcosSDK getJniBcosSdk() {
37+
// return jniBcosSdk;
38+
// }
39+
//
40+
// public void setJniBcosSdk(org.fisco.bcos.sdk.jni.BcosSDK jniBcosSdk) {
41+
// this.jniBcosSdk = jniBcosSdk;
42+
// }
4343

4444
/**
4545
* Build BcosSDK instance

sdk-service/src/main/java/org/fisco/bcos/sdk/client/ClientImpl.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.fisco.bcos.sdk.model.JsonRpcResponse;
3838
import org.fisco.bcos.sdk.model.Response;
3939
import org.fisco.bcos.sdk.model.callback.TransactionCallback;
40+
import org.fisco.bcos.sdk.utils.Hex;
4041
import org.fisco.bcos.sdk.utils.ObjectMapperFactory;
4142
import org.slf4j.Logger;
4243
import org.slf4j.LoggerFactory;
@@ -71,7 +72,7 @@ protected void initGroupInfo() {
7172
if (nodeList == null || nodeList.isEmpty()) {
7273
logger.error("There has no nodes in the group, groupID: {}, groupInfo: {}", groupInfo);
7374
throw new ClientException(
74-
"There has no nodes in the group, please check the group, groupID: "
75+
"There has no nodes in the group, maybe the group has been removed, groupID: "
7576
+ this.groupID);
7677
}
7778

@@ -246,7 +247,10 @@ public Call call(String node, Transaction transaction) {
246247
new JsonRpcRequest(
247248
JsonRpcMethods.CALL,
248249
Arrays.asList(
249-
this.groupID, node, transaction.getTo(), transaction.getData())),
250+
this.groupID,
251+
node,
252+
Hex.trimPrefix(transaction.getTo()),
253+
transaction.getData())),
250254
Call.class);
251255
}
252256

@@ -262,7 +266,12 @@ public void callAsync(String node, Transaction transaction, RespCallback<Call> c
262266
this.groupID,
263267
node,
264268
new JsonRpcRequest(
265-
JsonRpcMethods.CALL, Arrays.asList(this.groupID, node, transaction)),
269+
JsonRpcMethods.CALL,
270+
Arrays.asList(
271+
this.groupID,
272+
node,
273+
Hex.trimPrefix(transaction.getTo()),
274+
transaction.getData())),
266275
Call.class,
267276
callback);
268277
}
@@ -307,10 +316,12 @@ public Code getCode(String address) {
307316
@Override
308317
public Code getCode(String node, String address) {
309318
node = Objects.isNull(node) ? "" : node;
319+
310320
// create request
311321
JsonRpcRequest request =
312322
new JsonRpcRequest(
313-
JsonRpcMethods.GET_CODE, Arrays.asList(this.groupID, node, address));
323+
JsonRpcMethods.GET_CODE,
324+
Arrays.asList(this.groupID, node, Hex.trimPrefix(address)));
314325
return this.callRemoteMethod(this.groupID, node, request, Code.class);
315326
}
316327

@@ -326,7 +337,8 @@ public void getCodeAsync(String node, String address, RespCallback<Code> callbac
326337
this.groupID,
327338
node,
328339
new JsonRpcRequest(
329-
JsonRpcMethods.GET_CODE, Arrays.asList(this.groupID, node, address)),
340+
JsonRpcMethods.GET_CODE,
341+
Arrays.asList(this.groupID, node, Hex.trimPrefix(address))),
330342
Code.class,
331343
callback);
332344
}

sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/builder/TransactionBuilderService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.fisco.bcos.sdk.codec.ABICodecException;
2525
import org.fisco.bcos.sdk.crypto.CryptoSuite;
2626
import org.fisco.bcos.sdk.transaction.codec.encode.TransactionEncoderService;
27+
import org.fisco.bcos.sdk.utils.Hex;
2728

2829
public class TransactionBuilderService implements TransactionBuilderInterface {
2930
private Client client;
@@ -88,7 +89,13 @@ public TransactionData createTransaction(
8889
Random r = ThreadLocalRandom.current();
8990
BigInteger randomId = new BigInteger(250, r);
9091
return new TransactionData(
91-
0, chainId, groupId, blockLimit.intValue(), randomId.toString(), to, data);
92+
0,
93+
chainId,
94+
groupId,
95+
blockLimit.intValue(),
96+
randomId.toString(),
97+
Hex.trimPrefix(to),
98+
data);
9299
}
93100

94101
/** @return the client */

sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/AssembleTransactionProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public TransactionData getRawTransaction(
364364
String to, String abi, String functionName, List<Object> params)
365365
throws ABICodecException {
366366
return this.transactionBuilder.createTransaction(
367-
to,
367+
Hex.trimPrefix(to),
368368
this.abiCodec.encodeMethod(abi, functionName, params),
369369
this.chainId,
370370
this.groupId);
@@ -376,7 +376,7 @@ public TransactionData getRawTransaction(
376376
throws ABICodecException {
377377
return this.transactionBuilder.createTransaction(
378378
blockLimit,
379-
to,
379+
Hex.trimPrefix(to),
380380
this.abiCodec.encodeMethod(abi, functionName, params),
381381
this.chainId,
382382
this.groupId);

src/integration-test/java/org/fisco/bcos/sdk/BcosSDKTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public void testGetGroupList() throws ConfigException, JniException {
359359
System.out.println("getGroupList: " + groupList);
360360

361361
BcosSDK bcosSDK = new BcosSDK(configOption);
362-
for(String groupId: groupList) {
362+
for (String groupId : groupList) {
363363
Client client = bcosSDK.getClient(groupId);
364364
System.out.println("build client, groupId: " + groupId);
365365
System.out.println("getBlockNumber, blk: " + client.getBlockNumber().getBlockNumber());

0 commit comments

Comments
 (0)