Completed
Pull Request — 0.0.35 (#660)
by thomas
22:14
created

PrefixRegistry::getPrefixes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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