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

Hasher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 42
ccs 12
cts 13
cp 0.9231
rs 10
c 1
b 0
f 0
wmc 5
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
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\Bitcoin\Serializer\Transaction\TransactionSerializer;
9
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializerInterface;
10
use BitWasp\Bitcoin\Transaction\TransactionInterface;
11
use BitWasp\Buffertools\Buffer;
12
use BitWasp\Buffertools\BufferInterface;
13
14
class Hasher extends SigHash
15
{
16
    /**
17
     * Hasher constructor.
18
     * @param TransactionInterface $transaction
19
     * @param TransactionSerializerInterface|null $txSerializer
20
     */
21 130
    public function __construct(TransactionInterface $transaction, TransactionSerializerInterface $txSerializer = null)
0 ignored issues
show
Unused Code introduced by
The parameter $txSerializer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23 130
        parent::__construct($transaction);
24 130
    }
25
26
    /**
27
     * Calculate the hash of the current transaction, when you are looking to
28
     * spend $txOut, and are signing $inputToSign. The SigHashType defaults to
29
     * SIGHASH_ALL, though SIGHASH_SINGLE, SIGHASH_NONE, SIGHASH_ANYONECANPAY
30
     * can be used.
31
     *
32
     * @param ScriptInterface $txOutScript
33
     * @param int $inputToSign
34
     * @param int $sighashType
35
     * @return BufferInterface
36
     * @throws \Exception
37
     */
38 130
    public function calculate(ScriptInterface $txOutScript, $inputToSign, $sighashType = SigHash::ALL)
39
    {
40 130
        $math = Bitcoin::getMath();
41 130
        if ($inputToSign >= count($this->tx->getInputs())) {
42
            return Buffer::hex('0100000000000000000000000000000000000000000000000000000000000000', 32, $math);
43
        }
44
45 130
        if (($sighashType & 0x1f) == SigHash::SINGLE) {
46 8
            if ($inputToSign >= count($this->tx->getOutputs())) {
47 2
                return Buffer::hex('0100000000000000000000000000000000000000000000000000000000000000', 32, $math);
48
            }
49
        }
50
51 130
        $serializer = new TxSigHashSerializer($this->tx, $txOutScript, $inputToSign, $sighashType);
52 130
        $sigHashData = new Buffer($serializer->serializeTransaction() . pack('V', $sighashType));
53 130
        return Hash::sha256d($sigHashData);
54
    }
55
}
56