monero-integrations / monerophp

Monero PHP library + JsonRPC Client
MIT License
116 stars 76 forks source link

Sending XMR to wrong address #136

Closed bar-boss closed 1 year ago

bar-boss commented 1 year ago

Hello. I have 2 php files: validate_address.php and send.php validate_address.php

require_once('src/jsonRPCClient.php');
require_once('src/daemonRPC.php');
require_once('src/walletRPC.php');

$walletRPC = new walletRPC('127.0.0.1', 18083);
$open_wallet = $walletRPC->open_wallet('WalletValidate', 'PASSWORD');
$validate = $walletRPC->validate_address($_GET['address']);
if($validate['valid'] == true)
{
    echo 'yes';
}
else
{
    echo 'no';
}
$walletRPC->close_wallet();

send.php

require_once('src/jsonRPCClient.php');
require_once('src/daemonRPC.php');
require_once('src/walletRPC.php');

$walletRPC = new walletRPC('127.0.0.1', 18083);

$wallets[1] = array('file' => 'Wallet1', 'address' => '48G6******', 'password' => 'PASSWORD_1');
$wallets[2] = array('file' => 'Wallet2', 'address' => '453ML*****', 'password' => 'PASSWORD_2');
$wallets[3] = array('file' => 'Wallet3', 'address' => '42UPU*****', 'password' => 'PASSWORD_3');

foreach($wallets as $key => $wallet)
{
  $open_wallet = $walletRPC->open_wallet($wallet['file'], $wallet['password']);
  $walletRPC->refresh();
  $balance = $walletRPC->get_balance();
  $address = $walletRPC->get_address();
  $walletRPC->close_wallet();
  if($balance['unlocked_balance'] < 1000000) // like 1 XMR or something
  {
    $open_wallet = $walletRPC->open_wallet('WalletMain', 'PASSWORD_MAIN');
    $walletRPC->refresh();
    $transfer = $walletRPC->transfer(['address' => $address['address'], 'amount' => 1]);
    $walletRPC->close_wallet();
  }
}

From WalletMain 1 XMR sometimes sent to WalletValidate. Why?

serhack commented 1 year ago

Sorry for the late response. I guess it's the typical issue of using one resource by two entities. The resource here shared by the two scripts is the walletRPC. Remember that "sometimes" internal scheduler of the two processes (e.g. php-fpm) might send at first the RPC command to open WalletValidate from first file, and then send the second RPC command to transfer 1 XMR. I suggest you implementing some sort of mutex to manage this condition.