sdk

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: Apache-2.0 Imports: 20 Imported by: 4

README

IRISHUB Chain Go SDK

Irishub Chain GO SDK makes a simple package of API provided by Irishub, which provides great convenience for users to quickly develop applications based on irishub chain.

install

Requirement

Go version above 1.13.5

Use Go Mod
require (
    github.com/irisnet/irishub-sdk-go latest
)
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

Usage

Init Client

The initialization SDK code is as follows:

options := []types.Option{
types.KeyDAOOption(store.NewMemory(nil)),
types.TimeoutOption(10),
}
cfg, err := types.NewClientConfig(nodeURI, chainID, options...)
if err != nil {
panic(err)
}
client := sdk.NewIRISHUBClient(cfg)

The ClientConfig component mainly contains the parameters used in the SDK, the specific meaning is shown in the table below

Iterm Type Description
NodeURI string The RPC address of the irishub node connected to the SDK, for example: localhost: 26657
GRPCAddr string The GRPC address of the irishub node connected to the SDK, for example: localhost: 9090
Network enum irishub network type, value: Testnet,Mainnet
ChainID string ChainID of irishub, for example: irishub
Gas uint64 The maximum gas to be paid for the transaction, for example: 20000
Fee DecCoins Transaction fees to be paid for transactions
KeyDAO KeyDAO Private key management interface, If the user does not provide it, the default LevelDB will be used
Mode enum Transaction broadcast mode, value: Sync,Async, Commit
StoreType enum Private key storage method, value: Keystore,PrivKey
Timeout time.Duration Transaction timeout, for example: 5s
Level string Log output level, for example: info

If you want to use SDK to send a transfer transaction, the example is as follows:

There is more example of query and send tx

coins, err := types.ParseDecCoins("0.1iris")
to := "iaa1hp29kuh22vpjjlnctmyml5s75evsnsd8r4x0mm"
baseTx := types.BaseTx{
    From:     "username",
    Gas:      20000,
    Memo:     "test",
    Mode:     types.Commit,
    Password: "password",
}

result, err := client.Bank.Send(to, coins, baseTx)

query Latest Block info

block, err := client.BaseClient.Block(context.Background(),nil)

query Tx from specify TxHash

txHash := "D9280C9217B5626107DF9BC97A44C42357537806343175F869F0D8A5A0D94ADD"
txResult, err := client.BaseClient.QueryTx(txHash)

get TxHash before sending transactions

baseTx := types.BaseTx{
    From:     "username",
    Gas:      20000,
    Memo:     "test",
    Mode:     types.Commit,
    Password: "password",
}

amt, err := types.ParseCoins("10iris")

from := "iaa12py6r8hhzpwdhat93cde4p3rfl6w4qnmwcqfhn"
to := "iaa1hp29kuh22vpjjlnctmyml5s75evsnsd8r4x0mm"
msg := &bank.MsgSend{
	FromAddress: addr,
	ToAddress:   to,
	Amount:      amt,
}
txhash, err := s.BuildTxHash([]sdk.Msg{msg}, baseTx)

Note: If you use the relevant API for sending transactions, you should implement the KeyDAO interface. Use the NewKeyDaoWithAES method to initialize a KeyDAO instance, which will use the AES encryption method by default.

KeyDAO

The interface definition is as follows:

type KeyDAO interface {
    AccountAccess
    Crypto
}

type AccountAccess interface {
    Write(name string, store Store) error
    Read(name string) (Store,error)
    Delete(name string) error
}
type Crypto interface {
    Encrypt(data string, password string) (string, error)
    Decrypt(data string, password string) (string, error)
}

Among them, Store includes two storage methods, one is based on the private key, which is defined as follows:

type KeyInfo struct {
    PrivKey string `json:"priv_key"`
    Address string `json:"address"`
}

The other is based on the keystore, defined as follows:

type KeystoreInfo struct {
    Keystore string `json:"keystore"`
}

You can flexibly choose any of the private key management methods. The Encrypt and Decrypt interfaces are used to encrypt and decrypt the key. If the user does not implement it, the default is to use AES. Examples are as follows:

KeyDao implements the AccountAccess interface:

// Use memory as storage, use with caution in build environment
type MemoryDB struct {
    store map[string]Store
    AES
}

func NewMemoryDB() MemoryDB {
    return MemoryDB{
        store: make(map[string]Store),
    }
}
func (m MemoryDB) Write(name string, store Store) error {
    m.store[name] = store
    return nil
}

func (m MemoryDB) Read(name string) (Store, error) {
    return m.store[name], nil
}

func (m MemoryDB) Delete(name string) error {
    delete(m.store, name)
    return nil
}

func (m MemoryDB) Has(name string) bool {
    _, ok := m.store[name]
    return ok
}

If your keystore generated before irishub V0.16, you can see package keystore to convert

For more API usage documentation, please check documentation

Documentation

Overview

Package client is the entrance of the entire SDK function. SDKConfig is used to configure SDK parameters.

The SDK mainly provides the functions of the following modules, including: asset, bank, distribution, gov, keys, oracle, random, service, slashing, staking

