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\TrezorProto\Failure; |
12
|
|
|
use Protobuf\Message as ProtoMessage; |
13
|
|
|
|
14
|
|
|
class Session |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Device |
23
|
|
|
*/ |
24
|
|
|
private $device; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $sessionId; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $active = true; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Session constructor. |
38
|
|
|
* @param Client $client |
39
|
|
|
* @param Device $device |
40
|
|
|
* @param string $sessionId |
41
|
|
|
*/ |
42
|
32 |
|
public function __construct(Client $client, Device $device, string $sessionId) |
43
|
|
|
{ |
44
|
32 |
|
$this->client = $client; |
45
|
32 |
|
$this->device = $device; |
46
|
32 |
|
$this->sessionId = $sessionId; |
47
|
32 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws InactiveSessionException |
51
|
|
|
*/ |
52
|
32 |
|
private function assertSessionIsActive() |
53
|
|
|
{ |
54
|
32 |
|
if (!$this->active) { |
55
|
1 |
|
throw new InactiveSessionException("Attempted command on inactive session"); |
56
|
|
|
} |
57
|
32 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
2 |
|
public function isActive(): bool |
63
|
|
|
{ |
64
|
2 |
|
return $this->active; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @throws InactiveSessionException |
69
|
|
|
*/ |
70
|
2 |
|
public function release() |
71
|
|
|
{ |
72
|
2 |
|
$this->assertSessionIsActive(); |
73
|
2 |
|
$this->client->release($this->sessionId); |
74
|
2 |
|
$this->active = false; |
75
|
2 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
* @throws InactiveSessionException |
80
|
|
|
*/ |
81
|
30 |
|
public function getSessionId(): string |
82
|
|
|
{ |
83
|
30 |
|
$this->assertSessionIsActive(); |
84
|
30 |
|
return $this->sessionId; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Device |
89
|
|
|
*/ |
90
|
2 |
|
public function getDevice(): Device |
91
|
|
|
{ |
92
|
2 |
|
return $this->device; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param Message $message |
97
|
|
|
* @return ProtoMessage |
98
|
|
|
* @throws FailureException |
99
|
|
|
* @throws InactiveSessionException |
100
|
|
|
*/ |
101
|
29 |
|
public function sendMessage(Message $message): ProtoMessage |
102
|
|
|
{ |
103
|
29 |
|
$this->assertSessionIsActive(); |
104
|
28 |
|
$message = $this->client->call($this->getSessionId(), $message); |
105
|
28 |
|
$proto = $message->getProto(); |
106
|
28 |
|
if ($proto instanceof Failure) { |
107
|
1 |
|
FailureException::handleFailure($proto); |
108
|
|
|
} |
109
|
|
|
|
110
|
28 |
|
return $proto; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|