Skip to content

Commit

Permalink
Merge pull request #357 from web3p/update-is-address
Browse files Browse the repository at this point in the history
Return true only when the address is lowercase or in checksum format
  • Loading branch information
sc0Vu authored Jul 5, 2024
2 parents 1e6213d + 6d9c359 commit 716082d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
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

0 comments on commit 716082d

Please sign in to comment.