Completed
Pull Request — 0.0.35 (#660)
by thomas
29:47 queued 20:44
created

PrefixRegistry   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 19 8
A getPrefixes() 0 7 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\Deterministic\Slip132;
4
5
class PrefixRegistry
6
{
7
    /**
8
     * @var array
9
     */
10
    private $registry = [];
11
12
    /**
13
     * PrefixRegistry constructor.
14
     * @param array $registry
15
     */
16 41
    public function __construct(array $registry)
17
    {
18 41
        foreach ($registry as $scriptType => $prefixes) {
19 40
            if (!is_string($scriptType)) {
20 1
                throw new \InvalidArgumentException("Expecting script type as key");
21
            }
22 39
            if (count($prefixes) !== 2) {
23 1
                throw new \InvalidArgumentException("Expecting two BIP32 prefixes");
24
            }
25
            // private, public
26 38
            if (strlen($prefixes[0]) !== 8 || !ctype_xdigit($prefixes[0])) {
27 1
                throw new \InvalidArgumentException("Invalid private prefix");
28
            }
29 37
            if (strlen($prefixes[1]) !== 8 || !ctype_xdigit($prefixes[1])) {
30 37
                throw new \InvalidArgumentException("Invalid public prefix");
31
            }
32
        }
33 37
        $this->registry = $registry;
34 37
    }
35
36
    /**
37
     * @param string $scriptType
38
     * @return array
39
     */
40 37
    public function getPrefixes($scriptType)
41
    {
42 37
        if (!array_key_exists($scriptType, $this->registry)) {
43 2
            throw new \InvalidArgumentException("Unknown script type");
44
        }
45 35
        return $this->registry[$scriptType];
46
    }
47
}
48