Completed
Pull Request — master (#662)
by thomas
38:24
created

PrefixRegistry::getPrefixes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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