Completed
Push — master ( af1b10...bf42d9 )
by thomas
24:09
created

Validator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 60%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 7
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 48
ccs 9
cts 15
cp 0.6
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkSignature() 0 6 2
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 54
    public function __construct(TransactionInterface $transaction)
20
    {
21 54
        $this->transaction = $transaction;
22 54
    }
23
24
    /**
25
     * @param ConsensusInterface $consensus
26
     * @param int $nInput
27
     * @param TransactionOutputInterface $output
28
     * @return bool
29
     */
30 54
    public function checkSignature(ConsensusInterface $consensus, $nInput, TransactionOutputInterface $output)
31
    {
32 54
        $witnesses = $this->transaction->getWitnesses();
33 54
        $witness = isset($witnesses[$nInput]) ? $witnesses[$nInput] : null;
34 54
        return $consensus->verify($this->transaction, $output->getScript(), $nInput, $output->getValue(), $witness);
35
    }
36
37
    /**
38
     * @param ConsensusInterface $consensus
39
     * @param TransactionOutputInterface[] $outputs
40
     * @return bool
41
     */
42 54
    public function checkSignatures(ConsensusInterface $consensus, array $outputs)
43
    {
44
        if (count($this->transaction->getInputs()) !== count($outputs)) {
45
            throw new \InvalidArgumentException('Incorrect scriptPubKey count');
46
        }
47
48 54
        $result = true;
49
        foreach ($outputs as $i => $txOut) {
50
            $result = $result && $this->checkSignature($consensus, $i, $txOut);
51
        }
52
53
        return $result;
54
    }
55
}
56