| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace BitWasp\Trezor\Bridge; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BitWasp\Trezor\Bridge\Exception\InvalidMessageException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BitWasp\Trezor\Bridge\Exception\SchemaValidationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BitWasp\Trezor\Bridge\Message\Device; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BitWasp\Trezor\Bridge\Message\ListDevicesResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BitWasp\Trezor\Bridge\Message\ListenResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use BitWasp\Trezor\Bridge\Message\VersionResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use BitWasp\Trezor\Bridge\Schema\ValidatorFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use BitWasp\Trezor\Bridge\Http\HttpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BitWasp\Trezor\Device\Message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var HttpClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var ValidatorFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $validation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * TrezorClient constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param HttpClient $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 44 |  |     public function __construct(HttpClient $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 44 |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 44 |  |         $this->validation = new ValidatorFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 44 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param \Psr\Http\Message\StreamInterface $body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 14 |  |     protected function parseResponse(\Psr\Http\Message\StreamInterface $body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 14 |  |         $data = json_decode($body->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 14 |  |         if (json_last_error() !== JSON_ERROR_NONE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |             throw new InvalidMessageException("Invalid JSON received in response"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 12 |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param \stdClass $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param \stdClass $schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 12 |  |     protected function validateSchema($data, \stdClass $schema) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 12 |  |         $validator = new \JsonSchema\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 12 |  |         $validator->coerce($data, $schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 12 |  |         if ($validator->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 7 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 5 |  |         throw new SchemaValidationException($validator->getErrors()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param ResponseInterface $response - Response message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param \stdClass $schema - JSON schema to validate against | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 14 |  |     protected function processResponse(ResponseInterface $response, \stdClass $schema) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 14 |  |         $result = $this->parseResponse($response->getBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 12 |  |         $this->validateSchema($result, $schema); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 7 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return VersionResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 5 |  |     public function bridgeVersion(): VersionResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 5 |  |         $result = $this->processResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 5 |  |             $this->client->bridgeVersion(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 5 |  |             $this->validation->versionResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         return new VersionResponse($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return ListDevicesResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |     public function listDevices(): ListDevicesResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         $result = $this->processResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |             $this->client->listDevices(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 2 |  |             $this->validation->listDevicesResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         $devices = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         foreach ($result as $device) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |             $devices[] = new Device($device); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |         return new ListDevicesResponse($devices); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |     public function listen(Device ...$devices): ListenResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         $result = $this->processResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |             $this->client->listen(...$devices), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |             $this->validation->listDevicesResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |         $devices = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |         foreach ($result as $device) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |             $devices[] = new Device($device); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |         return new ListenResponse($devices); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param Device $device | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 3 |  |     public function acquire(Device $device): Session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 3 |  |         $result = $this->processResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 3 |  |             $this->client->acquire($device), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 3 |  |             $this->validation->acquireResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  |         return new Session($this, $device, $result->session); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 | 3 |  |     public function release(string $sessionId): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 3 |  |         $this->processResponse( | 
            
                                                                        
                            
            
                                    
            
            
                | 143 | 3 |  |             $this->client->release($sessionId), | 
            
                                                                        
                            
            
                                    
            
            
                | 144 | 3 |  |             $this->validation->releaseResponse() | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 3 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 29 |  |     public function call(string $sessionId, Message $message): Message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 29 |  |         return $this->client->call($sessionId, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  |