Completed
Pull Request — master (#403)
by thomas
31:50
created

SignatureSort::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 60
    public function __construct(EcAdapterInterface $ecAdapter = null)
21
    {
22 60
        $this->ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
23 60
    }
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 60
    public function link(array $signatures, array $publicKeys, BufferInterface $messageHash)
32
    {
33 60
        $sigCount = count($signatures);
34 60
        $storage = new \SplObjectStorage();
35 60
        foreach ($signatures as $signature) {
36 60
            foreach ($publicKeys as $key) {
37 60
                if ($this->ecAdapter->verify($messageHash, $key, $signature)) {
38 54
                    $storage->attach($key, $signature);
39 54
                    if (count($storage) === $sigCount) {
40 54
                        break 2;
41
                    }
42
43 8
                    break;
44
                }
45 4
            }
46 20
        }
47
48 60
        return $storage;
49
    }
50
}
51