cosmosign

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: Apache-2.0 Imports: 17 Imported by: 1

README

Cosmosign

Go Reference Build

Cosmosign is a lightweight Go library for signing and broadcasting transactions to Cosmos blockchains.

Installation

go get github.com/shapeshed/cosmosign

Quickstart

package main

import (
	"log"

	"github.com/cosmos/cosmos-sdk/types"
	banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
	cosmosign "github.com/shapeshed/cosmosign"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"

)

func main() {
	conn, err := grpc.NewClient("localhost:1919", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("Failed to initialise grpc connection: %v", err)
		return
	}

	cs, err := cosmosign.NewClient(
		cosmosign.WithGRPCConn(conn),
		cosmosign.WithGasPrices("0.0ustake"),
		cosmosign.WithKeyringBackend("pass"),
		cosmosign.WithKeyringRootDir("/home/cosmos/"),
		cosmosign.WithKeyringUID("account1"),
	)
	if err != nil {
		log.Fatalf("Failed to initialise cosmosign: %v", err)
	}

	fromAddress := "cosmos1..."
	toAddress := "cosmos1..."
	amount, err := types.ParseCoinsNormalized("1000ustake")
	if err != nil {
		log.Fatalf("Failed to parse amount: %v", err)
	}

	msg := banktypes.NewMsgSend(fromAddress, toAddress, amount)

	res, err := cs.SendMessages(msg)
	if err != nil {
		log.Fatalf("Failed to send transaction: %v", err)
	}

	if res.TxResponse.Code == 0 {
		log.Printf("Transaction successful, hash: %s", res.TxResponse.TxHash)
	} else {
		log.Printf("Transaction failed, code: %d, log: %s", res.TxResponse.Code, res.TxResponse.RawLog)
	}
}

Options

You may pass an arbitrary number of options when creating the cosmosign client. Each option has a default value, but you may override them using the available With methods.

Option Description Default Value Method to Override
Fees Transaction fees "" WithFees(string)
FeeGranter Address of the fee granter "" WithFeeGranter(string)
FeePayer Address of the fee payer "" WithFeePayer(string)
Gas Gas limit "" WithGas(uint64)
GasPrices Gas prices to pay per unit of gas "0.0ustake" WithGasPrices(string)
GasMultiplier Multipler for gas estimation "1.0" WithGasMultiplier(float64)
KeyringUID Identifier for keyring account "" WithKeyringUID(string)
KeyringBackend Backend used for keyring "" WithKeyringBackend(string)
KeyringRootDir Root directory path for the keyring "" WithKeyringRootDir(string)
Memo Transaction memo "" WithMemo(string)

Updating an existing client

Options can be updated on an instantiated client.

err = cs.ApplyOptions(
    cosmosign.WithKeyringUID("another-signer"),
    cosmosign.WithGasMulplier(2.0),
    cosmosign.WithGasPrices("0.025ustake"),
    cosmosign.WithMemo("doge ftw"),
)

License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for more details.

Documentation

Index

Examples

Constants

View Source
const (
	DefaultAddressPrefix = "cosmos"
	DefaultGasMultiplier = 1.0
	DefaultGasPrices     = "0.0ustake"
	DefaultTimeout       = 5 * time.Second
)

Variables

View Source
var ErrGRPCClientIsNil = errors.New("grpc client must be set")

Functions

This section is empty.

Types

type Cosmosign

type Cosmosign struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(ctx context.Context, opts ...Option) (*Cosmosign, error)

NewClient initializes a new cosmosign instance

Example
package main

import (
	"context"
	"log"

	"github.com/shapeshed/cosmosign"
)

func main() {
	ctx := context.Background()
	// Initialize cosmosign client (this requires network connectivity)
	cs, err := cosmosign.NewClient(ctx,
		cosmosign.WithGasPrices("0.0ustake"),
		cosmosign.WithKeyringBackend("pass"),
		cosmosign.WithKeyringRootDir("/home/cosmos/"),
		cosmosign.WithKeyringUID("account1"),
	)
	if err != nil {
		log.Fatalf("Failed to initialise cosmosign: %v", err)
	}

	// Log the initialized client
	log.Printf("Cosmosign client initialized: %+v", cs)

	// Note: This example will not run because it requires external network connectivity
	// Output is omitted intentionally to avoid failures in testing environments.
}
Output:

func (*Cosmosign) ApplyOptions

func (c *Cosmosign) ApplyOptions(opts ...Option) error

ApplyOptions applies options to the running client

Example
package main

import (
	"context"
	"log"

	"github.com/shapeshed/cosmosign"
)

func main() {
	ctx := context.Background()
	cs, err := cosmosign.NewClient(ctx,
		cosmosign.WithGasPrices("0.0ustake"),
		cosmosign.WithKeyringBackend("pass"),
		cosmosign.WithKeyringRootDir("/home/cosmos/"),
		cosmosign.WithKeyringUID("account1"),
	)
	if err != nil {
		log.Fatalf("Failed to initialise cosmosign: %v", err)
	}

	err = cs.ApplyOptions(
		cosmosign.WithGasPrices("0.01ustake"),
	)
	if err != nil {
		log.Fatalf("Failed to apply options: %v", err)
	}

	log.Printf("Cosmosign client after applying options: %+v", cs)

	// This example will not run as it requires network connectivity
}
Output:

func (*Cosmosign) SendMessages

func (c *Cosmosign) SendMessages(msgs ...sdktypes.Msg) (*txtypes.BroadcastTxResponse, error)

SendMessages signs and broadcasts a transaction with one or more messages

func (*Cosmosign) SendMessagesWaitTx

func (c *Cosmosign) SendMessagesWaitTx(msgs ...sdktypes.Msg) (*txtypes.GetTxResponse, error)

SendMessagesWaitTx broadcasts a message and waits for it to be confirmed, returning the result.

type Option

type Option func(*Cosmosign)

Option is a function that configures the Cosmosign client.

func WithFeeGranter

func WithFeeGranter(feeGranter sdktypes.AccAddress) Option

WithFeeGranter sets the fee granter for the client.

func WithFeePayer

func WithFeePayer(feePayer sdktypes.AccAddress) Option

WithFeePayer sets the fee payer for the client.

func WithFees

func WithFees(fees string) Option

WithFees sets the fees for the client.

func WithGRPCConn added in v0.1.0

func WithGRPCConn(grpcConn *grpc.ClientConn) Option

WithGasPrices sets the multipler for gas simulation amount.

func WithGas

func WithGas(gas uint64) Option

WithGas sets the gas limit for the client.

func WithGasMultipler

func WithGasMultipler(gasMultiplier float64) Option

WithGasPrices sets the multipler for gas simulation amount.

func WithGasPrices

func WithGasPrices(gasPrices string) Option

WithGasPrices sets the gas prices used for the client.

func WithKeyringAppName added in v0.2.0

func WithKeyringAppName(keyringAppName string) Option

WithKeyringUID sets the keyring uid (account) to use in signing.

func WithKeyringBackend

func WithKeyringBackend(keyringBackend string) Option

WithKeyringBackend sets the backend to use for the keyring.

func WithKeyringRootDir

func WithKeyringRootDir(keyringRootDir string) Option

WithKeyringBackend sets the backend to use for the keyring.

func WithKeyringUID

func WithKeyringUID(keyringUID string) Option

WithKeyringUID sets the keyring uid (account) to use in signing.

func WithMemo

func WithMemo(memo string) Option

WithMemo sets the memo to use on the transaction.

Jump to

Keyboard shortcuts

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