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