Skip to main content
Sometimes, you may want to sign a transaction without broadcasting it. This is useful in situations where you want to sign a transaction and then broadcast it later. To showcase this, you will write a script that will sign a transaction, compute the transaction hash from the signed transaction, and compare the transaction hash to what you receive from broadcasting the transaction. First, import the necessary classes and functions.
  • Coins: A class that represents a list of Cosmos SDK coins.
  • Fee: A class that’s used to define the fee for a transaction.
  • RESTClient: A class that represents a REST client.
  • MnemonicKey: A class that’s used to create a key from a mnemonic.
  • MsgSend: A class that represents a message to send.
  • Wallet: A class that represents a wallet.
Next, set up your environment variables. You will need the following:
  • mnemonic: The mnemonic of the account that will be signing the transaction.
  • chainId: The chain ID of the network you are working on.
  • restUrl: The URL of the Initia REST.
  • gasPrices: The gas prices for the transaction.
You will also need to create a helper function to convert the signed transaction data into a transaction hash hex string.
Finally, create the main function to sign the transaction and compare the transaction hash. Let’s call this function offlineSigningTest. To start, create two RESTClient instances. One that is connected to the network, and one that is offline. You will use the offline REST client to sign the transaction, and the online REST client to broadcast the transaction to later compare.
Next, generate the wallet client, as well as define the message, gas limit, and fee for the transaction. You need to define the latter two because you will be signing the transaction completely offline without interacting with the network or any nodes.
Once you have all of that, create the transaction by calling wallet.createAndSignTx. This function takes
  • msgs: The message to send.
  • accountNumber: The account number of the account that will be signing the transaction.
  • sequence: The sequence number of the account that will be signing the transaction.
  • fee: The fee for the transaction.
and returns a signed transaction object. Then convert the signed transaction object to a byte array and compute the transaction hash using the helper function created earlier.
Finally, if you want to broadcast the transaction to the network later, you can do so by using the onlineRestClient client. If you then retrieve the transaction hash from the online REST client and compare it to the transaction hash you computed earlier, you should see that they match.

Full Example

src/signing-transaction.ts