Skip to content

Commit

Permalink
added basic addrr validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekliptor authored and Ekliptor committed Feb 8, 2020
1 parent c0ea92e commit 0fd1133
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ returns `string` - The button HTML.

---

##### isValidBchAddress(string $bchAddress): bool
Check if a BCH address is valid.
* `string $bchAddress` - The address in CashAddress format starting with 'bitcoincash:'

returns `bool` - True if the address is valid, false otherwise.

---

##### isValidSlpAddress(string $slpAddress): bool
Check if a SLP address is valid.
* `string $slpAddress` - The address starting with 'simpleledger:'

returns `bool` - True if the address is valid, false otherwise.

---


#### CashpOptions class
A set of advanced config properties.
Expand Down
40 changes: 40 additions & 0 deletions src/CashP.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,45 @@ public function getBadgerButton(array $btnConf, string $address, float $amountBC
$this->includedButtonCode = $btnConf['includedButtonCode'];
return $buttonHtml;
}

/**
* Check if a BCH address is valid.
* @param string $bchAddress The address in CashAddress format starting with 'bitcoincash:'
* @return bool True if the address is valid, false otherwise.
*/
public function isValidBchAddress(string $bchAddress): bool {
if (empty($bchAddress))
return false;
// TODO improve this by checking the actual address format
$bchAddress = strtolower(trim($bchAddress));
if (preg_match("/^bitcoincash:/", $bchAddress) !== 1)
return false;
if (strlen($bchAddress) !== 54)
return false;
$addressParts = explode(':', $bchAddress);
if (count($addressParts) !== 2)
return false;
return preg_match("/^[a-z0-9]+$/", $addressParts[1]) === 1;
}

/**
* Check if a SLP address is valid.
* @param string $slpAddress The address starting with 'simpleledger:'
* @return bool True if the address is valid, false otherwise.
*/
public function isValidSlpAddress(string $slpAddress): bool {
if (empty($slpAddress))
return false;
// TODO improve this by checking the actual address format
$slpAddress = strtolower(trim($slpAddress));
if (preg_match("/^simpleledger:/", $slpAddress) !== 1)
return false;
if (strlen($slpAddress) !== 55)
return false;
$addressParts = explode(':', $slpAddress);
if (count($addressParts) !== 2)
return false;
return preg_match("/^[a-z0-9]+$/", $addressParts[1]) === 1;
}
}
?>

0 comments on commit 0fd1133

Please sign in to comment.