1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Serializer\Transaction; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Script\Opcodes; |
8
|
|
|
use BitWasp\Bitcoin\Script\Script; |
9
|
|
|
use BitWasp\Bitcoin\Serializer\Types; |
10
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionOutput; |
11
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface; |
12
|
|
|
use BitWasp\Buffertools\Buffer; |
13
|
|
|
use BitWasp\Buffertools\BufferInterface; |
14
|
|
|
use BitWasp\Buffertools\Parser; |
15
|
|
|
|
16
|
|
|
class TransactionOutputSerializer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \BitWasp\Buffertools\Types\Uint64 |
20
|
|
|
*/ |
21
|
|
|
private $uint64le; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \BitWasp\Buffertools\Types\VarString |
25
|
|
|
*/ |
26
|
|
|
private $varstring; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Opcodes |
30
|
|
|
*/ |
31
|
|
|
private $opcodes; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* TransactionOutputSerializer constructor. |
35
|
|
|
* @param Opcodes|null $opcodes |
36
|
|
|
*/ |
37
|
5243 |
|
public function __construct(Opcodes $opcodes = null) |
38
|
|
|
{ |
39
|
5243 |
|
$this->uint64le = Types::uint64le(); |
40
|
5243 |
|
$this->varstring = Types::varstring(); |
41
|
5243 |
|
$this->opcodes = $opcodes ?: new Opcodes(); |
42
|
5243 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param TransactionOutputInterface $output |
46
|
|
|
* @return BufferInterface |
47
|
|
|
*/ |
48
|
5228 |
|
public function serialize(TransactionOutputInterface $output): BufferInterface |
49
|
|
|
{ |
50
|
5228 |
|
return new Buffer( |
51
|
5228 |
|
$this->uint64le->write($output->getValue()) . |
52
|
5228 |
|
$this->varstring->write($output->getScript()->getBuffer()) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Parser $parser |
58
|
|
|
* @return TransactionOutputInterface |
59
|
|
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
60
|
|
|
*/ |
61
|
68 |
|
public function fromParser(Parser $parser): TransactionOutputInterface |
62
|
|
|
{ |
63
|
68 |
|
return new TransactionOutput( |
64
|
68 |
|
(int) $this->uint64le->read($parser), |
65
|
68 |
|
new Script($this->varstring->read($parser), $this->opcodes) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param BufferInterface $string |
71
|
|
|
* @return TransactionOutputInterface |
72
|
|
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
73
|
|
|
*/ |
74
|
1 |
|
public function parse(BufferInterface $string): TransactionOutputInterface |
75
|
|
|
{ |
76
|
1 |
|
return $this->fromParser(new Parser($string)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|