Completed
Pull Request — master (#591)
by thomas
24:57
created

BitcoinCashChecker::getSigHash()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 3
dl 0
loc 26
ccs 0
cts 14
cp 0
crap 30
rs 8.439
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 Checker
15
{
16
    /**
17
     * @var int
18
     */
19
    protected $sigHashOptionalBits = SigHash::ANYONECANPAY | SigHash::BITCOINCASH;
20
21
    /**
22
     * @param ScriptInterface $script
23
     * @param int $sigHashType
24
     * @param int $sigVersion
25
     * @return BufferInterface
26
     */
27
    public function getSigHash(ScriptInterface $script, $sigHashType, $sigVersion)
28
    {
29
        if ($sigVersion !== 0) {
30
            throw new \RuntimeException("SigVersion must be 0");
31
        }
32
33
        $cacheCheck = $sigHashType . $script->getBuffer()->getBinary();
34
        if (!isset($this->sigHashCache[$cacheCheck])) {
35
            if ($sigHashType & SigHash::BITCOINCASH) {
36
                $hasher = new V1Hasher($this->transaction, $this->amount);
37
            } else {
38
                if ($this->hasherV0) {
39
                    $hasher = $this->hasherV0;
40
                } else {
41
                    $hasher = $this->hasherV0 = new Hasher($this->transaction);
42
                }
43
            }
44
45
            $hash = $hasher->calculate($script, $this->nInput, $sigHashType);
46
            $this->sigHashCache[$cacheCheck] = $hash->getBinary();
47
        } else {
48
            $hash = new Buffer($this->sigHashCache[$cacheCheck], 32, $this->adapter->getMath());
49
        }
50
51
        return $hash;
52
    }
53
}
54