Skip to content

Commit

Permalink
Correctly encode structs without members
Browse files Browse the repository at this point in the history
The JSON RPC API specification for 'eth_signTypedData_v4' does not
explicitly state that the required struct 'EIP712Domain' has to define
any members [1]. The current implementation cannot correctly encode
such a structure, as it assumes that at least one member has been
parsed and added to the struct's string representation.

This commit refactors the struct encoding function such that it now
accepts structures that does not define any members.

[1] https://eips.ethereum.org/EIPS/eip-712#specification-of-the-eth_signtypeddata-json-rpc
  • Loading branch information
Supertuba12 committed Sep 26, 2023
1 parent ac0618c commit d349a79
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 d349a79

Please sign in to comment.