1

I have a signature from the Phantom wallet in the Uint8Array format which I receive in the following way.

const encodedMessage = new TextEncoder().encode("sign me please");
const signedMessage = await provider.signMessage(encodedMessage, "utf8");

I need to serialise/convert this signature as a string so that I can pass it via an API.

Problem:

I have tried using TextDecoder like this:

const decodedSignature = new TextDecoder().decode(signedMessage.signature);

however if I try to encode it back, I get a Uint8Array that contains different bytes that the original signature:

// the result is different from signedMessage.signature
const encodedSignature = new TextEncoder().encode(decodedSignature);

Question:

How can I convert a Phantom wallet signature to string an back in order to pass it via API calls?

1 Answer 1

1

You need to convert it to base64

const base64str = Buffer.from(signedMessage).toString('base64');

At the backend just decode base64 string back to u8 array.

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.