1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Bitcoin\Transaction\Mutator; |
6
|
|
|
|
7
|
|
|
use BitWasp\Bitcoin\Script\ScriptWitnessInterface; |
8
|
|
|
use BitWasp\Bitcoin\Transaction\Transaction; |
9
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInputInterface; |
10
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionInterface; |
11
|
|
|
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface; |
12
|
|
|
|
13
|
|
|
class TxMutator |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var TransactionInterface |
17
|
|
|
*/ |
18
|
|
|
private $transaction; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var InputCollectionMutator |
22
|
|
|
*/ |
23
|
|
|
private $inputsMutator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var OutputCollectionMutator |
27
|
|
|
*/ |
28
|
|
|
private $outputsMutator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param TransactionInterface $transaction |
32
|
|
|
*/ |
33
|
97 |
|
public function __construct(TransactionInterface $transaction) |
34
|
|
|
{ |
35
|
97 |
|
$this->transaction = clone $transaction; |
36
|
97 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return InputCollectionMutator |
40
|
|
|
*/ |
41
|
94 |
|
public function inputsMutator(): InputCollectionMutator |
42
|
|
|
{ |
43
|
94 |
|
if (null === $this->inputsMutator) { |
44
|
94 |
|
$this->inputsMutator = new InputCollectionMutator($this->transaction->getInputs()); |
45
|
|
|
} |
46
|
|
|
|
47
|
94 |
|
return $this->inputsMutator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return OutputCollectionMutator |
52
|
|
|
*/ |
53
|
|
|
public function outputsMutator(): OutputCollectionMutator |
54
|
|
|
{ |
55
|
|
|
if (null === $this->outputsMutator) { |
56
|
|
|
$this->outputsMutator = new OutputCollectionMutator($this->transaction->getOutputs()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->outputsMutator; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return TransactionInterface |
64
|
|
|
*/ |
65
|
96 |
|
public function done(): TransactionInterface |
66
|
|
|
{ |
67
|
96 |
|
if (null !== $this->inputsMutator) { |
68
|
94 |
|
$this->inputs($this->inputsMutator->done()); |
69
|
|
|
} |
70
|
|
|
|
71
|
96 |
|
if (null !== $this->outputsMutator) { |
72
|
|
|
$this->outputs($this->outputsMutator->done()); |
73
|
|
|
} |
74
|
|
|
|
75
|
96 |
|
return $this->transaction; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $array |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
96 |
|
private function replace(array $array = []) |
83
|
|
|
{ |
84
|
96 |
|
$this->transaction = new Transaction( |
85
|
96 |
|
array_key_exists('version', $array) ? $array['version'] : $this->transaction->getVersion(), |
86
|
96 |
|
array_key_exists('inputs', $array) ? $array['inputs'] : $this->transaction->getInputs(), |
87
|
96 |
|
array_key_exists('outputs', $array) ? $array['outputs'] : $this->transaction->getOutputs(), |
88
|
96 |
|
array_key_exists('witness', $array) ? $array['witness'] : $this->transaction->getWitnesses(), |
89
|
96 |
|
array_key_exists('nLockTime', $array) ? $array['nLockTime'] : $this->transaction->getLockTime(), |
90
|
|
|
array_key_exists('type', $array) ? $array['type'] : $this->transaction->getType() |
91
|
|
|
); |
92
|
96 |
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $nVersion |
98
|
|
|
* @return $this |
99
|
1 |
|
*/ |
100
|
|
|
public function version(int $nVersion) |
101
|
1 |
|
{ |
102
|
|
|
return $this->replace(array('version' => $nVersion)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int $nType |
107
|
|
|
* @return $this |
108
|
96 |
|
*/ |
109
|
|
|
public function type(int $nType) |
110
|
96 |
|
{ |
111
|
|
|
return $this->replace(array('type' => $nType)); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param TransactionInputInterface[] $inputCollection |
116
|
|
|
* @return $this |
117
|
2 |
|
*/ |
118
|
|
|
public function inputs(array $inputCollection) |
119
|
2 |
|
{ |
120
|
|
|
return $this->replace(array('inputs' => $inputCollection)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param TransactionOutputInterface[] $outputCollection |
125
|
|
|
* @return $this |
126
|
92 |
|
*/ |
127
|
|
|
public function outputs(array $outputCollection) |
128
|
92 |
|
{ |
129
|
|
|
return $this->replace(array('outputs' => $outputCollection)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param ScriptWitnessInterface[] $witnessCollection |
134
|
|
|
* @return $this |
135
|
1 |
|
*/ |
136
|
|
|
public function witness(array $witnessCollection) |
137
|
1 |
|
{ |
138
|
|
|
return $this->replace(array('witness' => $witnessCollection)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int $locktime |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public function locktime(int $locktime) |
146
|
|
|
{ |
147
|
|
|
return $this->replace(array('nLockTime' => $locktime)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|