Completed
Pull Request — master (#593)
by thomas
15:26
created

Checker::getSigHash()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 3
dl 0
loc 18
ccs 10
cts 10
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\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 Checker extends CheckerBase
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $sigHashCache = [];
20
21
    /**
22
     * @param ScriptInterface $script
23
     * @param int $sigHashType
24
     * @param int $sigVersion
25
     * @return BufferInterface
26
     */
27 330
    public function getSigHash(ScriptInterface $script, int $sigHashType, int $sigVersion): BufferInterface
28
    {
29 330
        $cacheCheck = $sigVersion . $sigHashType . $script->getBuffer()->getBinary();
30 330
        if (!isset($this->sigHashCache[$cacheCheck])) {
31 330
            if (SigHash::V1 === $sigVersion) {
32 82
                $hasher = new V1Hasher($this->transaction, $this->amount);
33
            } else {
34 250
                $hasher = new Hasher($this->transaction);
35
            }
36
37 330
            $hash = $hasher->calculate($script, $this->nInput, $sigHashType);
38 330
            $this->sigHashCache[$cacheCheck] = $hash->getBinary();
39
        } else {
40 46
            $hash = new Buffer($this->sigHashCache[$cacheCheck], 32);
41
        }
42
43 330
        return $hash;
44
    }
45
}
46