Handle nested structs encoding for EIP-712

This commit is contained in:
Anton Bukov 2023-05-04 13:36:06 +02:00
parent 54b46ce39e
commit f9a48b94d4
2 changed files with 17 additions and 2 deletions

View File

@ -389,15 +389,27 @@ std::vector<std::pair<ASTPointer<IdentifierPath>, std::optional<Token>>> UsingFo
return ranges::zip_view(m_functionsOrLibrary, m_operators) | ranges::to<vector>; return ranges::zip_view(m_functionsOrLibrary, m_operators) | ranges::to<vector>;
} }
util::h256 StructDefinition::typehash() const std::string StructDefinition::encodeType() const
{ {
std::string str = name() + "("; std::string str = name() + "(";
std::set<std::string> nested; // std::set enables duplicates elimination and ordered enumeration
for (size_t i = 0; i < m_members.size(); i++) for (size_t i = 0; i < m_members.size(); i++)
{ {
str += i == 0 ? "" : ","; str += i == 0 ? "" : ",";
str += m_members[i]->type()->canonicalName() + " " + m_members[i]->name(); str += m_members[i]->type()->canonicalName() + " " + m_members[i]->name();
if (m_members[i]->type()->category() == Type::Category::Struct) {
Declaration const* declaration = m_members[i]->type()->typeDefinition();
if (StructDefinition const* structDef = dynamic_cast<StructDefinition const*>(declaration)) {
nested.insert(structDef->encodeType());
}
}
} }
return util::keccak256(str + ")"); return std::accumulate(nested.begin(), nested.end(), str + ")");
}
util::h256 StructDefinition::typehash() const
{
return util::keccak256(encodeType());
} }
Type const* StructDefinition::type() const Type const* StructDefinition::type() const

View File

@ -741,6 +741,9 @@ public:
std::vector<ASTPointer<VariableDeclaration>> const& members() const { return m_members; } std::vector<ASTPointer<VariableDeclaration>> const& members() const { return m_members; }
/// @returns the EIP-712 compatible struct encoding.
std::string encodeType() const;
/// @returns the EIP-712 compatible typehash of this struct. /// @returns the EIP-712 compatible typehash of this struct.
util::h256 typehash() const; util::h256 typehash() const;