cosmos

package module
v0.0.0-...-be50d8c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 19 Imported by: 1

README

cosmos-sdk

Cosmos SDK is used to interact with the Cosmos blockchain, it contains various functions can be used to web3 wallet. The SDK not only support Atom, it also supports following chains:

  • Atom
  • Axelar
  • Cronos
  • Evmos
  • Iris
  • Juno
  • Kava
  • Kujira
  • Okc
  • Osmos
  • Secret
  • Sei
  • Stargaze
  • Terra
  • Tia

Installation

go get

To obtain the latest version, simply require the project using :

go get -u github.com/okx/go-wallet-sdk/coins/cosmos

Usage

Supported Functions
// NewAddress
// GetAddressByPublicKey
// ValidateAddress
// GetRawTransaction
// SignRawTransaction
// SignMessage
// Transfer
// IbcTransfer
address
	// address
	hrp := "cosmos"
	pri := "1790962db820729606cd7b255ace1ac5ebb129ac8e9b2d8534d022194ab25b37"
	address, err := cosmos.NewAddress(pri, hrp, false)
	if err != nil {
		// todo
		fmt.Println(err)
	}
	fmt.Println(address)
	p, _ := hex.DecodeString(pri)
	_, pub := btcec.PrivKeyFromBytes(p)
	a, err := cosmos.GetAddressByPublicKey(hex.EncodeToString(pub.SerializeCompressed()), hrp)
	if err != nil {
		// todo
		fmt.Println(err)
	}
	fmt.Println(a == address)

	// ValidateAddress
	valid := cosmos.ValidateAddress(address, hrp)
	fmt.Println(valid)
transfer
	// GetRawTransaction and SignRawTransaction
	// transfer
	pk, err := hex.DecodeString(pri)
	k, _ := btcec.PrivKeyFromBytes(pk)
	param := cosmos.TransferParam{}
	param.FromAddress = "cosmos145q0tcdur4tcx2ya5cphqx96e54yflfyqjrdt5"
	param.ToAddress = "cosmos1jun53r4ycc8g2v6tffp4cmxjjhv6y7ntat62wn"
	param.Demon = "uatom"
	param.Amount = "10000"
	param.CommonParam.ChainId = "cosmoshub-4"
	param.CommonParam.Sequence = 0
	param.CommonParam.AccountNumber = 623151
	param.CommonParam.FeeDemon = "uatom"
	param.CommonParam.FeeAmount = "10"
	param.CommonParam.GasLimit = 100
	param.CommonParam.Memo = "memo"
	param.CommonParam.TimeoutHeight = 0
	doc, err := cosmos.GetRawTransaction(param, hex.EncodeToString(k.PubKey().SerializeCompressed()))
	signature, err := cosmos.SignRawTransaction(doc, k)
	signedTransaction, err := cosmos.GetSignedTransaction(doc, signature)
	if err != nil {
		// todo
		fmt.Println(err)
	}
	fmt.Println(signedTransaction)
SignMessage
	// SignMessage
	data := "{\n  \"chain_id\": \"cosmoshub-4\",\n  \"account_number\": \"584406\",\n  \"sequence\": \"1\",\n  \"fee\": {\n    \"gas\": \"250000\",\n    \"amount\": [\n      {\n        \"denom\": \"uatom\",\n        \"amount\": \"0\"\n      }\n    ]\n  },\n  \"msgs\": [\n    {\n      \"type\": \"atom/gamm/swap-exact-amount-in\",\n      \"value\": {\n        \"sender\": \"cosmos145q0tcdur4tcx2ya5cphqx96e54yflfyqjrdt5\",\n        \"routes\": [\n          {\n            \"poolId\": \"722\",\n            \"tokenOutDenom\": \"ibc/6AE98883D4D5D5FF9E50D7130F1305DA2FFA0C652D1DD9C123657C6B4EB2DF8A\"\n          }\n        ],\n        \"tokenIn\": {\n          \"denom\": \"uatom\",\n          \"amount\": \"10000\"\n        },\n        \"tokenOutMinAmount\": \"3854154180813018\"\n      }\n    }\n  ],\n  \"memo\": \"\"\n}"
	signedMessage, _, err := cosmos.SignMessage(data, pri)
	if err != nil {
		// todo
		fmt.Println(err)
	}
	fmt.Println(signedMessage)
IbcTransfer
	// IbcTransfer
	paramIbc := cosmos.IbcTransferParam{}
	paramIbc.CommonParam.ChainId = "evmos_9001-2"
	paramIbc.CommonParam.Sequence = 1
	paramIbc.CommonParam.AccountNumber = 2091572
	paramIbc.CommonParam.FeeDemon = "aevmos"
	paramIbc.CommonParam.FeeAmount = "4000000000000000"
	paramIbc.CommonParam.GasLimit = 200000
	paramIbc.CommonParam.Memo = ""
	paramIbc.CommonParam.TimeoutHeight = 0
	paramIbc.FromAddress = "evmos1yc4q6svsl9xy9g2gplgnlpxwhnzr3y73wfs0xh"
	paramIbc.ToAddress = "cosmos1rvs5xph4l3px2efynqsthus8p6r4exyr7ckyxv"
	paramIbc.Demon = "aevmos"
	paramIbc.Amount = "10000000000000000"
	paramIbc.SourcePort = "transfer"
	paramIbc.SourceChannel = "channel-3"
	paramIbc.TimeOutInSeconds = uint64(time.Now().UnixMilli()/1000) + 300
	signedIBCTx, err := cosmos.IbcTransferAction(paramIbc, pri, true)
	if err != nil {
		// todo
		fmt.Println(err)
	}
	fmt.Println(signedIBCTx)

