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