forked from Bit-Wasp/bitcoin-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutPointSerializer.php
67 lines (58 loc) · 1.68 KB
/
OutPointSerializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace BitWasp\Bitcoin\Serializer\Transaction;
use BitWasp\Bitcoin\Serializer\Types;
use BitWasp\Bitcoin\Transaction\OutPoint;
use BitWasp\Bitcoin\Transaction\OutPointInterface;
use BitWasp\Buffertools\Buffer;
use BitWasp\Buffertools\BufferInterface;
use BitWasp\Buffertools\Parser;
class OutPointSerializer implements OutPointSerializerInterface
{
/**
* @var \BitWasp\Buffertools\Types\ByteString
*/
private $txid;
/**
* @var \BitWasp\Buffertools\Types\Uint32
*/
private $vout;
public function __construct()
{
$this->txid = Types::bytestringle(32);
$this->vout = Types::uint32le();
}
/**
* @param OutPointInterface $outpoint
* @return BufferInterface
* @throws \Exception
*/
public function serialize(OutPointInterface $outpoint): BufferInterface
{
return new Buffer(
$this->txid->write($outpoint->getTxId()) .
$this->vout->write($outpoint->getVout())
);
}
/**
* @param Parser $parser
* @return OutPointInterface
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
*/
public function fromParser(Parser $parser): OutPointInterface
{
return new OutPoint(
new Buffer(strrev($parser->readBytes(32)->getBinary()), 32),
unpack("V", $parser->readBytes(4)->getBinary())[1]
);
}
/**
* @param BufferInterface $data
* @return OutPointInterface
* @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange
*/
public function parse(BufferInterface $data): OutPointInterface
{
return $this->fromParser(new Parser($data));
}
}