Esempio n. 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);
}
<?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;
 }
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);
}