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

Validator::checkSignature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 3
crap 2
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