| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BitWasp\Bitcoin\Address; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BitWasp\Bitcoin\Base58; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use BitWasp\Bitcoin\Bitcoin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BitWasp\Bitcoin\Key\PublicKeyFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BitWasp\Bitcoin\Network\NetworkInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BitWasp\Bitcoin\Script\Classifier\OutputClassifier; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BitWasp\Bitcoin\Script\P2shScript; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use BitWasp\Bitcoin\Script\ScriptInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use BitWasp\Bitcoin\Script\ScriptType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use BitWasp\Bitcoin\Script\WitnessProgram; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BitWasp\Bitcoin\Script\WitnessScript; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use BitWasp\Bitcoin\SegwitBech32; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use BitWasp\Buffertools\BufferInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class AddressFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * Returns a pay-to-pubkey-hash address for the given public key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param KeyInterface $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return PayToPubKeyHashAddress | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 44 |  |     public static function p2pkh(KeyInterface $key) | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 44 |  |         return new PayToPubKeyHashAddress($key->getPubKeyHash()); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Takes the $p2shScript and generates the scriptHash address. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param ScriptInterface $p2shScript | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return ScriptHashAddress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 20 |  |     public static function p2sh(ScriptInterface $p2shScript) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 20 |  |         return new ScriptHashAddress($p2shScript->getScriptHash()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param WitnessProgram $wp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return SegwitAddress | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 12 |  |     public static function fromWitnessProgram(WitnessProgram $wp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 12 |  |         return new SegwitAddress($wp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param ScriptInterface $outputScript | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return AddressInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 52 |  |     public static function fromOutputScript(ScriptInterface $outputScript) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 52 |  |         if ($outputScript instanceof P2shScript || $outputScript instanceof WitnessScript) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             throw new \RuntimeException("P2shScript & WitnessScript's are not accepted by fromOutputScript"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 52 |  |         $wp = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 52 |  |         if ($outputScript->isWitness($wp)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             /** @var WitnessProgram $wp */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 12 |  |             return new SegwitAddress($wp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 40 |  |         $decode = (new OutputClassifier())->decode($outputScript); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 40 |  |         switch ($decode->getType()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 40 |  |             case ScriptType::P2PKH: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 /** @var BufferInterface $solution */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 20 |  |                 return new PayToPubKeyHashAddress($decode->getSolution()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 22 |  |             case ScriptType::P2SH: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 /** @var BufferInterface $solution */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 18 |  |                 return new ScriptHashAddress($decode->getSolution()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 4 |  |                 throw new \RuntimeException('Script type is not associated with an address'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param NetworkInterface $network | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return AddressInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 68 |  |     public static function fromString($address, NetworkInterface $network = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 68 |  |         $network = $network ?: Bitcoin::getNetwork(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 68 |  |             $data = Base58::decodeCheck($address); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 56 |  |             $prefixByte = $data->slice(0, 1)->getHex(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 56 |  |             if ($prefixByte === $network->getP2shByte()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 20 |  |                 return new ScriptHashAddress($data->slice(1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 36 |  |             } else if ($prefixByte === $network->getAddressByte()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 36 |  |                 return new PayToPubKeyHashAddress($data->slice(1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 12 |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             // continue on for Bech32 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 14 |  |             return new SegwitAddress(SegwitBech32::decode($address, $network)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             // continue on | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         throw new \InvalidArgumentException("Address not recognized"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param string $address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param NetworkInterface $network | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\Base58ChecksumFailure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 46 |  |     public static function isValidAddress($address, NetworkInterface $network = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 46 |  |             self::fromString($address, $network); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 44 |  |             $is_valid = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  |             $is_valid = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 46 |  |         return $is_valid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * Following a loose definition of 'associated', returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * the current script types, and a PayToPubKeyHash address for P2PK. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param ScriptInterface $script | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return AddressInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 4 |  |     public static function getAssociatedAddress(ScriptInterface $script) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 4 |  |         $classifier = new OutputClassifier(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 4 |  |         $decode = $classifier->decode($script); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 4 |  |         if ($decode->getType() === ScriptType::P2PK) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  |             $pubKey = PublicKeyFactory::fromHex($decode->getSolution()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 2 |  |             return new PayToPubKeyHashAddress($pubKey->getPubKeyHash()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 4 |  |             return self::fromOutputScript($script); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  |