# useSignTransaction

Since Comet accounts come with a built-in cloud wallet, you can use this wallet to provide a signature for any Solana transaction using the `useSignTransaction` hook. The user will be prompted to log in if necessary, and will be asked to confirm the transaction to be signed (similar to the flow for Metamask or Phantom wallets).

<figure><img src="/files/hK8UH9LtFTO5X4WBcBDy" alt=""><figcaption></figcaption></figure>

## API

```javascript
const { signTransaction } = useSignTransaction({ transaction: TRANSACTION });
```

In the example above, `TRANSACTION` is a `Transaction` object generated by the Solana Web3 library (see [`@solana/web3.js`](https://docs.solana.com/developing/clients/javascript-api)).

`signTransaction` is a function that can be called later on, which brings up a confirmation dialog. When the user clicks "Sign", the transaction is partially signed by the private key in the user's Comet wallet. Keep in mind that if the transaction requires additional signatures, those will need to be provided separately.

`signTransaction` returns a successful `Promise` containing the `Transaction` object with the added signature, or a failed `Promise` if the user clicks "Cancel".

## Example

```tsx
import { useAccount, useSignTransaction } from '@comet-labs/react';
import { PublicKey, Transaction, SystemProgram } from '@solana/web3.js';

function App() {
  const account = useAccount();

  return (
    <div className="App">
      {
        account && <SignTransactionButton account={account} />
      }
    </div>
  );
}

function SignTransactionButton(props) {
  const tx = new Transaction().add(
    SystemProgram.transfer({
      fromPubkey: new PublicKey(props.account.address),
      toPubkey: new PublicKey('Dgq5B8i5NJJfPoUgpkFZDzRr84zd1BJrUBntJt1EBvgd'),
      lamports: 2000000,
    })
  );

  const { signTransaction } = useSignTransaction({ transaction: tx });

  return (
    <button onClick={signTransaction} className='bg-gray-800 text-white p-4 rounded-full'>
      sign transaction! &#x1F389;
    </button>
  );
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://comet-3.gitbook.io/comet-sdk/reference/comet-sdk-reference/comet-react-sdk/hooks/usesigntransaction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
