Completed
Pull Request — master (#591)
by thomas
22:54 queued 09:12
created

Hasher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 34
ccs 8
cts 9
cp 0.8889
rs 10
c 1
b 0
f 0
wmc 4
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A calculate() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Transaction\SignatureHash;
6
7
use BitWasp\Bitcoin\Bitcoin;
8
use BitWasp\Bitcoin\Crypto\Hash;
9
use BitWasp\Bitcoin\Script\ScriptInterface;
10
use BitWasp\Buffertools\Buffer;
11
use BitWasp\Buffertools\BufferInterface;
12
13
class Hasher extends SigHash
14
{
15
    /**
16
     * Calculate the hash of the current transaction, when you are looking to
17
     * spend $txOut, and are signing $inputToSign. The SigHashType defaults to
18
     * SIGHASH_ALL, though SIGHASH_SINGLE, SIGHASH_NONE, SIGHASH_ANYONECANPAY
19
     * can be used.
20
     *
21
     * @param ScriptInterface $txOutScript
22
     * @param int $inputToSign
23
     * @param int $sighashType
24
     * @return BufferInterface
25
     * @throws \Exception
26
     */
27 250
    public function calculate(
28
        ScriptInterface $txOutScript,
29
        int $inputToSign,
30
        int $sighashType = SigHash::ALL
31
    ): BufferInterface {
32 250
        if ($inputToSign >= count($this->tx->getInputs())) {
33
            return Buffer::hex('0100000000000000000000000000000000000000000000000000000000000000', 32);
34
        }
35
36 250
        if (($sighashType & 0x1f) == SigHash::SINGLE) {
37 8
            if ($inputToSign >= count($this->tx->getOutputs())) {
38 2
                return Buffer::hex('0100000000000000000000000000000000000000000000000000000000000000', 32);
39
            }
40
        }
41
42 250
        $serializer = new TxSigHashSerializer($this->tx, $txOutScript, $inputToSign, $sighashType);
43 250
        $sigHashData = new Buffer($serializer->serializeTransaction() . pack('V', $sighashType));
44 250
        return Hash::sha256d($sigHashData);
45
    }
46
}
47