Skip to content

Commit

Permalink
Update hash eip712 message
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jan 19, 2024
1 parent f689aab commit 56f17fc
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions src/Contracts/TypedDataEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected function findType(string $type, array $types)
$result = [];
if ($this->strEndsWith($type, ']')) {
$pos = strpos($type, '[');
$type = ($pos !== false) ? substr($type, 0, $pos) : $typs;
$type = ($pos !== false) ? substr($type, 0, $pos) : $type;
}
if (in_array($type, $this->eip712SolidityTypes) || in_array($type, $result)) {
return $result;
Expand Down Expand Up @@ -290,15 +290,47 @@ protected function hashStruct(string $type, array $types, array $data)
return Utils::sha3($encodedData);
}

/**
* getPrimaryType
*
* @param array types
* @return string
*/
protected function getPrimaryType(array $types)
{
$customTypes = array_keys($types);
$customDepsTypes = [];
foreach ($types as $key => $typeFields) {
foreach ($typeFields as $field) {
$type = $field['type'];
if ($this->strEndsWith($type, ']')) {
$pos = strpos($type, '[');
$type = ($pos !== false) ? substr($type, 0, $pos) : $type;
}
if (!in_array($type, $customTypes) && $type !== $key) {
$customDepsTypes[] = $type;
}
}
}
$primaryType = array_diff($customTypes, $customDepsTypes);
if (count($primaryType) === 0) {
throw new InvalidArgumentException('Unable to determine primary type');
}
return $primaryType[0];
}

/**
* hashEIP712Message
*
* @param array $types
* @param array $message
* @return
* @param array $messageTypes
* @param array $messageData
* @return string
*/
public function hashEIP712Message(array $types, array $message)
{}
public function hashEIP712Message(array $messageTypes, array $messageData)
{
$primaryType = $this->getPrimaryType($messageTypes);
return $this->hash_struct($primaryType, $messageTypes, $messageData);
}

/**
* hashDomain
Expand Down Expand Up @@ -345,4 +377,18 @@ public function hashDomain(array $domainData)
}
return $this->hashStruct('EIP712Domain', $domainTypes, $domainData);
}

/**
* encodeTypedData
*
* @param array $domainData
* @param array $messageTypes
* @param array $messageData
*/
public function encodeTypedData(array $domainData, array $messageTypes, array $messageData)
{
$hashedDomain = $this->hashDomain($domainData);
$hashedMessage = $this->hashEIP712Message($messageTypes, $messageData);
return sprintf('\x01%s%s', Utils::stripZero($hashedDomain), Utils::stripZero($hashedMessage));
}
}

0 comments on commit 56f17fc

Please sign in to comment.