Completed
Pull Request — master (#331)
by thomas
34:30
created

Validator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction;
4
5
use BitWasp\Bitcoin\Script\Consensus\ConsensusInterface;
6
7
class Validator implements ValidatorInterface
8
{
9
    /**
10
     * @var TransactionInterface
11
     */
12
    private $transaction;
13
14
    /**
15
     * Validator constructor.
16
     * @param TransactionInterface $transaction
17
     */
18 75
    public function __construct(TransactionInterface $transaction)
19
    {
20 75
        $this->transaction = $transaction;
21 75
    }
22
23
    /**
24
     * @param ConsensusInterface $consensus
25
     * @param int $nInput
26
     * @param TransactionOutputInterface $output
27
     * @return bool
28
     */
29 75
    public function checkSignature(ConsensusInterface $consensus, $nInput, TransactionOutputInterface $output)
30
    {
31 75
        $witnesses = $this->transaction->getWitnesses();
32 75
        $witness = isset($witnesses[$nInput]) ? $witnesses[$nInput] : null;
33 75
        return $consensus->verify($this->transaction, $output->getScript(), $nInput, $output->getValue(), $witness);
34
    }
35
36
    /**
37
     * @param ConsensusInterface $consensus
38
     * @param TransactionOutputInterface[] $outputs
39
     * @return bool
40
     */
41 66
    public function checkSignatures(ConsensusInterface $consensus, array $outputs)
42
    {
43 48
        if (count($this->transaction->getInputs()) !== count($outputs)) {
44
            throw new \InvalidArgumentException('Incorrect scriptPubKey count');
45
        }
46
47 48
        $result = true;
48 66
        foreach ($outputs as $i => $txOut) {
49 48
            $result = $result && $this->checkSignature($consensus, $i, $txOut);
50 32
        }
51
52 48
        return $result;
53
    }
54
}
55