|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Trezor\Device\Command; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\Trezor\Bridge\Session; |
|
8
|
|
|
use BitWasp\Trezor\Device\Exception\IncorrectNonceException; |
|
9
|
|
|
use BitWasp\Trezor\Device\Message; |
|
10
|
|
|
use BitWasp\Trezor\Device\UserInput\CurrentPassphraseInputInterface; |
|
11
|
|
|
use BitWasp\Trezor\Device\UserInput\CurrentPinInputInterface; |
|
12
|
|
|
use BitWasp\TrezorProto\ButtonRequest; |
|
13
|
|
|
use BitWasp\TrezorProto\ButtonRequestType; |
|
14
|
|
|
use BitWasp\TrezorProto\PassphraseRequest; |
|
15
|
|
|
use BitWasp\TrezorProto\Ping; |
|
16
|
|
|
use BitWasp\TrezorProto\PinMatrixRequest; |
|
17
|
|
|
use BitWasp\TrezorProto\Success; |
|
18
|
|
|
|
|
19
|
|
|
class PingService extends DeviceService |
|
20
|
|
|
{ |
|
21
|
8 |
|
public function call( |
|
22
|
|
|
Session $session, |
|
23
|
|
|
Ping $ping, |
|
24
|
|
|
CurrentPinInputInterface $pinInput = null, |
|
25
|
|
|
CurrentPassphraseInputInterface $passphraseInput = null |
|
26
|
|
|
): Success { |
|
27
|
8 |
|
if ($ping->hasPinProtection() && $ping->getPinProtection() && $pinInput === null) { |
|
28
|
1 |
|
throw new \InvalidArgumentException("Missing pin input"); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
7 |
|
if ($ping->hasPassphraseProtection() && $ping->getPassphraseProtection() && $passphraseInput === null) { |
|
32
|
1 |
|
throw new \InvalidArgumentException("Missing passphrase input"); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
6 |
|
$proto = $session->sendMessage(Message::ping($ping)); |
|
36
|
6 |
|
if ($proto instanceof ButtonRequest) { |
|
37
|
|
|
// allow user to accept with the button |
|
38
|
1 |
|
$proto = $session->sendMessage($this->confirmWithButton($proto, ButtonRequestType::ButtonRequest_ProtectCall_VALUE)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
6 |
|
if ($ping->hasPinProtection()) { |
|
42
|
|
|
// allow user to accept with their pin |
|
43
|
6 |
|
if ($proto instanceof PinMatrixRequest) { |
|
44
|
1 |
|
$proto = $session->sendMessage($this->provideCurrentPin($proto, $pinInput)); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
6 |
|
if ($ping->hasPassphraseProtection()) { |
|
49
|
|
|
// allow user to accept with their passphrase |
|
50
|
6 |
|
if ($proto instanceof PassphraseRequest) { |
|
51
|
1 |
|
$proto = $session->sendMessage($this->provideCurrentPassphrase($passphraseInput)); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
if (!($proto instanceof Success)) { |
|
56
|
1 |
|
throw new \RuntimeException("Unexpected response, expecting Success, got " . get_class($proto)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @var Success $proto */ |
|
60
|
5 |
|
if (!hash_equals($proto->getMessage(), $ping->getMessage())) { |
|
61
|
1 |
|
throw new IncorrectNonceException("Nonce returned by device was incorrect"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
return $proto; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|