sdkgo

package module
v0.3.11 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: Apache-2.0 Imports: 5 Imported by: 2

README

Go sdk for CESS network

GitHub license Go Reference build Go Report Card

The go sdk implementation of the CESS network, which provides RPC calls, status queries, block transactions and other functions.

📝 Reporting Vulnerability

If you find out any system bugs or you have a better suggestions, please send an email to frode@cess.one or join CESS discord to communicate with us.

📢 Announcement

CESS test network rpc endpoints

wss://testnet-rpc0.cess.cloud/ws/
wss://testnet-rpc1.cess.cloud/ws/
wss://testnet-rpc2.cess.cloud/ws/

CESS test network bootstrap node

_dnsaddr.boot-kldr-testnet.cess.cloud

🚰 CESS test network faucet

https://testnet-faucet.cess.cloud/

🏗 Usage

To get the package use the standard:

go get -u "github.com/CESSProject/cess-go-sdk"

✅ Testing

To run test:

  1. Run a CESS node locally.

  2. Run the command

    go test -v
    

📖 Documentation & Examples

Please refer to: https://pkg.go.dev/github.com/CESSProject/cess-go-sdk

💡 Example

Usually, you only care about how to access your data in the CESS network, you need to build such a web service yourself, this sdk will help you quickly realize data access. Note that p2p-go library needs to be used to enable the data transmission.

Create an sdk client

To create an sdk client, you need to provide some configuration information: your rpc address (if not, use the rpc address disclosed by CESS), your wallet private key, and transaction timeout. Please refer to the following examples:

sdk, err := cess.New(
	context.Background(),
	config.CharacterName_Client,
	cess.ConnectRpcAddrs([]string{"<rpc addr>"}),
	cess.Mnemonic("<your account mnmonic>"),
	cess.TransactionTimeout(time.Second * 10),
)
Create an sdk client with p2p functionality

When you need to use all the functions of the sdk, you need to initialize an sdk with a p2p network. For example, the storage nodes of cess and deoss both use the sdk with a p2p network. Refer to the following code:

sdk, err := cess.New(
	context.Background(),
	config.CharacterName_Client,
	cess.ConnectRpcAddrs([]string{"<rpc addr>"}),
	cess.Mnemonic("<your account mnmonic>"),
	cess.TransactionTimeout(time.Second * 10),
	cess.Workspace("<work space>"),
	cess.P2pPort(<port>),
	cess.Bootnodes([]string{"<bootstrap node>"}),
	cess.ProtocolPrefix("<protocol prefix>"),
)
Create storage data bucket

cess as an object storage service, the data are stored in buckets, which can be created automatically when uploading data, or separately, refer to the following code:

sdk.CreateBucket(sdk.GetSignatureAccPulickey(), "<your bucket name>")
Store data

You need to purchase space with your account before uploading files, please refer to Buy Space. The following is an example of uploading a file:

sdk.StoreFile("<your file>", "<your bucket name>")
Retrieve data

To retrieve the data, you need to provide the unique hash of the data, which will be returned to you when the data is uploaded successfully, here is the sample code to retrieve the data:

sdk.RetrieveFile("<file hash>", "<save path>")

License

Licensed under Apache 2.0

Documentation

Overview

Example (Register_deoss)
package main

import (
	"context"
	"log"
	"os"
	"strings"
	"time"

	cess "github.com/CESSProject/cess-go-sdk"
	"github.com/CESSProject/cess-go-sdk/config"
)

const DEFAULT_WAIT_TIME = time.Second * 15
const P2P_PORT = 4001
const TMP_DIR = "/tmp"

func main() {
	cli, err := cess.New(
		context.Background(),
		config.CharacterName_Deoss,
		cess.ConnectRpcAddrs(strings.Split(os.Getenv("RPC_ADDRS"), " ")),
		cess.Mnemonic(os.Getenv("MY_MNEMONIC")),
		cess.TransactionTimeout(time.Duration(DEFAULT_WAIT_TIME)),
		cess.Bootnodes([]string{os.Getenv("BOOTSTRAP_NODES")}),
		cess.P2pPort(P2P_PORT),
		cess.Workspace(TMP_DIR),
	)
	if err != nil {
		log.Fatalf("err: %v", err.Error())
	}

	_, err = cli.RegisterOrUpdateDeoss(cli.GetPeerPublickey())
	if err != nil {
		log.Fatalf("err: %v", err.Error())
	}
}
Output:

