magiclabs / magic-js

Magic browser/React Native JavaScript SDK is your entry-point to integrating passwordless authentication inside your application.
https://magic.link/docs/api-reference/client-side-sdks/web
Apache License 2.0
462 stars 86 forks source link

What is the expected argument for Solana Extension signMessage function? #354

Closed jp30566347 closed 2 years ago

jp30566347 commented 2 years ago

✅ Prerequisites

❓ Question

Why the signMessage function always throws this error?

 [Error: Magic RPC Error: [-32603] Internal error: unexpected type, use Uint8Array]

I've been trying to invoke it using a string as well as a Uint8Array but no luck. The Uint8Array is created from tweetnacl-ts lib's decodeUTF8 function.

🌎 Environment

Software Version(s)
magic-sdk "@magic-sdk/react-native": "^10.0.0",
solana extension "@magic-ext/solana": "^5.0.0",
Browser NA
npm 8.18.0
Operating System MacOS 12.5.1
am-hernandez commented 2 years ago

Hello @jp-melanson , thank you for reporting this issue. The expected argument is Uint8Array. Here is a working code snippet:

const signAndVerify = async () => {
    const message = "Hello";
    const encodedMsg = decodeUTF8(message);
    const signature = await magic.solana.signMessage(encodedMsg);

    const verifiedMsg = nacl.sign.detached.verify(
      encodedMsg,
      signature,
      base58_to_binary(userMetadata.publicAddress)
    );
    console.log("Sig verified:", verifiedMsg);
  };

You may also check out this working Codesandbox demo.