Completed
Pull Request — 0.0.35 (#660)
by thomas
25:11
created

Slip132::loadPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Key\Deterministic\Slip132;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Key\Deterministic\Slip132\PrefixRegistry;
7
use BitWasp\Bitcoin\Key\Deterministic\HdPrefix\ScriptPrefix;
8
use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactory;
9
use BitWasp\Bitcoin\Key\KeyToScript\KeyToScriptHelper;
10
11
class Slip132
12
{
13
    /**
14
     * @var KeyToScriptHelper
15
     */
16
    private $helper;
17
18
    /**
19
     * Slip132PrefixRegistry constructor.
20
21
     * @param KeyToScriptHelper $helper
22
     */
23 21
    public function __construct(KeyToScriptHelper $helper = null)
24
    {
25 21
        $this->helper = $helper ?: new KeyToScriptHelper(Bitcoin::getEcAdapter());
26 21
    }
27
28
    /**
29
     * @param PrefixRegistry $registry
30
     * @param ScriptDataFactory $factory
31
     * @return ScriptPrefix
32
     * @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter
33
     */
34 21
    private function loadPrefix(PrefixRegistry $registry, ScriptDataFactory $factory)
35
    {
36 21
        list ($private, $public) = $registry->getPrefixes($factory->getScriptType());
37 21
        return new ScriptPrefix($factory, $private, $public);
38
    }
39
40
    /**
41
     * @param PrefixRegistry $registry
42
     * @return ScriptPrefix
43
     * @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter
44
     */
45 6
    public function xpubP2pkh(PrefixRegistry $registry)
46
    {
47 6
        return $this->loadPrefix($registry, $this->helper->getP2pkhFactory());
48
    }
49
50
    /**
51
     * @param PrefixRegistry $registry
52
     * @return ScriptPrefix
53
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
54
     * @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter
55
     */
56 5
    public function ypubP2shP2wpkh(PrefixRegistry $registry)
57
    {
58 5
        return $this->loadPrefix($registry, $this->helper->getP2shFactory($this->helper->getP2wpkhFactory()));
59
    }
60
61
    /**
62
     * @param PrefixRegistry $registry
63
     * @return ScriptPrefix
64
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
65
     * @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter
66
     */
67 2
    public function ypubP2shP2wshP2pkh(PrefixRegistry $registry)
68
    {
69 2
        return $this->loadPrefix($registry, $this->helper->getP2shP2wshFactory($this->helper->getP2pkhFactory()));
70
    }
71
72
    /**
73
     * @param PrefixRegistry $registry
74
     * @return ScriptPrefix
75
     * @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter
76
     */
77 6
    public function zpubP2wpkh(PrefixRegistry $registry)
78
    {
79 6
        return $this->loadPrefix($registry, $this->helper->getP2wpkhFactory());
80
    }
81
82
    /**
83
     * @param PrefixRegistry $registry
84
     * @return ScriptPrefix
85
     * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException
86
     * @throws \BitWasp\Bitcoin\Exceptions\InvalidNetworkParameter
87
     */
88 2
    public function zpubP2wshP2pkh(PrefixRegistry $registry)
89
    {
90 2
        return $this->loadPrefix($registry, $this->helper->getP2wshFactory($this->helper->getP2pkhFactory()));
91
    }
92
}
93