Beispiel #1
0
function generateAndPersistKeys()
{
    $privateKey = new \Bitpay\PrivateKey('/tmp/bitpay.pri');
    $privateKey->generate();
    $publicKey = new \Bitpay\PublicKey('/tmp/bitpay.pub');
    $publicKey->setPrivateKey($privateKey);
    $publicKey->generate();
    $sinKey = new \Bitpay\SinKey('/tmp/sin.key');
    $sinKey->setPublicKey($publicKey);
    $sinKey->generate();
    //Persist Keys
    $storageEngine = new \Bitpay\Storage\EncryptedFilesystemStorage('YourTopSecretPassword');
    $storageEngine->persist($privateKey);
    $storageEngine->persist($publicKey);
    return array($privateKey, $publicKey, $sinKey);
}
 public function testConfigAbleToPersistAndLoadKeys()
 {
     $root = vfsStream::setup($this->temp_path_root);
     $bitpay = new \Bitpay\Bitpay(array('bitpay' => array('network' => 'testnet', 'private_key' => vfsStream::url($this->temp_path_pri), 'public_key' => vfsStream::url($this->temp_path_pub))));
     $pri = new \Bitpay\PrivateKey(vfsStream::url($this->temp_path_pri));
     $pri->generate();
     $pub = new \Bitpay\PublicKey(vfsStream::url($this->temp_path_pub));
     $pub->setPrivateKey($pri)->generate();
     /**
      * Save keys to the filesystem
      */
     $storage = $bitpay->get('key_storage');
     $storage->persist($pri);
     $storage->persist($pub);
     /**
      * This will load the keys, if you have not already persisted them, than
      * this WILL throw an Exception since this will load the keys from the
      * storage class
      */
     $pri = $bitpay->get('private_key');
     $pub = $bitpay->get('public_key');
 }
<?php

/**
 * Copyright (c) 2014-2015 BitPay
 */
require __DIR__ . '/../vendor/autoload.php';
/**
 * The first argument can either be the path to the key or can be
 * some other unique value. This is a basic example, and more advanced
 * examples can be used to store keys in the database or other places. In
 * this example, however, the keys are not persisted on disk or in a database.
 */
