Failed Conditions
Pull Request — master (#12)
by thomas
03:33
created

RequestFactory::ping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 4
crap 1
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\HDNodeType;
12
use BitWasp\TrezorProto\Initialize;
13
use BitWasp\TrezorProto\InputScriptType;
14
use BitWasp\TrezorProto\LoadDevice;
15
use BitWasp\TrezorProto\Ping;
16
use BitWasp\TrezorProto\SignMessage;
17
use BitWasp\TrezorProto\VerifyMessage;
18
19
class RequestFactory
20
{
21 3
    public function initialize(): Initialize
22
    {
23 3
        return new Initialize();
24
    }
25
26 3
    public function getEntropy(int $bytes): GetEntropy
27
    {
28 3
        $getEntropy = new GetEntropy();
29 3
        $getEntropy->setSize($bytes);
30 3
        return $getEntropy;
31
    }
32
33 7
    public function getPublicKey(string $coinName, array $path, bool $showDisplay, string $curveName = null): GetPublicKey
34
    {
35 7
        $getPublicKey = new GetPublicKey();
36 7
        $getPublicKey->setShowDisplay($showDisplay);
37 7
        $getPublicKey->setCoinName($coinName);
38 7
        foreach ($path as $sequence) {
39 7
            $getPublicKey->addAddressN($sequence);
40
        }
41 7
        if ($curveName) {
42 2
            $getPublicKey->setEcdsaCurveName($curveName);
43
        }
44 7
        return $getPublicKey;
45
    }
46
47 22
    public function getAddress(string $coinName, array $path, InputScriptType $inScriptType, bool $showDisplay): GetAddress
48
    {
49 22
        $getAddress = new GetAddress();
50 22
        $getAddress->setCoinName($coinName);
51 22
        foreach ($path as $sequence) {
52 22
            $getAddress->addAddressN($sequence);
53
        }
54 22
        $getAddress->setShowDisplay($showDisplay);
55 22
        $getAddress->setScriptType($inScriptType);
56
57 22
        return $getAddress;
58
    }
59
60 4
    public function getKeyHashAddress(string $coinName, array $path, bool $showDisplay): GetAddress
61
    {
62 4
        return $this->getAddress($coinName, $path, InputScriptType::SPENDADDRESS(), $showDisplay);
63
    }
64
65 4
    public function getWitnessKeyHashAddress(string $coinName, array $path, bool $showDisplay): GetAddress
66
    {
67 4
        return $this->getAddress($coinName, $path, InputScriptType::SPENDWITNESS(), $showDisplay);
68
    }
69
70 4
    public function getP2shWitnessKeyHashAddress(string $coinName, array $path, bool $showDisplay): GetAddress
71
    {
72 4
        return $this->getAddress($coinName, $path, InputScriptType::SPENDP2SHWITNESS(), $showDisplay);
73
    }
74
75 3
    public function verifyMessage(string $coinName, string $address, string $signature, string $message): VerifyMessage
76
    {
77 3
        $verifyMsg = new VerifyMessage();
78 3
        $verifyMsg->setCoinName($coinName);
79 3
        $verifyMsg->setAddress($address);
80 3
        $verifyMsg->setSignature(\Protobuf\Stream::fromString($signature));
81 3
        $verifyMsg->setMessage(\Protobuf\Stream::fromString($message));
82 3
        return $verifyMsg;
83
    }
84
85 4
    public function rawSignMessage(string $coinName, array $path, InputScriptType $inScriptType, string $message): SignMessage
86
    {
87 4
        $signMessage = new SignMessage();
88 4
        $signMessage->setCoinName($coinName);
89 4
        foreach ($path as $sequence) {
90 4
            $signMessage->addAddressN($sequence);
91
        }
92 4
        $signMessage->setScriptType($inScriptType);
93 4
        $signMessage->setMessage(\Protobuf\Stream::fromString($message));
94 4
        return $signMessage;
95
    }
96
97 3
    public function signMessagePubKeyHash(string $coinName, array $path, string $message): SignMessage
98
    {
99 3
        return $this->rawSignMessage($coinName, $path, InputScriptType::SPENDADDRESS(), $message);
100
    }
101
102 10
    public function ping(string $nonce, bool $hasButtonProtect, bool $hasPinProtect, bool $hasPasswordProtect): Ping
103
    {
104 10
        $ping = new Ping();
105 10
        $ping->setMessage($nonce);
106 10
        $ping->setButtonProtection($hasButtonProtect);
107 10
        $ping->setPinProtection($hasPinProtect);
108 10
        $ping->setPassphraseProtection($hasPasswordProtect);
109 10
        return $ping;
110
    }
111
112 3
    public function clearSession(): ClearSession
113
    {
114 3
        return new ClearSession();
115
    }
116
117
    private function prepareLoadDevice(
118
        bool $skipChecksum,
119
        bool $usePassphrase,
120
        string $language = null,
121
        int $u2fCounter = null,
122
        string $pin = null,
123
        string $label = null
124
    ): LoadDevice {
125
        $loadDevice = new LoadDevice();
126
        $loadDevice->setSkipChecksum($skipChecksum);
127
        $loadDevice->setPassphraseProtection($usePassphrase);
128
        if (is_string($language)) {
129
            $loadDevice->setLanguage($language);
130
        }
131
        if (is_string($pin)) {
132
            $loadDevice->setPin($pin);
133
        }
134
        if (is_string($label)) {
135
            $loadDevice->setLabel($label);
136
        }
137
        if (is_int($u2fCounter)) {
138
            $loadDevice->setU2fCounter($u2fCounter);
139
        }
140
        return $loadDevice;
141
    }
142
143
    public function loadDeviceWithHdNode(
144
        HDNodeType $hdNode,
145
        string $language,
146
        string $label = null,
147
        string $pin = null,
148
        bool $usePassphrase = false,
149
        bool $skipChecksum = false,
150
        int $u2fCounter = null
151
    ): LoadDevice {
152
        $loadDevice = $this->prepareLoadDevice($skipChecksum, $usePassphrase, $language, $u2fCounter, $pin, $label);
153
        $loadDevice->setNode($hdNode);
154
        return $loadDevice;
155
    }
156
157
    public function loadDeviceWithMnemonic(
158
        string $mnemonic,
159
        string $language = null,
160
        string $label = null,
161
        string $pin = null,
162
        bool $usePassphrase = false,
163
        bool $skipChecksum = false,
164
        int $u2fCounter = null
165
    ): LoadDevice {
166
        $loadDevice = $this->prepareLoadDevice($skipChecksum, $usePassphrase, $language, $u2fCounter, $pin, $label);
167
        $loadDevice->setMnemonic($mnemonic);
168
        return $loadDevice;
169
    }
170
    public function privateHdNode(int $depth, int $fingerprint, int $childNum, \Protobuf\Stream $chainCode, \Protobuf\Stream $privateKey): HDNodeType
171
    {
172
        $hdNode = new HDNodeType();
173
        $hdNode->setDepth($depth);
174
        $hdNode->setFingerprint($fingerprint);
175
        $hdNode->setChildNum($childNum);
176
        $hdNode->setChainCode($chainCode);
177
        $hdNode->setPrivateKey($privateKey);
178
        return $hdNode;
179
    }
180
    public function publicHdNode(int $depth, int $fingerprint, int $childNum, \Protobuf\Stream $chainCode, \Protobuf\Stream $publicKey): HDNodeType
181
    {
182
        $hdNode = new HDNodeType();
183
        $hdNode->setDepth($depth);
184
        $hdNode->setFingerprint($fingerprint);
185
        $hdNode->setChildNum($childNum);
186
        $hdNode->setChainCode($chainCode);
187
        $hdNode->setPublicKey($publicKey);
188
        return $hdNode;
189
    }
190
}
191