Completed
Pull Request — master (#2)
by thomas
02:17
created

RequestFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
dl 0
loc 95
c 0
b 0
f 0
ccs 54
cts 54
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getEntropy() 0 5 1
A clearSession() 0 3 1
A initialize() 0 3 1
A getP2shWitnessKeyHashAddress() 0 3 1
A getPublicKey() 0 11 3
A getKeyHashAddress() 0 3 1
A getAddress() 0 11 2
A getWitnessKeyHashAddress() 0 3 1
A signMessagePubKeyHash() 0 3 1
A verifyMessage() 0 8 1
A ping() 0 8 1
A rawSignMessage() 0 10 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 3
    public function initialize(): Initialize
20
    {
21 3
        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 12
    public function ping(string $nonce, bool $hasButtonProtect, bool $hasPinProtect, bool $hasPasswordProtect): Ping
100
    {
101 12
        $ping = new Ping();
102 12
        $ping->setMessage($nonce);
103 12
        $ping->setButtonProtection($hasButtonProtect);
104 12
        $ping->setPinProtection($hasPinProtect);
105 12
        $ping->setPassphraseProtection($hasPasswordProtect);
106 12
        return $ping;
107
    }
108
109 1
    public function clearSession(): ClearSession
110
    {
111 1
        return new ClearSession();
112
    }
113
}
114