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