|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Serializer\Transaction; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Buffertools\Buffertools; |
|
6
|
|
|
use BitWasp\Buffertools\Parser; |
|
7
|
|
|
use BitWasp\Buffertools\Buffer; |
|
8
|
|
|
use BitWasp\Bitcoin\Script\Script; |
|
9
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInput; |
|
10
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInputInterface; |
|
11
|
|
|
use BitWasp\Buffertools\TemplateFactory; |
|
12
|
|
|
|
|
13
|
|
|
class TransactionInputSerializer |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var OutPointSerializer |
|
17
|
322 |
|
*/ |
|
18
|
|
|
private $outpointSerializer; |
|
19
|
322 |
|
|
|
20
|
322 |
|
/** |
|
21
|
322 |
|
* TransactionInputSerializer constructor. |
|
22
|
322 |
|
* @param OutPointSerializer $outPointSerializer |
|
23
|
322 |
|
*/ |
|
24
|
322 |
|
public function __construct(OutPointSerializer $outPointSerializer) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->outpointSerializer = $outPointSerializer; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return \BitWasp\Buffertools\Template |
|
31
|
292 |
|
*/ |
|
32
|
|
|
private function getInputTemplate() |
|
33
|
292 |
|
{ |
|
34
|
292 |
|
return (new TemplateFactory()) |
|
35
|
292 |
|
->varstring() |
|
36
|
292 |
|
->uint32le() |
|
37
|
292 |
|
->getTemplate(); |
|
38
|
292 |
|
} |
|
39
|
292 |
|
|
|
40
|
292 |
|
/** |
|
41
|
|
|
* @param TransactionInputInterface $input |
|
42
|
|
|
* @return Buffer |
|
43
|
|
|
*/ |
|
44
|
|
|
public function serialize(TransactionInputInterface $input) |
|
45
|
|
|
{ |
|
46
|
|
|
return Buffertools::concat( |
|
47
|
|
|
$this->outpointSerializer->serialize($input->getOutPoint()), |
|
48
|
240 |
|
$this->getInputTemplate()->write([ |
|
49
|
|
|
$input->getScript()->getBuffer(), |
|
50
|
240 |
|
$input->getSequence() |
|
51
|
|
|
]) |
|
52
|
240 |
|
); |
|
53
|
|
|
} |
|
54
|
240 |
|
|
|
55
|
|
|
/** |
|
56
|
240 |
|
* @param Parser $parser |
|
57
|
|
|
* @return TransactionInput |
|
58
|
240 |
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
|
59
|
240 |
|
*/ |
|
60
|
240 |
|
public function fromParser(Parser $parser) |
|
61
|
240 |
|
{ |
|
62
|
240 |
|
$outpoint = $this->outpointSerializer->fromParser($parser); |
|
63
|
|
|
|
|
64
|
240 |
|
/** |
|
65
|
|
|
* @var Buffer $scriptBuf |
|
66
|
|
|
* @var int|string $sequence |
|
67
|
|
|
*/ |
|
68
|
|
|
list ($scriptBuf, $sequence) = $this->getInputTemplate()->parse($parser); |
|
69
|
|
|
|
|
70
|
|
|
return new TransactionInput( |
|
71
|
|
|
$outpoint, |
|
72
|
12 |
|
new Script($scriptBuf), |
|
73
|
|
|
$sequence |
|
74
|
12 |
|
); |
|
75
|
12 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param $string |
|
79
|
|
|
* @return TransactionInput |
|
80
|
|
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
|
81
|
|
|
*/ |
|
82
|
|
|
public function parse($string) |
|
83
|
|
|
{ |
|
84
|
|
|
$parser = new Parser($string); |
|
85
|
|
|
return $this->fromParser($parser); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|