Completed
Pull Request — master (#276)
by thomas
40:21
created

SignatureSort   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 18.75%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 42
ccs 3
cts 16
cp 0.1875
rs 10
c 2
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B link() 0 19 5
1
<?php
2
3
namespace BitWasp\Bitcoin\Signature;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
6
use BitWasp\Buffertools\BufferInterface;
7
8
class SignatureSort implements SignatureSortInterface
9
{
10
    /**
11
     * @var EcAdapterInterface
12
     */
13
    private $ecAdapter;
14
15
    /**
16
     * SignatureSort constructor.
17
     * @param EcAdapterInterface $ecAdapter
18
     */
19 24
    public function __construct(EcAdapterInterface $ecAdapter)
20
    {
21 24
        $this->ecAdapter = $ecAdapter;
22 24
    }
23
24
    /**
25
     * @param \BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface[] $signatures
26
     * @param \BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface[] $publicKeys
27
     * @param BufferInterface $messageHash
28
     * @return \SplObjectStorage
29
     */
30
    public function link(array $signatures, array $publicKeys, BufferInterface $messageHash)
31
    {
32
        $sigCount = count($signatures);
33
        $storage = new \SplObjectStorage();
34
        foreach ($signatures as $signature) {
35
            foreach ($publicKeys as $key) {
36
                if ($this->ecAdapter->verify($messageHash, $key, $signature)) {
37
                    $storage->attach($key, $signature);
38
                    if (count($storage) === $sigCount) {
39
                        break 2;
40
                    }
41
42
                    break;
43
                }
44
            }
45
        }
46
47
        return $storage;
48
    }
49
}
50