Last active
May 30, 2022 10:09
-
-
Save codebender828/ce768efcf1ba20cb61d699bdeb84a297 to your computer and use it in GitHub Desktop.
Sign Solana transaction from back end and send to chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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