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