Commit 7231d3ce authored by beardedwarrior's avatar beardedwarrior

improved the jsonRPCClient

parent 14601bbf
<?php
namespace Monero\Contracts;
/**
* Interface WalletManager
* @package Monero
*/
interface WalletManager
{
/**
* Gets the balance
*
* @return int the overall value after inputs unlock
*/
public function balance();
/**
* Gets the unlocked balance
*
* @return int the spendable balance
*/
public function unlockedBalance();
/**
* Gets the primary address
*
* @return string wallets primary address
*/
public function address();
/**
* Gets the current block height
*
* @return int block height
*/
public function blockHeight();
/**
* Creates a new integrated address
*
* @return array ['integrated_address', 'payment_id']
*/
public function createIntegratedAddress();
/**
* Gets any incoming transactions
*
* @return array
*/
public function incomingTransfers();
/**
* Checks for any payments made to the paymentIds
*
* @param array $paymentIds list of payment ids to be searched for
* @param int $minHeight the lowest block the search should start with
*
* @return array payments received since min block height with a payment id provided
*/
public function payments($paymentIds, $minHeight);
/**
* creates a uri for easier wallet parsing
*
* @param string $address address comprising of primary, sub or integrated address
* @param string $paymentId payment id when not using integrated addresses
* @param int $amount atomic amount requested
*
* @return string the uri string which can be used to generate a QR code
*/
public function createUri($address, $paymentId = null, $amount = null);
/**
* creates a random 64 char payment id
*
* @return string
*/
public function generatePaymentId();
}
<?php
namespace Monero;
use App\Project;
use Carbon\Carbon;
use Illuminate\Support\Collection;
class WalletOld
{
/**
* WalletOld constructor.
*
* @param null $client
*/
public function __construct($client = null)
{
$this->client = $client ?: new jsonRPCClient(env('RPC_URL'));
}
/**
* Gets a Payment address for receiving payments
*
* @return array
*
* @internal param \WalletOld $wallet
*/
public function getPaymentAddress()
{
$integratedAddress = $this->createIntegratedAddress();
if (!$integratedAddress) {
return ['address' => 'not valid'];
}
return ['address' => $integratedAddress['integrated_address'], 'paymentId' => $integratedAddress['payment_id']];
}
/**
* Returns the actual available and useable balance (unlocked balance)
*
* @return float|int|mixed
*/
public function balance()
{
return $this->client->balance();
}
public function mempoolTransfers()
{
return $this->client->incomingTransfers();
}
public function bulkPayments($paymentIds)
{
$blockBuffer = 10;
return $this->client->payments($paymentIds, intval($this->wallet->last_scanned_block_height) - $blockBuffer);
}
/**
* Scans the monero blockchain for transactions for the payment ids
*
* @param $blockheight
* @param $paymentIDs
*
* @return array|Transaction
*/
public function scanBlocks($blockheight, $paymentIDs)
{
$response = $this->bulkPayments($paymentIDs);
$address = $this->getAddress();
$transactions = [];
if ($response && isset($response['payments'])) {
foreach ($response['payments'] as $payment) {
$transaction = new Transaction(
$payment['tx_hash'],
$payment['amount'],
$address,
$blockheight - $payment['block_height'],
0,
Carbon::now(),
$payment['payment_id'],
$payment['block_height']
);
$transactions[] = $transaction;
}
}
return collect($transactions);
}
/**
* @param $blockheight
*
* @return \Illuminate\Support\Collection
*/
public function scanMempool($blockheight)
{
$address = $this->getAddress();
$transactions = [];
$response = $this->mempoolTransfers();
if ($response && isset($response['pool'])) {
foreach ($response['pool'] as $payment) {
$transaction = new Transaction(
$payment['txid'],
$payment['amount'],
$address,
0,
0,
Carbon::now(),
$payment['payment_id'],
$blockheight
);
$transactions[] = $transaction;
}
}
return collect($transactions);
}
/**
* Gets the current blockheight of xmr
*
* @return int
*/
public function blockHeight()
{
return $this->client->blockHeight();
}
/**
* Returns monero wallet address
*
* @return string
*/
public function getAddress()
{
return $this->client->address();
}
/**
* Returns XMR integrated address
*
* @return mixed
*/
public function createIntegratedAddress()
{
return $this->client->createIntegratedAddress();
}
/**
* @param $amount
* @param $address
* @param $paymentId
*
* @return string
*/
public function createQrCodeString($address, $amount = null, $paymentId = null): string
{
return $this->client->createUri($address, $amount, $paymentId);
}
/**
* gets all the payment_ids outstanding from the address_pool, we use these to check against the latest mined blocks
*
* @return Collection
*/
public function getPaymentIds()
{
return Project::pluck('payment_id'); //stop scanning for payment_ids after 24h
}
}
...@@ -10,31 +10,41 @@ use Illuminate\Support\Facades\Log; ...@@ -10,31 +10,41 @@ use Illuminate\Support\Facades\Log;
* Class jsonRPCClient * Class jsonRPCClient
* JSON 2.0 RPC Client for cryptocurrency wallet * JSON 2.0 RPC Client for cryptocurrency wallet
*/ */
class jsonRPCClient class jsonRPCClient implements Contracts\WalletManager
{ {
/** @var string */ /** @var string */
private $username; private $username = 'test2';
/** @var string */ /** @var string */
private $password; private $password = 'test2';
/** @var string */
private $url = 'http://127.0.0.1:28080/json_rpc';
/** @var Client|null */ /** @var Client|null */
private $client; private $client;
/** /**
* JsonRPCClient constructor. * JsonRPCClient constructor.
* @param array $options
* @param null $client * @param null $client
*/ */
public function __construct($client = null) public function __construct($options, $client = null)
{ {
$this->username = $options['username'] ?? $this->username;
$this->password = $options['password'] ?? $this->password;
$this->url = $options['url'] ?? $this->url;
if (empty($client)) { if (empty($client)) {
$client = new Client([ $client = new Client([
'base_uri' => env('RPC_URL'), 'base_uri' => $this->url,
'headers' => [
'Content-Type' => 'application/json',
]
]); ]);
} }
$this->username = env('MONERO_USERNAME');
$this->password = env('MONERO_PASSWORD');
$this->client = $client; $this->client = $client;
} }
...@@ -135,6 +145,16 @@ class jsonRPCClient ...@@ -135,6 +145,16 @@ class jsonRPCClient
return $response['uri']; return $response['uri'];
} }
/**
* creates a random 64 char payment id
*
* @return string
*/
public function generatePaymentId(): string
{
return bin2hex(openssl_random_pseudo_bytes(32));
}
/** /**
* Sets up the request data body * Sets up the request data body
* *
...@@ -155,6 +175,7 @@ class jsonRPCClient ...@@ -155,6 +175,7 @@ class jsonRPCClient
} }
/** /**
* Send off request to rpc server
* *
* @param string $method name of the rpc command * @param string $method name of the rpc command
* @param array $params associative array of variables being passed to the method * @param array $params associative array of variables being passed to the method
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment