| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace BitWasp\Bitcoin\Networking\Serializer\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BitWasp\Bitcoin\Networking\Messages\Version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BitWasp\Bitcoin\Networking\Serializer\Structure\NetworkAddressSerializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BitWasp\Bitcoin\Serializer\Types; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BitWasp\Buffertools\Buffer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BitWasp\Buffertools\BufferInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use BitWasp\Buffertools\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class VersionSerializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var NetworkAddressSerializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $netAddr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 96 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var \BitWasp\Buffertools\Types\Uint32 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 96 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 96 |  |     private $uint32le; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var \BitWasp\Buffertools\Types\Uint64 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $uint64le; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 21 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var \BitWasp\Buffertools\Types\ByteString | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 21 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 21 |  |     private $bs26le; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 21 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 21 |  |      * @var \BitWasp\Buffertools\Types\VarString | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 21 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 21 |  |     private $varstring; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 21 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 21 |  |      * @var \BitWasp\Buffertools\Types\Uint8 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 21 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 21 |  |     private $uint8le; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 21 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param NetworkAddressSerializer $netAddr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function __construct(NetworkAddressSerializer $netAddr) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->netAddr = $netAddr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 15 |  |         $this->uint32le = Types::uint32le(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $this->uint64le = Types::uint64le(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 15 |  |         $this->bs26le = Types::bytestringle(26); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->varstring = Types::varstring(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 15 |  |         $this->uint8le = Types::uint8le(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 10 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 10 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 15 |  |      * @param Parser $parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 15 |  |      * @return Version | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 | 10 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 10 |  |     public function fromParser(Parser $parser) | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 10 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 5 |  |         return new Version( | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 10 |  |             (int) $this->uint32le->read($parser), | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             (int) $this->uint64le->read($parser), | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             (int) $this->uint64le->read($parser), | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             $this->netAddr->fromParser($parser), | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             $this->netAddr->fromParser($parser), | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             (int) $this->uint64le->read($parser), | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             $this->varstring->read($parser), | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 15 |  |             (int) $this->uint32le->read($parser), | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             (bool) $this->uint8le->read($parser) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 15 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param BufferInterface $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return Version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 21 |  |     public function parse(BufferInterface $string): Version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 21 |  |         return $this->fromParser(new Parser($string)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 21 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 21 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 21 |  |      * @param Version $version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 21 |  |      * @return BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 21 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 21 |  |     public function serialize(Version $version): BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 21 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 21 |  |         return new Buffer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 14 |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 "%s%s%s%s%s%s%s%s%s", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $this->uint32le->write($version->getVersion()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 $this->uint64le->write($version->getServices()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $this->uint64le->write($version->getTimestamp()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $this->netAddr->serialize($version->getRecipientAddress())->getBinary(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $this->netAddr->serialize($version->getSenderAddress())->getBinary(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $this->uint64le->write($version->getNonce()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $this->varstring->write($version->getUserAgent()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $this->uint32le->write($version->getStartHeight()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $this->uint8le->write((int) $version->getRelay()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  |