nostrver-se / nostr-php

PHP helper library for Nostr https://nostr-php.dev
https://nostr-php.dev
MIT License
50 stars 15 forks source link

$signer->signEvent is breaking the script but returns no error #33

Closed pedromvpg closed 11 months ago

pedromvpg commented 1 year ago

For some reason the line $signer->signEvent($note, "nsec1s3ul24jggks7fn2...5u6ph3flgxdjkcsl5tc7q"); is stopping the script but I can't see any error.

Here's the code I'm running.

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

use swentel\nostr\Event\Event;
use swentel\nostr\Message\EventMessage;
use swentel\nostr\Relay\Relay;
use swentel\nostr\Sign\Sign;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

function send($message) {
  $note = new Event();
  $note->setContent($message)->setKind(1);
  print_r($note);  
  $signer = new Sign();

  /* The line below breaks the script and retruns no error */
  $signer->signEvent($note, "nsec1s3ul24jggks7fn2...5u6ph3flgxdjkcsl5tc7q");

  $eventMessage = new EventMessage($note);
  $websocket = 'wss://eden.nostr.land';
  $relay = new Relay($websocket, $eventMessage);
  $result = $relay->send();

  if ($result->isSuccess()) {
    print "Sent to Nostr!\n";
  } else {
    print 'Something went wrong: ' . $result->message() . "\n";
  }

}

$message = 'Hello World';
send($message);
Sebastix commented 1 year ago

$signer->signEvent($note, "nsec1s3ul24jggks7fn2...5u6ph3flgxdjkcsl5tc7q");

Looks like the second argument (nsec1s3ul24jggks7fn2...5u6ph3flgxdjkcsl5tc7q) in signEvent is not a valid private key string. You could try nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5 which can be found in one of the tests.

pedromvpg commented 1 year ago

Thanks, the private key was not valide because I replaced some of the characters with "...". Tried with the sample one provided from other tests and returns the same result.

Does it work for you?

Sebastix commented 1 year ago

A quick test: https://play.phpsandbox.io/swentel/nostr-php/VBgz6pXzOm5kdnvN

Exception with message "Invalid characters"
   null
pedromvpg commented 1 year ago

Possibly related to https://github.com/swentel/nostr-php/issues/23? @swentel any ideas?

pjv commented 11 months ago

If it is related to #23 you could try it again while running the patch I supplied in #27 which reliably fixes #23 for me.

Sebastix commented 11 months ago

@pedromvpg Have you tried it with private key in hex value? I just found out that the code is expecting a private key in hex and doesn't convert your nsec to hex when the event is signed.

In your test you can convert your keys to hex in this way:

$key = new Key();
$private_key_in_hex = $key->convertToHex('nsec1...');
Sebastix commented 11 months ago

Fix in https://github.com/swentel/nostr-php/pull/38