As a quick start:

fees, err := types.ParseCoins("1iris")
if err != nil {
	panic(err)
}

client := sdk.NewClient(types.ClientConfig{
	NodeURI:   NodeURI,
	Network:   Network,
	ChainID:   ChainID,
	Gas:       Gas,
	Fee:       fees,
	Mode:      Mode,
	Online:    Online,
	StoreType: types.PrivKey,
	Level:     "debug",
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterInterfaces

func RegisterInterfaces(registry cdctypes.InterfaceRegistry)

RegisterInterfaces registers the sdk message type.

func RegisterLegacyAminoCodec

func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino)

RegisterLegacyAminoCodec registers the sdk message type.

Types

type IRISHUBClient

type IRISHUBClient struct {
	types.BaseClient
	Key     keys.Client
	Bank    bank.Client
	Token   token.Client
	Staking staking.Client
	Gov     gov.Client
	Service service.Client
	Record  record.Client
	Random  random.Client
	NFT     nft.Client
	Oracle  oracle.Client
	HTLC    htlc.Client
	Swap    coinswap.Client
	// contains filtered or unexported fields
}

func NewIRISHUBClient

func NewIRISHUBClient(cfg types.ClientConfig) IRISHUBClient

func (*IRISHUBClient) AppCodec

func (client *IRISHUBClient) AppCodec() codec.Marshaler

func (*IRISHUBClient) Codec

func (client *IRISHUBClient) Codec() *codec.LegacyAmino

func (*IRISHUBClient) EncodingConfig

func (client *IRISHUBClient) EncodingConfig() types.EncodingConfig

func (*IRISHUBClient) Manager

func (client *IRISHUBClient) Manager() types.BaseClient

func (*IRISHUBClient) Module

func (client *IRISHUBClient) Module(name string) types.Module

func (*IRISHUBClient) RegisterModule

func (client *IRISHUBClient) RegisterModule(ms ...types.Module)

func (*IRISHUBClient) SetLogger

func (client *IRISHUBClient) SetLogger(logger log.Logger)

Directories

Path Synopsis
client
tx
legacy
Package legacy contains a global amino Cdc which is deprecated but still used in several places within the SDK.
Package legacy contains a global amino Cdc which is deprecated but still used in several places within the SDK.
types
Package types defines a custom wrapper for google.protobuf.Any which supports cached values as well as InterfaceRegistry which keeps track of types which can be used with Any for both security and introspection
Package types defines a custom wrapper for google.protobuf.Any which supports cached values as well as InterfaceRegistry which keeps track of types which can be used with Any for both security and introspection
unknownproto
unknownproto implements functionality to "type check" protobuf serialized byte sequences against an expected proto.Message to report: a) Unknown fields in the stream -- this is indicative of mismatched services, perhaps a malicious actor b) Mismatched wire types for a field -- this is indicative of mismatched services Its API signature is similar to proto.Unmarshal([]byte, proto.Message) in the strict case if err := RejectUnknownFieldsStrict(protoBlob, protoMessage, false); err != nil { // Handle the error.
unknownproto implements functionality to "type check" protobuf serialized byte sequences against an expected proto.Message to report: a) Unknown fields in the stream -- this is indicative of mismatched services, perhaps a malicious actor b) Mismatched wire types for a field -- this is indicative of mismatched services Its API signature is similar to proto.Unmarshal([]byte, proto.Message) in the strict case if err := RejectUnknownFieldsStrict(protoBlob, protoMessage, false); err != nil { // Handle the error.
hd
keys/secp256k1/internal/secp256k1
nolint:gocritic Package secp256k1 wraps the bitcoin secp256k1 C library.
nolint:gocritic Package secp256k1 wraps the bitcoin secp256k1 C library.
Package modules is to warpped the API provided by each module of IRIS-Hub
Package modules is to warpped the API provided by each module of IRIS-Hub
gov
keys
Package keys allows you to manage your local tendermint keystore (wallets) for iris.
Package keys allows you to manage your local tendermint keystore (wallets) for iris.
nft
service
Package service bridge the gap between the blockchain world and the conventional business application world, by mediating a complete lifecycle of off-chain services -- from their definition, binding (provider registration), invocation, to their governance (profiling and dispute resolution).
Package service bridge the gap between the blockchain world and the conventional business application world, by mediating a complete lifecycle of off-chain services -- from their definition, binding (provider registration), invocation, to their governance (profiling and dispute resolution).
third_party
kv
tx
log
uuid
reference: https://github.com/binance-chain/go-sdk/blob/master/common/uuid/codec.go reference: https://github.com/binance-chain/go-sdk/blob/master/common/uuid/generator.go Package uuid reference: https://github.com/binance-chain/go-sdk/blob/master/common/uuid/uuid.go
reference: https://github.com/binance-chain/go-sdk/blob/master/common/uuid/codec.go reference: https://github.com/binance-chain/go-sdk/blob/master/common/uuid/generator.go Package uuid reference: https://github.com/binance-chain/go-sdk/blob/master/common/uuid/uuid.go

Jump to

Keyboard shortcuts

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