Skip to content

Commit

Permalink
- First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Buedo committed Aug 19, 2019
1 parent 5f38f19 commit 004edaf
Show file tree
Hide file tree
Showing 24 changed files with 2,511 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/bin/**
/build/cache/
/build/docs/
/build/dist/
/build/logs/
/docs/_build/
/node_modules/
/vendor/
/.settings/
.project
.buildpath
composer.lock
composer.phar
.DS_Store
build/.DS_Store
docs/.DS_Store
examples/.DS_Store
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "bitpay/key-utils",
"description": "BitPay Utils pack for cryptography",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Antonio Buedo",
"email": "sales-engineering@bitpay.com"
}
],
"autoload": {
"psr-4": {
"": "src/"
}
},
"require": {
"ext-bcmath": "*",
"ext-openssl": "*",
"ext-curl": "*",
"ext-json": "*",
"ext-iconv": "*",
"ext-gmp": "*"
}
}
87 changes: 87 additions & 0 deletions examples.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

use BitPayKeyUtils\KeyHelper\PrivateKey;
use BitPayKeyUtils\Storage\EncryptedFilesystemStorage;

require __DIR__ . '/vendor/autoload.php';

/**
* Generate new private key for every new merchant.
* Make sure you provide an easy recognizable name for each private key/Merchant
*
* WARNING: It is EXTREMELY IMPORTANT to place this key files in a very SECURE location
**/
$privateKey = new PrivateKey(__DIR__ . '/secure/SecurePathPlusYourClientName.key');
$storageEngine = new EncryptedFilesystemStorage('YourMasterPassword');

try {
// Use the EncryptedFilesystemStorage to load the Merchant's encrypted private key with the Master Password.
$privateKey = $storageEngine->load(__DIR__ . '/secure/SecurePathPlusYourClientName.key');
} catch (Exception $ex) {
// Check if the loaded keys is a valid key
if (!$privateKey->isValid()) {
$privateKey->generate();
}

// Encrypt and store it securely.
// This Master password could be one for all keys or a different one for each merchant
$storageEngine->persist($privateKey);
}

/**
* Generate the public key from the private key every time (no need to store the public key).
**/
try {
$publicKey = $privateKey->getPublicKey();
} catch (Exception $ex) {
echo $ex->getMessage();
}

/**
* Derive the SIN from the public key.
**/
$sin = $publicKey->getSin()->__toString();

/**
* Use the SIN to request a pairing code and token.
* The pairing code has to be approved in the BitPay Dashboard
* THIS is just a cUrl example, which explains how to use the key pair for signing requests
**/
$resourceUrl = 'https://test.bitpay.com/tokens';

$facade = 'merchant';

$postData = json_encode([
'id' => $sin,
'facade' => $facade
]);

$curlCli = curl_init($resourceUrl);

curl_setopt($curlCli, CURLOPT_HTTPHEADER, [
'x-accept-version: 2.0.0',
'Content-Type: application/json',
'x-identity' => $publicKey->__toString(),
'x-signature' => $privateKey->sign($resourceUrl . $postData),
]);

curl_setopt($curlCli, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curlCli, CURLOPT_POSTFIELDS, stripslashes($postData));
curl_setopt($curlCli, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($curlCli);
$resultData = json_decode($result, TRUE);
curl_close($curlCli);

if (array_key_exists('error', $resultData)) {
echo $resultData['error'];
exit;
}

/**
* Example of a pairing Code returned from the BitPay API
* which needs to be APPROVED on the BitPay Dashboard before being able to use it.
**/
echo $resultData['data'][0]['pairingCode'];

/** End of request **/
111 changes: 111 additions & 0 deletions src/BitPayKeyUtils/KeyHelper/Key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace BitPayKeyUtils\KeyHelper;

use BitPayKeyUtils\Util\Point;

/**
* Abstract object that is used for Public, Private, and SIN keys
*
* @package Bitcore
*/
abstract class Key extends Point implements KeyInterface
{
/**
* @var string
*/
protected $hex;

/**
* @var string
*/
protected $dec;

/**
* @var string
*/
protected $id;

/**
* @param string $id
*/
public function __construct($id = null)
{
$this->id = $id;
}

/**
* Returns a new instance of self.
*
* @param string $id
* @return KeyInterface
*/
public static function create($id = null)
{
$class = get_called_class();

return new $class($id);
}

/**
* @return string
*/
public function getId()
{
return $this->id;
}

/**
* @return string
*/
public function getHex()
{
return $this->hex;
}

/**
* @return string
*/
public function getDec()
{
return $this->dec;
}

/**
* @inheritdoc
*/
public function serialize()
{
return serialize(
array(
$this->id,
$this->x,
$this->y,
$this->hex,
$this->dec,
)
);
}

/**
* @inheritdoc
*/
public function unserialize($data)
{
list(
$this->id,
$this->x,
$this->y,
$this->hex,
$this->dec
) = unserialize($data);
}

/**
* @return boolean
*/
public function isGenerated()
{
return (!empty($this->hex));
}
}
21 changes: 21 additions & 0 deletions src/BitPayKeyUtils/KeyHelper/KeyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace BitPayKeyUtils\KeyHelper;

use Serializable;

/**
* @package Bitcore
*/
interface KeyInterface extends Serializable
{
/**
* Generates a new key
*/
public function generate();

/**
* @return boolean
*/
public function isValid();
}
Loading

0 comments on commit 004edaf

Please sign in to comment.