Skip to content

Instantly share code, notes, and snippets.

@codebender828
Last active May 30, 2022 10:09
Show Gist options
  • Save codebender828/ce768efcf1ba20cb61d699bdeb84a297 to your computer and use it in GitHub Desktop.
Save codebender828/ce768efcf1ba20cb61d699bdeb84a297 to your computer and use it in GitHub Desktop.
Sign Solana transaction from back end and send to chain
import { Transaction, Connection, clusterApiUrl } from "@solana/web3.js";
const connection = new Connection(clusterApiUrl("devnet"));
// Get the wallet object from your wallet provider
const wallet = useWallet()
export async function signTransaction(transaction) {
// 1. Get transaction form back end and construct buffer form it.
const serializedTransaction = Buffer.from(transaction, "base64");
const txt = Transaction.from(serializedTransaction);
// 2. Sign the transaction
const signedTransaction = await wallet.signTransaction(txt);
// 3. Send transaction to blockchain
const signature = await connection.sendRawTransaction(
signedTransaction.serialize()
);
// 4. Await confirmation on the blockchain
const result = await connection.confirmTransaction(signature);
return { result, signature };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment