1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Serializer\Transaction; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Transaction\Factory\TxBuilder; |
6
|
|
|
use BitWasp\Buffertools\Parser; |
7
|
|
|
use BitWasp\Bitcoin\Transaction\Transaction; |
8
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInterface; |
9
|
|
|
use BitWasp\Buffertools\TemplateFactory; |
10
|
|
|
|
11
|
|
|
class OldTransactionSerializer |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var TransactionInputSerializer |
15
|
|
|
*/ |
16
|
|
|
public $inputSerializer; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var TransactionOutputSerializer |
20
|
|
|
*/ |
21
|
|
|
public $outputSerializer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->inputSerializer = new TransactionInputSerializer(new OutPointSerializer()); |
29
|
|
|
$this->outputSerializer = new TransactionOutputSerializer; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return \BitWasp\Buffertools\Template |
34
|
|
|
*/ |
35
|
|
|
private function getTemplate() |
36
|
|
|
{ |
37
|
|
|
return (new TemplateFactory()) |
38
|
|
|
->uint32le() |
39
|
|
|
->vector(function (Parser & $parser) { |
40
|
|
|
return $this->inputSerializer->fromParser($parser); |
41
|
|
|
}) |
42
|
|
|
->vector(function (Parser &$parser) { |
43
|
|
|
return $this->outputSerializer->fromParser($parser); |
44
|
|
|
}) |
45
|
|
|
->uint32le() |
46
|
|
|
->getTemplate(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param TransactionInterface $transaction |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function serialize(TransactionInterface $transaction) |
54
|
|
|
{ |
55
|
|
|
return $this->getTemplate()->write([ |
56
|
|
|
$transaction->getVersion(), |
57
|
|
|
$transaction->getInputs()->all(), |
58
|
|
|
$transaction->getOutputs()->all(), |
59
|
|
|
$transaction->getLockTime() |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Parser $parser |
65
|
|
|
* @return Transaction |
66
|
|
|
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
public function fromParser(Parser $parser) |
70
|
|
|
{ |
71
|
|
|
$p = $this->getTemplate()->parse($parser); |
72
|
|
|
|
73
|
|
|
list ($nVersion, $inputArray, $outputArray, $nLockTime) = $p; |
74
|
|
|
|
75
|
|
|
return (new TxBuilder()) |
76
|
|
|
->version($nVersion) |
77
|
|
|
->inputs($inputArray) |
78
|
|
|
->outputs($outputArray) |
79
|
|
|
->locktime($nLockTime) |
80
|
|
|
->get(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $hex |
85
|
|
|
* @return Transaction |
86
|
|
|
*/ |
87
|
|
|
public function parse($hex) |
88
|
|
|
{ |
89
|
|
|
$parser = new Parser($hex); |
90
|
|
|
return $this->fromParser($parser); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|