Completed
Pull Request — master (#519)
by thomas
80:31 queued 75:30
created

Hasher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 90%

Importance

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

1 Method

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