|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Test\Bech32; |
|
4
|
|
|
|
|
5
|
|
|
use function BitWasp\Bech32\decodeSegwit; |
|
6
|
|
|
use function BitWasp\Bech32\encodeSegwit; |
|
7
|
|
|
use BitWasp\Bech32\Exception\Bech32Exception; |
|
8
|
|
|
use BitWasp\Test\Bech32\Provider\InvalidAddresses; |
|
9
|
|
|
use BitWasp\Test\Bech32\Provider\ValidAddresses; |
|
10
|
|
|
|
|
11
|
|
|
class SegwitAddressTest extends TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
public function validAddressProvider() |
|
17
|
|
|
{ |
|
18
|
|
|
return ValidAddresses::load(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L106 |
|
23
|
|
|
* @param string $hrp |
|
24
|
|
|
* @param string $bech32 |
|
25
|
|
|
* @param string $hexScript |
|
26
|
|
|
* @dataProvider validAddressProvider |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testValidAddress($hrp, $bech32, $hexScript) |
|
29
|
|
|
{ |
|
30
|
|
|
list ($version, $program) = decodeSegwit($hrp, $bech32); |
|
31
|
|
|
$this->assertEquals($hexScript, Util::witnessProgram($version, $program)); |
|
32
|
|
|
|
|
33
|
|
|
$addr = encodeSegwit($hrp, $version, $program); |
|
34
|
|
|
$this->assertEquals(strtolower($bech32), strtolower($addr)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
public function invalidAddressProvider() |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
["tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty"], |
|
42
|
|
|
["bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5"], |
|
43
|
|
|
["BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2"], |
|
44
|
|
|
["bc1rw5uspcuh"], |
|
45
|
|
|
["bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90"], |
|
46
|
|
|
["BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P"], |
|
47
|
|
|
["tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7"], |
|
48
|
|
|
["tb1pw508d6qejxtdg4y5r3zarqfsj6c3"], |
|
49
|
|
|
["tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv"], |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $bech32 |
|
55
|
|
|
* @dataProvider invalidAddressProvider |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testInvalidAddress($bech32) |
|
58
|
|
|
{ |
|
59
|
|
|
try { |
|
60
|
|
|
decodeSegwit("bc", $bech32); |
|
61
|
|
|
$threw = false; |
|
62
|
|
|
} catch (\Exception $e) { |
|
63
|
|
|
$threw = true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->assertTrue($threw, "expected mainnet hrp to fail"); |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
decodeSegwit("tb", $bech32); |
|
70
|
|
|
$threw = false; |
|
71
|
|
|
} catch (\Exception $e) { |
|
72
|
|
|
$threw = true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->assertTrue($threw, "expected testnet hrp to fail"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function invalidAddressProvider2() |
|
82
|
|
|
{ |
|
83
|
|
|
return InvalidAddresses::load(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $prefix |
|
88
|
|
|
* @param $bech32 |
|
89
|
|
|
* @param $exceptionMsg |
|
90
|
|
|
* @dataProvider invalidAddressProvider2 |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testInvalidAddressReasons($prefix, $bech32, $exceptionMsg) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->expectException(Bech32Exception::class); |
|
95
|
|
|
$this->expectExceptionMessage($exceptionMsg); |
|
96
|
|
|
|
|
97
|
|
|
decodeSegwit($prefix, $bech32); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|