Skip to content

Commit 196920c

Browse files
authored
add 0x prefix to solidity address (#431)
1 parent b46d248 commit 196920c

File tree

7 files changed

+29
-25
lines changed

7 files changed

+29
-25
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
id 'maven-publish'
44
id 'java-library'
55
id 'java'
6-
id 'org.ajoberstar.grgit' version '4.1.0'
6+
id 'org.ajoberstar.grgit' version '4.1.1'
77
id 'com.github.sherter.google-java-format' version '0.9'
88
}
99
println("Notice: current gradle version is " + gradle.gradleVersion)

sdk-core/src/main/java/org/fisco/bcos/sdk/model/TransactionReceipt.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.fasterxml.jackson.annotation.JsonProperty;
1919
import java.util.List;
2020
import java.util.Objects;
21+
import org.fisco.bcos.sdk.utils.AddressUtils;
2122

2223
public class TransactionReceipt {
2324
private String version;
@@ -177,7 +178,7 @@ public void setFrom(String from) {
177178
}
178179

179180
public String getTo() {
180-
return this.to;
181+
return AddressUtils.addHexPrefixToAddress(this.to);
181182
}
182183

183184
public void setTo(String to) {
@@ -193,11 +194,7 @@ public void setGasUsed(String gasUsed) {
193194
}
194195

195196
public String getContractAddress() {
196-
if (!isWasm() && Objects.nonNull(contractAddress) && !contractAddress.isEmpty()) {
197-
return "0x" + contractAddress;
198-
}
199-
200-
return contractAddress;
197+
return AddressUtils.addHexPrefixToAddress(contractAddress);
201198
}
202199

203200
public void setContractAddress(String contractAddress) {
@@ -244,14 +241,6 @@ public void setTransactionProof(List<MerkleProofUnit> transactionProof) {
244241
this.transactionProof = transactionProof;
245242
}
246243

247-
public boolean isWasm() {
248-
return wasm;
249-
}
250-
251-
public void setWasm(boolean wasm) {
252-
this.wasm = wasm;
253-
}
254-
255244
@Override
256245
public boolean equals(Object o) {
257246
if (this == o) return true;
@@ -306,7 +295,7 @@ public String toString() {
306295
+ this.from
307296
+ '\''
308297
+ ", to='"
309-
+ this.to
298+
+ this.getTo()
310299
+ '\''
311300
+ ", gasUsed='"
312301
+ this.gasUsed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package org.fisco.bcos.sdk.utils;
1717

18+
import java.util.Objects;
19+
1820
public class AddressUtils {
1921
public static final int ADDRESS_SIZE = 160;
2022
public static final int ADDRESS_LENGTH_IN_HEX = ADDRESS_SIZE >> 2;
@@ -23,4 +25,14 @@ public static boolean isValidAddress(String address) {
2325
String addressNoPrefix = Numeric.cleanHexPrefix(address);
2426
return addressNoPrefix.length() == ADDRESS_LENGTH_IN_HEX;
2527
}
28+
29+
public static String addHexPrefixToAddress(String address) {
30+
if (!Objects.isNull(address)
31+
&& !(address.startsWith("0x") || address.startsWith("0X"))
32+
&& isValidAddress(address)) {
33+
return Hex.addPrefix(address);
34+
}
35+
36+
return address;
37+
}
2638
}

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
@@ -24,6 +24,14 @@
2424
public class Hex {
2525
private static final HexEncoder encoder = new HexEncoder();
2626

27+
public static boolean hasHexPrefix(String data) {
28+
if ((data != null) && (data.startsWith("0x") || data.startsWith("0X"))) {
29+
return true;
30+
}
31+
32+
return false;
33+
}
34+
2735
public static String addPrefix(String data) {
2836
if (Objects.isNull(data)) {
2937
return data;

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ public BcosTransactionReceipt sendTransaction(
204204
Arrays.asList(
205205
this.groupID, node, signedTransactionData, withProof)),
206206
BcosTransactionReceipt.class);
207-
if (bcosTransactionReceipt.getResult() != null) {
208-
bcosTransactionReceipt.getResult().setWasm(isWASM());
209-
}
210207
return bcosTransactionReceipt;
211208
}
212209

@@ -233,9 +230,6 @@ public void sendTransactionAsync(
233230
new RespCallback<BcosTransactionReceipt>() {
234231
@Override
235232
public void onResponse(BcosTransactionReceipt transactionReceiptWithProof) {
236-
if (transactionReceiptWithProof.getResult() != null) {
237-
transactionReceiptWithProof.getResult().setWasm(isWASM());
238-
}
239233
callback.onResponse(transactionReceiptWithProof.getTransactionReceipt());
240234
}
241235

sdk-service/src/main/java/org/fisco/bcos/sdk/client/protocol/model/JsonTransactionResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.fisco.bcos.sdk.client.protocol.model.tars.TransactionData;
2323
import org.fisco.bcos.sdk.crypto.CryptoSuite;
2424
import org.fisco.bcos.sdk.model.MerkleProofUnit;
25+
import org.fisco.bcos.sdk.utils.AddressUtils;
2526
import org.fisco.bcos.sdk.utils.Hex;
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
@@ -94,7 +95,7 @@ public void setNonce(String nonce) {
9495
}
9596

9697
public String getTo() {
97-
return this.to;
98+
return AddressUtils.addHexPrefixToAddress(this.to);
9899
}
99100

100101
public void setTo(String to) {
@@ -213,7 +214,7 @@ public String toString() {
213214
+ this.nonce
214215
+ '\''
215216
+ ", to='"
216-
+ this.to
217+
+ this.getTo()
217218
+ '\''
218219
+ ", blockLimit='"
219220
+ this.blockLimit

sdk-service/src/test/java/org/fisco/bcos/sdk/test/client/ResponseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ public void testTransactionReceipt() throws JsonProcessingException {
744744
+ " \"output\": \"0x0000000000000000000000000000000000000000000000000000000000000000\",\n"
745745
+ " \"root\":\"0x38723a2e5e8a17aa7950dc008209944e898f69a7bd10a23c839d341e935fd5ca\",\n"
746746
+ " \"status\": \"12\",\n"
747-
+ " \"to\": \"0x15538acd403ac1b2ff09083c70d04856b8c0bdfd\",\n"
747+
+ " \"to\": \"15538acd403ac1b2ff09083c70d04856b8c0bdfd\",\n"
748748
+ " \"transactionHash\": \"0x708b5781b62166bd86e543217be6cd954fd815fd192b9a124ee9327580df8f3f\",\n"
749749
+ " \"transactionIndex\": \"0x10\"\n"
750750
+ " }\n"

0 commit comments

Comments
 (0)