Skip to content

signMessage

Sign a message using the connected wallet. Supports UTF-8, Base64, and Hex encoding formats.

Usage

ts
const result = await Turing.signMessage({
  message: "hello world",
  encoding: "utf-8",
});

Parameters

ts
interface SignMessageParams {
  message: string;                        // The message to sign
  encoding: "utf-8" | "base64" | "hex";  // Encoding format
}
ParameterTypeRequiredDescription
messagestringYesThe message content to sign
encoding"utf-8" | "base64" | "hex"YesEncoding format for the message

Returns

ts
interface SignMessageResponse {
  address: string;  // Wallet address that signed
  pubkey: string;   // Public key used for signing
  sig: string;      // The generated signature
  message: string;  // The original message
}
FieldTypeDescription
addressstringThe wallet address
pubkeystringThe public key
sigstringThe signature
messagestringThe original message

Error Handling

This method throws an exception on failure. Use try-catch:

ts
try {
  const { address, pubkey, sig, message } = await Turing.signMessage({
    message: "hello world",
    encoding: "base64",
  });
  console.log("Signed by:", address);
  console.log("Signature:", sig);
} catch (error) {
  console.error("Signing failed:", error);
}

Verify Signature

You can verify the signature using tbc-lib-js:

ts
import * as tbc from "tbc-lib-js";

const msg_buf = Buffer.from(message, encoding);
const isValid = tbc.Message.verify(msg_buf, address, sig);
console.log("Signature valid:", isValid);