Skip to content

Make validation for BTC and BCH soften #29

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

Merged
merged 3 commits into from
Jun 30, 2023
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
6 changes: 5 additions & 1 deletion src/Drivers/Base32Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Merkeleon\PhpCryptocurrencyAddressValidation\Drivers;

use Illuminate\Support\Str;
use Merkeleon\PhpCryptocurrencyAddressValidation\Utils\Base32Decoder;
use RuntimeException;
use Throwable;
Expand All @@ -15,6 +16,7 @@
use function pack;
use function preg_match;
use function sprintf;
use function str_contains;
use function strtolower;

class Base32Driver extends AbstractDriver
Expand Down Expand Up @@ -43,9 +45,11 @@ public function match(string $address): bool
public function check(string $address, array $networks = []): bool
{
try {
$hasPrefix = Str::contains($address, array_keys($this->options));

$address = strtolower($address);

[,$words] = Base32Decoder::decode($address);
[,$words] = Base32Decoder::decode($address, $hasPrefix);

$numWords = count($words);
$bytes = Base32Decoder::fromWords($numWords, $words);
Expand Down
3 changes: 3 additions & 0 deletions src/Drivers/Bech32Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function implode;
use function preg_match;
use function sprintf;
use function strtolower;

class Bech32Driver extends AbstractDriver
{
Expand All @@ -22,6 +23,8 @@ public function match(string $address): bool
public function check(string $address): bool
{
try {
$address = strtolower($address);

$expr = $this->getPattern();
preg_match($expr, $address, $match);

Expand Down
70 changes: 12 additions & 58 deletions src/Utils/Base32Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,59 +131,15 @@ protected static function polyModStep($prev)
*
* @return resource
*/
protected static function prefixChk(string $prefix)
public static function prefixChk(string $prefix)
{
$chk = gmp_init(1);
$length = strlen($prefix);
for ($i = 0; $i < $length; $i++) {
$char = ord($prefix[$i]) & 0x1f;
$chk = self::bitwiseXor(self::polyModStep($chk), gmp_init($char, 10));
}

$chk = self::polyModStep($chk);

return $chk;
}

/**
* @param string $prefix - string prefix
* @param array $words - 5bit words (array)
*
* @return string
* @throws Base32Exception
*/
public static function encode(string $prefix, array $words): string
{
if ((strlen($prefix) + 7 + count($words)) > 90) {
throw new Base32Exception();
}

$prefix = strtolower($prefix);

$chk = self::prefixChk($prefix);
$result = $prefix . self::SEPARATOR;

foreach ($words as $iValue) {
$x = $iValue;
if ($x >> 5 !== 0) {
throw new \RuntimeException("Non 5-bit word");
}
$chk = self::bitwiseXor(self::polyModStep($chk), gmp_init($x));
$result .= self::$charset[$x];
}

for ($i = 0; $i < self::$checksumLen; ++$i) {
$chk = self::polyModStep($chk);
}
$chk = self::bitwiseXor($chk, gmp_init(1));

for ($i = 0; $i < self::$checksumLen; ++$i) {
$pos = 5 * (self::$checksumLen - 1 - $i);
$v2 = self::bitwiseAnd(self::rightShift($chk, $pos), gmp_init('1f', 16));
$result .= self::$charset[(int) gmp_strval($v2, 10)];
}

return $result;
return self::polyModStep($chk);
}

/**
Expand All @@ -193,7 +149,7 @@ public static function encode(string $prefix, array $words): string
* @throws Base32Exception
* @throws InvalidChecksumException
*/
public static function decode(string $string): array
public static function decode(string $string, bool $hasPrefix = true): array
{
$stringLen = strlen($string);
if ($stringLen < 8) {
Expand Down Expand Up @@ -234,16 +190,15 @@ public static function decode(string $string): array
throw new Base32Exception("Data contains mixture of higher/lower case characters");
}

if ($idxSeparator === -1) {
if ($hasPrefix && $idxSeparator === -1) {
throw new Base32Exception("Missing separator character");
} else {
if ($idxSeparator === 0) {
throw new Base32Exception("Missing prefix");
} else {
if (($idxSeparator + 7) > $stringLen) {
throw new Base32Exception("Invalid location for separator character");
}
}
}
if ($hasPrefix && $idxSeparator === 0) {
throw new Base32Exception("Missing prefix");
}

if (($idxSeparator + 7) > $stringLen) {
throw new Base32Exception("Invalid location for separator character");
}

$prefix = "";
Expand All @@ -252,7 +207,6 @@ public static function decode(string $string): array
$prefix .= pack("C*", $byte);
}


$chk = self::prefixChk($prefix);

$words = [];
Expand All @@ -266,7 +220,7 @@ public static function decode(string $string): array
$words[] = $word;
}

if (gmp_cmp($chk, gmp_init(1)) !== 0) {
if ($hasPrefix && gmp_cmp($chk, gmp_init(1)) !== 0) {
throw new InvalidChecksumException();
}

Expand Down
12 changes: 9 additions & 3 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ public function currencyAddressProvider(): array
'BitcoinCash #1' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, 'bitcoincash:qp009ldhprp75mgn4kgaw8jvrpadnvg8qst37j42kx'],
'BitcoinCash #2' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, 'bitcoincash:qz7032ylhvxmndkx438pd8kjd7k7zcqxzsf26q0lvr'],
'BitcoinCash #3' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, '32uLhn19ZasD5bsVhLdDthhM37JhJHiEE2'],
'BitcoinCash #4' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', false, 'qz52zsruu43sq7ed0srym3g0ktpyjkdkxcm949pl2z'],
'BitcoinCash #5' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', false, 'qpf8eq7ygvhqjwydk9n29f6nyc8rcjhlwcuwngn6xk'],
'BitcoinCash #4' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, 'qz52zsruu43sq7ed0srym3g0ktpyjkdkxcm949pl2z'],
'BitcoinCash #5' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, 'qpf8eq7ygvhqjwydk9n29f6nyc8rcjhlwcuwngn6xk'],
'BitcoinCash #6' => [CurrencyEnum::BITCOIN_CASH, 'testnet', true, 'bchtest:qp2vjh349lcd22hu0hv6hv9d0pwlk43f6u04d5jk36'],
'BitcoinCash #7' => [CurrencyEnum::BITCOIN_CASH, 'testnet', false, 'qp2vjh349lcd22hu0hv6hv9d0pwlk43f6u04d5jk36'],
'BitcoinCash #7' => [CurrencyEnum::BITCOIN_CASH, 'testnet', true, 'qp2vjh349lcd22hu0hv6hv9d0pwlk43f6u04d5jk36'],
'BitcoinCash #8' => [CurrencyEnum::BITCOIN_CASH, 'testnet', false, '1KADKOasjxpNKzbfcKjnigLYWjEFPcMXqf'],
'BitcoinCash #9' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, 'qpnxwdu09eq4gqxv0ala37yj5evmmakf5vpp770edu'],
'BitcoinCash #10' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, 'bitcoincash:qpnxwdu09eq4gqxv0ala37yj5evmmakf5vpp770edu'],
'BitcoinCash #11' => [CurrencyEnum::BITCOIN_CASH, 'testnet', false, 'bchtest:qpnxwdu09eq4gqxv0ala37yj5evmmakf5vpp770edu'],
'BitcoinCash #12' => [CurrencyEnum::BITCOIN_CASH, 'testnet', false, 'bchreg:qpnxwdu09eq4gqxv0ala37yj5evmmakf5vpp770edu'],
'BitcoinCash #13' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, 'bitcoincash:pwqwzrf7z06m7nn58tkdjyxqfewanlhyrpxysack85xvf3mt0rv02l9dxc5uf'],
'BitcoinCash #14' => [CurrencyEnum::BITCOIN_CASH, 'mainnet', true, 'bitcoincash:qr6m7j9njldwwzlg9v7v53unlr4jkmx6eylep8ekg2'],
//
'Bitcoin #1' => [CurrencyEnum::BITCOIN, 'mainnet', true, '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2'],
'Bitcoin #2' => [CurrencyEnum::BITCOIN, 'mainnet', true, 'bc1q6v096h88xmpl662af0nc7wd3vta56zv6pyccl8'],
Expand Down