Completed
Pull Request — master (#591)
by thomas
22:54 queued 09:12
created

BitcoinConsensus::verify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 5
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Script\Consensus;
6
7
use BitWasp\Bitcoin\Script\Consensus\Exception\BitcoinConsensusException;
8
use BitWasp\Bitcoin\Script\Interpreter\InterpreterInterface;
9
use BitWasp\Bitcoin\Script\ScriptInterface;
10
use BitWasp\Bitcoin\Transaction\TransactionInterface;
11
12
class BitcoinConsensus implements ConsensusInterface
13
{
14
    /**
15
     * @param TransactionInterface $tx
16
     * @param ScriptInterface $scriptPubKey
17
     * @param int $flags
18
     * @param int $nInputToSign
19
     * @param int $amount
20
     * @return bool
21
     * @throws BitcoinConsensusException
22
     */
23 500
    public function verify(TransactionInterface $tx, ScriptInterface $scriptPubKey, int $flags, int $nInputToSign, int $amount): bool
24
    {
25 500
        if ($flags !== ($flags & BITCOINCONSENSUS_VERIFY_ALL)) {
26 1
            throw new BitcoinConsensusException("Invalid flags for bitcoinconsensus");
27
        }
28
29 499
        $error = 0;
30 499
        if ($flags & InterpreterInterface::VERIFY_WITNESS) {
31 163
            $verify = (bool) bitcoinconsensus_verify_script_with_amount($scriptPubKey->getBinary(), $amount, $tx->getBinary(), $nInputToSign, $flags, $error);
32
        } else {
33 336
            $verify = (bool) bitcoinconsensus_verify_script($scriptPubKey->getBinary(), $tx->getBaseSerialization()->getBinary(), $nInputToSign, $flags, $error);
34
        }
35
36 499
        return $verify;
37
    }
38
}
39