diff --git a/src/Utils.php b/src/Utils.php index d623b58..59c26de 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -136,6 +136,26 @@ class Utils return $value; } + /** + * isAddress + * To do: check address checksum + * + * @param string + * @return bool + */ + 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-fA-F0-9]{40}$/', $value) == 1) { + return true; + } else { + // validate address checksum + } + return false; + } + /** * sha3 * keccak256 diff --git a/test/unit/UtilsTest.php b/test/unit/UtilsTest.php index 529884d..a464cc0 100644 --- a/test/unit/UtilsTest.php +++ b/test/unit/UtilsTest.php @@ -112,6 +112,34 @@ class UtilsTest extends TestCase $this->assertEquals($isPrefixed, true); } + /** + * testIsAddress + * + * @return void + */ + public function testIsAddress() + { + $isAddress = Utils::isAddress('ca35b7d915458ef540ade6068dfe2f44e8fa733c'); + + $this->assertEquals($isAddress, true); + + $isAddress = Utils::isAddress('0xca35b7d915458ef540ade6068dfe2f44e8fa733c'); + + $this->assertEquals($isAddress, true); + + $isAddress = Utils::isAddress('0Xca35b7d915458ef540ade6068dfe2f44e8fa733c'); + + $this->assertEquals($isAddress, true); + + $isAddress = Utils::isAddress('0XCA35B7D915458EF540ADE6068DFE2F44E8FA733C'); + + $this->assertEquals($isAddress, true); + + $isAddress = Utils::isAddress('0xCA35B7D915458EF540ADE6068DFE2F44E8FA733C'); + + $this->assertEquals($isAddress, true); + } + /** * testStripZero *