import {
pipe,
createTransactionMessage,
setTransactionMessageFeePayer,
setTransactionMessageLifetimeUsingBlockhash,
appendTransactionMessageInstruction,
compileTransaction,
createSolanaRpc,
getTransactionEncoder,
getBase64EncodedWireTransaction,
address,
signatureBytes,
type Transaction
} from '@solana/kit';
import {useSignTransaction, useWallets} from '@privy-io/react-auth/solana';
const {signTransaction} = useSignTransaction();
const {wallets} = useWallets();
const wallet = wallets[0];
const rpc = createSolanaRpc('YOUR_SOLANA_RPC_URL');
const {value: latestBlockhash} = await rpc.getLatestBlockhash().send();
// 1. Build and compile the transaction
const compiledTransaction = pipe(
createTransactionMessage({version: 0}),
(m) => setTransactionMessageFeePayer(address(wallet.address), m),
(m) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, m),
(m) => appendTransactionMessageInstruction(/* your instruction */, m),
(m) => compileTransaction(m)
);
// 2. Encode the compiled transaction for signing
const encodedTransaction = new Uint8Array(
getTransactionEncoder().encode(compiledTransaction)
);
// 3. Sign via Privy — for sign-only wallets, this returns a 64-byte signature
const {signedTransaction: signature} = await signTransaction({
transaction: encodedTransaction,
wallet
});
// 4. Attach the signature to the compiled transaction
const signedMessage: Transaction = {
...compiledTransaction,
signatures: {
[address(wallet.address)]: signatureBytes(signature)
}
};
// 5. Encode as a base64 wire transaction and send
await rpc
.sendTransaction(getBase64EncodedWireTransaction(signedMessage), {
preflightCommitment: 'confirmed',
encoding: 'base64'
})
.send();