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

BitcoinCashChecker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B getSigHash() 0 26 5
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