com.alphawallet.token.web.Ethereum.web3j.StructuredData Maven / Gradle / Ivy
/*
* Copyright 2019 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.alphawallet.token.web.Ethereum.web3j;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.HashMap;
import java.util.List;
/**
* This class is here due to a bug in Web3j which prevented EIP712 working for some structures
* The fix has been submitted to Web3j repo, when fixed this code can be removed. See also class StructuredDataEncoder
*/
public class StructuredData {
public static class Entry {
private final String name;
private final String type;
@JsonCreator
public Entry(
@JsonProperty(value = "name") String name,
@JsonProperty(value = "type") String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
}
public static class EIP712Domain {
private final String name;
private final String version;
// Should be uint256, but this does not serialize, so this is a hack working as long as chainId is 64 bits
private final Long chainId;
// Should be Address, but this does not serialize
private final String verifyingContract;
private final String salt;
@JsonCreator
public EIP712Domain(
@JsonProperty(value = "name") String name,
@JsonProperty(value = "version") String version,
@JsonProperty(value = "chainId") Long chainId,
@JsonProperty(value = "verifyingContract") String verifyingContract,
@JsonProperty(value = "salt") String salt) {
this.name = name;
this.version = version;
this.chainId = chainId;
this.verifyingContract = verifyingContract;
this.salt = salt;
}
public String getName() {
return name;
}
public String getVersion() {
return version;
}
public Long getChainId() {
return chainId;
}
public String getVerifyingContract() {
return verifyingContract;
}
public String getSalt() {
return salt;
}
}
public static class EIP712Message {
private final HashMap> types;
private final String primaryType;
private final Object message;
private final EIP712Domain domain;
@JsonCreator
public EIP712Message(
@JsonProperty(value = "types") HashMap> types,
@JsonProperty(value = "primaryType") String primaryType,
@JsonProperty(value = "message") Object message,
@JsonProperty(value = "domain") EIP712Domain domain) {
this.types = types;
this.primaryType = primaryType;
this.message = message;
this.domain = domain;
}
public HashMap> getTypes() {
return types;
}
public String getPrimaryType() {
return primaryType;
}
public Object getMessage() {
return message;
}
public EIP712Domain getDomain() {
return domain;
}
@Override
public String toString() {
return "EIP712Message{"
+ "primaryType='"
+ this.primaryType
+ '\''
+ ", message='"
+ this.message
+ '\''
+ '}';
}
}
}