Skip to content

signTransaction

Sign one or more raw transactions using the wallet. This is a low-level API that gives you full control over transaction construction and signing.

Usage

ts
const { sigs } = await Turing.signTransaction({
  txraws,
  utxos_satoshis,
  script_pubkeys,
});

Parameters

ts
interface SignTransactionParams {
  txraws: string[];             // Array of serialized transaction hex strings
  utxos_satoshis: number[][];   // Satoshi values for each input of each transaction
  script_pubkeys: string[][];   // Script pubkeys for each input of each transaction
}
ParameterTypeRequiredDescription
txrawsstring[]YesArray of unsigned transaction hex strings
utxos_satoshisnumber[][]Yes2D array — satoshi values for each input, per transaction
script_pubkeysstring[][]Yes2D array — script pubkeys for each input, per transaction

Returns

ts
interface SignTransactionResponse {
  sigs: string[][];  // 2D array — signatures for each input, per transaction
}
FieldTypeDescription
sigsstring[][]Signatures indexed as sigs[txIndex][inputIndex]

Error Handling

ts
try {
  const { sigs } = await Turing.signTransaction({
    txraws,
    utxos_satoshis,
    script_pubkeys,
  });
  console.log("Signatures:", sigs);
} catch (error) {
  console.error("Signing failed:", error);
}

Example

Complete example: construct two transactions, sign them, and assemble the scripts.

ts
const utxosA: tbc.Transaction.IUnspentOutput[] = [];
const utxosB: tbc.Transaction.IUnspentOutput[] = [];
const utxos_satoshis: number[][] = [[], []];
const script_pubkeys: string[][] = [[], []];
const txraws: string[] = [];
const txs: tbc.Transaction[] = [];

const tx0 = new tbc.Transaction()
  .from(utxosA)
  .to(address, 100000)
  .change(address)
  .fee(80);

const tx1 = new tbc.Transaction()
  .from(utxosB)
  .to(address, 100000)
  .change(address)
  .fee(80);

txraws.push(tx0.uncheckedSerialize(), tx1.uncheckedSerialize());

for (let i = 0; i < utxosA.length; i++) {
  utxos_satoshis[0].push(utxosA[i].satoshis);
  script_pubkeys[0].push(utxosA[i].script);
}

for (let i = 0; i < utxosB.length; i++) {
  utxos_satoshis[1].push(utxosB[i].satoshis);
  script_pubkeys[1].push(utxosB[i].script);
}

const { sigs } = await Turing.signTransaction({
  txraws,
  utxos_satoshis,
  script_pubkeys,
});

for (let i = 0; i < utxosA.length; i++) {
  tx0.setInputScript({ inputIndex: i }, (tx) => {
    const sig = sigs[0][i];
    const sig_length = (sig.length / 2).toString(16);
    const publicKey_length = (
      publicKey.toBuffer().toString("hex").length / 2
    ).toString(16);
    return new tbc.Script(
      sig_length + sig + publicKey_length + publicKey.toString()
    );
  });
  txs.push(tx0);
}

for (let i = 0; i < utxosB.length; i++) {
  tx1.setInputScript({ inputIndex: i }, (tx) => {
    const sig = sigs[1][i];
    const sig_length = (sig.length / 2).toString(16);
    const publicKey_length = (
      publicKey.toBuffer().toString("hex").length / 2
    ).toString(16);
    return new tbc.Script(
      sig_length + sig + publicKey_length + publicKey.toString()
    );
  });
  txs.push(tx1);
}

broadcastTXsraw(txs.map((tx) => ({ txraw: tx.uncheckedSerialize() })));