Example (Register_storage_node)
package main

import (
	"context"
	"log"
	"os"
	"strings"
	"time"

	cess "github.com/CESSProject/cess-go-sdk"
	"github.com/CESSProject/cess-go-sdk/config"
)

const DEFAULT_WAIT_TIME = time.Second * 15
const P2P_PORT = 4001
const TMP_DIR = "/tmp"

func main() {
	cli, err := cess.New(
		context.Background(),
		config.CharacterName_Bucket,
		cess.ConnectRpcAddrs(strings.Split(os.Getenv("RPC_ADDRS"), " ")),
		cess.Mnemonic(os.Getenv("MY_MNEMONIC")),
		cess.TransactionTimeout(time.Duration(DEFAULT_WAIT_TIME)),
		cess.Bootnodes([]string{os.Getenv("BOOTSTRAP_NODES")}),
		cess.P2pPort(P2P_PORT),
		cess.Workspace(TMP_DIR),
	)
	if err != nil {
		log.Fatalf("err: %v", err.Error())
	}

	_, _, err = cli.RegisterOrUpdateSminer(cli.GetPeerPublickey(), os.Getenv("MY_ADDR"), 0)
	if err != nil {
		log.Fatalf("err: %v", err.Error())
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultRpcAddrs = func(cfg *Config) error {
	rpcAddrs := []string{
		"wss://testnet-rpc0.cess.cloud/ws/",
		"wss://testnet-rpc1.cess.cloud/ws/",
	}
	return cfg.Apply(ConnectRpcAddrs(rpcAddrs))
}

DefaultRpcAddrs configures the default rpc address

View Source
var DefaultTimeout = func(cfg *Config) error {
	return cfg.Apply(TransactionTimeout(pattern.BlockInterval))
}

DefaultTimeout configures the default transaction waiting timeout

Functions

func New

func New(ctx context.Context, serviceName string, opts ...Option) (sdk.SDK, error)

New constructs a new sdk client with the given options, falling back on reasonable defaults. The defaults are:

- If no rpc address is provided, the sdk client uses the default address "wss://testnet-rpc0.cess.cloud/ws/"" or "wss://testnet-rpc1.cess.cloud/ws/";

- If no transaction timeout is provided, the sdk client uses the default timeout: time.Duration(time.Second * 6)

- The serviceName is used to specify the name of your service Warning:

cess-bucket (cess storage service) must be set to bucket
DeOSS (cess decentralized object storage service) must be set to deoss
cess-cli (cess client) must be set to client

func NewWithoutDefaults

func NewWithoutDefaults(ctx context.Context, serviceName string, opts ...Option) (sdk.SDK, error)

NewWithoutDefaults constructs a new client with the given options but *without* falling back on reasonable defaults.

Warning: This function should not be considered a stable interface. We may choose to add required services at any time and, by using this function, you opt-out of any defaults we may provide.

Types

type Config

type Config = config.Config

Config describes a set of settings for the sdk.

type Option

type Option = config.Option

Option is a client config option that can be given to the client constructor

var FallbackDefaults Option = func(cfg *Config) error {
	for _, def := range defaults {
		if !def.fallback(cfg) {
			continue
		}
		if err := cfg.Apply(def.opt); err != nil {
			return err
		}
	}
	return nil
}

FallbackDefaults applies default options to the libp2p node if and only if no other relevant options have been applied. will be appended to the options passed into New.

func Bootnodes added in v0.2.4

func Bootnodes(bootnodes []string) Option

P2pPort configuration boot node list

func ConnectRpcAddrs

func ConnectRpcAddrs(s []string) Option

ConnectRpcAddrs configuration rpc address

func Mnemonic

func Mnemonic(mnemonic string) Option

Mnemonic configures the mnemonic of the signature account

func P2pPort added in v0.2.4

func P2pPort(port int) Option

P2pPort configuration p2p communication port

func ProtocolPrefix added in v0.2.9

func ProtocolPrefix(protocolPrefix string) Option

P2pPort configuration boot node list

func TransactionTimeout

func TransactionTimeout(timeout time.Duration) Option

TransactionTimeout configures the waiting timeout for a transaction

func Workspace added in v0.2.4

func Workspace(workspace string) Option

Workspace configuration working directory

Directories

Path Synopsis
core
sdk

Jump to

Keyboard shortcuts

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