|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BitWasp\Trezor\Device; |
|
6
|
|
|
|
|
7
|
|
|
use BitWasp\TrezorProto\ClearSession; |
|
8
|
|
|
use BitWasp\TrezorProto\GetAddress; |
|
9
|
|
|
use BitWasp\TrezorProto\GetEntropy; |
|
10
|
|
|
use BitWasp\TrezorProto\GetPublicKey; |
|
11
|
|
|
use BitWasp\TrezorProto\Initialize; |
|
12
|
|
|
use BitWasp\TrezorProto\InputScriptType; |
|
13
|
|
|
use BitWasp\TrezorProto\Ping; |
|
14
|
|
|
use BitWasp\TrezorProto\SignMessage; |
|
15
|
|
|
use BitWasp\TrezorProto\VerifyMessage; |
|
16
|
|
|
|
|
17
|
|
|
class RequestFactory |
|
18
|
|
|
{ |
|
19
|
3 |
|
public function initialize(): Initialize |
|
20
|
|
|
{ |
|
21
|
3 |
|
return new Initialize(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
3 |
|
public function getEntropy(int $bytes): GetEntropy |
|
25
|
|
|
{ |
|
26
|
3 |
|
$getEntropy = new GetEntropy(); |
|
27
|
3 |
|
$getEntropy->setSize($bytes); |
|
28
|
3 |
|
return $getEntropy; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
7 |
|
public function getPublicKey(string $coinName, array $path, bool $showDisplay, string $curveName = null): GetPublicKey |
|
32
|
|
|
{ |
|
33
|
7 |
|
$getPublicKey = new GetPublicKey(); |
|
34
|
7 |
|
$getPublicKey->setShowDisplay($showDisplay); |
|
35
|
7 |
|
$getPublicKey->setCoinName($coinName); |
|
36
|
7 |
|
foreach ($path as $sequence) { |
|
37
|
7 |
|
$getPublicKey->addAddressN($sequence); |
|
38
|
|
|
} |
|
39
|
7 |
|
if ($curveName) { |
|
40
|
2 |
|
$getPublicKey->setEcdsaCurveName($curveName); |
|
41
|
|
|
} |
|
42
|
7 |
|
return $getPublicKey; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
22 |
|
public function getAddress(string $coinName, array $path, InputScriptType $inScriptType, bool $showDisplay): GetAddress |
|
46
|
|
|
{ |
|
47
|
22 |
|
$getAddress = new GetAddress(); |
|
48
|
22 |
|
$getAddress->setCoinName($coinName); |
|
49
|
22 |
|
foreach ($path as $sequence) { |
|
50
|
22 |
|
$getAddress->addAddressN($sequence); |
|
51
|
|
|
} |
|
52
|
22 |
|
$getAddress->setShowDisplay($showDisplay); |
|
53
|
22 |
|
$getAddress->setScriptType($inScriptType); |
|
54
|
|
|
|
|
55
|
22 |
|
return $getAddress; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
public function getKeyHashAddress(string $coinName, array $path, bool $showDisplay): GetAddress |
|
59
|
|
|
{ |
|
60
|
4 |
|
return $this->getAddress($coinName, $path, InputScriptType::SPENDADDRESS(), $showDisplay); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
public function getWitnessKeyHashAddress(string $coinName, array $path, bool $showDisplay): GetAddress |
|
64
|
|
|
{ |
|
65
|
4 |
|
return $this->getAddress($coinName, $path, InputScriptType::SPENDWITNESS(), $showDisplay); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
public function getP2shWitnessKeyHashAddress(string $coinName, array $path, bool $showDisplay): GetAddress |
|
69
|
|
|
{ |
|
70
|
4 |
|
return $this->getAddress($coinName, $path, InputScriptType::SPENDP2SHWITNESS(), $showDisplay); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
public function verifyMessage(string $coinName, string $address, string $signature, string $message): VerifyMessage |
|
74
|
|
|
{ |
|
75
|
3 |
|
$verifyMsg = new VerifyMessage(); |
|
76
|
3 |
|
$verifyMsg->setCoinName($coinName); |
|
77
|
3 |
|
$verifyMsg->setAddress($address); |
|
78
|
3 |
|
$verifyMsg->setSignature(\Protobuf\Stream::fromString($signature)); |
|
79
|
3 |
|
$verifyMsg->setMessage(\Protobuf\Stream::fromString($message)); |
|
80
|
3 |
|
return $verifyMsg; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
public function rawSignMessage(string $coinName, array $path, InputScriptType $inScriptType, string $message): SignMessage |
|
84
|
|
|
{ |
|
85
|
4 |
|
$signMessage = new SignMessage(); |
|
86
|
4 |
|
$signMessage->setCoinName($coinName); |
|
87
|
4 |
|
foreach ($path as $sequence) { |
|
88
|
4 |
|
$signMessage->addAddressN($sequence); |
|
89
|
|
|
} |
|
90
|
4 |
|
$signMessage->setScriptType($inScriptType); |
|
91
|
4 |
|
$signMessage->setMessage(\Protobuf\Stream::fromString($message)); |
|
92
|
4 |
|
return $signMessage; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
public function signMessagePubKeyHash(string $coinName, array $path, string $message): SignMessage |
|
96
|
|
|
{ |
|
97
|
3 |
|
return $this->rawSignMessage($coinName, $path, InputScriptType::SPENDADDRESS(), $message); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
10 |
|
public function ping(string $nonce, bool $hasButtonProtect, bool $hasPinProtect, bool $hasPasswordProtect): Ping |
|
101
|
|
|
{ |
|
102
|
10 |
|
$ping = new Ping(); |
|
103
|
10 |
|
$ping->setMessage($nonce); |
|
104
|
10 |
|
$ping->setButtonProtection($hasButtonProtect); |
|
105
|
10 |
|
$ping->setPinProtection($hasPinProtect); |
|
106
|
10 |
|
$ping->setPassphraseProtection($hasPasswordProtect); |
|
107
|
10 |
|
return $ping; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
3 |
|
public function clearSession(): ClearSession |
|
111
|
|
|
{ |
|
112
|
3 |
|
return new ClearSession(); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|