Completed
Pull Request — master (#591)
by thomas
20:21
created

PrivateKeyFactory::fromBuffer()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 3
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Key;
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\Exceptions\InvalidPrivateKey;
14
use BitWasp\Bitcoin\Network\NetworkInterface;
15
use BitWasp\Bitcoin\Serializer\Key\PrivateKey\WifPrivateKeySerializer;
16
use BitWasp\Buffertools\Buffer;
17
use BitWasp\Buffertools\BufferInterface;
18
19
class PrivateKeyFactory
20
{
21
    /**
22
     * @param bool $compressed
23
     * @param EcAdapterInterface|null $ecAdapter
24
     * @return PrivateKeyInterface
25
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
26
     */
27 15
    public static function create(bool $compressed = false, EcAdapterInterface $ecAdapter = null): PrivateKeyInterface
28
    {
29 15
        return self::fromBuffer(self::generateSecret(), $compressed, $ecAdapter);
30
    }
31
32
    /**
33
     * Generate a buffer containing a valid key
34
     *
35
     * @param EcAdapterInterface|null $ecAdapter
36
     * @return BufferInterface
37
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
38
     */
39 15
    public static function generateSecret(EcAdapterInterface $ecAdapter = null): BufferInterface
40
    {
41 15
        $random = new Random();
42 15
        $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
43
44
        do {
45 15
            $buffer = $random->bytes(32);
46 15
        } while (!$ecAdapter->validatePrivateKey($buffer));
47
48 15
        return $buffer;
49
    }
50
51
    /**
52
     * @param string $hex
53
     * @param bool $compressed
54
     * @param EcAdapterInterface|null $ecAdapter
55
     * @return PrivateKeyInterface
56
     * @throws \Exception
57
     */
58 39
    public static function fromHex(string $hex, bool $compressed = false, EcAdapterInterface $ecAdapter = null): PrivateKeyInterface
59
    {
60 39
        return self::fromBuffer(Buffer::hex($hex), $compressed, $ecAdapter);
61
    }
62
63
    /**
64
     * @param BufferInterface $buffer
65
     * @param bool $compressed
66
     * @param EcAdapterInterface $ecAdapter
67
     * @return PrivateKeyInterface
68
     */
69 86
    public static function fromBuffer(BufferInterface $buffer, bool $compressed, EcAdapterInterface $ecAdapter = null): PrivateKeyInterface
70
    {
71 86
        $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
72
73
        /** @var PrivateKeySerializerInterface $serializer */
74 86
        $serializer = EcSerializer::getSerializer(PrivateKeySerializerInterface::class, true, $ecAdapter);
75
76 86
        $parsed = $serializer->parse($buffer);
77 86
        if ($compressed) {
78 72
            $parsed = $ecAdapter->getPrivateKey($parsed->getSecret(), $compressed);
79
        }
80
81 86
        return $parsed;
82
    }
83
84
    /**
85
     * @param int|string $int
86
     * @param bool $compressed
87
     * @param EcAdapterInterface|null $ecAdapter
88
     * @return PrivateKeyInterface
89
     */
90 21
    public static function fromInt($int, bool $compressed = false, EcAdapterInterface $ecAdapter = null)
91
    {
92 21
        $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
93 21
        $secret = Buffer::int($int, 32)->getGmp();
94 21
        return $ecAdapter->getPrivateKey($secret, $compressed);
95
    }
96
97
    /**
98
     * @param string $wif
99
     * @param EcAdapterInterface|null $ecAdapter
100
     * @param NetworkInterface|null $network
101
     * @return PrivateKeyInterface
102
     * @throws InvalidPrivateKey
103
     * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure
104
     */
105 7
    public static function fromWif(string $wif, EcAdapterInterface $ecAdapter = null, NetworkInterface $network = null)
106
    {
107 7
        if (null === $ecAdapter) {
108 3
            $ecAdapter = Bitcoin::getEcAdapter();
109
        }
110
111 7
        $serializer = EcSerializer::getSerializer(PrivateKeySerializerInterface::class, true, $ecAdapter);
112 7
        $wifSerializer = new WifPrivateKeySerializer($ecAdapter, $serializer);
113
114 7
        return $wifSerializer->parse($wif, $network);
115
    }
116
}
117