How to sign not string message?

I use type script SDK to sign message off chain, but function signMessage accepts only string type data I need to sign Uint8Array.I cant change signing message type from Uint8Array as in smart contract I handle with data using bytes. Is there any workaround or solution for this case ?

Hi @nerses

The signMessage function in the TypeScript SDK indeed accepts only string type data for signing. However, you can convert your Uint8Array to a string format before signing and then handle it accordingly in your smart contract.

Here’s a simple workaround:

  1. Convert Uint8Array to a Hex String: You can convert your Uint8Array to a hex string representation, which can then be signed using signMessage.

  2. Sign the Hex String: Use the signMessage function to sign the hex string.

  3. Handle the Signed Message: In your smart contract, you can convert the signed message back to a Uint8Array if needed.

Here’s an example of how you might implement this:

// Convert Uint8Array to Hex String
function uint8ArrayToHexString(uint8Array: Uint8Array): string {
  return Array.from(uint8Array, byte => byte.toString(16).padStart(2, '0')).join('');
}

// Example Uint8Array
const data = new Uint8Array([1, 2, 3, 4]);

// Convert to Hex String
const hexString = uint8ArrayToHexString(data);

// Sign the Hex String
const signedMessage = await wallet.signMessage(hexString);
console.log("Signed Message:", signedMessage);

This way, you can maintain the integrity of your data while using the existing signMessage function. For more information, you can refer to the Signing Messages documentation.

Does that help?

@maschad I have used this workaround signerWallet.signer().sign(sha256(msgBytes)) as under the the hood signMessage is doing the same and returns hexlify value. Do you think it will cause some security issues ?

I think that should be fine @nerses

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.