1
const {signTransaction} = useWallet()

const transaction = new Transaction()

const tx = .... //arbitrary anchor program method as transaction

transaction.add(tx)

const signature =  signTransaction(transaction) //errors out

How does the signTransaction() function from the wallet adapter work?

https://github.com/solana-labs/wallet-adapter/blob/master/packages/core/react/src/useWallet.ts

It says it takes a transaction and returns a promise but when I try to use it I get the following error.

WalletSignTransactionError: Transaction recentBlockhash required
at PhantomWalletAdapter.signTransaction

I know I can send my transaction a different way but I want to get a signed transaction object without sending it.

1
  • You dont get the signature when you use the signTransaction function it gives you the signed tx
    – Pratik.js
    Commented Sep 5, 2022 at 8:35

1 Answer 1

2

When creating a Transaction object without sending it, it is required to set recentBlockhash and feePayer fields like so:

const latestBlockhash = await connection.getLatestBlockhash();
transaction.feePayer = wallet.publicKey;
transaction.recentBlockhash = latestBlockhash.blockhash;

and only then sign the transaction. recentBlockchash is used to check by Solana whether the transaction is outdated. If, for example, you sign a transaction but send it after 5 minutes, that transaction will be rejected and you will need to resign. This is why it is part of the transaction object.

I had the same issue in the Place | Solana project. Here is how I create a transaction on the server, send the serialized transaction to client who signs it and forwards to the server for sending.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.