Completed
Pull Request — master (#759)
by thomas
25:10
created

ParseUtil::parseBip32DerivationValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Transaction\PSBT;
6
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PublicKey;
8
use BitWasp\Bitcoin\Exceptions\InvalidPSBTException;
9
use BitWasp\Buffertools\BufferInterface;
10
11
trait ParseUtil
12
{
13 8
    private static function parsePublicKeyKey(BufferInterface $key): BufferInterface
14
    {
15 8
        $keySize = $key->getSize();
16 8
        if ($keySize !== PublicKey::LENGTH_COMPRESSED + 1 && $keySize !== PublicKey::LENGTH_UNCOMPRESSED + 1) {
17 3
            throw new InvalidPSBTException("Invalid key length");
18
        }
19 6
        $pubKey = $key->slice(1);
20 6
        if (!PublicKey::isCompressedOrUncompressed($pubKey)) {
21 1
            throw new InvalidPSBTException("Invalid public key encoding");
22
        }
23 5
        return $pubKey;
24
    }
25
26
    /**
27
     * Returns array[fpr(int), path(int[])]
28
     *
29
     * @param BufferInterface $value
30
     * @return array
31
     * @throws InvalidPSBTException
32
     */
33 2
    private static function parseBip32DerivationValue(BufferInterface $value): array
34
    {
35 2
        $len = $value->getSize();
36 2
        if ($len % 4 !== 0 || $len === 0) {
37
            throw new InvalidPSBTException("Invalid length for BIP32 derivation");
38
        }
39
40 2
        $pieces = $len / 4;
41 2
        $path = unpack("N{$pieces}", $value->getBinary());
42 2
        $fpr = array_shift($path);
43 2
        return [$fpr, $path];
44
    }
45
}
46