Completed
Push — master ( 1c8af5...5745ff )
by thomas
04:13 queued 02:50
created

SegwitAddressTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidAddress() 0 7 1
A validAddressProvider() 0 3 1
A invalidAddressProvider() 0 11 1
A testInvalidAddress() 0 19 3
1
<?php
2
3
namespace BitWasp\Test\Bech32;
4
5
6
use function BitWasp\Bech32\decodeSegwit;
0 ignored issues
show
Bug introduced by
The type BitWasp\Bech32\decodeSegwit was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use function BitWasp\Bech32\encodeSegwit;
0 ignored issues
show
Bug introduced by
The type BitWasp\Bech32\encodeSegwit was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use BitWasp\Test\Bech32\Provider\ValidAddresses;
9
10
class SegwitAddressTest extends TestCase
11
{
12
    /**
13
     * @return array
14
     */
15
    public function validAddressProvider()
16
    {
17
        return ValidAddresses::load();
18
    }
19
20
    /**
21
     * https://github.com/sipa/bech32/blob/master/ref/python/tests.py#L106
22
     * @param string $hrp
23
     * @param string $bech32
24
     * @param string $hexScript
25
     * @dataProvider validAddressProvider
26
     */
27
    public function testValidAddress($hrp, $bech32, $hexScript)
28
    {
29
        list ($version, $program) = decodeSegwit($hrp, $bech32);
30
        $this->assertEquals($hexScript, Util::witnessProgram($version, $program));
31
32
        $addr = encodeSegwit($hrp, $version, $program);
33
        $this->assertEquals(strtolower($bech32), strtolower($addr));
34
    }
35
36
37
    public function invalidAddressProvider() {
38
        return [
39
            ["tc1qw508d6qejxtdg4y5r3zarvary0c5xw7kg3g4ty"],
40
            ["bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5"],
41
            ["BC13W508D6QEJXTDG4Y5R3ZARVARY0C5XW7KN40WF2"],
42
            ["bc1rw5uspcuh"],
43
            ["bc10w508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kw5rljs90"],
44
            ["BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P"],
45
            ["tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sL5k7"],
46
            ["tb1pw508d6qejxtdg4y5r3zarqfsj6c3"],
47
            ["tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3pjxtptv"],
48
        ];
49
    }
50
51
52
    /**
53
     * @param string $bech32
54
     * @dataProvider invalidAddressProvider
55
     */
56
    public function testInvalidAddress($bech32)
57
    {
58
        try {
59
            decodeSegwit("bc", $bech32);
60
            $threw = false;
61
        } catch (\Exception $e) {
62
            $threw = true;
63
        }
64
65
        $this->assertTrue($threw, "expected mainnet hrp to fail");
66
67
        try {
68
            decodeSegwit("tb", $bech32);
69
            $threw = false;
70
        } catch (\Exception $e) {
71
            $threw = true;
72
        }
73
74
        $this->assertTrue($threw, "expected testnet hrp to fail");
75
    }
76
}
77