Skip to content

signMessage

使用已连接的钱包对消息进行签名。支持 UTF-8、Base64 和 Hex 编码格式。

用法

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

参数

ts
interface SignMessageParams {
  message: string;                        // 要签名的消息
  encoding: "utf-8" | "base64" | "hex";  // 编码格式
}
参数类型必填说明
messagestring要签名的消息内容
encoding"utf-8" | "base64" | "hex"消息的编码格式

返回值

ts
interface SignMessageResponse {
  address: string;  // 签名使用的钱包地址
  pubkey: string;   // 签名使用的公钥
  sig: string;      // 生成的签名
  message: string;  // 原始消息
}
字段类型说明
addressstring钱包地址
pubkeystring公钥
sigstring签名
messagestring原始消息

错误处理

此方法在失败时会抛出异常,请使用 try-catch:

ts
try {
  const { address, pubkey, sig, message } = await Turing.signMessage({
    message: "hello world",
    encoding: "base64",
  });
  console.log("签名地址:", address);
  console.log("签名:", sig);
} catch (error) {
  console.error("签名失败:", error);
}

验证签名

使用 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("签名是否有效:", isValid);