Skip to main content

signTransaction

Signs a Stellar transaction XDR using the connected wallet. The wallet provider handles the actual signing via its native API (e.g. Freighter, Lobstr, OneKey).

Usage

import { signTransaction, isStellarWalletAccount } from '@dynamic-labs-sdk/stellar';
import { getPrimaryWalletAccount } from '@dynamic-labs-sdk/client';

const walletAccount = getPrimaryWalletAccount();

if (walletAccount && isStellarWalletAccount(walletAccount)) {
  const { signedXdr } = await signTransaction({
    transactionXdr: 'AAAA...', // base64-encoded transaction envelope
    walletAccount,
  });

  console.log('Signed XDR:', signedXdr);
}

Building a Transaction XDR

Use the @stellar/stellar-sdk to build a transaction before signing:
import {
  TransactionBuilder,
  Operation,
  Asset,
  BASE_FEE,
  Horizon,
  Networks,
} from '@stellar/stellar-sdk';
import { signTransaction, isStellarWalletAccount } from '@dynamic-labs-sdk/stellar';
import { getPrimaryWalletAccount } from '@dynamic-labs-sdk/client';

const walletAccount = getPrimaryWalletAccount();

if (walletAccount && isStellarWalletAccount(walletAccount)) {
  const server = new Horizon.Server('https://horizon.stellar.org');
  const sourceAccount = await server.loadAccount(walletAccount.address);

  const transaction = new TransactionBuilder(sourceAccount, {
    fee: BASE_FEE,
    networkPassphrase: Networks.PUBLIC,
  })
    .addOperation(
      Operation.payment({
        destination: 'GDEST...',
        asset: Asset.native(),
        amount: '10',
      })
    )
    .setTimeout(180)
    .build();

  const { signedXdr } = await signTransaction({
    transactionXdr: transaction.toXDR(),
    walletAccount,
  });
}

Parameters

ParameterTypeDescription
transactionXdrstringThe base64-encoded transaction envelope XDR to sign
walletAccountStellarWalletAccountThe wallet account to sign the transaction with
clientDynamicClient(optional) The Dynamic client instance. Only required when using multiple Dynamic clients

Returns

Promise<{ signedXdr: string }> - A promise that resolves to an object containing the signed transaction XDR.

Errors

ErrorDescription
NotStellarProviderErrorThrown if the wallet account’s provider is not a Stellar provider

React

import { signTransaction, isStellarWalletAccount } from '@dynamic-labs-sdk/stellar';
import { useWalletAccounts } from '@dynamic-labs-sdk/react-hooks';
import { useState } from 'react';

function StellarSignButton({ transactionXdr }) {
  const walletAccounts = useWalletAccounts();
  const walletAccount = walletAccounts.find(isStellarWalletAccount);
  const [signedXdr, setSignedXdr] = useState('');

  const handleSign = async () => {
    if (!walletAccount) return;

    const { signedXdr } = await signTransaction({
      transactionXdr,
      walletAccount,
    });

    setSignedXdr(signedXdr);
  };

  return (
    <div>
      <button onClick={handleSign} disabled={!walletAccount}>
        Sign Transaction
      </button>
      {signedXdr && <p>Signed</p>}
    </div>
  );
}