/** * @param Buffer $entropy * @param EcAdapterInterface $ecAdapter * @return HierarchicalKey */ public static function fromEntropy(Buffer $entropy, EcAdapterInterface $ecAdapter = null) { $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter(); $hash = Hash::hmac('sha512', $entropy, new Buffer('Bitcoin seed', null, $ecAdapter->getMath())); return new HierarchicalKey($ecAdapter, 0, 0, 0, $hash->slice(32, 32)->getInt(), PrivateKeyFactory::fromHex($hash->slice(0, 32)->getHex(), true, $ecAdapter)); }
/** * Derive a child key * * @param $sequence * @return HierarchicalKey * @throws \Exception */ public function deriveChild($sequence) { $chain = Buffer::hex($this->ecAdapter->getMath()->decHex($this->getChainCode()), 32); $hash = Hash::hmac('sha512', $this->getHmacSeed($sequence), $chain); $offset = $hash->slice(0, 32); $chain = $hash->slice(32); if (false === $this->ecAdapter->validatePrivateKey($offset)) { return $this->deriveChild($sequence + 1); } return new HierarchicalKey($this->ecAdapter, $this->getDepth() + 1, $this->getChildFingerprint(), $sequence, $chain->getInt(), $this->isPrivate() ? $this->ecAdapter->privateKeyAdd($this->getPrivateKey(), $offset->getInt()) : $this->ecAdapter->publicKeyAdd($this->getPublicKey(), $offset->getInt())); }