Completed
Pull Request — master (#224)
by thomas
26:02 queued 07:51
created

Validator::checkSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
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
    public function __construct(TransactionInterface $transaction)
20
    {
21
        $this->transaction = $transaction;
22
    }
23
24
    /**
25
     * @param ConsensusInterface $consensus
26
     * @param int $nInput
27
     * @param ScriptInterface $scriptPubKey
28
     * @return bool
29
     */
30
    public function checkSignature(ConsensusInterface $consensus, $nInput, ScriptInterface $scriptPubKey)
31
    {
32
        return $consensus->verify($this->transaction, $scriptPubKey, $nInput);
33
    }
34
35
    /**
36
     * @param ConsensusInterface $consensus
37
     * @param array $scriptPubKeys
38
     * @return bool
39
     */
40
    public function checkSignatures(ConsensusInterface $consensus, array $scriptPubKeys)
41
    {
42
        if (count($this->transaction->getInputs()) !== count($scriptPubKeys)) {
43
            throw new \InvalidArgumentException('Incorrect scriptPubKey count');
44
        }
45
46
        $result = true;
47
        foreach ($scriptPubKeys as $i => $scriptPubKey) {
48
            $result = $result && $this->checkSignature($consensus, $i, $scriptPubKey);
49
        }
50
51
        return $result;
52
    }
53
}
54