Skip to content

Commit

Permalink
Fix encoding and make test happier
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jan 26, 2024
1 parent a20f2a6 commit 18660ea
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
46 changes: 44 additions & 2 deletions src/Contracts/Ethabi.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,48 @@ public function parseTupleType($name)
return false;
}

/**
* parseTupleTypes
* TODO: replace with lexical analysis
*
* @param string $type
* @param bool $parseInner
* @return array
*/
protected function parseTupleTypes($type, $parseInner = false) {
$leftBrackets = [];
$results = [];
$offset = 0;
for ($key = 0; $key < mb_strlen($type); $key++) {
$char = $type[$key];
if ($char === '(') {
$leftBrackets[] = $key;
} else if ($char === ')') {
$leftKey = array_pop($leftBrackets);
if (is_null($leftKey)) {
throw new InvalidArgumentException('Wrong tuple type: ' . $type);
}
} else if ($char === ',') {
if (empty($leftBrackets)) {
$length = $key - $offset;
$results[] = mb_substr($type, $offset, $length);
$offset += $length + 1;
}
}
}
if ($offset < mb_strlen($type)) {
$results[] = mb_substr($type, $offset);
}
if ($parseInner) {
if (count($results) === 1) {
if ($results[0] === $type && $type[0] === '(' && $type[mb_strlen($type) - 1] === ')') {
$results[0] = mb_substr($type, 1, mb_strlen($type) - 2);
}
}
}
return $results;
}

/**
* findAbiType
*
Expand Down Expand Up @@ -235,8 +277,8 @@ protected function findAbiType($type)
}
return $result;
} elseif ($this->isTuple($type)) {
$nestedType = $this->parseTupleType($type);
$parsedNestedTypes = (!$this->isTuple($nestedType)) ? preg_split('/,/', $nestedType) : [ $nestedType ];
$nestedType = $this->parseTupleTypes($type, true)[0];
$parsedNestedTypes = $this->parseTupleTypes($nestedType);
$tupleAbi = [
'type' => $type,
'dynamic' => false,
Expand Down
1 change: 0 additions & 1 deletion src/Contracts/Types/SizedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public function outputFormat($value, $abiType)
}
$offset += 64;
}
var_dump('Length: ' . $length . ' Decode length: ' . $valueLength, $abiType['coders']);
$results = [];
$decoder = $abiType['coders'];
for ($i = 0; $i < $length; $i++) {
Expand Down
12 changes: 6 additions & 6 deletions src/Contracts/Types/Tuple.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ public function inputFormat($params, $abiTypes)
$headLength += 32;
continue;
}
$headLength += (int) mb_strlen($head) / 2;
$headLength += (int) (mb_strlen($head) / 2);
}
$tailOffsets = [0];
$totalOffset = 0;
foreach ($tail as $key => $val) {
if ($key === count($tail) - 1) {
break;
}
$tailOffsets[] = (int) (mb_strlen($val) / 2);
$totalOffset += (int) (mb_strlen($val) / 2);
$tailOffsets[] = $totalOffset;
}
$headChunks = [];
$totalOffset = 0;
foreach ($rawHead as $key => $head) {
if (!array_key_exists($key, $tail)) continue;
if (!array_key_exists($key, $tailOffsets)) continue;
$offset = $tailOffsets[$key];
if (is_null($head)) {
$totalOffset += $offset;
$headChunks[] = IntegerFormatter::format($headLength + $totalOffset);
$headChunks[] = IntegerFormatter::format($headLength + $offset);
continue;
}
$headChunks[] = $head;
Expand Down
4 changes: 2 additions & 2 deletions src/Formatters/IntegerFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class IntegerFormatter implements IFormatter
*/
public static function format($value)
{
$isNegative = (int) $value < 0;
$value = (string) $value;
$arguments = func_get_args();
$digit = 64;
Expand All @@ -35,8 +34,9 @@ public static function format($value)
}
$bn = Utils::toBn($value);
$bnHex = $bn->toHex(true);
$bnStr = $bn->toString();
$bnHexLen = mb_strlen($bnHex);
$padded = ($isNegative) ? 'f' : mb_substr($bnHex, 0, 1);
$padded = ($bnStr[0] === '-') ? 'f' : mb_substr($bnHex, 0, 1);

if ($bnHexLen > $digit) {
$zeroPos = 0;
Expand Down
Loading

0 comments on commit 18660ea

Please sign in to comment.