| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace BitWasp\Bitcoin\Key\Deterministic; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BitWasp\Bitcoin\Address\BaseAddressCreator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BitWasp\Bitcoin\Bitcoin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Key\KeyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PrivateKeyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use BitWasp\Bitcoin\Crypto\Hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use BitWasp\Bitcoin\Exceptions\InvalidDerivationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use BitWasp\Bitcoin\Key\KeyToScript\ScriptDataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use BitWasp\Bitcoin\Network\NetworkInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\Base58ExtendedKeySerializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use BitWasp\Bitcoin\Util\IntRange; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use BitWasp\Buffertools\Buffer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use BitWasp\Buffertools\BufferInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class HierarchicalKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var EcAdapterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $ecAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $depth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $parentFingerprint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private $sequence; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @var BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private $chainCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @var KeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @var ScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private $scriptDataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @var ScriptAndSignData|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     private $scriptAndSignData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param EcAdapterInterface $ecAdapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param ScriptDataFactory $scriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param int $depth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param int $parentFingerprint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param int $sequence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param BufferInterface $chainCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param KeyInterface $key | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 79 |  |     public function __construct(EcAdapterInterface $ecAdapter, ScriptDataFactory $scriptDataFactory, int $depth, int $parentFingerprint, int $sequence, BufferInterface $chainCode, KeyInterface $key) | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 79 |  |         if ($depth < 0 || $depth > IntRange::U8_MAX) { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             throw new \InvalidArgumentException('Invalid depth for BIP32 key, must be in range [0 - 255] inclusive'); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 79 |  |         if ($parentFingerprint < 0 || $parentFingerprint > IntRange::U32_MAX) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             throw new \InvalidArgumentException('Invalid fingerprint for BIP32 key, must be in range [0 - (2^31)-1] inclusive'); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 79 |  |         if ($sequence < 0 || $sequence > IntRange::U32_MAX) { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |             throw new \InvalidArgumentException('Invalid sequence for BIP32 key, must be in range [0 - (2^31)-1] inclusive'); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 79 |  |         if ($chainCode->getSize() !== 32) { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             throw new \RuntimeException('Chaincode should be 32 bytes'); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 79 |  |         if (!$key->isCompressed()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |             throw new \InvalidArgumentException('A HierarchicalKey must always be compressed'); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 78 |  |         $this->ecAdapter = $ecAdapter; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 78 |  |         $this->depth = $depth; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 78 |  |         $this->sequence = $sequence; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 78 |  |         $this->parentFingerprint = $parentFingerprint; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 78 |  |         $this->chainCode = $chainCode; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 78 |  |         $this->key = $key; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 78 |  |         $this->scriptDataFactory = $scriptDataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 78 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Return the depth of this key. This is limited to 256 sequential derivations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 34 |  |     public function getDepth(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 34 |  |         return $this->depth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Get the sequence number for this address. Hardened keys are | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * created with sequence > 0x80000000. a sequence number lower | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * than this can be derived with the public key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 28 |  |     public function getSequence(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 28 |  |         return $this->sequence; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * Get the fingerprint of the parent key. For master keys, this is 00000000. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 30 |  |     public function getFingerprint(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 30 |  |         if ($this->getDepth() === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 14 |  |             return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 26 |  |         return $this->parentFingerprint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * Return the fingerprint to be used for child keys. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 36 |  |     public function getChildFingerprint(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 36 |  |         $pubKeyHash = $this->getPublicKey()->getPubKeyHash(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 36 |  |         return (int) $pubKeyHash->slice(0, 4)->getInt(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * Return the chain code - a deterministic 'salt' for HMAC-SHA512 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * in child derivations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 28 |  |     public function getChainCode(): BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 28 |  |         return $this->chainCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @return PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 42 |  |     public function getPrivateKey(): PrivateKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 42 |  |         if ($this->key->isPrivate()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             /** @var PrivateKeyInterface $key */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 40 |  |             $key = $this->key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 40 |  |             return $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 2 |  |         throw new \RuntimeException('Unable to get private key, not known'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * Get the public key the private key or public key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      * @return PublicKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 50 |  |     public function getPublicKey(): PublicKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 50 |  |         if ($this->isPrivate()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 36 |  |             return $this->getPrivateKey()->getPublicKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             /** @var PublicKeyInterface $key */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 33 |  |             $key = $this->key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 33 |  |             return $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @return HierarchicalKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 | 21 |  |     public function withoutPrivateKey(): HierarchicalKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 21 |  |         $clone = clone $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 21 |  |         $clone->key = $clone->getPublicKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 21 |  |         return $clone; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * @return ScriptDataFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 30 |  |     public function getScriptDataFactory() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 | 30 |  |         return $this->scriptDataFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |      * @return \BitWasp\Bitcoin\Key\KeyToScript\ScriptAndSignData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 | 22 |  |     public function getScriptAndSignData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 | 22 |  |         if (null === $this->scriptAndSignData) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 | 22 |  |             $this->scriptAndSignData = $this->scriptDataFactory->convertKey($this->key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 22 |  |         return $this->scriptAndSignData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |      * @param BaseAddressCreator $addressCreator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |      * @return \BitWasp\Bitcoin\Address\Address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 | 16 |  |     public function getAddress(BaseAddressCreator $addressCreator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 | 16 |  |         return $this->getScriptAndSignData()->getAddress($addressCreator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |      * Return whether this is a private key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 | 54 |  |     public function isPrivate(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 | 54 |  |         return $this->key->isPrivate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |      * Return whether the key is hardened | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 | 2 |  |     public function isHardened(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 | 2 |  |         return ($this->sequence >> 31) === 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |      * Create a buffer containing data to be hashed hashed to yield the child offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |      * @param int $sequence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |      * @return BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 | 43 |  |     public function getHmacSeed(int $sequence): BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 | 43 |  |         if ($sequence < 0 || $sequence > IntRange::U32_MAX) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 | 6 |  |             throw new \InvalidArgumentException("Sequence is outside valid range, must be >= 0 && <= (2^31)-1"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 | 37 |  |         if (($sequence >> 31) === 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 | 32 |  |             if ($this->isPrivate() === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 | 2 |  |                 throw new \Exception("Can't derive a hardened key without the private key"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 | 30 |  |             $data = "\x00{$this->getPrivateKey()->getBinary()}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 | 35 |  |             $data = $this->getPublicKey()->getBinary(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 | 35 |  |         return new Buffer($data . pack("N", $sequence)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |      * Derive a child key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |      * @param int $sequence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |      * @return HierarchicalKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |      * @throws \BitWasp\Bitcoin\Exceptions\InvalidDerivationException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 | 40 |  |     public function deriveChild(int $sequence): HierarchicalKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 | 40 |  |         $nextDepth = $this->depth + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 | 40 |  |         if ($nextDepth > 255) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |             throw new \InvalidArgumentException('Invalid depth for BIP32 key, cannot exceed 255'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 | 40 |  |         $hash = Hash::hmac('sha512', $this->getHmacSeed($sequence), $this->chainCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 35 |  |         $offset = $hash->slice(0, 32); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 | 35 |  |         $chain = $hash->slice(32); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 | 35 |  |         if (!$this->ecAdapter->validatePrivateKey($offset)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 1 |  |             throw new InvalidDerivationException("Derived invalid key for index {$sequence}, use next index"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 | 34 |  |         $key = $this->isPrivate() ? $this->getPrivateKey() : $this->getPublicKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 34 |  |         $key = $key->tweakAdd($offset->getGmp()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 | 34 |  |         return new HierarchicalKey( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 | 34 |  |             $this->ecAdapter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 | 34 |  |             $this->scriptDataFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 | 34 |  |             $nextDepth, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 | 34 |  |             $this->getChildFingerprint(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 | 34 |  |             $sequence, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 | 34 |  |             $chain, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 | 34 |  |             $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |      * Decodes a BIP32 path into actual 32bit sequence numbers and derives the child key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |      * @return HierarchicalKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 | 26 |  |     public function derivePath(string $path): HierarchicalKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 | 26 |  |         if ($path == "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 | 26 |  |             throw new \InvalidArgumentException("Path cannot be empty!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 | 26 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 | 26 |  |         $sequences = new HierarchicalKeySequence(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 | 26 |  |         if ($this->depth == 0 && ($path[0] == 'm' || $path[0] == 'M')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |             list($retainPrivate, $parts) = $sequences->decodeAbsolute($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 | 26 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |             $retainPrivate = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |             $parts = $sequences->decodeRelative($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |         $numParts = count($parts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |         $key = $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |         for ($i = 0; $i < $numParts; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |                 $key = $key->deriveChild($parts[$i]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 | 26 |  |             } catch (InvalidDerivationException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |                 if ($i === $numParts - 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |                     throw new InvalidDerivationException($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |                     throw new InvalidDerivationException("Invalid derivation for non-terminal index: cannot use this path!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 | 18 |  |         if (!$retainPrivate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |             $key = $key->withoutPrivateKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 | 18 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |         return $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 | 18 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 | 18 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |      * Serializes the instance according to whether it wraps a private or public key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |      * @param NetworkInterface $network | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |     public function toExtendedKey(NetworkInterface $network = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |         $network = $network ?: Bitcoin::getNetwork(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |         $extendedSerializer = new Base58ExtendedKeySerializer(new ExtendedKeySerializer($this->ecAdapter)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 | 18 |  |         $extended = $extendedSerializer->serialize($network, $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |         return $extended; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 | 2 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |      * Explicitly serialize as a private key. Throws an exception if | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 | 16 |  |      * the key isn't private. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |      * @param NetworkInterface $network | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |     public function toExtendedPrivateKey(NetworkInterface $network = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |         if (!$this->isPrivate()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |             throw new \LogicException('Cannot create extended private key from public'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 | 13 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 | 13 |  |         return $this->toExtendedKey($network); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |      * Explicitly serialize as a public key. This will always work. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |      * @param NetworkInterface $network | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |     public function toExtendedPublicKey(NetworkInterface $network = null): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |         return $this->withoutPrivateKey()->toExtendedKey($network); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 394 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 395 |  |  |  |