Completed
Pull Request — master (#518)
by thomas
58:22 queued 55:50
created

BitcoinCashChecker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 64.29%

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 9
cts 14
cp 0.6429
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
namespace BitWasp\Bitcoin\Script\Interpreter;
4
5
6
use BitWasp\Bitcoin\Script\ScriptInterface;
7
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer;
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
    protected $sigHashOptionalBits = SigHash::ANYONECANPAY | SigHash::BITCOINCASH;
17
18
    /**
19
     * @param ScriptInterface $script
20
     * @param int $sigHashType
21
     * @param int $sigVersion
22
     * @return BufferInterface
23
     */
24 2
    public function getSigHash(ScriptInterface $script, $sigHashType, $sigVersion)
25
    {
26 2
        if ($sigVersion !== 0) {
27
            throw new \RuntimeException("SigVersion must be 0");
28
        }
29
30 2
        $cacheCheck = $sigVersion . $sigHashType . $script->getBuffer()->getBinary();
31 2
        if (!isset($this->sigHashCache[$cacheCheck])) {
32 2
            if ($sigHashType & SigHash::BITCOINCASH) {
33 2
                $hasher = new V1Hasher($this->transaction, $this->amount);
34
            } else {
35
                if ($this->hasherV0) {
36
                    $hasher = $this->hasherV0;
37
                } else {
38
                    $hasher = $this->hasherV0 = new Hasher($this->transaction, new TransactionSerializer());
39
                }
40
            }
41
42 2
            $hash = $hasher->calculate($script, $this->nInput, $sigHashType);
43 2
            $this->sigHashCache[$cacheCheck] = $hash->getBinary();
44
        } else {
45
            $hash = new Buffer($this->sigHashCache[$cacheCheck], 32, $this->adapter->getMath());
46
        }
47
48 2
        return $hash;
49
    }
50
}
51