|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Test\Unit\Bech32; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Test\Bech32\Provider\ValidAddresses; |
|
6
|
|
|
use BitWasp\Test\Bech32\TestCase; |
|
7
|
|
|
use BitWasp\Test\Bech32\Util; |
|
8
|
|
|
|
|
9
|
|
|
class EncodeTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
public function validAddressProvider() |
|
15
|
|
|
{ |
|
16
|
|
|
return ValidAddresses::load(); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L106 |
|
21
|
|
|
* @param string $hrp |
|
22
|
|
|
* @param string $bech32 |
|
23
|
|
|
* @param string $hexScript |
|
24
|
|
|
* @dataProvider validAddressProvider |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testValidAddress($hrp, $bech32, $hexScript) |
|
27
|
|
|
{ |
|
28
|
|
|
// Check we decode, and that HRP matches test fixture |
|
29
|
|
|
list ($gotHRP, $data) = \Bitwasp\Bech32\decode($bech32); |
|
30
|
|
|
$this->assertEquals($hrp, $gotHRP); |
|
31
|
|
|
|
|
32
|
|
|
$decoded = \BitWasp\Bech32\convertBits(array_slice($data, 1), count($data) - 1, 5, 8, false); |
|
33
|
|
|
$program = ''; |
|
34
|
|
|
foreach ($decoded as $char) { |
|
35
|
|
|
$program .= chr($char); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Check decoded details against our known witness program |
|
39
|
|
|
$version = $data[0]; |
|
40
|
|
|
$checkWitnessProgram = Util::witnessProgram($version, $program); |
|
41
|
|
|
$this->assertEquals($hexScript, $checkWitnessProgram); |
|
42
|
|
|
|
|
43
|
|
|
// Simple re-encoding test |
|
44
|
|
|
|
|
45
|
|
|
$encoded = \BitWasp\Bech32\encode($hrp, $data); |
|
46
|
|
|
$this->assertEquals(strtolower($bech32), $encoded); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|