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
|
|
|
|