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

Checker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 32
ccs 10
cts 10
cp 1
rs 10
c 1
b 0
f 0
wmc 3
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A getSigHash() 0 18 3
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