signTransaction

import { signTransaction } from 'sats-connect'

To request signing of a PSBT, you can use the signTransaction function.

The PSBT to be signed must be base64-encoded. You can use any Bitcoin library to construct this transaction. See examples using @scure/btc-signerhere: Creating PSBTs

payloadDescription

network

an object specifying the target Bitcoin chain (Mainnet / Testnet)

psbtBase64

a valid psbt encoded in base64

broadcast

a flag that specifies whether to broadcast the signed transaction

inputsToSign

an array of objects describing the address and index of input to sign

const signPsbtOptions = {
  payload: {
    network: {
      type:'Mainnet'
    },
    psbtBase64: `cHNidP8BAJwCAmO+JvQJxhVDDpm3tV5PmPfzvJOSL4GOdjEOpAAAAAAnrAAA==`,
    broadcast: false,
    inputsToSign: [{
        address: "33TKH4kkiFPyTLDNmdNsLggyLeAYre57Qm",
        signingIndexes: [1],
    }],
  },
  onFinish: (response) => {
    console.log(response.psbtBase64)
  },
  onCancel: () => alert('Canceled'),
}

await signTransaction(signPsbtOptions);

Your request must specify

  • The index of the input to sign

  • which address to use to sign the input

For example:

inputsToSign: [{
  address: "bc1pr09enf3yc43cz8qh7xwaasuv3xzlgfttdr3wn0q2dy9frkhrpdtsk05jqq",
  signingIndexes: [0]
}, {
  address: "33TKH4kkiFPyTLDNmdNsLggyLeAYre57Qm",
  signingIndexes: [1,2]
}]

The above parameters will ask the wallet to sign for address bc1pr09enf3yc43cz8qh7xwaasuv3xzlgfttdr3wn0q2dy9frkhrpdtsk05jqq at input index 0. And for address 33TKH4kkiFPyTLDNmdNsLggyLeAYre57Qm at input indexes 1 and 2.

You can also optionally specify a signature hash:

inputsToSign: [{
  address: "bc1pr09enf3yc43cz8qh7xwaasuv3xzlgfttdr3wn0q2dy9frkhrpdtsk05jqq",
  signingIndexes: [0],
  sigHash: 131 // SIGHASH_SINGLE | ANYONECANPAY
}]

Depending on your use case, you can request that the PSBT be finalized and broadcasted after the user signs, by setting the payload's broadcast flag to true. Otherwise, the signed PSBT will be returned in the response without broadcasting.

If the transaction is broadcasted, you will receive a TXID in the response.

Last updated