Skip to content

feature(validators): add bech32m support #35

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
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
9 changes: 7 additions & 2 deletions src/Utils/Bech32Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class Bech32Decoder
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
];

public const BECH32_POLY = 1;
public const BECH32M_POLY = 0x2bc830a3;
private const ALLOWED_POLY = [
self::BECH32_POLY,
self::BECH32M_POLY,
];
/**
* Validates a bech32 string and returns [$hrp, $dataChars] if
* the conversion was successful. An exception is thrown on invalid
Expand Down Expand Up @@ -139,7 +144,7 @@ private function verifyChecksum(string $hrp, array $convertedDataChars): bool
$r = array_merge($expandHrp, $convertedDataChars);
$poly = $this->polyMod($r, count($r));

return $poly === 1;
return in_array($poly, self::ALLOWED_POLY, true);
}


Expand Down
1 change: 1 addition & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function currencyAddressProvider(): array
'Bitcoin #6' => [CurrencyEnum::BITCOIN, 'testnet', false, '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2'],
'Bitcoin #7' => [CurrencyEnum::BITCOIN, 'testnet', false, 'bc1q6v096h88xmpl662af0nc7wd3vta56zv6pyccl8'],
'Bitcoin #8' => [CurrencyEnum::BITCOIN, 'mainnet', true, 'BC1QL2725QLXHGWQ7F7XLJ8363FJCUF25XZ35SWRU5'],
'Bitcoin #9' => [CurrencyEnum::BITCOIN, 'mainnet', true, 'bc1p5gyty9x2lrk65yndaeh242zm6xklgrv9nze9477dg0kyv6yvfljq0lqjkh'],
//
'Cardano #1' => [CurrencyEnum::CARDANO, 'mainnet', true, 'addr1v9ywm0h3r8cnxrs04gfy7c3s2j44utjyvn5ldjdca0c2ltccgqdes'],
'Cardano #2' => [CurrencyEnum::CARDANO, 'mainnet', false, 'stake1u9f9v0z5zzlldgx58n8tklphu8mf7h4jvp2j2gddluemnssjfnkzz'],
Expand Down