Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return true only when the address is lowercase or in checksum format #357

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,14 @@ public static function isNegative($value)
* isAddress
*
* @param string $value
* @return bool
* @return bool true if the address is lowercase or in checksum format otherwise false
*/
public static function isAddress($value)
{
if (!is_string($value)) {
throw new InvalidArgumentException('The value to isAddress function must be string.');
}
if (preg_match('/^(0x|0X)?[a-f0-9A-F]{40}$/', $value) !== 1) {
return false;
} elseif (preg_match('/^(0x|0X)?[a-f0-9]{40}$/', $value) === 1 || preg_match('/^(0x|0X)?[A-F0-9]{40}$/', $value) === 1) {
if (preg_match('/^(0x)?[a-f0-9]{40}$/', $value) === 1) {
return true;
}
return self::isAddressChecksum($value);
Expand All @@ -202,6 +200,9 @@ public static function isAddressChecksum($value)
throw new InvalidArgumentException('The value to isAddressChecksum function must be string.');
}
$value = self::stripZero($value);
if (mb_strlen($value) !== 40) {
return false;
}
$hash = self::stripZero(self::sha3(mb_strtolower($value)));

for ($i = 0; $i < 40; $i++) {
Expand Down
9 changes: 6 additions & 3 deletions test/unit/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,20 @@ public function testIsAddress()
$this->assertEquals($isAddress, true);

$isAddress = Utils::isAddress('0Xca35b7d915458ef540ade6068dfe2f44e8fa733c');
$this->assertEquals($isAddress, true);
$this->assertEquals($isAddress, false);

$isAddress = Utils::isAddress('0XCA35B7D915458EF540ADE6068DFE2F44E8FA733C');
$this->assertEquals($isAddress, true);
$this->assertEquals($isAddress, false);

$isAddress = Utils::isAddress('0xCA35B7D915458EF540ADE6068DFE2F44E8FA733C');
$this->assertEquals($isAddress, true);
$this->assertEquals($isAddress, false);

$isAddress = Utils::isAddress('0xCA35B7D915458EF540ADE6068DFE2F44E8FA73cc');
$this->assertEquals($isAddress, false);

$isAddress = Utils::isAddress('0xCA35b7d915458Ef540ADE6068DfE2F44E8fA73cC');
$this->assertEquals($isAddress, true);

$this->expectException(InvalidArgumentException::class);
$isAddress = Utils::isAddress(new stdClass);
}
Expand Down
Loading