diff --git a/monero/Contracts/WalletManager.php b/monero/Contracts/WalletManager.php new file mode 100644 index 0000000000000000000000000000000000000000..91d33e3e085753b9306db7e9dba8115a359295d6 --- /dev/null +++ b/monero/Contracts/WalletManager.php @@ -0,0 +1,81 @@ +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 + } +} diff --git a/monero/jsonRPCClient.php b/monero/jsonRPCClient.php index b2de9140e625166263cf3e0f21bfbe69f37669cf..b141b5b2e71fa8200e34fa4b814873401ed0b589 100644 --- a/monero/jsonRPCClient.php +++ b/monero/jsonRPCClient.php @@ -10,31 +10,41 @@ use Illuminate\Support\Facades\Log; * Class jsonRPCClient * JSON 2.0 RPC Client for cryptocurrency wallet */ -class jsonRPCClient +class jsonRPCClient implements Contracts\WalletManager { /** @var string */ - private $username; + private $username = 'test2'; /** @var string */ - private $password; + private $password = 'test2'; + + /** @var string */ + private $url = 'http://127.0.0.1:28080/json_rpc'; /** @var Client|null */ private $client; /** * JsonRPCClient constructor. + * @param array $options * @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)) { $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; } @@ -135,6 +145,16 @@ class jsonRPCClient 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 * @@ -155,6 +175,7 @@ class jsonRPCClient } /** + * Send off request to rpc server * * @param string $method name of the rpc command * @param array $params associative array of variables being passed to the method