Completed
Pull Request — master (#248)
by thomas
58:32 queued 33:01
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 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 4
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 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