/** * Generates an uncompressed and compressed EC public key. * * @param \Bitpay\PrivateKey $privateKey * @return \Bitpay\PublicKey * @throws \Exception */ public function generate(PrivateKey $privateKey = null) { if ($privateKey instanceof PrivateKey) { $this->setPrivateKey($privateKey); } if (!empty($this->hex)) { return $this; } if (is_null($this->privateKey)) { throw new \Exception('Please `setPrivateKey` before you generate a public key'); } if (!$this->privateKey->isGenerated()) { $this->privateKey->generate(); } if (!$this->privateKey->isValid()) { throw new \Exception('Private Key is invalid and cannot be used to generate a public key'); } $point = new Point('0x' . substr(Secp256k1::G, 2, 64), '0x' . substr(Secp256k1::G, 66, 64)); $R = Util::doubleAndAdd('0x' . $this->privateKey->getHex(), $point); $RxHex = Util::encodeHex($R->getX()); $RyHex = Util::encodeHex($R->getY()); $RxHex = str_pad($RxHex, 64, '0', STR_PAD_LEFT); $RyHex = str_pad($RyHex, 64, '0', STR_PAD_LEFT); $this->x = $RxHex; $this->y = $RyHex; $this->hex = sprintf('%s%s', $RxHex, $RyHex); $this->dec = Util::decodeHex($this->hex); return $this; }
/** * @param RequestInterface $request * @throws \Exception */ protected function addSignatureHeader(RequestInterface $request) { if (null === $this->privateKey) { throw new \Exception('Please set your Private Key'); } if (true == property_exists($this->network, 'isPortRequiredInUrl')) { if ($this->network->isPortRequiredInUrl === true) { $url = $request->getUriWithPort(); } else { $url = $request->getUri(); } } else { $url = $request->getUri(); } $message = sprintf('%s%s', $url, $request->getBody()); $signature = $this->privateKey->sign($message); $request->setHeader('x-signature', $signature); }
public function createBitPayKeys() { if (_BIT_PAY_PRODUCTION_) { $privateKey = new Bitpay\PrivateKey('/tmp/bitpay.pri'); } else { $privateKey = new Bitpay\PrivateKey('/tmp/bitpaydev.pri'); } $privateKey->generate(); if (_BIT_PAY_PRODUCTION_) { $publicKey = new Bitpay\PublicKey('/tmp/bitpay.pub'); } else { $publicKey = new Bitpay\PublicKey('/tmp/bitpaydev.pub'); } $publicKey->setPrivateKey($privateKey); $publicKey->generate(); $storageEngine = new Bitpay\Storage\FilesystemStorage(); $storageEngine->persist($privateKey); $storageEngine->persist($publicKey); }
* * 001 - Generate and Persist Keys * * Requirements: * - Basic PHP knowledge */ // If you have not already done so, please run `composer.phar install` require __DIR__ . '/../../vendor/autoload.php'; /** * Start by creating a PrivateKey object */ $privateKey = new \Bitpay\PrivateKey('/tmp/bitpay.pri'); // Generate a random number $privateKey->generate(); // You can generate a private key with only one line of code like so $privateKey = \Bitpay\PrivateKey::create('/tmp/bitpay.pri')->generate(); // NOTE: This has overridden the previous $privateKey variable, although its // not an issue in this case since we have not used this key for // anything yet. /** * Once we have a private key, a public key is created from it. */ $publicKey = new \Bitpay\PublicKey('/tmp/bitpay.pub'); // Inject the private key into the public key $publicKey->setPrivateKey($privateKey); // Generate the public key $publicKey->generate(); // NOTE: You can again do all of this with one line of code like so: // `$publicKey = \Bitpay\PublicKey::create('/tmp/bitpay.pub')->setPrivateKey($privateKey)->generate();` /** * Now that you have a private and public key generated, you will need to store