TransactionInputSerializer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 18
c 1
b 1
f 0
dl 0
loc 72
ccs 18
cts 18
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 3 1
A serialize() 0 6 1
A __construct() 0 6 2
A fromParser() 0 6 1
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