Completed
Pull Request — master (#403)
by thomas
73:13 queued 70:38
created

SignatureSort   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

2 Methods

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