Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt

Use this file to discover all available pages before exploring further.

signPsbt

Signs a Partially Signed Bitcoin Transaction (PSBT). PSBTs are used for more complex Bitcoin transactions that may require multiple signatures or advanced scripting.

Usage

import { signPsbt, isBitcoinWalletAccount } from "@dynamic-labs-sdk/bitcoin";
import { getPrimaryWalletAccount } from "@dynamic-labs-sdk/client";

const walletAccount = getPrimaryWalletAccount();

if (walletAccount && isBitcoinWalletAccount(walletAccount)) {
  const { signedPsbt } = await signPsbt({
    walletAccount,
    request: {
      unsignedPsbtBase64: "cHNidP8BAH...", // Base64-encoded PSBT
      allowedSighash: [1], // SIGHASH_ALL
      signature: [
        {
          address: walletAccount.address,
          signingIndexes: [0],
        },
      ],
    },
  });

  console.log("Signed PSBT:", signedPsbt);
}

Parameters

ParameterTypeDescription
request.unsignedPsbtBase64stringThe unsigned PSBT encoded in Base64
request.allowedSighashnumber[]Array of allowed signature hash types
request.signatureArray (optional)Array of signature configuration objects
request.signature[].addressstringThe address to sign with
request.signature[].signingIndexesnumber[]The input indexes to sign
request.signature[].disableAddressValidationboolean (optional)Disable address validation
walletAccountBitcoinWalletAccountThe wallet account to sign with
clientDynamicClient (optional)The Dynamic client instance. Only required when using multiple clients.

Returns

Promise<{ signedPsbt: string }> - A promise that resolves to an object containing the signed PSBT in Base64 format.

Errors

ErrorDescription
NotBitcoinProviderErrorThrown if the wallet account’s provider is not a Bitcoin provider

React

import { signPsbt, isBitcoinWalletAccount } from '@dynamic-labs-sdk/bitcoin';
import { useWalletAccounts } from '@dynamic-labs-sdk/react-hooks';
import { useState } from 'react';

function SignPsbtButton({ unsignedPsbtBase64 }) {
  const walletAccounts = useWalletAccounts();
  const walletAccount = walletAccounts.find(isBitcoinWalletAccount);
  const [signedPsbt, setSignedPsbt] = useState('');

  const handleSign = async () => {
    if (!walletAccount) return;
    const result = await signPsbt({
      walletAccount,
      request: {
        unsignedPsbtBase64,
        allowedSighash: [1],
        signature: [{ address: walletAccount.address, signingIndexes: [0] }],
      },
    });
    setSignedPsbt(result.signedPsbt);
  };

  return (
    <div>
      <button onClick={handleSign} disabled={!walletAccount}>Sign PSBT</button>
      {signedPsbt && <p>Signed: {signedPsbt.slice(0, 20)}...</p>}
    </div>
  );
}