| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace BitWasp\Bitcoin\Key\KeyToScript\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BitWasp\Bitcoin\Script\ScriptFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BitWasp\Bitcoin\Script\ScriptType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use BitWasp\Bitcoin\Transaction\Factory\SignData; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | class MultisigScriptDataFactory extends KeyToScriptDataFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @var int | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     private $numSigners; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @var int | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     private $numKeys; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @var bool | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     private $sortKeys; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 35 |  |     public function __construct(int $numSigners, int $numKeys, bool $sortKeys, PublicKeySerializerInterface $pubKeySerializer = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 35 |  |         $this->numSigners = $numSigners; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 35 |  |         $this->numKeys = $numKeys; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 35 |  |         $this->sortKeys = $sortKeys; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 35 |  |         parent::__construct($pubKeySerializer); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 | 35 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 28 |  |     public function getScriptType(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 28 |  |         return ScriptType::MULTISIG; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param PublicKeyInterface ...$keys | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @return ScriptAndSignData | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 29 |  |     protected function convertKeyToScriptData(PublicKeyInterface ...$keys): ScriptAndSignData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 29 |  |         if (count($keys) !== $this->numKeys) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |             throw new \InvalidArgumentException("Incorrect number of keys"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 29 |  |         $keyBuffers = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 29 |  |         for ($i = 0; $i < $this->numKeys; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 29 |  |             $keyBuffers[] = $this->pubKeySerializer->serialize($keys[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 29 |  |         return new ScriptAndSignData( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 29 |  |             ScriptFactory::scriptPubKey()->multisigKeyBuffers($this->numSigners, $keyBuffers, $this->sortKeys), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 29 |  |             new SignData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |  |