Completed
Pull Request — master (#12)
by thomas
19:02 queued 01:07
created

RequestFactory::rawSignMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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