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