1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BitWasp\Trezor\Bridge; |
6
|
|
|
|
7
|
|
|
use BitWasp\Trezor\Bridge\Exception\InactiveSessionException; |
8
|
|
|
use BitWasp\Trezor\Bridge\Message\Device; |
9
|
|
|
use BitWasp\Trezor\Device\Exception\FailureException; |
10
|
|
|
use BitWasp\Trezor\Device\Message; |
11
|
|
|
use BitWasp\Trezor\Device\MessageBase; |
12
|
|
|
use BitWasp\TrezorProto\Failure; |
13
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
14
|
|
|
|
15
|
|
|
class Session |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Client |
19
|
|
|
*/ |
20
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Device |
24
|
|
|
*/ |
25
|
|
|
private $device; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $sessionId; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
private $active = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Session constructor. |
39
|
|
|
* @param Client $client |
40
|
|
|
* @param Device $device |
41
|
|
|
* @param string $sessionId |
42
|
|
|
*/ |
43
|
32 |
|
public function __construct(Client $client, Device $device, string $sessionId) |
44
|
|
|
{ |
45
|
32 |
|
$this->client = $client; |
46
|
32 |
|
$this->device = $device; |
47
|
32 |
|
$this->sessionId = $sessionId; |
48
|
32 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws InactiveSessionException |
52
|
|
|
*/ |
53
|
32 |
|
private function assertSessionIsActive() |
54
|
|
|
{ |
55
|
32 |
|
if (!$this->active) { |
56
|
1 |
|
throw new InactiveSessionException("Attempted command on inactive session"); |
57
|
|
|
} |
58
|
32 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
2 |
|
public function isActive(): bool |
64
|
|
|
{ |
65
|
2 |
|
return $this->active; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws InactiveSessionException |
70
|
|
|
*/ |
71
|
2 |
|
public function release() |
72
|
|
|
{ |
73
|
2 |
|
$this->assertSessionIsActive(); |
74
|
2 |
|
$this->client->release($this->sessionId); |
75
|
2 |
|
$this->active = false; |
76
|
2 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
* @throws InactiveSessionException |
81
|
|
|
*/ |
82
|
30 |
|
public function getSessionId(): string |
83
|
|
|
{ |
84
|
30 |
|
$this->assertSessionIsActive(); |
85
|
30 |
|
return $this->sessionId; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return Device |
90
|
|
|
*/ |
91
|
2 |
|
public function getDevice(): Device |
92
|
|
|
{ |
93
|
2 |
|
return $this->device; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param MessageBase $request |
98
|
|
|
* @return PromiseInterface |
99
|
|
|
*/ |
100
|
28 |
|
public function sendMessageAsync(MessageBase $request): PromiseInterface |
101
|
|
|
{ |
102
|
28 |
|
$this->assertSessionIsActive(); |
103
|
28 |
|
return $this->client->callAsync($this->getSessionId(), $request) |
104
|
28 |
|
->then(function (Message $message): \Protobuf\Message { |
105
|
28 |
|
$proto = $message->getProto(); |
106
|
28 |
|
if ($proto instanceof Failure) { |
107
|
1 |
|
FailureException::handleFailure($proto); |
108
|
|
|
} |
109
|
|
|
|
110
|
28 |
|
return $proto; |
111
|
28 |
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param MessageBase $message |
116
|
|
|
* @return \Protobuf\Message |
117
|
|
|
* @throws FailureException |
118
|
|
|
* @throws InactiveSessionException |
119
|
|
|
*/ |
120
|
29 |
|
public function sendMessage(MessageBase $message): \Protobuf\Message |
121
|
|
|
{ |
122
|
29 |
|
$this->assertSessionIsActive(); |
123
|
28 |
|
return $this->sendMessageAsync($message) |
124
|
28 |
|
->wait(true); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param MessageBase $message |
129
|
|
|
* @throws FailureException |
130
|
|
|
* @throws InactiveSessionException |
131
|
|
|
*/ |
132
|
|
|
public function postMessage(MessageBase $message) |
133
|
|
|
{ |
134
|
|
|
$this->assertSessionIsActive(); |
135
|
|
|
return $this->client->post($this->getSessionId(), $message); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|