Skip to content

Commit

Permalink
Throw an exception if bcmath is not installed
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
kielabokkie committed Sep 28, 2023
1 parent 9520191 commit ccf3d36
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions src/AddressValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@ class AddressValidator
private $includeTestnet = false;
private $onlyTestnet = false;

public function __construct()
{
if (extension_loaded('bcmath') === false) {
throw new \RuntimeException(
'The required BCMath extension is missing. Please install it to use this package.'
);
}
}

/**
* Allow both mainnet and testnet addresses.
*
* @return AddressValidator
*/
public function includeTestnet()
public function includeTestnet(): AddressValidator
{
$this->includeTestnet = true;
return $this;
}

/**
* Allow only testnet addresses.
*
* @return AddressValidator
*/
public function onlyTestnet()
public function onlyTestnet(): AddressValidator
{
$this->onlyTestnet = true;
return $this;
}

/**
* Validates a given address.
*
* @param string $address
* @return boolean
*/
public function isValid($address)
public function isValid(string $address): bool
{
if ($this->isPayToPublicKeyHash($address)) {
return true;
Expand All @@ -58,53 +60,46 @@ public function isValid($address)

/**
* Validates a P2PKH address.
*
* @param string $address
* @return boolean
*/
public function isPayToPublicKeyHash($address)
public function isPayToPublicKeyHash(string $address): bool
{
$prefix = $this->onlyTestnet ? 'nm' : ($this->includeTestnet ? '1nm' : '1');
$expr = sprintf('/^[%s][a-km-zA-HJ-NP-Z1-9]{25,34}$/', $prefix);

if (preg_match($expr, $address) === 1) {
try {
$base58 = new Base58;
return $base58->verify($address);
return (new Base58)->verify($address);
} catch (\Throwable $th) {
return false;
}
}

return false;
}

/**
* Validates a P2SH (segwit) address.
*
* @param string $address
* @return boolean
*/
public function isPayToScriptHash($address)
public function isPayToScriptHash(string $address): bool
{
$prefix = $this->onlyTestnet ? '2' : ($this->includeTestnet ? '23' : '3');
$expr = sprintf('/^[%s][a-km-zA-HJ-NP-Z1-9]{25,34}$/', $prefix);

if (preg_match($expr, $address) === 1) {
try {
$base58 = new Base58;
return $base58->verify($address);
return (new Base58)->verify($address);
} catch (\Throwable $th) {
return false;
}
}

return false;
}

/**
* Validates a P2TR (taproot) address.
*
* @param string $address
* @return boolean
*/
public function isPayToTaproot($address)
public function isPayToTaproot(string $address): bool
{
if (in_array(substr($address, 0, 4), ['bc1p', 'bcrt1p', 'tb1p']) === false) {
return false;
Expand All @@ -131,11 +126,8 @@ public function isPayToTaproot($address)

/**
* Validates a bech32 (native segwit) address.
*
* @param string $address
* @return boolean
*/
public function isBech32($address)
public function isBech32(string $address): bool
{
$prefix = $this->onlyTestnet ? 'tb' : ($this->includeTestnet ? 'bc|tb' : 'bc');
$expr = sprintf(
Expand Down

0 comments on commit ccf3d36

Please sign in to comment.