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

ParseUtil   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 33
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseBip32DerivationValue() 0 11 3
A parsePublicKeyKey() 0 11 4
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