How to sign transactions on Solana
In this post we are going to generate a simple transaction and then sign it on Solana Blockchain.
Last updated
In this post we are going to generate a simple transaction and then sign it on Solana Blockchain.
Last updated
There are 2 ways in which you can sign a Solana transaction:
Using a Solana wallet (phantom, sollet, etc) and approving manually in the browser.
Using a private key, which is useful if you want to automate the signing process say for example in the backend.
We have deployed a small project to test out transaction signing while working with Shyft APIs. Try it out here:
In this tutorial, we will cover how to sign transactions automatically without having to approve every time through a wallet.
We will create a simple transaction instruction to transfer some SOL from account A to B. Then sign it using a code snippet.
Below is the code to generate simple transfer instructions to transfer 1 SOL
, on devnet
:
The above code snippet returns base64
encoded version of the transaction.
Now, let's come to the second part where we take the response from the above function, sign, and send this transaction into the blockchain. Along with the encodedTransaction
string from the previous function, this function also needs the private_key
of the sender to successfully sign the transaction. The sender's private key is needed because while creating the transaction above we assigned the fromPubKey
as the tx.feePayer.
The above function signs and sends the transaction to Solana's devnet
and return the transaction signature.
You can use the code for above 2 functions to create a backend API to transfer SOL. This can be achived by exposing the first function via API call, which returns the encodedTransaction
. You can put the code in second function in your client side or FE. So that, your users never have to pass their privates keys over the network.
Hope this is useful. Happy hacking.