Completed
Pull Request — master (#224)
by thomas
26:02 queued 07:51
created

Validator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 46
ccs 0
cts 19
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkSignature() 0 4 1
A checkSignatures() 0 13 4
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Script\Consensus\ConsensusInterface;
6
use BitWasp\Bitcoin\Script\ScriptInterface;
7
8
class Validator implements ValidatorInterface
9
{
10
    /**
11
     * @var TransactionInterface
12
     */
13
    private $transaction;
14
15
    /**
16
     * Validator constructor.
17
     * @param TransactionInterface $transaction
18
     */
19
    public function __construct(TransactionInterface $transaction)
20
    {
21
        $this->transaction = $transaction;
22
    }
23
24
    /**
25
     * @param ConsensusInterface $consensus
26
     * @param int $nInput
27
     * @param ScriptInterface $scriptPubKey
28
     * @return bool
29
     */
30
    public function checkSignature(ConsensusInterface $consensus, $nInput, ScriptInterface $scriptPubKey)
31
    {
32
        return $consensus->verify($this->transaction, $scriptPubKey, $nInput);
33
    }
34
35
    /**
36
     * @param ConsensusInterface $consensus
37
     * @param array $scriptPubKeys
38
     * @return bool
39
     */
40
    public function checkSignatures(ConsensusInterface $consensus, array $scriptPubKeys)
41
    {
42
        if (count($this->transaction->getInputs()) !== count($scriptPubKeys)) {
43
            throw new \InvalidArgumentException('Incorrect scriptPubKey count');
44
        }
45
46
        $result = true;
47
        foreach ($scriptPubKeys as $i => $scriptPubKey) {
48
            $result = $result && $this->checkSignature($consensus, $i, $scriptPubKey);
49
        }
50
51
        return $result;
52
    }
53
}
54