Skip to content

Commit

Permalink
Add typed-data test fixtures (from ethers.js)
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Jan 21, 2024
1 parent 7c18995 commit fed356e
Show file tree
Hide file tree
Showing 6 changed files with 4,431 additions and 24 deletions.
33 changes: 13 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">

<testsuite name="Web3.php unit test">
<directory suffix="Test.php">./test/unit</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuite name="Web3.php unit test">
<directory suffix="Test.php">./test/unit</directory>
</testsuite>
<php>
<ini name="memory_limit" value="256M"/>
</php>
</phpunit>
6 changes: 3 additions & 3 deletions src/Contracts/TypedDataEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function __set($name, $value)
* @param mixed $value
* @return array
*/
protected function encodeField(array $types, string $name, string $type, mixed $value)
protected function encodeField(array $types, string $name, string $type, $value)
{
if (array_key_exists($type, $types)) {
if (is_null($value)) {
Expand Down Expand Up @@ -140,7 +140,7 @@ protected function encodeField(array $types, string $name, string $type, mixed $
}
return ["bytes32", Utils::sha3($this->ethabi->encodeParameters($dataTypes, $dataValues))];
} else if ($type === 'bool') {
return [$type, bool($value)];
return [$type, (bool)$value];
} else if (substr($type, 0, 5) === 'bytes') {
if ($type === 'bytes') {
return ['bytes32', Utils::sha3($value)];
Expand Down Expand Up @@ -395,6 +395,6 @@ public function encodeTypedData(array $domainData, array $messageTypes, array $m
{
$hashedDomain = $this->hashDomain($domainData);
$hashedMessage = $this->hashEIP712Message($messageTypes, $messageData);
return sprintf('\x01%s%s', Utils::stripZero($hashedDomain), Utils::stripZero($hashedMessage));
return sprintf('0x1901%s%s', Utils::stripZero($hashedDomain), Utils::stripZero($hashedMessage));
}
}
13 changes: 12 additions & 1 deletion src/Formatters/IntegerFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,22 @@ public static function format($value)
}
$bn = Utils::toBn($value);
$bnHex = $bn->toHex(true);
$bnHexLen = mb_strlen($bnHex);
$padded = mb_substr($bnHex, 0, 1);

if ($bnHexLen >= $digit) {
$zeroPos = mb_strrpos($bnHex, '0');
if ($zeroPos !== false) {
$bnHex = mb_substr($bnHex, $zeroPos, $digit);
$bnHexLen = mb_strlen($bnHex);
}
if ($bnHexLen >= $digit) {
return mb_substr($bnHex, 0, $digit);
}
}
if ($padded !== 'f') {
$padded = '0';
}
}
return implode('', array_fill(0, $digit-mb_strlen($bnHex), $padded)) . $bnHex;
}
}
20 changes: 20 additions & 0 deletions test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class TestCase extends BaseTestCase
*/
protected $EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';

/**
* test fixtures
*
* TODO: add more fixtures
* @var array
*/
protected array $testFixtures = [];

/**
* setUp
*/
Expand All @@ -84,6 +92,18 @@ public function setUp(): void
$this->coinbase = $coinbase;
// }
});

// load test fixtures
$fixtureFileName = __DIR__ . '/fixtures/typed-data.json';
$json = \file_get_contents($fixtureFileName);
if (false === $json) {
throw new \RuntimeException("Unable to load file {$fixtureFileName}");
}

$data = \json_decode($json, true);
$this->testFixtures = [
'typed-data' => $data
];
}

/**
Expand Down
Loading

0 comments on commit fed356e

Please sign in to comment.