Skip to content

Commit

Permalink
Add unit test for TypedDataEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jan 20, 2024
1 parent dd37131 commit 7c18995
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 18 deletions.
40 changes: 23 additions & 17 deletions src/Contracts/TypedDataEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected function encodeField(array $types, string $name, string $type, mixed $
{
if (array_key_exists($type, $types)) {
if (is_null($value)) {
return ['bytes32', '0x' . str_repeat('0', 64)];
return ['bytes32', '0x0000000000000000000000000000000000000000000000000000000000000000'];
} else {
return ['bytes32', Utils::sha3($this->encodeData($type, $types, $value))];
}
Expand All @@ -126,14 +126,7 @@ protected function encodeField(array $types, string $name, string $type, mixed $
'Invalid value for field ' . $name . ' of type ' . $type . ': expected array'
);
}
$pos = strpos($type, '[');
if ($pos === false) {
// should not happen
throw new InvalidArgumentException(
'Invalid value for field ' . $name . ' of type ' . $type . ': expected array'
);
}
$parsedType = substr($type, 0, $pos);
$parsedType = $this->parseParentArrayType($type);
$dataTypes = [];
$dataValues = [];
foreach ($value as $item) {
Expand All @@ -145,7 +138,7 @@ protected function encodeField(array $types, string $name, string $type, mixed $
# the keccak hash of `encode((), ())`
return ['bytes32', '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'];
}
return ["bytes32", Utils::sha3($this->ethabi->encode($dataTypes, $dataValues))];
return ["bytes32", Utils::sha3($this->ethabi->encodeParameters($dataTypes, $dataValues))];
} else if ($type === 'bool') {
return [$type, bool($value)];
} else if (substr($type, 0, 5) === 'bytes') {
Expand Down Expand Up @@ -175,6 +168,21 @@ protected function strEndsWith(string $haystack, string $needle)
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, - $needle_len));
}

/**
* parseParentArrayType
*
* @param string $type
* @return string
*/
protected function parseParentArrayType(string $type)
{
if ($this->strEndsWith($type, ']')) {
$pos = strrpos($type, '[');
$type = ($pos !== false) ? substr($type, 0, $pos) : $type;
}
return $type;
}

/**
* parseArrayType
*
Expand Down Expand Up @@ -229,12 +237,10 @@ protected function encodeType(string $type, array $types)
$result = '';
$unsortedDeps = $this->findType($type, $types);
if (in_array($type, $unsortedDeps)) {
$unsortedDeps = array_splice($unsortedDeps, array_search($type, $unsortedDeps), 1);
} else {
sort($unsortedDeps);
array_splice($unsortedDeps, array_search($type, $unsortedDeps), 1);
}
$deps = [ $type ];
$deps = array_merge($unsortedDeps);
sort($unsortedDeps);
$deps = array_merge([ $type ], $unsortedDeps);
foreach ($deps as $type) {
$params = [];
foreach ($types[$type] as $param) {
Expand Down Expand Up @@ -307,12 +313,12 @@ protected function getPrimaryType(array $types)
foreach ($types as $key => $typeFields) {
foreach ($typeFields as $field) {
$type = $this->parseArrayType($field['type']);
if (!in_array($type, $customTypes) && $type !== $key) {
if (in_array($type, $customTypes) && $type !== $key) {
$customDepsTypes[] = $type;
}
}
}
$primaryType = array_diff($customTypes, $customDepsTypes);
$primaryType = array_values(array_diff($customTypes, $customDepsTypes));
if (count($primaryType) === 0) {
throw new InvalidArgumentException('Unable to determine primary type');
}
Expand Down
1 change: 0 additions & 1 deletion src/Contracts/Types/Bytes.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function inputFormat($value, $name)

if (mb_strlen($value) % 2 !== 0) {
$value = "0" . $value;
// throw new InvalidArgumentException('The value to inputFormat has invalid length. Value: ' . $value);
}

if (mb_strlen($value) > 64) {
Expand Down
Loading

0 comments on commit 7c18995

Please sign in to comment.