Completed
Push — master ( 410ea8...0c9ae2 )
by thomas
105:23 queued 33:17
created

TxBuilder::spendOutPoint()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 2
cts 2
cp 1
rs 9.4286
cc 2
eloc 6
nc 1
nop 3
crap 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction\Factory;
4
5
use BitWasp\Bitcoin\Address\AddressInterface;
6
use BitWasp\Bitcoin\Collection\Transaction\TransactionInputCollection;
7
use BitWasp\Bitcoin\Collection\Transaction\TransactionOutputCollection;
8
use BitWasp\Bitcoin\Locktime;
9
use BitWasp\Bitcoin\Script\Script;
10
use BitWasp\Bitcoin\Script\ScriptFactory;
11
use BitWasp\Bitcoin\Script\ScriptInterface;
12
use BitWasp\Bitcoin\Transaction\OutPoint;
13
use BitWasp\Bitcoin\Transaction\OutPointInterface;
14
use BitWasp\Bitcoin\Transaction\Transaction;
15
use BitWasp\Bitcoin\Transaction\TransactionInput;
16
use BitWasp\Bitcoin\Transaction\TransactionInputInterface;
17
use BitWasp\Bitcoin\Transaction\TransactionInterface;
18
use BitWasp\Bitcoin\Transaction\TransactionOutput;
19
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
20
use BitWasp\Buffertools\Buffer;
21
22
class TxBuilder
23
{
24
    /**
25
     * @var int
26
     */
27
    private $nVersion;
28
29
    /**
30
     * @var array
31
     */
32
    private $inputs;
33
34
    /**
35
     * @var array
36
     */
37
    private $outputs;
38
39
    /**
40
     * @var int
41 1042
     */
42
    private $nLockTime;
43 1042
44 1042
    public function __construct()
45
    {
46
        $this->reset();
47
    }
48
49 1042
    /**
50
     * @return $this
51 1042
     */
52 1042
    public function reset()
53 1042
    {
54 1042
        $this->nVersion = 1;
55 1042
        $this->inputs = [];
56
        $this->outputs = [];
57
        $this->nLockTime = 0;
58
        return $this;
59
    }
60
61 1036
    /**
62
     * @return TransactionInterface
63 1036
     */
64 1036
    private function makeTransaction()
65 1036
    {
66 1036
        return new Transaction(
67 1036
            $this->nVersion,
68 1036
            new TransactionInputCollection($this->inputs),
69
            new TransactionOutputCollection($this->outputs),
70
            $this->nLockTime
71
        );
72
    }
73
74 1030
    /**
75
     * @return TransactionInterface
76 1030
     */
77
    public function get()
78
    {
79
        return $this->makeTransaction();
80
    }
81
82 30
    /**
83
     * @return TransactionInterface
84 30
     */
85 30
    public function getAndReset()
86 30
    {
87
        $transaction = $this->makeTransaction();
88
        $this->reset();
89
        return $transaction;
90
    }
91
92
    /**
93 246
     * @param int $nVersion
94
     * @return $this
95 246
     */
96 246
    public function version($nVersion)
97
    {
98
        $this->nVersion = $nVersion;
99
        return $this;
100
    }
101
102
    /**
103
     * @param Buffer|string $hashPrevOut
104
     * @param int $nPrevOut
105
     * @param Script|null $script
106 802
     * @param int $nSequence
107
     * @return $this
108 802
     */
109 802
    public function input($hashPrevOut, $nPrevOut, Script $script = null, $nSequence = TransactionInputInterface::SEQUENCE_FINAL)
110 802
    {
111 802
        $this->inputs[] = new TransactionInput(
112
            new OutPoint(
113 802
                $hashPrevOut instanceof Buffer ? $hashPrevOut : Buffer::hex($hashPrevOut),
114
                $nPrevOut
115 802
            ),
116
            $script ?: new Script(),
117
            $nSequence
118
        );
119
120
        return $this;
121
    }
122 234
123
    /**
124
     * @param TransactionInputInterface[] $inputs
125 234
     * @return $this
126 234
     */
127
    public function inputs(array $inputs)
128 234
    {
129
        array_walk($inputs, function (TransactionInputInterface $input) {
130
            $this->inputs[] = $input;
131
        });
132
133
        return $this;
134
    }
135
136 148
    /**
137
     * @param int|string $value
138 148
     * @param ScriptInterface $script
139 148
     * @return $this
140
     */
141
    public function output($value, ScriptInterface $script)
142
    {
143
        $this->outputs[] = new TransactionOutput($value, $script);
144
        return $this;
145
    }
146
147
    /**
148 298
     * @param TransactionOutputInterface[] $outputs
149 234
     * @return $this
150 298
     */
151
    public function outputs(array $outputs)
152 234
    {
153
        array_walk($outputs, function (TransactionOutputInterface $output) {
154
            $this->outputs[] = $output;
155
        });
156
157
        return $this;
158
    }
159 246
160
    /**
161 246
     * @param int $locktime
162 246
     * @return $this
163
     */
164
    public function locktime($locktime)
165
    {
166
        $this->nLockTime = $locktime;
167
        return $this;
168
    }
169
170
    /**
171 6
     * @param Locktime $lockTime
172
     * @param int $nTimestamp
173 6
     * @return $this
174 6
     * @throws \Exception
175
     */
176
    public function lockToTimestamp(Locktime $lockTime, $nTimestamp)
177
    {
178
        $this->locktime($lockTime->fromTimestamp($nTimestamp));
179
        return $this;
180
    }
181
182
    /**
183 6
     * @param Locktime $lockTime
184
     * @param int $blockHeight
185 6
     * @return $this
186 6
     * @throws \Exception
187
     */
188
    public function lockToBlockHeight(Locktime $lockTime, $blockHeight)
189
    {
190
        $this->locktime($lockTime->fromBlockHeight($blockHeight));
191
        return $this;
192
    }
193
194 88
    /**
195
     * @param OutPointInterface $outpoint
196
     * @param ScriptInterface|null $script
197 88
     * @param int $nSequence
198 88
     * @return $this
199 88
     */
200
    public function spendOutPoint(OutPointInterface $outpoint, ScriptInterface $script = null, $nSequence = TransactionInputInterface::SEQUENCE_FINAL)
201 88
    {
202
        $this->inputs[] = new TransactionInput(
203 88
            $outpoint,
204
            $script ?: new Script(),
205
            $nSequence
206
        );
207
208
        return $this;
209
    }
210
211
    /**
212
     * @param TransactionInterface $transaction
213 54
     * @param int $outputToSpend
214
     * @param ScriptInterface|null $script
215
     * @param int $nSequence
216 54
     * @return $this
217 54
     */
218 54
    public function spendOutputFrom(TransactionInterface $transaction, $outputToSpend, ScriptInterface $script = null, $nSequence = TransactionInputInterface::SEQUENCE_FINAL)
219 54
    {
220
        // Check TransactionOutput exists in $tx
221 54
        $transaction->getOutput($outputToSpend);
222
        $this->input(
223
            $transaction->getTxId(),
224
            $outputToSpend,
225
            $script,
0 ignored issues
show
Bug introduced by
It seems like $script defined by parameter $script on line 218 can also be of type object<BitWasp\Bitcoin\Script\ScriptInterface>; however, BitWasp\Bitcoin\Transact...tory\TxBuilder::input() does only seem to accept null|object<BitWasp\Bitcoin\Script\Script>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
226
            $nSequence
227
        );
228
229
        return $this;
230
    }
231
232
    /**
233
     * Create an output paying $value to an Address.
234
     *
235
     * @param int $value
236
     * @param AddressInterface $address
237
     * @return $this
238
     */
239
    public function payToAddress($value, AddressInterface $address)
240
    {
241
        // Create Script from address, then create an output.
242
        $this->output(
243
            $value,
244
            ScriptFactory::scriptPubKey()->payToAddress($address)
245
        );
246
247
        return $this;
248
    }
249
}
250