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.
signTransaction
Signs an Aptos transaction without submitting it to the network. This is useful when you need to collect signatures before submitting, or when working with multi-signature transactions.
Usage
import { signTransaction, isAptosWalletAccount, getAptosClient } from "@dynamic-labs-sdk/aptos";
import { getPrimaryWalletAccount } from "@dynamic-labs-sdk/client";
const walletAccount = getPrimaryWalletAccount();
if (walletAccount && isAptosWalletAccount(walletAccount)) {
const aptosClient = await getAptosClient({ walletAccount });
// Build a transaction
const transaction = await aptosClient.transaction.build.simple({
sender: walletAccount.address,
data: {
function: "0x1::aptos_account::transfer",
functionArguments: [recipientAddress, amount],
},
});
// Sign the transaction
const { signature } = await signTransaction({
transaction,
walletAccount,
});
}
Parameters
| Parameter | Type | Description |
|---|
transaction | AnyRawTransaction | The transaction to sign |
walletAccount | AptosWalletAccount | The wallet account to sign the transaction with |
asFeePayer | boolean (optional) | Whether to sign as the fee payer |
client | DynamicClient (optional) | The Dynamic client instance. Only required when using multiple clients. |
Returns
Promise<{ signature: AptosSignTransactionOutput }> - A promise that resolves to an object containing the signature output.
Errors
| Error | Description |
|---|
NotAptosProviderError | Thrown if the wallet account’s provider is not an Aptos provider |
React
import { signTransaction, isAptosWalletAccount, getAptosClient } from '@dynamic-labs-sdk/aptos';
import { useWalletAccounts } from '@dynamic-labs-sdk/react-hooks';
import { useState } from 'react';
function SignTransactionButton({ recipientAddress, amount }) {
const walletAccounts = useWalletAccounts();
const walletAccount = walletAccounts.find(isAptosWalletAccount);
const [signed, setSigned] = useState(false);
const handleSign = async () => {
if (!walletAccount) return;
const aptosClient = await getAptosClient({ walletAccount });
const transaction = await aptosClient.transaction.build.simple({
sender: walletAccount.address,
data: {
function: '0x1::aptos_account::transfer',
functionArguments: [recipientAddress, amount],
},
});
await signTransaction({ transaction, walletAccount });
setSigned(true);
};
return (
<div>
<button onClick={handleSign} disabled={!walletAccount}>Sign Transaction</button>
{signed && <p>Transaction signed</p>}
</div>
);
}