TransactionInputSerializer::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Serializer\Transaction;
6
7
use BitWasp\Bitcoin\Script\Opcodes;
8
use BitWasp\Bitcoin\Script\Script;
9
use BitWasp\Bitcoin\Serializer\Types;
10
use BitWasp\Bitcoin\Transaction\TransactionInput;
11
use BitWasp\Bitcoin\Transaction\TransactionInputInterface;
12
use BitWasp\Buffertools\Buffer;
13
use BitWasp\Buffertools\BufferInterface;
14
use BitWasp\Buffertools\Parser;
15
16
class TransactionInputSerializer
17
{
18
    /**
19
     * @var OutPointSerializerInterface
20
     */
21
    private $outpointSerializer;
22
23
    /**
24
     * @var \BitWasp\Buffertools\Types\VarString
25
     */
26
    private $varstring;
27
28
    /**
29
     * @var \BitWasp\Buffertools\Types\Uint32
30
     */
31
    private $uint32le;
32
33
    /**
34
     * @var Opcodes
35
     */
36
    private $opcodes;
37
38
    /**
39
     * TransactionInputSerializer constructor.
40
     * @param OutPointSerializerInterface $outPointSerializer
41
     * @param Opcodes|null $opcodes
42
     */
43 5239
    public function __construct(OutPointSerializerInterface $outPointSerializer, Opcodes $opcodes = null)
44
    {
45 5239
        $this->outpointSerializer = $outPointSerializer;
46 5239
        $this->varstring = Types::varstring();
47 5239
        $this->uint32le = Types::uint32le();
48 5239
        $this->opcodes = $opcodes ?: new Opcodes();
49 5239
    }
50
51
    /**
52
     * @param TransactionInputInterface $input
53
     * @return BufferInterface
54
     */
55 5228
    public function serialize(TransactionInputInterface $input): BufferInterface
56
    {
57 5228
        return new Buffer(
58 5228
            $this->outpointSerializer->serialize($input->getOutPoint())->getBinary() .
59 5228
            $this->varstring->write($input->getScript()->getBuffer()) .
60 5228
            $this->uint32le->write($input->getSequence())
61
        );
62
    }
63
64
    /**
65
     * @param Parser $parser
66
     * @return TransactionInputInterface
67
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
68
     * @throws \Exception
69
     */
70 62
    public function fromParser(Parser $parser): TransactionInputInterface
71
    {
72 62
        return new TransactionInput(
73 62
            $this->outpointSerializer->fromParser($parser),
74 62
            new Script($this->varstring->read($parser), $this->opcodes),
75 62
            (int) $this->uint32le->read($parser)
76
        );
77
    }
78
79
    /**
80
     * @param BufferInterface $string
81
     * @return TransactionInputInterface
82
     * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
83
     * @throws \Exception
84
     */
85 2
    public function parse(BufferInterface $string): TransactionInputInterface
86
    {
87 2
        return $this->fromParser(new Parser($string));
88
    }
89
}
90