Ejemplo 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);
}
Ejemplo n.º 2
0
 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;
 }
Ejemplo n.º 5
0
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
 * them somewhere. This optioin is up to you and how you store them is up to
 * you. Please be aware that you MUST store the private key with some type
 * of security. If the private key is comprimised you will need to repeat this
 * process.
 */
/**
 * It's recommended that you use the EncryptedFilesystemStorage engine to persist your
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);
}
Ejemplo n.º 7
0
/**
 * WARNING - This example will NOT work until you have generated your public
 * keys and also see the documentation on how to save those keys.
 *
 * Also please be aware that you CANNOT create an invoice until you have paired
 * the keys and received a token back. The token is usesd with the request.
 */
require __DIR__ . '/../vendor/autoload.php';
$time = gmdate("Y-m-d\\TH:i:s\\.", 1414691179) . "000Z";
$token = new \Bitpay\Token();
$token->setFacade('payroll')->setToken('<your payroll facade-enable token>');
//this is a special api that requires a explicit payroll relationship with BitPay
$instruction1 = new \Bitpay\PayoutInstruction();
$instruction1->setAmount(100)->setAddress('2NA5EVH9HHHhM5RxSEWf54gP4v397EmFTxi')->setLabel('Paying Chris');
$payout = new \Bitpay\Payout();
$payout->setEffectiveDate($time)->setAmount(100)->setCurrency(new \Bitpay\Currency('USD'))->setPricingMethod('bitcoinbestbuy')->setReference('a reference, can be json')->setNotificationEmail('*****@*****.**')->setNotificationUrl('https://example.com/ipn.php')->setToken($token)->addInstruction($instruction1);
$private = new \Bitpay\PrivateKey();
$private->setHex('662be90968bc659873d723374213fa5bf7a30c24f0f0713aa798eb7daa7230fc');
//this is your private key in some form (see GetKeys.php)
$public = new \Bitpay\PublicKey();
$public->generate($private);
$network = new \Bitpay\Network\Testnet();
$adapter = new \Bitpay\Client\Adapter\CurlAdapter();
$bitpay = new \Bitpay\Bitpay();
$client = new \Bitpay\Client\Client();
$client->setPrivateKey($private);
$client->setPublicKey($public);
$client->setNetwork($network);
$client->setAdapter($adapter);
$client->createPayout($payout);
print_r($payout);
Ejemplo n.º 8
0
<?php

require __DIR__ . '/../vendor/autoload.php';
$storageEngine = new \Bitpay\Storage\FilesystemStorage();
$private = $storageEngine->load('/tmp/private.key');
$public = new \Bitpay\PublicKey('/tmp/public.key');
$public->setPrivateKey($private);
$public->generate();
printf("Public Key:  %s\n", $public);
printf("Private Key: %s\n", $private);
$message = 'https://test.bitpay.com/subscriptions{"schedule":"weekly","token":"some token","billData":{"currency":"USD","price":"2.00","quantity":1}}';
$signedMessage = $private->sign($message);
print_r('message to be signed:: ' . $message . "\n");
print_r('signed message:: ' . $signedMessage);
Ejemplo n.º 9
0
<?php

/**
 * Copyright (c) 2014-2016 BitPay
 */
require __DIR__ . '/../vendor/autoload.php';
$private = new \Bitpay\PrivateKey();
//if you've got a hex-encoded private key string, you can use it to create a private key
$private->setHex('662be90968bc659873d723374213fa5bf7a30c24f0f0713aa798eb7daa7230fc');
$public = new \Bitpay\PublicKey();
$public->generate($private);
$sin = $public->getSin();
printf("Public Key:  %s\n", $public);
printf("Private Key: %s\n", $private);
printf("Sin Key:     %s\n\n", $sin);
$keypair = array($private->getHex(), $public->getHex());
printf("PEM keypair:  %s\n", $private->pemEncode($keypair));
// -or- if you've got a PEM-encoded text file containing your key pair, we can use this
$keys = file_get_contents(getenv('HOME') . '/.php-bitpay-client/key.pem');
if (isset($keys) && strlen($keys) > 0) {
    $keys = chop($keys);
    $private = new \Bitpay\PrivateKey();
    $private->setHex($private->pemDecode($keys)['private_key']);
    printf("\n\n");
    printf("Public Key:  %s\n", $private->getPublicKey());
    printf("Private Key: %s\n", $private);
    printf("Sin Key:     %s\n\n", $private->getPublicKey()->getSin());
}