License

Most packages or folder are MIT licensed, see package or folder for the respective license.

Documentation

Index

Constants

View Source
const (
	ATOM_HRP = "cosmos"
)

Variables

This section is empty.

Functions

func BuildTx

func BuildTx(param CommonParam, messages []*types.Any, privateKeyHex string) (string, error)

func BuildTxAction

func BuildTxAction(param CommonParam, messages []*types.Any, privateKeyHex string, useEthSecp256k1 bool) (string, error)

func BuildTxActionForSignMessage

func BuildTxActionForSignMessage(param CommonParam, messages []*types.Any, privateKeyHex string, useEthSecp256k1 bool) (string, string, error)

func GetAddressByPublicKey

func GetAddressByPublicKey(pubKeyHex string, HRP string) (string, error)

func GetRawJsonTransaction

func GetRawJsonTransaction(param interface{}) (string, error)

func GetRawTransaction

func GetRawTransaction(param interface{}, publicKeyCompressed string) (string, error)

GetRawTransaction Gets the string to be signed param - (TransferParam | IbcTransferParam) publicKeyCompressed - (hex 33 bytes) return signDoc

func GetSignedJsonTransaction

func GetSignedJsonTransaction(signDoc string, publicKey string, signature string) (string, error)

func GetSignedTransaction

func GetSignedTransaction(signDoc string, signature string) (string, error)

GetSignedTransaction - Getting signed transactions signDoc - Transaction string to be signed(hex) signature - Transaction signature (hex)

func GetSigningHash

func GetSigningHash(rawTxByte string) (string, error)

func HashMessage

func HashMessage(p []byte) []byte

func IbcTransfer

func IbcTransfer(param IbcTransferParam, privateKeyHex string) (string, error)

func IbcTransferAction

func IbcTransferAction(param IbcTransferParam, privateKeyHex string, useEthSecp256k1 bool) (string, error)

func MakeTransactionWithMessage

func MakeTransactionWithMessage(p CommonParam, publicKeyCompressed string, messages []*types.Any) (string, error)

func MakeTransactionWithSignDoc

func MakeTransactionWithSignDoc(body string, auth string, ChainId string, AccountNumber uint64) (string, error)

func NewAddress

func NewAddress(privateKey string, hrp string, followETH bool) (string, error)

func SignAminoMessage

func SignAminoMessage(data string, privateKeyHex string) (string, error)

func SignDoc

func SignDoc(body string, auth string, privateKeyHex string, ChainId string, AccountNumber uint64) (string, string, error)

func SignMessage

func SignMessage(data string, privateKeyHex string) (string, string, error)

SignMessage Sign the message, using JSON format

func SignMessageAction

func SignMessageAction(data string, privateKeyHex string, useEthSecp256k1 bool) (string, string, error)

func SignRawJsonTransaction

func SignRawJsonTransaction(signDoc string, privateKey *btcec.PrivateKey) (string, error)

func SignRawTransaction

func SignRawTransaction(signDoc string, privateKey *btcec.PrivateKey) (string, error)

func Transfer

func Transfer(param TransferParam, privateKeyHex string) (string, error)

func TransferAction

func TransferAction(param TransferParam, privateKeyHex string, useEthSecp256k1 bool) (string, error)

func ValidateAddress

func ValidateAddress(address string, hrp string) bool

Types

type CommonParam

type CommonParam struct {
	ChainId       string
	Sequence      uint64
	AccountNumber uint64
	FeeDemon      string
	FeeAmount     string
	GasLimit      uint64
	Memo          string
	TimeoutHeight uint64
}

type IbcTransferParam

type IbcTransferParam struct {
	CommonParam
	FromAddress      string
	ToAddress        string
	Demon            string
	Amount           string
	SourcePort       string
	SourceChannel    string
	TimeOutHeight    ibc.Height
	TimeOutInSeconds uint64
}

type MessageData

type MessageData struct {
	ChainId       string `json:"chain_id,omitempty"`
	AccountNumber string `json:"account_number,omitempty"`
	Sequence      string `json:"sequence,omitempty"`
	Fee           struct {
		Gas    string       `json:"gas,omitempty"`
		Amount []types.Coin `json:"amount,omitempty"`
	} `json:"fee,omitempty"`
	Msgs []struct {
		T string      `json:"type,omitempty"`
		V interface{} `json:"value,omitempty"`
	} `json:"msgs,omitempty"`
	Memo string `json:"memo,omitempty"`
}

type TransferParam

type TransferParam struct {
	CommonParam
	FromAddress string
	ToAddress   string
	Demon       string
	Amount      string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL