Skip to content

btc.signTransaction

通过 btc 对象对单个 BTC 裸交易进行签名,支持 legacy、segwit_v0、taproot 三种签名类型。

taproot 类型下通过 leafHashesHex 区分 key path 和 script path:对应输入为 undefined 则走 key path,有值则走 script path。

用法

ts
const result = await Turing.btc.signTransaction({
  txHex: "0200000001...",
  type: "legacy",
  prevOutScriptsHex: ["76a914...88ac"],
});

参数

ts
type BtcSigHashType = "legacy" | "segwit_v0" | "taproot";

interface BtcSignTransaction {
  txHex: string;                          // 裸交易 hex
  type: BtcSigHashType;                   // 签名哈希类型
  prevOutScriptsHex: string[];            // 每个输入对应的前置输出脚本 hex
  values?: number[];                      // 每个输入对应的金额(satoshis),segwit_v0/taproot 必填
  leafHashesHex?: (string | undefined)[]; // 每个输入对应的叶子哈希,仅 taproot 类型使用,undefined 表示 key path,有值表示 script path
}
字段类型必填说明
txHexstring裸交易 hex
typeBtcSigHashType签名哈希类型:"legacy""segwit_v0""taproot"
prevOutScriptsHexstring[]每个输入对应的前置输出脚本 hex
valuesnumber[]条件必填每个输入对应的金额(satoshis),segwit_v0taproot 类型必填
leafHashesHex(string | undefined)[]taproot 类型使用;undefined 走 key path,有值走 script path

返回值

ts
interface BtcSignTransactionResponse {
  sigs?: string[];  // 每个输入对应的签名 hex
  error?: string;   // 错误信息
}
字段类型说明
sigsstring[]每个输入对应的签名 hex 数组
errorstring签名失败时的错误信息

错误处理

ts
const result = await Turing.btc.signTransaction({
  txHex: "0200000001...",
  type: "legacy",
  prevOutScriptsHex: ["76a914...88ac"],
});

if (result.error) {
  console.error("签名失败:", result.error);
  return;
}

console.log("签名列表:", result.sigs);

示例

Legacy (P2PKH) 签名

ts
const result = await Turing.btc.signTransaction({
  txHex: "0200000001...",
  type: "legacy",
  prevOutScriptsHex: ["76a914...88ac"],
});

if (result.sigs) {
  console.log("签名列表:", result.sigs);
} else if (result.error) {
  console.error("签名失败:", result.error);
}

SegWit V0 (P2WPKH) 签名

ts
const result = await Turing.btc.signTransaction({
  txHex: "0200000001...",
  type: "segwit_v0",
  prevOutScriptsHex: ["76a914...88ac"],
  values: [100000],
});

if (result.sigs) {
  console.log("签名列表:", result.sigs);
}

Taproot Key Path 签名

ts
const result = await Turing.btc.signTransaction({
  txHex: "0200000001...",
  type: "taproot",
  prevOutScriptsHex: ["5120..."],
  values: [100000],
});

if (result.sigs) {
  console.log("签名列表:", result.sigs);
}

Taproot Script Path 签名

ts
const result = await Turing.btc.signTransaction({
  txHex: "0200000001...",
  type: "taproot",
  prevOutScriptsHex: ["5120..."],
  values: [100000],
  leafHashesHex: ["ab12cd34..."],
});

if (result.sigs) {
  console.log("签名列表:", result.sigs);
}