SignatureSort::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Signature;
6
7
use BitWasp\Bitcoin\Bitcoin;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
9
use BitWasp\Buffertools\BufferInterface;
10
11
class SignatureSort implements SignatureSortInterface
12
{
13
    /**
14
     * @var EcAdapterInterface
15
     */
16
    private $ecAdapter;
17
18
    /**
19
     * SignatureSort constructor.
20
     * @param EcAdapterInterface $ecAdapter
21
     */
22
    public function __construct(EcAdapterInterface $ecAdapter = null)
23
    {
24
        $this->ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
25
    }
26
27
    /**
28
     * @param \BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface[] $signatures
29
     * @param \BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface[] $publicKeys
30
     * @param BufferInterface $messageHash
31
     * @return \SplObjectStorage
32
     */
33
    public function link(array $signatures, array $publicKeys, BufferInterface $messageHash): \SplObjectStorage
34
    {
35
        $sigCount = count($signatures);
36
        $storage = new \SplObjectStorage();
37
        foreach ($signatures as $signature) {
38
            foreach ($publicKeys as $key) {
39
                if ($key->verify($messageHash, $signature)) {
40
                    $storage->attach($key, $signature);
41
                    if (count($storage) === $sigCount) {
42
                        break 2;
43
                    }
44
45
                    break;
46
                }
47
            }
48
        }
49
50
        return $storage;
51
    }
52
}
53