| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace BitWasp\Bitcoin\Key\KeyToScript; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2shP2wshScriptDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2shScriptDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\Decorator\P2wshScriptDecorator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\Factory\KeyToScriptDataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\Factory\MultisigScriptDataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2pkhScriptDataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2wpkhScriptDataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use BitWasp\Bitcoin\Script\ScriptType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class KeyToScriptHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var ScriptDataFactory[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $cache = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var PublicKeySerializerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $pubKeySer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Slip132PrefixRegistry constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param EcAdapterInterface $ecAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 24 |  |     public function __construct(EcAdapterInterface $ecAdapter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 24 |  |         $this->pubKeySer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, true, $ecAdapter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 24 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return P2pkhScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 6 |  |     public function getP2pkhFactory(): P2pkhScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |         $key = ScriptType::P2PKH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 6 |  |         if (!array_key_exists($key, $this->cache)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |             $this->cache[$key] = new P2pkhScriptDataFactory($this->pubKeySer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  |         return $this->cache[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param int $numSignatures | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param int $numKeys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param bool $sortCosignKeys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return MultisigScriptDataFactory | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 8 |  |     public function getMultisigFactory(int $numSignatures, int $numKeys, bool $sortCosignKeys): MultisigScriptDataFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 8 |  |         return new MultisigScriptDataFactory($numSignatures, $numKeys, $sortCosignKeys, $this->pubKeySer); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @return P2wpkhScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 10 |  |     public function getP2wpkhFactory(): P2wpkhScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 10 |  |         $key = ScriptType::P2WKH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 10 |  |         if (!array_key_exists($key, $this->cache)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 10 |  |             $this->cache[$key] = new P2wpkhScriptDataFactory($this->pubKeySer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 10 |  |         return $this->cache[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param KeyToScriptDataFactory $scriptFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return ScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |     public function getP2shFactory(KeyToScriptDataFactory $scriptFactory): ScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |         $key = sprintf("%s|%s", ScriptType::P2SH, $scriptFactory->getScriptType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |         if (!array_key_exists($key, $this->cache)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |             $this->cache[$key] = new P2shScriptDecorator($scriptFactory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 4 |  |         return $this->cache[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param KeyToScriptDataFactory $scriptFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return ScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 6 |  |     public function getP2wshFactory(KeyToScriptDataFactory $scriptFactory): ScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 6 |  |         $key = sprintf("%s|%s", ScriptType::P2WSH, $scriptFactory->getScriptType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 6 |  |         if (!array_key_exists($key, $this->cache)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 6 |  |             $this->cache[$key] = new P2wshScriptDecorator($scriptFactory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |         return $this->cache[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param KeyToScriptDataFactory $scriptFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return ScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\DisallowedScriptDataFactoryException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |     public function getP2shP2wshFactory(KeyToScriptDataFactory $scriptFactory): ScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         $key = sprintf("%s|%s|%s", ScriptType::P2SH, ScriptType::P2WSH, $scriptFactory->getScriptType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |         if (!array_key_exists($key, $this->cache)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |             $this->cache[$key] = new P2shP2wshScriptDecorator($scriptFactory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |         return $this->cache[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |  |