$private = new \Bitpay\PrivateKey('/tmp/private.key');
$public = new \Bitpay\PublicKey('/tmp/public.key');
$sin = new \Bitpay\SinKey('/tmp/sin.key');
// Generate Private Key values
$private->generate();
// Generate Public Key values
$public->setPrivateKey($private);
$public->generate();
// Generate Sin Key values
$sin->setPublicKey($public);
$sin->generate();
printf("Public Key:  %s\n", $public);
printf("Private Key: %s\n", $private);
printf("Sin Key:     %s\n\n", $sin);
/**
 * NOTE: You MUST save your keypairs and not regenerate them once you have already
 * generated a pair and have paired them with the BitPay's API. To see how to
 * persist keys to the filesystem, please see the SaveKeypairsToFilesystem.php
 */
 function ajax_bitpay_pair_code()
 {
     $nonce = $_POST['pairNonce'];
     if (!wp_verify_nonce($nonce, 'bitpay-pair-nonce')) {
         die('Unauthorized!');
     }
     if (current_user_can('manage_options')) {
         if (true === isset($_POST['pairing_code']) && trim($_POST['pairing_code']) !== '') {
             // Validate the Pairing Code
             $pairing_code = trim($_POST['pairing_code']);
         } else {
             wp_send_json_error("Pairing Code is required");
             return;
         }
         if (!preg_match('/^[a-zA-Z0-9]{7}$/', $pairing_code)) {
             wp_send_json_error("Invalid Pairing Code");
             return;
         }
         // Validate the Network
         $network = $_POST['network'] === 'livenet' ? 'livenet' : 'testnet';
         // Generate Private Key
         $key = new \Bitpay\PrivateKey();
         if (true === empty($key)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a PrivateKey object. Cannot continue!');
         }
         $key->generate();
         // Generate Public Key
         $pub = new \Bitpay\PublicKey();
         if (true === empty($pub)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a PublicKey object. Cannot continue!');
         }
         $pub->setPrivateKey($key);
         $pub->generate();
         // Get SIN Format
         $sin = new \Bitpay\SinKey();
         if (true === empty($sin)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a SinKey object. Cannot continue!');
         }
         $sin->setPublicKey($pub);
         $sin->generate();
         // Create an API Client
         $client = new \Bitpay\Client\Client();
         if (true === empty($client)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a Client object. Cannot continue!');
         }
         if ($network === 'livenet') {
             $client->setNetwork(new \Bitpay\Network\Livenet());
         } else {
             $client->setNetwork(new \Bitpay\Network\Testnet());
         }
         $curlAdapter = new \Bitpay\Client\Adapter\CurlAdapter();
         if (true === empty($curlAdapter)) {
             throw new \Exception('The Bitpay payment plugin was called to process a pairing code but could not instantiate a CurlAdapter object. Cannot continue!');
         }
         $client->setAdapter($curlAdapter);
         $client->setPrivateKey($key);
         $client->setPublicKey($pub);
         // Sanitize label
         $label = preg_replace('/[^a-zA-Z0-9 \\-\\_\\.]/', '', get_bloginfo());
         $label = substr('WooCommerce - ' . $label, 0, 59);
         try {
             $token = $client->createToken(array('id' => (string) $sin, 'pairingCode' => $pairing_code, 'label' => $label));
         } catch (\Exception $e) {
             wp_send_json_error($e->getMessage());
             return;
         }
         update_option('woocommerce_bitpay_key', bitpay_encrypt($key));
         update_option('woocommerce_bitpay_pub', bitpay_encrypt($pub));
         update_option('woocommerce_bitpay_sin', (string) $sin);
         update_option('woocommerce_bitpay_token', bitpay_encrypt($token));
         update_option('woocommerce_bitpay_label', $label);
         update_option('woocommerce_bitpay_network', $network);
         wp_send_json(array('sin' => (string) $sin, 'label' => $label, 'network' => $network));
     }
     exit;
 }
Beispiel #5
0
 /**
  * Generates a new set of keys to interact with BitPay's API
  * @return PrivateKey
  */
 public function generateId()
 {
     // Generate new keys
     $private_key = new Bitpay\PrivateKey();
     $private_key->generate();
     $public_key = $private_key->getPublicKey();
     // Persist the keys to the database
     $this->setting('private_key', $this->encryption->encrypt(serialize($private_key)));
     $this->setting('public_key', $this->encryption->encrypt(serialize($public_key)));
     $this->setting('connection', 'disconnected');
     $this->setting('token', null);
     return $private_key;
 }
Beispiel #6
0
/**
 * Copyright (c) 2014-2015 BitPay
 *
 * 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();`
function generate_keys()
{
    $private = new \Bitpay\PrivateKey();
    $public = new \Bitpay\PublicKey();
    $sin = new \Bitpay\SinKey();
    try {
        // Generate Private Key values
        $private->generate();
        // Generate Public Key values
        $public->setPrivateKey($private);
        $public->generate();
        // Generate Sin Key values
        $sin->setPublicKey($public);
        $sin->generate();
    } catch (\Exception $e) {
        debuglog('[Error] In Bitpay plugin, generate_keys() function on line ' . $e->getLine() . ', with the error "' . $e->getMessage() . '" .');
        throw $e;
    }
    return array($private, $public, $sin);
}
<?php

/**
 * Copyright (c) 2014-2015 BitPay
 */
/**
 * This example show how to persist and load keys from the filesystem. Any key
 * can be persisted and loaded and the same way.
 */
require __DIR__ . '/../vendor/autoload.php';
// Create and generate
$pri = new \Bitpay\PrivateKey('/tmp/private.key');
$pri->generate();
// Use the key manager to persist key
$manager = new \Bitpay\KeyManager(new \Bitpay\Storage\EncryptedFilesystemStorage('password'));
// Saved to /tmp/private.key
$manager->persist($pri);
// Load from /tmp/private.key
$key = $manager->load('/tmp/private.key');
printf("Private Key: %s\n", $key);