Completed
Push — master ( cc9f4e...3a552b )
by thomas
38:55 queued 36:36
created

BitcoinCashChecker::getSigHash()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.25

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 4
nop 3
dl 0
loc 22
ccs 9
cts 12
cp 0.75
crap 4.25
rs 8.9197
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Script\Interpreter;
6
7
use BitWasp\Bitcoin\Script\ScriptInterface;
8
use BitWasp\Bitcoin\Transaction\SignatureHash\Hasher;
9
use BitWasp\Bitcoin\Transaction\SignatureHash\SigHash;
10
use BitWasp\Bitcoin\Transaction\SignatureHash\V1Hasher;
11
use BitWasp\Buffertools\Buffer;
12
use BitWasp\Buffertools\BufferInterface;
13
14
class BitcoinCashChecker extends CheckerBase
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $sigHashCache = [];
20
21
    /**
22
     * @var int
23
     */
24
    protected $sigHashOptionalBits = SigHash::ANYONECANPAY | SigHash::BITCOINCASH;
25
26
    /**
27
     * @param ScriptInterface $script
28
     * @param int $sigHashType
29
     * @param int $sigVersion
30
     * @return BufferInterface
31
     */
32 1
    public function getSigHash(ScriptInterface $script, int $sigHashType, int $sigVersion): BufferInterface
33
    {
34 1
        if ($sigVersion !== 0) {
35
            throw new \RuntimeException("SigVersion must be 0");
36
        }
37
38 1
        $cacheCheck = $sigHashType . $script->getBuffer()->getBinary();
39 1
        if (!isset($this->sigHashCache[$cacheCheck])) {
40 1
            if ($sigHashType & SigHash::BITCOINCASH) {
41 1
                $hasher = new V1Hasher($this->transaction, $this->amount);
42
            } else {
43
                $hasher = new Hasher($this->transaction);
44
            }
45
46 1
            $hash = $hasher->calculate($script, $this->nInput, $sigHashType);
47 1
            $this->sigHashCache[$cacheCheck] = $hash->getBinary();
48
        } else {
49
            $hash = new Buffer($this->sigHashCache[$cacheCheck], 32);
50
        }
51
52 1
        return $hash;
53
    }
54
}
55