monero-integrations / monerophp

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

subaddress.php bug #123

Closed solovenkoU closed 3 years ago

solovenkoU commented 3 years ago

Installed latest master dev via composer. Using PHP 7.4. Wrote a sample script for a test subaddress generation via random seed.

<?php
require 'vendor/autoload.php';

use MoneroIntegrations\MoneroPhp;
use MoneroIntegrations\MoneroPhp\Cryptonote;
use MoneroIntegrations\MoneroPhp\ed25519;
use MoneroIntegrations\MoneroPhp\base58;

require_once('vendor/monero-integrations/monerophp/src/ed25519.php');
require_once('vendor/monero-integrations/monerophp/src/base58.php');
require_once('vendor/monero-integrations/monerophp/src/subaddress.php');

$Cryptonote = new Cryptonote();
$seed = $Cryptonote->gen_new_hex_seed(); //generate new random hex seed
$address = $Cryptonote->gen_private_keys($seed); //generate new random private key

print_r($address);

// use subaddress->generate_subaddress
//
$subaddress = new subaddress();
print_r( $subaddress->generate_subaddress(0, 0, $address['viewKey'], $address['spendKey']) );

?>

In about 25% of cases having error:

# php poc.php
Array
(
    [spendKey] => 3c0b43f1b952e60c2111dc52c36f985e3bd7e3d55bf11a543de9c981db60790d
    [viewKey] => cc278427ca2bf859065b73a268217ba8189bba86e8f5deee868d86ee60bb6201
)
PHP Fatal error:  Uncaught Exception: Decoding point that is not on curve in /opt/monero/php/vendor/monero-integrations/monerophp/src/ed25519.php:436
Stack trace:
#0 /opt/monero/php/vendor/monero-integrations/monerophp/src/subaddress.php(49): MoneroIntegrations\MoneroPhp\ed25519->decodepoint()
#1 /opt/monero/php/vendor/monero-integrations/monerophp/src/subaddress.php(104): subaddress->ge_add()
#2 /opt/monero/php/vendor/monero-integrations/monerophp/src/subaddress.php(116): subaddress->generate_subaddress_spend_public_key()
#3 /opt/monero/php/poc.php(22): subaddress->generate_subaddress()
#4 {main}
  thrown in /opt/monero/php/vendor/monero-integrations/monerophp/src/ed25519.php on line 436
solovenkoU commented 3 years ago

It looks like the problem is with _spend_public_key_ point decode.

Oh. I had to generate public key from a private key.

serhack commented 3 years ago

This should fix your problem :)

print_r( $subaddress->generate_subaddress(0, 0, $address['viewKey'], $Cryptonote->pk_from_sk($address['spendKey'])) );
solovenkoU commented 3 years ago

Please fix the following:

if($this->ed25519->gmp)

should be this:

if( property_exists($this->ed25519, 'gmp') )