diff --git a/crypto/src/main/java/org/web3j/crypto/StructuredDataEncoder.java b/crypto/src/main/java/org/web3j/crypto/StructuredDataEncoder.java index ff9e3369e..143fcfdb1 100644 --- a/crypto/src/main/java/org/web3j/crypto/StructuredDataEncoder.java +++ b/crypto/src/main/java/org/web3j/crypto/StructuredDataEncoder.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.StringJoiner; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -120,14 +121,10 @@ public Set getDependencies(String primaryType) { public String encodeStruct(String structName) { HashMap> types = jsonMessageObject.getTypes(); - StringBuilder structRepresentation = new StringBuilder(structName + "("); + StringJoiner structRepresentation = new StringJoiner(",", structName + "(", ")"); for (StructuredData.Entry entry : types.get(structName)) { - structRepresentation.append(String.format("%s %s,", entry.getType(), entry.getName())); + structRepresentation.add(String.format("%s %s", entry.getType(), entry.getName())); } - structRepresentation = - new StringBuilder( - structRepresentation.substring(0, structRepresentation.length() - 1)); - structRepresentation.append(")"); return structRepresentation.toString(); } diff --git a/crypto/src/test/java/org/web3j/crypto/StructuredDataTest.java b/crypto/src/test/java/org/web3j/crypto/StructuredDataTest.java index 5f15636b7..970f933a7 100644 --- a/crypto/src/test/java/org/web3j/crypto/StructuredDataTest.java +++ b/crypto/src/test/java/org/web3j/crypto/StructuredDataTest.java @@ -113,6 +113,20 @@ public void testEncodeType() throws IOException, RuntimeException { expectedTypeEncoding); } + @Test + public void testMinimalEncodeType() throws IOException, RuntimeException { + String validMinimalStructuredDataJSONFilePath = + "build/resources/test/" + + "structured_data_json_files/ValidMinimalStructuredData.json"; + StructuredDataEncoder dataEncoder = + new StructuredDataEncoder(getResource(validMinimalStructuredDataJSONFilePath)); + String expectedTypeEncoding = "EIP712Domain()"; + + assertEquals( + dataEncoder.encodeType(dataEncoder.jsonMessageObject.getPrimaryType()), + expectedTypeEncoding); + } + @Test public void testTypeHash() throws IOException, RuntimeException { StructuredDataEncoder dataEncoder = new StructuredDataEncoder(jsonMessageString); diff --git a/crypto/src/test/resources/structured_data_json_files/ValidMinimalStructuredData.json b/crypto/src/test/resources/structured_data_json_files/ValidMinimalStructuredData.json new file mode 100644 index 000000000..6444e1ba2 --- /dev/null +++ b/crypto/src/test/resources/structured_data_json_files/ValidMinimalStructuredData.json @@ -0,0 +1,8 @@ +{ + "types": { + "EIP712Domain": [] + }, + "primaryType": "EIP712Domain", + "domain": {}, + "message": {} +} \ No newline at end of file