Connect with us

Crypto News

How To Sign Transactions Offline in Solana Using Anchor | by Senudajayalath | Coinmonks | Mar, 2023

This article explains how to sign and execute your transactions separately using Anchor framework

Some security models require keeping signing keys, and thus the signing process, separated from transaction creation and network broadcast. For example, imagine you have built a DAPP on Solana in which the transactions should be executed in the backend. Since it is not possible to transfer signing keys to the backend (due to safety issues), the approach to follow is to get your users to sign each transaction on the frontend and send it to the backend to be executed.

Now let us see step by step on how to do this.

VScode β€” Code editor
Anchor version β€” 0.24.2

Previous knowledge on Anchor framework

After you have anchor installed create a new anchor project using anchor init<project_name>

This is the code I have written for this tutorial which will transfer SOL from to account to from account

As you can see here I have created a function called transfer_sol which takes in a struct called TransferSOL. This struct has 3 accounts called from,to, & system_program. The from account act as the signer for this transaction.

So the intention of this exercise would be to sign the transfer_sol transaction in the frontend and pass the signed transaction to the backend using an API request.

View the full Anchor code here

So now lets see how the frontend code for this looks like,

For this I have created a React project.

This is the part where we call the smart contract we created earlier using the frontend. Prior to this we should have deployed the smart contract and imported the idl.json generated from it.

As shown above we have created a Transaction which calls the transferSOL function. Unlike the most common program.methods.transferSol().rps() function which executes the Anchor function immediately program.transfer.transferSol() creates a transaction of the type web3.Transaction without executing it.

Now we need to sign the created transaction. Before we do that, we need to configure the blockhash and feepayer of the transaction as follows,

An important point to remember is that the latestBlockHash is valid till around 1 minute from the point it was added to the transaction. Exactly speaking it is valid until another 150 blocks have validated or around 1 min 19secs according to the Solana documentation.
Therefore, the transaction has to be executed before that.

Now lets get to the signing part.

As you can remember the transfersol function only needs one signer who is the from account. Therefore, we sign the transaction with the provider.wallet who is the from account.

If your transaction required any other signers except your connected wallet you can use
let signed = await tx.sign(); or
let signed = await tx.setSigners(); for a list of signers.

Now we are ready to execute the signed transaction. You can always send the variable signed over an API request to your backend or do whatever you want with it and execute the transaction. But keep in mind you have to do it within 1 minute πŸ˜‰

In my case I will be sending it to my backend where the transaction will be executed. The code is shown below,

If you are also sending your signed transaction over an API request to your backend keep in mind you should be sending the blockhash as well with the variable signed. (This is not a necessity, but always good)

And there we have it 😌. Now we don’t need to execute the transaction when we call the function from the frontend. We can sign the transaction and pass it on!!.

The complete code for the frontend is here

  1. Anchor Code β€”
  2. Frontend Code β€”

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

#Sign #Transactions #Offline #Solana #Anchor #Senudajayalath #Coinmonks #Mar

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *