Failed Conditions
Push — master ( ba8c0d...a9ded0 )
by thomas
04:50
created

RequestFactory::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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