Completed
Pull Request — 0.0.35 (#660)
by thomas
28:27
created

PrefixRegistry::__construct()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 11
nc 6
nop 1
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 8
rs 7.7777
c 0
b 0
f 0
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 35
    public function __construct(array $registry)
17
    {
18 35
        foreach ($registry as $scriptType => $prefixes) {
19 34
            if (!is_string($scriptType)) {
20 1
                throw new \InvalidArgumentException("Expecting script type as key");
21
            }
22 33
            if (count($prefixes) !== 2) {
23 1
                throw new \InvalidArgumentException("Expecting two BIP32 prefixes");
24
            }
25 32
            if (strlen($prefixes[0]) !== 8 || !ctype_xdigit($prefixes[0])) {
26 1
                throw new \InvalidArgumentException("Invalid private prefix");
27
            }
28 31
            if (strlen($prefixes[1]) !== 8 || !ctype_xdigit($prefixes[1])) {
29 31
                throw new \InvalidArgumentException("Invalid public prefix");
30
            }
31
        }
32 31
        $this->registry = $registry;
33 31
    }
34
35
    /**
36
     * @param string $scriptType
37
     * @return array
38
     */
39 31
    public function getPrefixes($scriptType)
40
    {
41 31
        if (!array_key_exists($scriptType, $this->registry)) {
42 1
            throw new \InvalidArgumentException("Unknown script type");
43
        }
44 30
        return $this->registry[$scriptType];
45
    }
46
}
47