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