Completed
Pull Request — master (#248)
by thomas
58:32 queued 33:01
created

Validator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 46.67%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 49
ccs 7
cts 15
cp 0.4667
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 int $amount
28
     * @param ScriptInterface $scriptPubKey
29
     * @return bool
30
     */
31 54
    public function checkSignature(ConsensusInterface $consensus, $nInput, $amount, ScriptInterface $scriptPubKey)
32
    {
33 54
        $witnesses = $this->transaction->getWitnesses();
34 54
        $witness = isset($witnesses[$nInput]) ? $witnesses[$nInput] : null;
35 54
        return $consensus->verify($this->transaction, $scriptPubKey, $nInput, $amount, $witness);
36
    }
37
38
    /**
39
     * @param ConsensusInterface $consensus
40
     * @param TransactionOutputInterface[] $outputs
41
     * @return bool
42
     */
43
    public function checkSignatures(ConsensusInterface $consensus, array $outputs)
44
    {
45
        if (count($this->transaction->getInputs()) !== count($outputs)) {
46
            throw new \InvalidArgumentException('Incorrect scriptPubKey count');
47
        }
48
49
        $result = true;
50
        foreach ($outputs as $i => $txOut) {
51
            $result = $result && $this->checkSignature($consensus, $i, $txOut->getValue(), $txOut->getScript());
52
        }
53
54
        return $result;
55
    }
56
}
57