Completed
Pull Request — master (#2)
by thomas
01:56
created

RequestFactory::getWitnessKeyHashAddress()   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 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 6
    public function getPublicKey(string $coinName, array $path, bool $showDisplay, string $curveName = null): GetPublicKey
32
    {
33 6
        $getPublicKey = new GetPublicKey();
34 6
        $getPublicKey->setShowDisplay($showDisplay);
35 6
        $getPublicKey->setCoinName($coinName);
36 6
        foreach ($path as $sequence) {
37 6
            $getPublicKey->addAddressN($sequence);
38
        }
39 6
        if ($curveName) {
40 2
            $getPublicKey->setEcdsaCurveName($curveName);
41
        }
42 6
        return $getPublicKey;
43
    }
44
45 16
    public function getAddress(string $coinName, array $path, InputScriptType $inScriptType, bool $showDisplay): GetAddress
46
    {
47 16
        $getAddress = new GetAddress();
48 16
        $getAddress->setCoinName($coinName);
49 16
        foreach ($path as $sequence) {
50 16
            $getAddress->addAddressN($sequence);
51
        }
52 16
        $getAddress->setShowDisplay($showDisplay);
53 16
        $getAddress->setScriptType($inScriptType);
54
55 16
        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 1
    public function verifyMessage(string $coinName, string $address, string $signature, string $message): VerifyMessage
74
    {
75 1
        $verifyMsg = new VerifyMessage();
76 1
        $verifyMsg->setCoinName($coinName);
77 1
        $verifyMsg->setAddress($address);
78 1
        $verifyMsg->setSignature(\Protobuf\Stream::fromString($signature));
79 1
        $verifyMsg->setMessage(\Protobuf\Stream::fromString($message));
80 1
        return $verifyMsg;
81
    }
82
83 2
    public function rawSignMessage(string $coinName, array $path, InputScriptType $inScriptType, string $message): SignMessage
84
    {
85 2
        $signMessage = new SignMessage();
86 2
        $signMessage->setCoinName($coinName);
87 2
        foreach ($path as $sequence) {
88 2
            $signMessage->addAddressN($sequence);
89
        }
90 2
        $signMessage->setScriptType($inScriptType);
91 2
        $signMessage->setMessage(\Protobuf\Stream::fromString($message));
92 2
        return $signMessage;
93
    }
94
95 1
    public function signMessagePubKeyHash(string $coinName, array $path, string $message): SignMessage
96
    {
97 1
        return $this->rawSignMessage($coinName, $path, InputScriptType::SPENDADDRESS(), $message);
98
    }
99
100 12
    public function ping(string $nonce, bool $hasButtonProtect, bool $hasPinProtect, bool $hasPasswordProtect): Ping
101
    {
102 12
        $ping = new Ping();
103 12
        $ping->setMessage($nonce);
104 12
        $ping->setButtonProtection($hasButtonProtect);
105 12
        $ping->setPinProtection($hasPinProtect);
106 12
        $ping->setPassphraseProtection($hasPasswordProtect);
107 12
        return $ping;
108
    }
109
110 3
    public function clearSession(): ClearSession
111
    {
112 3
        return new ClearSession();
113
    }
114
}
115