Completed
Pull Request — master (#503)
by thomas
70:35
created

Bech32::decodeSegwit()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 7
nop 2
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin;
4
5
use BitWasp\Bitcoin\Script\WitnessProgram;
6
use BitWasp\Buffertools\Buffer;
7
8
class Bech32
9
{
10
    protected static $CHARSET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
11
12
    protected static $CHARSET_REV = [
13
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
14
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
15
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
16
        15, -1, 10, 17, 21, 20, 26, 30,  7,  5, -1, -1, -1, -1, -1, -1,
17
        -1, 29, -1, 24, 13, 25,  9,  8, 23, -1, 18, 22, 31, 27, 19, -1,
18
        1,  0,  3, 16, 11, 28, 12, 14,  6,  4,  2, -1, -1, -1, -1, -1,
19
        -1, 29, -1, 24, 13, 25,  9,  8, 23, -1, 18, 22, 31, 27, 19, -1,
20
        1,  0,  3, 16, 11, 28, 12, 14,  6,  4,  2, -1, -1, -1, -1, -1
21
    ];
22
23
    /**
24
     * @param int[] $values
25
     * @param int $numValues
26
     * @return int
27
     */
28
    public static function polyMod(array $values, $numValues)
29
    {
30
        $generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
31
        $chk = 1;
32
        for ($i = 0; $i < $numValues; $i++) {
33
            $top = $chk >> 25;
34
            $chk = ($chk & 0x1ffffff) << 5 ^ $values[$i];
35
36
            for ($j = 0; $j < 5; $j++) {
37
                $value = (($top >> $j) & 1) ? $generator[$j] : 0;
38
                $chk ^= $value;
39
            }
40
        }
41
42
        return $chk;
43
    }
44
45
    /**
46
     * @param string $hrp
47
     * @param int $hrpLen
48
     * @return array
49
     */
50
    public static function hrpExpand($hrp, $hrpLen)
51
    {
52
        $expand1 = [];
53
        $expand2 = [];
54
        for ($i = 0; $i < $hrpLen; $i++) {
55
            $o = ord($hrp[$i]);
56
            $expand1[] = $o >> 5;
57
            $expand2[] = $o & 31;
58
        }
59
60
        return array_merge($expand1, [0], $expand2);
61
    }
62
63
    /**
64
     * @param string $hrp
65
     * @param int[] $convertedDataChars
66
     * @return bool
67
     */
68
    public static function verifyChecksum($hrp, array $convertedDataChars)
69
    {
70
        $expandHrp = self::hrpExpand($hrp, strlen($hrp));
71
        $r = array_merge($expandHrp, $convertedDataChars);
72
        $poly = self::polyMod($r, count($r));
73
        return $poly === 1;
74
    }
75
76
    /**
77
     * @param string $sBech
78
     * @return array
79
     */
80
    public static function decodeCheck($sBech)
81
    {
82
        $length = strlen($sBech);
83
        if ($length > 90) {
84
            throw new \RuntimeException("Bech32 string cannot exceed 90 characters in length");
85
        }
86
87
        $chars = array_values(unpack("C*", $sBech));
88
89
        $haveUpper = false;
90
        $haveLower = false;
91
        $positionOne = -1;
92
93
        for ($i = 0; $i < $length; $i++) {
94
            $x = $chars[$i];
95
            if ($x < 33 || $x > 126) {
96
                throw new \RuntimeException("Out of range character in bech32 string");
97
            }
98
99
            if ($x >= 0x61 && $x <= 0x7a) {
100
                $haveLower = true;
101
            }
102
103
            if ($x >= 0x41 && $x <= 0x5a) {
104
                $haveUpper = true;
105
                $x = $chars[$i] = $x + 0x20;
106
            }
107
108
            // find location of last '1' character
109
            if ($x === 0x31) {
110
                $positionOne = $i;
111
            }
112
        }
113
114
        if ($haveUpper && $haveLower) {
115
            throw new \RuntimeException("Data contains mixture of higher/lower case characters");
116
        }
117
118
        if ($positionOne < 1 || ($positionOne + 7) > $length) {
119
            throw new \RuntimeException("Invalid location for '1' character");
120
        }
121
122
        $hrp = [];
123
        for ($i = 0; $i < $positionOne; $i++) {
124
            $hrp[$i] = chr($chars[$i]);
125
        }
126
127
        $hrp = implode("", $hrp);
128
        $data = [];
129
        for ($i = $positionOne + 1; $i < $length; $i++) {
130
            $data[] = ($chars[$i] & 0x80) ? -1 : self::$CHARSET_REV[$chars[$i]];
131
        }
132
133
        if (!self::verifyChecksum($hrp, $data)) {
134
            throw new \RuntimeException("Invalid bech32 checksum");
135
        }
136
137
        return [$hrp, array_slice($data, 0, -6)];
138
    }
139
140
    /**
141
     * @param array $data
142
     * @param int $inLen
143
     * @param int $fromBits
144
     * @param int $toBits
145
     * @param bool $pad
146
     * @return array
147
     */
148
    public static function convertBits(array $data, $inLen, $fromBits, $toBits, $pad = true)
149
    {
150
        $acc = 0;
151
        $bits = 0;
152
        $ret = [];
153
        $maxv = (1 << $toBits) - 1;
154
        $maxacc = (1 << ($fromBits + $toBits - 1)) - 1;
155
156
        for ($i = 0; $i < $inLen; $i++) {
157
            $value = $data[$i];
158
            if ($value < 0 || $value >> $fromBits) {
159
                throw new \InvalidArgumentException("Invalid value for convert bits");
160
            }
161
162
            $acc = (($acc << $fromBits) | $value) & $maxacc;
163
            $bits += $fromBits;
164
165
            while ($bits >= $toBits) {
166
                $bits -= $toBits;
167
                $ret[] = (($acc >> $bits) & $maxv);
168
            }
169
        }
170
171
        if ($pad) {
172
            if ($bits) {
173
                $ret[] = ($acc << $toBits - $bits) & $maxv;
174
            }
175
        } else if ($bits >= $fromBits || ((($acc << ($toBits - $bits))) & $maxv)) {
176
            throw new \RuntimeException("Invalid data");
177
        }
178
179
        return $ret;
180
    }
181
182
    /**
183
     * @param string $hrp
184
     * @param int[] $convertedDataChars
185
     * @return array
186
     */
187
    public static function createChecksum($hrp, array $convertedDataChars)
188
    {
189
        $values = array_merge(self::hrpExpand($hrp, strlen($hrp)), $convertedDataChars);
190
        $polyMod = self::polyMod(array_merge($values, [0, 0, 0, 0, 0, 0]), count($values) + 6) ^ 1;
191
        $results = [];
192
        for ($i = 0; $i < 6; $i++) {
193
            $results[$i] = ($polyMod >> 5 * (5 - $i)) & 31;
194
        }
195
196
        return $results;
197
    }
198
199
    /**
200
     * @param string $hrp
201
     * @param array $combinedDataChars
202
     * @return string
203
     */
204
    public static function encode($hrp, array $combinedDataChars)
205
    {
206
        $cs = self::createChecksum($hrp, $combinedDataChars);
207
        $combined = array_merge($combinedDataChars, $cs);
208
        $return = "{$hrp}1";
209
        for ($i = 0, $n = count($combined); $i < $n; $i++) {
210
            $return .= self::$CHARSET[$combined[$i]];
211
        }
212
213
        return $return;
214
    }
215
216
    /**
217
     * @param string $hrp
218
     * @param WitnessProgram $witnessProgram
219
     * @return string
220
     */
221
    public static function encodeSegwit($hrp, WitnessProgram $witnessProgram)
222
    {
223
        $programChars = array_values(unpack("C*", $witnessProgram->getProgram()->getBinary()));
224
        $programBits = self::convertBits($programChars, count($programChars), 8, 5);
225
        $encodeData = array_merge([$witnessProgram->getVersion()], $programBits);
226
227
        return self::encode($hrp, $encodeData);
228
    }
229
230
    /**
231
     * @param string $hrp
232
     * @param string $bech32
233
     * @return WitnessProgram
234
     */
235
    public static function decodeSegwit($hrp, $bech32)
236
    {
237
        list ($hrpGot, $data) = self::decodeCheck($bech32);
238
        if ($hrpGot !== $hrp) {
239
            throw new \RuntimeException("Invalid prefix for address");
240
        }
241
242
        $decoded = self::convertBits(array_slice($data, 1), count($data) - 1, 5, 8, false);
243
        $decodeLen = count($decoded);
244
        if ($decodeLen < 2 || $decodeLen > 40) {
245
            throw new \RuntimeException("Invalid segwit address");
246
        }
247
248
        if ($data[0] > 16) {
249
            throw new \RuntimeException("Invalid witness program version");
250
        }
251
252
        $bytes = '';
253
        foreach ($decoded as $char) {
254
            $bytes .= chr($char);
255
        }
256
        $decoded = new Buffer($bytes);
257
258
        if (0 === $data[0]) {
259
            return WitnessProgram::v0($decoded);
260
        }
261
262
        return new WitnessProgram($data[0], $decoded);
263
    }
264
}
265