Skip to content

Commit

Permalink
Merge pull request #1968 from Supertuba12/supertuba12/WEB3-1967
Browse files Browse the repository at this point in the history
Correctly encode structs without members
  • Loading branch information
NickSneo authored Oct 4, 2023
2 parents 6435c35 + d349a79 commit 2944092
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,14 +121,10 @@ public Set<String> getDependencies(String primaryType) {
public String encodeStruct(String structName) {
HashMap<String, List<StructuredData.Entry>> 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();
}
Expand Down
14 changes: 14 additions & 0 deletions crypto/src/test/java/org/web3j/crypto/StructuredDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"types": {
"EIP712Domain": []
},
"primaryType": "EIP712Domain",
"domain": {},
"message": {}
}

0 comments on commit 2944092

Please sign in to comment.