| 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 | 155 |  |     public function __construct(EcAdapterInterface $ecAdapter = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 155 |  |         $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 155 |  |         $this->privSerializer = EcSerializer::getSerializer(PrivateKeySerializerInterface::class, true, $ecAdapter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 155 |  |         $this->wifSerializer = new WifPrivateKeySerializer($this->privSerializer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 155 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param Random $random | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 5 |  |     public function generateCompressed(Random $random): PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |         return $this->privSerializer->parse($random->bytes(32), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param Random $random | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 13 |  |     public function generateUncompressed(Random $random): PrivateKeyInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 13 |  |         return $this->privSerializer->parse($random->bytes(32), false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param BufferInterface $raw | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 76 |  |     public function fromBufferCompressed(BufferInterface $raw): PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 76 |  |         return $this->privSerializer->parse($raw, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param BufferInterface $raw | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 35 |  |     public function fromBufferUncompressed(BufferInterface $raw): PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 35 |  |         return $this->privSerializer->parse($raw, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $hex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 39 |  |     public function fromHexCompressed(string $hex): PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 39 |  |         return $this->fromBufferCompressed(Buffer::hex($hex)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $hex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 26 |  |     public function fromHexUncompressed(string $hex): PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 26 |  |         return $this->fromBufferUncompressed(Buffer::hex($hex)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param string $wif | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param NetworkInterface $network | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\InvalidPrivateKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 6 |  |     public function fromWif(string $wif, NetworkInterface $network = null): PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 6 |  |         return $this->wifSerializer->parse($wif, $network); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  |