SignatureSort   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 40
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A link() 0 18 5
A __construct() 0 3 2
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