Passed
Pull Request — master (#755)
by thomas
38:10
created

PrivateKeyFactory::fromBufferUncompressed()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key\Factory;
6
7
use BitWasp\Bitcoin\Bitcoin;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface;
9
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface;
11
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PrivateKeySerializerInterface;
12
use BitWasp\Bitcoin\Crypto\Random\Random;
13
use BitWasp\Bitcoin\Network\NetworkInterface;
14
use BitWasp\Bitcoin\Serializer\Key\PrivateKey\WifPrivateKeySerializer;
15
use BitWasp\Buffertools\Buffer;
16
use BitWasp\Buffertools\BufferInterface;
17
18
class PrivateKeyFactory
19
{
20
    /**
21
     * @var PrivateKeySerializerInterface
22
     */
23
    private $privSerializer;
24
25
    /**
26
     * @var WifPrivateKeySerializer
27
     */
28
    private $wifSerializer;
29
30
    /**
31
     * PrivateKeyFactory constructor.
32
     * @param EcAdapterInterface $ecAdapter
33
     */
34
    public function __construct(EcAdapterInterface $ecAdapter = null)
35
    {
36
        $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
37
        $this->privSerializer = EcSerializer::getSerializer(PrivateKeySerializerInterface::class, true, $ecAdapter);
38
        $this->wifSerializer = new WifPrivateKeySerializer($this->privSerializer);
39
    }
40 155
    
41
    /**
42 155
     * @param Random $random
43 155
     * @return PrivateKeyInterface
44 155
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
45 155
     */
46 155
    public function generateCompressed(Random $random): PrivateKeyInterface
47
    {
48
        return $this->privSerializer->parse($random->bytes(32), true);
49
    }
50
51
    /**
52 33
     * @param Random $random
53
     * @return PrivateKeyInterface
54 33
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
55
     */
56
    public function generateUncompressed(Random $random): PrivateKeyInterface
57
    {
58
        return $this->privSerializer->parse($random->bytes(32), false);
59
    }
60
61 108
    /**
62
     * @param BufferInterface $raw
63 108
     * @return PrivateKeyInterface
64
     * @throws \Exception
65
     */
66
    public function fromBufferCompressed(BufferInterface $raw): PrivateKeyInterface
67
    {
68
        return $this->privSerializer->parse($raw, true);
69 2
    }
70
71 2
    /**
72
     * @param BufferInterface $raw
73
     * @return PrivateKeyInterface
74
     * @throws \Exception
75
     */
76
    public function fromBufferUncompressed(BufferInterface $raw): PrivateKeyInterface
77
    {
78
        return $this->privSerializer->parse($raw, false);
79 15
    }
80
81 15
    /**
82 15
     * @param string $hex
83
     * @return PrivateKeyInterface
84
     * @throws \Exception
85
     */
86
    public function fromHexCompressed(string $hex): PrivateKeyInterface
87
    {
88
        return $this->fromBufferCompressed(Buffer::hex($hex));
89
    }
90
91 47
    /**
92
     * @param string $hex
93 47
     * @return PrivateKeyInterface
94
     * @throws \Exception
95
     */
96
    public function fromHexUncompressed(string $hex): PrivateKeyInterface
97
    {
98
        return $this->fromBufferUncompressed(Buffer::hex($hex));
99
    }
100 108
101
    /**
102 108
     * @param string $wif
103 108
     * @param NetworkInterface $network
104 108
     * @return PrivateKeyInterface
105
     * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure
106
     * @throws \BitWasp\Bitcoin\Exceptions\InvalidPrivateKey
107
     * @throws \Exception
108
     */
109
    public function fromWif(string $wif, NetworkInterface $network = null): PrivateKeyInterface
110
    {
111
        return $this->wifSerializer->parse($wif, $network);
112
    }
113
}
114