Skip to content

Commit a014b76

Browse files
authored
Merge pull request #35 from LiquidCats/feature/add-bech32m-addresses-support
2 parents 4f54897 + 25e3538 commit a014b76

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Utils/Bech32Decoder.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ class Bech32Decoder
2828
-1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
2929
1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
3030
];
31-
31+
public const BECH32_POLY = 1;
32+
public const BECH32M_POLY = 0x2bc830a3;
33+
private const ALLOWED_POLY = [
34+
self::BECH32_POLY,
35+
self::BECH32M_POLY,
36+
];
3237
/**
3338
* Validates a bech32 string and returns [$hrp, $dataChars] if
3439
* the conversion was successful. An exception is thrown on invalid
@@ -139,7 +144,7 @@ private function verifyChecksum(string $hrp, array $convertedDataChars): bool
139144
$r = array_merge($expandHrp, $convertedDataChars);
140145
$poly = $this->polyMod($r, count($r));
141146

142-
return $poly === 1;
147+
return in_array($poly, self::ALLOWED_POLY, true);
143148
}
144149

145150

tests/ValidatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function currencyAddressProvider(): array
7070
'Bitcoin #6' => [CurrencyEnum::BITCOIN, 'testnet', false, '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2'],
7171
'Bitcoin #7' => [CurrencyEnum::BITCOIN, 'testnet', false, 'bc1q6v096h88xmpl662af0nc7wd3vta56zv6pyccl8'],
7272
'Bitcoin #8' => [CurrencyEnum::BITCOIN, 'mainnet', true, 'BC1QL2725QLXHGWQ7F7XLJ8363FJCUF25XZ35SWRU5'],
73+
'Bitcoin #9' => [CurrencyEnum::BITCOIN, 'mainnet', true, 'bc1p5gyty9x2lrk65yndaeh242zm6xklgrv9nze9477dg0kyv6yvfljq0lqjkh'],
7374
//
7475
'Cardano #1' => [CurrencyEnum::CARDANO, 'mainnet', true, 'addr1v9ywm0h3r8cnxrs04gfy7c3s2j44utjyvn5ldjdca0c2ltccgqdes'],
7576
'Cardano #2' => [CurrencyEnum::CARDANO, 'mainnet', false, 'stake1u9f9v0z5zzlldgx58n8tklphu8mf7h4jvp2j2gddluemnssjfnkzz'],

0 commit comments

Comments
 (0)