Skip to content

Commit

Permalink
add salt to EIP712 domain (#1260)
Browse files Browse the repository at this point in the history
* add salt to EIP-712 domain

* ran `gradlew spotlessApply`
  • Loading branch information
alpa-coder authored Oct 30, 2020
1 parent bc3f03d commit 8cb1dce
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crypto/src/main/java/org/web3j/crypto/StructuredData.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ static class EIP712Domain {
private final String version;
private final Uint256 chainId;
private final Address verifyingContract;
private final String salt;

@JsonCreator
public EIP712Domain(
@JsonProperty(value = "name") String name,
@JsonProperty(value = "version") String version,
@JsonProperty(value = "chainId") Uint256 chainId,
@JsonProperty(value = "verifyingContract") Address verifyingContract) {
@JsonProperty(value = "verifyingContract") Address verifyingContract,
@JsonProperty(value = "salt") String salt) {
this.name = name;
this.version = version;
this.chainId = chainId;
this.verifyingContract = verifyingContract;
this.salt = salt;
}

public String getName() {
Expand All @@ -76,6 +79,10 @@ public Uint256 getChainId() {
public Address getVerifyingContract() {
return verifyingContract;
}

public String getSalt() {
return salt;
}
}

static class EIP712Message {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ public byte[] hashDomain() throws RuntimeException {
data.put(
"verifyingContract",
((HashMap<String, Object>) data.get("verifyingContract")).get("value"));

if (data.get("salt") == null) {
data.remove("salt");
}
return sha3(encodeData("EIP712Domain", data));
}

Expand Down
15 changes: 15 additions & 0 deletions crypto/src/test/java/org/web3j/crypto/StructuredDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,19 @@ public void testDataNotPerfectArrayButDeclaredArrayInSchema()
new StructuredDataEncoder(getResource(invalidStructuredDataJSONFilePath));
assertThrows(RuntimeException.class, dataEncoder::hashStructuredData);
}

@Test
public void testHashDomainWithSalt() throws RuntimeException, IOException {
String validStructuredDataWithSaltJSONFilePath =
"build/resources/test/"
+ "structured_data_json_files/"
+ "ValidStructuredDataWithSalt.json";
StructuredDataEncoder dataEncoder =
new StructuredDataEncoder(getResource(validStructuredDataWithSaltJSONFilePath));
byte[] structHashDomain = dataEncoder.hashDomain();
String expectedDomainStructHash =
"0xbae4f6f7b9bfdfda060692099b0e1ccecd25d62b7c92cc9f3b907f33178b81e3";

assertEquals(Numeric.toHexString(structHashDomain), expectedDomainStructHash);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"types": {
"EIP712Domain": [
{"name": "name", "type": "string"},
{"name": "version", "type": "string"},
{"name": "chainId", "type": "uint256"},
{"name": "verifyingContract", "type": "address"},
{"name": "salt", "type": "bytes32"}
],
"Person": [
{"name": "name", "type": "string"},
{"name": "wallet", "type": "address"}
],
"Mail": [
{"name": "from", "type": "Person"},
{"name": "to", "type": "Person"},
{"name": "contents", "type": "string"}
]
},
"primaryType": "Mail",
"domain": {
"name": "Ether Mail",
"version": "1",
"chainId": 1,
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
"salt": "0xf2d857f4a3edcb9b78b4d503bfe733db1e3f6cdc2b7971ee739626c97e86a558"
},
"message": {
"from": {
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to": {
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents": "Hello, Bob!"
}
}

0 comments on commit 8cb1dce

Please sign in to comment.