import (
"strings"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types"
)
func NewKeyring() (keyring.Keyring, error) {
var kr keyring.Keyring
keyringPassword := "password"
runtimeHomeDir := "~/.cosmosHome"
reader := strings.NewReader("")
reader.Reset(keyringPassword + "\n")
kr, err := keyring.New(
types.KeyringServiceName(),
keyring.BackendTest
runtimeHomeDir,
reader,
)
if err != nil {
return kr, err
}
return kr, nil
}
import (
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)
func CreateAccount() (string, error) {
keyringPassword := "password"
kr, err = NewKeyring()
if err != nil {
return "", err
}
algo, _ := kr.SupportedAlgorithms()
signingAlgo, err := keyring.NewSigningAlgoFromString("secp256k1", algo)
if err != nil {
return "", err
}
uid := genUID()
mnemonicInfo, _, err := kr.NewMnemonic(uid, keyring.English, "", keyringPassword, signingAlgo)
if err != nil {
return "", err
}
return mnemonicInfo.GetAddress().String(), nil
}
func genUID() string {
// Generate a string composed of 40 randomized alphanumeric characters.
}
import (
"crypto/tls"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
/* Create a GRPC client connnection with TLS. */
func CreateGrpcConnTls() (*grpc.ClientConn, error) {
var grpcConn *grpc.ClientConn
var err error
// Specify the fully qualified hostname and port of the validator node.
grpcHost := "…Hostname…"
tlsCfg := &tls.Config{
InsecureSkipVerify: true,
}
grpcConn, err = grpc.Dial(
grpcHost,
grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg)),
)
return grpcConn, err
}
/* Create a GRPC client connnection without TLS. */
func CreateGrpcConn() (*grpc.ClientConn, error) {
var grpcConn *grpc.ClientConn
var err error
// Specify the fully qualified hostname and port of the validator node.
grpcHost := "…Hostname…"
grpcConn, err = grpc.Dial(
grpcHost,
grpc.WithInsecure(),
)
return grpcConn, err
}
import (
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/tendermint/tendermint/proto/tendermint/p2p"
)
func GetNodeInfo() (*p2p.DefaultNodeInfo, error) {
var nodeInfo *p2p.DefaultNodeInfo
grpcConn, err := CreateGrpcConnTls()
if err != nil {
return nodeInfo, err
}
defer grpcConn.Close()
serviceClient := tmservice.NewServiceClient(grpcConn)
nodeInfoRes, err := serviceClient.GetNodeInfo(
context.Background(),
&tmservice.GetNodeInfoRequest{},
)
if err != nil {
return nodeInfo, err
}
nodeInfo = nodeInfoRes.GetDefaultNodeInfo()
return nodeInfo, nil
}
import (
"context"
"encoding/base64"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
)
func GetLatestBlock() (*tmservice.GetLatestBlockResponse, error) {
var latestBlockRes *tmservice.GetLatestBlockResponse
grpcConn, err := CreateGrpcConnTls()
if err != nil {
return latestBlockRes, err
}
defer grpcConn.Close()
serviceClient := tmservice.NewServiceClient(grpcConn)
latestBlockRes, err = serviceClient.GetLatestBlock(
context.Background(),
&tmservice.GetLatestBlockRequest{},
)
if err != nil {
return latestBlockRes, err
}
return latestBlockRes, nil
}
/* Get the attributes */
latestBlockRes, err := GetLatestBlock()
block := latestBlockRes.GetBlock() // Type: Block
header := block.GetHeader() // Type: Header
height := header.GetHeight()
dataHash := header.GetDataHash()
blockId := latestBlockRes.GetBlockId() // Type: BlockID
blockIdHash := base64.StdEncoding.EncodeToString(blockId.GetHash())
import (
"context"
"encoding/base64"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
)
func GetBlockByHeight(height int64) (*tmservice.GetBlockByHeightResponse, error) {
var blockRes *tmservice.GetBlockByHeightResponse
grpcConn, err := CreateGrpcConnTls()
if err != nil {
return blockRes, err
}
defer grpcConn.Close()
serviceClient := tmservice.NewServiceClient(grpcConn)
blockRes, err = serviceClient.GetBlockByHeight(
context.Background(),
&tmservice.GetBlockByHeightRequest{Height: height},
)
if err != nil {
return blockRes, err
}
return blockRes, nil
}
/* Get the attributes */
height int64 := 12345
blockRes, err := GetBlockByHeight(height)
block := blockRes.GetBlock() // Type: Block
header := block.GetHeader() // Type: Header
blockId := blockRes.GetBlockId() // Type: BlockID
blockIdHash := base64.StdEncoding.EncodeToString(blockId.GetHash())
import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func GetBalance(accountAddress, denom string) (*sdktypes.Coin, error) {
var balance *sdktypes.Coin
addr, err := sdktypes.AccAddressFromBech32(accountAddress)
if err != nil {
return balance, err
}
grpcConn, err := CreateGrpcConnTls()
if err != nil {
return balance, err
}
defer grpcConn.Close()
bankClient := banktypes.NewQueryClient(grpcConn)
balanceRes, err := bankClient.Balance(
context.Background(),
&banktypes.QueryBalanceRequest{Address: addr.String(), Denom: denom},
)
if err != nil {
return balance, err
}
balance = balanceRes.GetBalance()
return balance, nil
}
/* Get the attributes */
accountAddress := "……"
denom := "……"
balance, err := GetBalance(accountAddress, denom) // Type: Coin
amount := balance.Amount
func createTxn() (string, error) {
txBytes, err := CreateSignedTxn()
if err != nil {
return "", err
}
txHash, _, err := BroadcastSignedTxn(txBytes)
if err != nil {
return "", err
}
return txHash, nil
}
Import (
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
func CreateSignedTxn() ([]byte, error) {
encodingConfig := cosmoscmd.MakeEncodingConfig(catxapp.ModuleBasics)
txConfig := encodingConfig.TxConfig
txBuilder := txConfig.NewTxBuilder()
kr, err := NewKeyring()
// Specify the from account address.
fromAddr, err := sdktypes.AccAddressFromBech32("…From Address…")
// Specify the to account address.
toAddr, err := sdktypes.AccAddressFromBech32("…To Address…")
// Specify the amount of tokens to be sent.
amount := ……
// Specify the denomination.
coin := sdktypes.NewInt64Coin("…Denomination…", amount)
coins := sdktypes.NewCoins(coin)
msg := banktypes.NewMsgSend(fromAddr, toAddr, coins)
if err := txBuilder.SetMsgs(msg); err != nil { return nil, err }
// Specify the fee.
fee := ……
// Specify the denomination.
feeAmt := sdktypes.Coin{Denom: "…Denomination…", Amount: sdktypes.NewInt(fee)}
feeAmount := sdktypes.NewCoins(feeAmt)
txBuilder.SetFeeAmount(feeAmount)
// Specify the gas limit.
gasLimit := ……
txBuilder.SetGasLimit(gasLimit)
keyInfo, err := kr.KeyByAddress(fromAddr)
// Specify the passphrase.
exportedPrivateKey, err := kr.ExportPrivKeyArmor(keyInfo.GetName(), "…Passphrase…")
// Specify the passphrase.
privateKey, _, err := crypto.UnarmorDecryptPrivKey(exportedPrivateKey, "…Passphrase…"])
// Specify the from account address.
account, err := GetAccount("…From Address…")
accNumber := account.AccountNumber
accSequence := account.Sequence
privKeyList := []cryptotypes.PrivKey{privateKey}
accNumberList := []uint64{accNumber}
accSeqList := []uint64{accSequence}
/* First round: Gather all signer infos. */
var signatureList []signing.SignatureV2
for idx, privKey := range privKeyList {
sig := signing.SignatureV2{
PubKey: privKey.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: encodingConfig.TxConfig.SignModeHandler().DefaultMode(),
Signature: nil,
},
Sequence: accSeqList[idx],
}
signatureList = append(signatureList, sig)
}
if err := txBuilder.SetSignatures(signatureList...); err != nil { return nil, err }
/* Second round: Signed by each signer. */
var signingList []signing.SignatureV2
for idx, privKey := range privKeyList {
signerData := authsigning.SignerData {
// Specify the chain ID.
ChainID: "……",
AccountNumber: accNumberList[idx],
Sequence: accSeqList[idx],
}
signMode := encodingConfig.TxConfig.SignModeHandler().DefaultMode()
sig, err := tx.SignWithPrivKey(signMode, signerData, txBuilder, privKey,
encodingConfig.TxConfig, accSeqList[idx])
signingList = append(signingList, sig)
}
if err := txBuilder.SetSignatures(signingList...); err != nil { return nil, err }
txBytes, err := encodingConfig.TxConfig.TxEncoder()(txBuilder.GetTx())
return txBytes, nil
}
import (
"context"
"strconv"
"github.com/cosmos/cosmos-sdk/types/tx"
)
func BroadcastSignedTxn(txBytes []byte) (string, int64, error) {
grpcConn, err := CreateGrpcConnTls()
if err != nil {
return "", int64(0), err
}
defer grpcConn.Close()
serviceClient := tx.NewServiceClient(grpcConn)
broadcastTxRes, err := serviceClient.BroadcastTx(
context.Background(),
&tx.BroadcastTxRequest{
Mode: tx.BroadcastMode_BROADCAST_MODE_BLOCK,
TxBytes: txBytes,
},
)
if err != nil {
return "", int64(0), err
}
txResponse := broadcastTxRes.GetTxResponse()
if txResponse.Code != 0 {
return "", int64(0),
errors.New("TxResponse.Code: " + strconv.FormatUint(uint64(txResponse.Code), 10))
}
return txResponse.TxHash, txResponse.Height, nil
}
import (
"context"
sdktypes "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
)
func GetTxnByHash(txHash string) (*sdktypes.TxResponse, error) {
var txResponse *sdktypes.TxResponse
grpcConn, err := CreateGrpcConnTls()
if err != nil {
return txResponse, err
}
defer grpcConn.Close()
serviceClient := txtypes.NewServiceClient(grpcConn)
txRes, err := serviceClient.GetTx(
context.Background(),
&txtypes.GetTxRequest{Hash: txHash},
)
if err != nil {
return txResponse, err
}
txResponse = txRes.GetTxResponse()
return txResponse, nil
}
/* Get the attributes */
txHash := "……"
txRes, err := GetTxnByHash(txHash)
height := txRes.Height
import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
func GetAccount(accountAddress string) (types.BaseAccount, error) {
var baseAccount types.BaseAccount
addr, err := sdktypes.AccAddressFromBech32(accountAddress)
if err != nil {
return baseAccount, err
}
grpcConn, err := CreateGrpcConnTls()
if err != nil {
return baseAccount, err
}
defer grpcConn.Close()
accountClient := types.NewQueryClient(grpcConn)
accountRes, err := accountClient.Account(
context.Background(),
&types.QueryAccountRequest{Address: addr.String()},
)
if err != nil {
return baseAccount, err
}
accountData := accountRes.GetAccount().Value
if err := baseAccount.XX_Unmarshal(accountData); err != nil {
return baseAccount, err
}
return baseAccount, nil
}
import (
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
func GetValidatorSet() ([]types.Validator, error) {
var validatorList []types.Validator
grpcConn, err := CreateGrpcConnTls()
if err != nil {
return validatorList, err
}
defer grpcConn.Close()
queryClient := types.NewQueryClient(grpcConn)
validatorsRes, err := queryClient.Validators(
context.Background(),
&types.QueryValidatorsRequest{},
if err != nil {
return validatorList, err
}
validatorList = validatorsRes.GetValidators()
return validatorList, nil
}
type Block struct {
Header `json:"header"`
……
}
type BlockID struct {
Hash tmbytes.HexBytes `json:"hash"`
……
}
type Coin struct {
Denom string `json:"denom,omitempty"`
Amount Int `json:"amount"`
}
type Header struct {
Height int64 `json:"height"`
DataHash tmbytes.HexBytes `json:"data_hash"`
……
}
type QueryBalanceRequest struct {
Address string `json:"address,omitempty"`
Denom string `json:"denom,omitempty"`
}
type QueryBalanceResponse struct {
Balance *types.Coin `json:"balance,omitempty"`
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
27 | |
25 | |
19 | |
14 | |
13 | |
11 | |
10 | |
9 | |
8 | |
6 |