Lesson 4. Complete the signing of transactions by API in CosmosSDK

Tram Ho

In the previous article, I showed you how to write a rest-server so that users can trade at API, but there is still a problem that users still have to use the command-line to sign and broadcast the transaction. Today, in this final article, I will show you how users can sign and broadcast transactions by API, contributing to making the application flow below for users more transparent.

1. Learn about the tx sign command and how to get user information from the system

Back to the previous article, when a user creates a transaction, that transaction is only in proposal form, the user must sign and broadcast it for it to take effect.

The tx sign statement has the following structure:

See the source of the command here . Pay attention to the following important parameters:

  • The parameter immediately behind the sign must be a file, in this case usignedTx.json
  • The parameter after the --from flag is either the name or address of the user who created the transaction
  • The after flag --sequence increases from zero after each person’s transaction whether or not the transaction is successful, the meaning of this parameter is to help the system to arrange the transactions in the correct order, avoiding being double spending.
  • The parameter immediately after the --accountn-number flag is the --accountn-number of that user in the system

Thus, in order for the user to be able to sign a transaction using the API, the request that the user has to send must have: unsigned transaction, the name or address of that user, sequence, account-number.

The parameters of address, sequence, account-number are parameters that are calculated and given to the user by the system, users cannot know it in advance, so I will first write a route so that users can query get my address through the name, then use the default route http://localhost:1317/auth/accounts/$address to query the sequence parameters, account-number , as well as the token balance of the that user.

Open the file x/nameservice/client/rest/query.go and add the following:

  • Import the os/exec package at the beginning of the file to be able to execute the command.

  • Add the request handler function below

Add a route to the file x/nameservice/client/rest/rest.go to call the above handler:

2. Learn the tx broadcast command

The tx broadcast statement has the following structure:

You can see the code of the statement here

  • The argument immediately following the broadcast must be a file in this case signedTx.json

Combining the two statements, we can visualize the flow of the route we are about to code as follows:

  • The user sends a request with the parameters: usignedTx , sequence , account number
  • The server writes unsignedTx to the file unsignedTx.json
  • The server implements the tx sign command implicitly and writes the result into the signedTx.json file
  • The server implements tx broadcast implicitly

3. Code

Open the file x/nameserivce/client/rest/tx.go and add the following code:

Open the file x/nameservice/client/rest/rest.go and add the following line:

4. Test

Make tools again, then init and start chain:

Open another terminal and start the server:

Open Post Man and test

  • Query address according to jack’s:
  • Query all jack infors:
  • jack to create the product:
  • The transaction log creates the product, noting the return value of the last route into the tx of this request
  • Query to see the product has been created
  • Query address of alice
  • Query all infice of alice:
  • Alice creates a buy product transaction
  • Alice signs a buy product transaction
  • Query the product again to see it passed to the owner
  • Query the information of the jack and the alice to see that 10nametoken has been moved from the alice to the jack

summary

So I have instructed you to complete this series, you have enough to be able to clone a repo model for the series here and customize it as you like.

Share the news now

Source : Viblo