1

I am building an app where I need to generate a message signed on the backend with NodeJS, something like window.solana.signMessage('hello'). I have the user's private key, I just need a signature to authenticate the user, but I cannot find how to generate it on the server.

Thanks!

1 Answer 1

1

You can use NaCl tweet library to generate the signature on the server

     // Encode anything as bytes
      const message = new TextEncoder().encode("Your message here");
     // Sign the bytes using the wallet
     const signature = await signMessage(message);

     // Verify that the bytes were signed using the private key that matches the known public key
    
    const result = nacl.sign.detached.verify(
      message,
      signature,
      publicKey.toBytes(),
    );
     console.log("result",result);
    if(!result) throw new Error('Invalid signature!');

result will be true or false depending upon whether the user is signing the message using his own private key.. Hope this helps.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.