Passed
Pull Request — master (#2)
by thomas
01:53
created

RequestFactory::getKeyHashAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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