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) |
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.