|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Block; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
|
8
|
|
|
use BitWasp\Bitcoin\Math\Math; |
|
9
|
|
|
use BitWasp\Bitcoin\Script\Opcodes; |
|
10
|
|
|
use BitWasp\Bitcoin\Serializer\Block\BlockHeaderSerializer; |
|
11
|
|
|
use BitWasp\Bitcoin\Serializer\Block\BlockSerializer; |
|
12
|
|
|
use BitWasp\Bitcoin\Serializer\Script\ScriptWitnessSerializer; |
|
13
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\OutPointSerializer; |
|
14
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\TransactionInputSerializer; |
|
15
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\TransactionOutputSerializer; |
|
16
|
|
|
use BitWasp\Bitcoin\Serializer\Transaction\TransactionSerializer; |
|
17
|
|
|
use BitWasp\Buffertools\Buffer; |
|
18
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
19
|
|
|
|
|
20
|
|
|
class BlockFactory |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $string |
|
24
|
|
|
* @param Math|null $math |
|
25
|
|
|
* @return BlockInterface |
|
26
|
|
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
|
27
|
|
|
* @throws \Exception |
|
28
|
|
|
*/ |
|
29
|
10 |
|
public static function fromHex(string $string, Math $math = null): BlockInterface |
|
30
|
|
|
{ |
|
31
|
10 |
|
return self::fromBuffer(Buffer::hex($string), $math); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param BufferInterface $buffer |
|
36
|
|
|
* @param Math|null $math |
|
37
|
|
|
* @return BlockInterface |
|
38
|
|
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
|
39
|
|
|
*/ |
|
40
|
10 |
|
public static function fromBuffer(BufferInterface $buffer, Math $math = null): BlockInterface |
|
41
|
|
|
{ |
|
42
|
10 |
|
$opcodes = new Opcodes(); |
|
43
|
10 |
|
$serializer = new BlockSerializer( |
|
44
|
10 |
|
$math ?: Bitcoin::getMath(), |
|
45
|
10 |
|
new BlockHeaderSerializer(), |
|
46
|
10 |
|
new TransactionSerializer( |
|
47
|
10 |
|
new TransactionInputSerializer(new OutPointSerializer(), $opcodes), |
|
48
|
10 |
|
new TransactionOutputSerializer($opcodes), |
|
49
|
10 |
|
new ScriptWitnessSerializer() |
|
50
|
|
|
) |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
10 |
|
return $serializer->parse($buffer); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|