Completed
Pull Request — master (#517)
by thomas
68:51 queued 66:33
created

Network::getSegwitBech32Prefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
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 0
dl 0
loc 8
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\Network;
4
5
class Network implements NetworkInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    private $addressByte;
11
12
    /**
13
     * @var string
14
     */
15
    private $privByte;
16
17
    /**
18
     * @var string
19
     */
20
    private $p2shByte;
21
22
    /**
23
     * @var bool
24
     */
25
    private $testnet;
26
27
    /**
28
     * @var null|string
29
     */
30
    private $xpubByte;
31
32
    /**
33
     * @var null|string
34
     */
35
    private $xprivByte;
36
37
    /**
38
     * @var string
39
     */
40
    private $netMagicBytes;
41
42
    /**
43
     * @var null|string
44
     */
45
    private $segwitAddrPrefix;
46
47
    /**
48
     * Load basic data, throw exception if it's not provided
49
     *
50
     * @param string $addressByte
51
     * @param string $p2shByte
52
     * @param string $privByte
53
     * @param bool $testnet
54
     * @throws \Exception
55
     */
56 134
    public function __construct($addressByte, $p2shByte, $privByte, $testnet = false)
57
    {
58 134
        if (!(ctype_xdigit($addressByte) && strlen($addressByte) === 2)) {
59 2
            throw new \InvalidArgumentException('address byte must be 1 hexadecimal byte');
60
        }
61
62 132
        if (!(ctype_xdigit($p2shByte) && strlen($p2shByte) === 2)) {
63 2
            throw new \InvalidArgumentException('p2sh byte must be 1 hexadecimal byte');
64
        }
65
66 130
        if (!(ctype_xdigit($privByte) && strlen($privByte) === 2)) {
67 2
            throw new \InvalidArgumentException('priv byte must be 1 hexadecimal byte');
68
        }
69
70 128
        if (!is_bool($testnet)) {
71 2
            throw new \InvalidArgumentException('Testnet parameter must be a boolean');
72
        }
73
74 126
        $this->addressByte = $addressByte;
75 126
        $this->p2shByte = $p2shByte;
76 126
        $this->privByte = $privByte;
77 126
        $this->testnet = $testnet;
78 126
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 8
    public function isTestnet()
84
    {
85 8
        return $this->testnet;
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 46
    public function getAddressByte()
92
    {
93 46
        return $this->addressByte;
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 14
    public function getPrivByte()
100
    {
101 14
        return $this->privByte;
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 56
    public function getP2shByte()
108
    {
109 56
        return $this->p2shByte;
110
    }
111
112
    /**
113
     * Get version bytes for XPUB key
114
     *
115
     * @return string
116
     * @throws \Exception
117
     */
118 60
    public function getHDPubByte()
119
    {
120 60
        if ($this->xpubByte === null) {
121 2
            throw new \Exception('No HD xpub byte was set');
122
        }
123
124 58
        return $this->xpubByte;
125
    }
126
127
    /**
128
     * Set version bytes for XPUB key
129
     *
130
     * @param string $bytes
131
     * @return $this
132
     */
133 108
    public function setHDPubByte($bytes)
134
    {
135 108
        if (strlen($bytes) === 8 && ctype_xdigit($bytes) === true) {
136 108
            $this->xpubByte = $bytes;
137
        }
138
139 108
        return $this;
140
    }
141
142
    /**
143
     * Get version bytes for XPRIV key
144
     *
145
     * @return string
146
     * @throws \Exception
147
     */
148 60
    public function getHDPrivByte()
149
    {
150 60
        if ($this->xprivByte === null) {
151 2
            throw new \Exception('No HD xpriv byte was set');
152
        }
153
154 58
        return $this->xprivByte;
155
    }
156
157
    /**
158
     * Set version bytes for XPRIV key
159
     *
160
     * @param string $bytes
161
     * @return $this
162
     */
163 108
    public function setHDPrivByte($bytes)
164
    {
165 108
        if (strlen($bytes) === 8 && ctype_xdigit($bytes) === true) {
166 108
            $this->xprivByte = $bytes;
167
        }
168
169 108
        return $this;
170
    }
171
172
    /**
173
     * @param string $bytes
174
     * @return $this
175
     */
176 104
    public function setNetMagicBytes($bytes)
177
    {
178 104
        $this->netMagicBytes = $bytes;
179 104
        return $this;
180
    }
181
182
    /**
183
     * @return string
184
     * @throws \Exception
185
     */
186 10
    public function getNetMagicBytes()
187
    {
188 10
        if ($this->netMagicBytes === null) {
189 2
            throw new \Exception('No network magic bytes were set');
190
        }
191
192 8
        return $this->netMagicBytes;
193
    }
194
195
    /**
196
     * @param string $hrp
197
     * @return $this
198
     */
199 104
    public function setSegwitBech32Prefix($hrp)
200
    {
201 104
        if ($hrp !== strtoupper($hrp) && $hrp !== strtolower($hrp)) {
202
            throw new \RuntimeException("Bech32 prefix for segwit address contains mixed case characters");
203
        }
204
205 104
        $this->segwitAddrPrefix = $hrp;
206 104
        return $this;
207
    }
208
209
    /**
210
     * @return bool
211
     * @throws \Exception
212
     */
213 40
    public function getSegwitBech32Prefix()
214
    {
215 40
        if ($this->segwitAddrPrefix === null) {
216
            throw new \Exception("No bech32 prefix for segwit addresses set");
217
        }
218
219 40
        return $this->segwitAddrPrefix;
220
    }
221
}
222