sui

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

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 16 Imported by: 0

README

sui-sdk

Sui SDK is used to interact with the Sui blockchain, it contains various functions can be used to web3 wallet.

Installation

go get

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

go get -u github.com/xiaohuasheng0x1/blockchains/coins/sui

Usage

New address
b, err := base64.StdEncoding.DecodeString("uemYAwkvsf/a7q2DdoMKNHWP7DlDhLLmgUh6coTtp94=")
addr := NewAddress(hex.EncodeToString(b[0:32]))
Stake
data, err := BuildStakeTx("0x19a8a5cadebbe8f73364cb1ce4d9aa2d0ffb62a3098c7ff93bd0426960f56406", "0x72169c90b7ea87f8101285c849c09cacced9968f83aa30786dad546bb94c78ab",
		[]*SuiObjectRef{{ObjectId: "0x26c26d20be986dbd99d33ff1b3a0bb16437039d3de85b4cb9d56a3f57066ef54", Digest: "AMGM65x2qTfM4kfPjbv7Aqpap6MBiVVa4W8hrakgvPjB", Version: 1978816}},
		1000000000, 0, 9644512, 820)
require.NoError(t, err)
fmt.Println(base64.StdEncoding.EncodeToString(data))
key := "b9e99803092fb1ffdaeead8376830a34758fec394384b2e681487a7284eda7de"
tx, err := SignTransaction(base64.StdEncoding.EncodeToString(data), key)
Withdraw
data, err := BuildWithdrawStakeTx("0x19a8a5cadebbe8f73364cb1ce4d9aa2d0ffb62a3098c7ff93bd0426960f56406",
    []*SuiObjectRef{{ObjectId: "0x26c26d20be986dbd99d33ff1b3a0bb16437039d3de85b4cb9d56a3f57066ef54", Digest: "AihBh2VjG96NDTCw1HvZj8TtWEpmYZUh9rb1D92oQ7Ak", Version: 5656730}},
    &SuiObjectRef{Digest: "CkmUVCkHFWyjH27Zg5xTd5xbUQZt1BReQnvtq2zeT6zW", Version: 5656730, ObjectId: "0x194acb4ec803ef63f15331efa9e701b4a334cf417fa15432d736d90978ce43e4"}, 0, 9534000, 820)
key := "b9e99803092fb1ffdaeead8376830a34758fec394384b2e681487a7284eda7de"
tx, err := SignTransaction(base64.StdEncoding.EncodeToString(data), key)
Sign message
b, err := base64.StdEncoding.DecodeString("uemYAwkvsf/a7q2DdoMKNHWP7DlDhLLmgUh6coTtp94=")
r, err := SignMessage("im from okx", hex.EncodeToString(b[0:32]))
Transfer amount
suiObjects := []*SuiObjectRef{{
    Digest:   "ESUg3nLfPmcMK2vf8kAyyX967w1whtgv8dk6pZhNHh6N",
    ObjectId: "0x0cd3e81f2130b922a25f113f017d8f76cae8c2f9d7ebed690e56754a0b3a5784",
    Version:  60784,
    }, {
    Digest:   "A3Nk1uPDmLLaYgBDPM235ZhTHBJVhXUD74mRVn9zZx4Z",
    ObjectId: "0x1d892361074249073f82613ad08b387bdec26185ea013d379f5ae0bb2a611ebc",
    Version:  60785,
    }, {
    Digest:   "2bacji3hre1MZiatXVjqQ1yVZXXfq5rw7yPyHwSH9CPj",
    ObjectId: "0x8565ab3ac7072abd8f7a0d4e81974a0c8669defaac9340dda7083e62542cd2f9",
    Version:  60783,
    }}
b, err := base64.StdEncoding.DecodeString("uemYAwkvsf/a7q2DdoMKNHWP7DlDhLLmgUh6coTtp94=")
addr := NewAddress(hex.EncodeToString(b[0:32]))
amount:=1
pay := &PaySuiRequest{suiObjects, amount, 0}
raw, err := json.Marshal(pay)
res, err := Execute(&Request{Data: string(raw)}, addr, recipient, gasBudget, gasPrice, hex.EncodeToString(b[0:32]))

License

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

Documentation

Index

Constants

View Source
const (
	SignatureScheme    = "ED25519"
	PUBLIC_KEY_SIZE    = 32
	SUI_ADDRESS_LENGTH = 32
)
View Source
const (
	Empty         = Type("")
	Transfer      = Type("transfer")      //transfer sui objects
	Split         = Type("split")         // split sui objects
	Merge         = Type("merge")         // merge sui objects
	Stake         = Type("stake")         //stake sui
	WithdrawStake = Type("withdrawStake") //withdraw stake of sui
	Raw           = Type("raw")           //sign the txbytes
)

Variables

View Source
var (
	ErrInvalidSuiRequest = errors.New("invalid sui request")
	ErrInvalidSuiSeedHex = errors.New("invalid sui seed hex")
	ErrInvalidSuiParam   = errors.New("invalid sui param")
	ErrUnknownSuiRequest = errors.New("unknown sui request")
)
View Source
var (
	ErrInvalidStruct              = errors.New("invalid struct tag")
	ErrEmptyCoins                 = errors.New("empty coins")
	ErrEmptyTokens                = errors.New("empty tokens")
	ErrInvalidUint64              = errors.New("invalid uint64")
	ErrInvalidString              = errors.New("invalid string")
	ErrInvalidPure                = errors.New("invalid pure")
	ErrInvalidBytes               = errors.New("invalid bytes")
	ErrInvalidSharedOrOwnedObject = errors.New("invalid shared or owned object")
	ErrInvalidMoveCall            = errors.New("invalid move call target")
	ErrInvalidParams              = errors.New("invalid params")
)

Functions

func BuildMergeTx

func BuildMergeTx(from string, coin []*SuiObjectRef, objects []*SuiObjectRef, epoch, gasBudget, gasPrice uint64) ([]byte, error)

func BuildMulTx

func BuildMulTx(from string, coin []*SuiObjectRef, amounts map[string]uint64, epoch, gasBudget, gasPrice uint64) ([]byte, error)

func BuildSplitTx

func BuildSplitTx(from, to string, coins []*SuiObjectRef, amounts []uint64, epoch, gasBudget, gasPrice uint64) ([]byte, error)

func BuildStakeTx

func BuildStakeTx(from, validator string, coins []*SuiObjectRef, amount uint64, epoch, gasBudget, gasPrice uint64) ([]byte, error)

func BuildTokenTx

func BuildTokenTx(from, to string, coins []*SuiObjectRef, tokens []*SuiObjectRef, amount uint64, epoch, gasBudget, gasPrice uint64) ([]byte, error)

func BuildTx

func BuildTx(from, to string, coins []*SuiObjectRef, amount uint64, epoch, gasBudget, gasPrice uint64) ([]byte, error)

func BuildWithdrawStakeTx

func BuildWithdrawStakeTx(from string, coins []*SuiObjectRef, stake *SuiObjectRef, epoch, gasBudget, gasPrice uint64) ([]byte, error)

func DecodeHexString

func DecodeHexString(s string) ([]byte, error)

func Execute

func Execute(r *Request, from, to string, gasBudget uint64, gasPrice uint64, seedHex string) (string, error)

func GenerateKey

func GenerateKey() (crypto_ed25519.PrivateKey, error)

func GetAddressByPubKey

func GetAddressByPubKey(publicKeyHex string) (string, error)

func GetRawTx

func GetRawTx(txBytes string) (string, error)

func GetTxHash

func GetTxHash(r *Request, to string, gasBudget uint64, gasPrice uint64, addr string) (string, error)

func Hash

func Hash(txBytes string) (string, error)

func NewAddress

func NewAddress(seedHex string) string

func NormalizeSuiAddress

func NormalizeSuiAddress(value string) string

func PrepareTx

func PrepareTx(r *Request, to string, gasBudget uint64, gasPrice uint64, addr string) (string, error)

func SignMessage

func SignMessage(input string, seedHex string) (string, error)

func UlebEncode

func UlebEncode(num uint64) []byte

func ValidateAddress

func ValidateAddress(address string) bool

func WriteAddress

func WriteAddress(buf *bytes.Buffer, address string) error

func WriteBigInt

func WriteBigInt(buf *bytes.Buffer, value *big.Int, bits int) error

func WriteBool

func WriteBool(buf *bytes.Buffer, value bool) error

func WriteHash

func WriteHash(buf *bytes.Buffer, hash string) error

func WriteLen

func WriteLen(buf *bytes.Buffer, l int) error

func WriteString

func WriteString(buf *bytes.Buffer, value string) error

func WriteUint16

func WriteUint16(buf *bytes.Buffer, value uint16) error

func WriteUint32

func WriteUint32(buf *bytes.Buffer, value uint32) error

func WriteUint64

func WriteUint64(buf *bytes.Buffer, value uint64) error

func WriteUint8

func WriteUint8(buf *bytes.Buffer, value uint8) error

Types

type Address

type Address struct {
	Kind  string `json:"kind"`
	Value string `json:"value"`
	Index uint16 `json:"index"`
	Type  string `json:"type"`
}

func (*Address) Write

func (s *Address) Write(buf *bytes.Buffer) error

type Amount

type Amount struct {
	Kind  string `json:"kind"`
	Value uint64 `json:"value"`
	Index uint16 `json:"index"`
	Type  string `json:"type"`
}

func (*Amount) Write

func (s *Amount) Write(buf *bytes.Buffer) error

type Coin

type Coin struct {
	Kind  string `json:"kind"`
	Value string `json:"value,omitempty"`
	Index uint16 `json:"index"`
	Type  string `json:"type,omitempty"`
}

func (*Coin) Write

func (s *Coin) Write(buf *bytes.Buffer) error

type Command

type Command struct {
	Kind          string        `json:"kind"`
	Target        string        `json:"target,omitempty"`
	Coin          *Coin         `json:"coin,omitempty"`
	Destination   *Coin         `json:"destination,omitempty"`
	Sources       []*Coin       `json:"sources,omitempty"`
	Amounts       []*Amount     `json:"amounts,omitempty"`
	Objects       []*Object     `json:"objects,omitempty"`
	Address       *Address      `json:"address,omitempty"`
	Arguments     []*Coin       `json:"arguments,omitempty"`
	TypeArguments []interface{} `json:"typeArguments,omitempty"`
}

func (*Command) Write

func (s *Command) Write(buf *bytes.Buffer) error

type Expiration

type Expiration struct {
	Epoch uint64 `json:"Epoch"`
}

type GasConfig

type GasConfig struct {
	Price   string          `json:"price"`
	Budget  string          `json:"budget"`
	Payment []*SuiObjectRef `json:"payment"`
}

type Input

type Input struct {
	Kind  string      `json:"kind"`
	Value interface{} `json:"value"`
	Index uint16      `json:"index"`
	Type  string      `json:"type"`
}

func (*Input) Write

func (s *Input) Write(buf *bytes.Buffer) error

type MergeSuiRequest

type MergeSuiRequest struct {
	Coins   []*SuiObjectRef `json:"coins"`
	Objects []*SuiObjectRef `json:"objects"`
	Epoch   uint64          `json:"epoch"`
}

type Object

type Object struct {
	Kind        string `json:"kind"`
	Index       uint16 `json:"index"`
	ResultIndex uint16 `json:"resultIndex"`
}

func (*Object) Write

func (s *Object) Write(buf *bytes.Buffer) error

type Pay

type Pay struct {
	Sender     string      `json:"sender"`
	Expiration *Expiration `json:"expiration,omitempty"`
	GasConfig  GasConfig   `json:"gasConfig"`
	Inputs     []*Input    `json:"inputs"`
	Commands   []*Command  `json:"transactions"`
}

func (*Pay) Build

func (p *Pay) Build() ([]byte, error)

type PaySuiRequest

type PaySuiRequest struct {
	Coins  []*SuiObjectRef `json:"coins"`
	Amount uint64          `json:"amount"`
	Epoch  uint64          `json:"epoch"`
}

type Request

type Request struct {
	Data string `json:"data"`
	Type Type   `json:"type"`
}

type Shared

type Shared struct {
	Mutable              bool   `json:"mutable"`
	InitialSharedVersion uint64 `json:"initialSharedVersion"`
	ObjectId             string `json:"objectId"`
}

func (*Shared) Write

func (s *Shared) Write(buf *bytes.Buffer) error

type SharedOrOwned

type SharedOrOwned struct {
	ImmOrOwned *SuiObjectRef `json:"ImmOrOwned"`
	Shared     *Shared       `json:"Shared"`
}

type SignedTransaction

type SignedTransaction struct {
	TxBytes   string `json:"tx_bytes,omitempty"`
	Signature string `json:"signature,omitempty"`
}

func SignTransaction

func SignTransaction(txBytes string, seedHex string) (*SignedTransaction, error)

type SplitSuiRequest

type SplitSuiRequest struct {
	Coins   []*SuiObjectRef `json:"coins"`
	Amounts []uint64        `json:"amounts"`
	Epoch   uint64          `json:"epoch"`
}

type StakeSuiRequest

type StakeSuiRequest struct {
	Coins  []*SuiObjectRef `json:"coins"`
	Amount uint64          `json:"amount"`
	Epoch  uint64          `json:"epoch"`
}

type SuiObject

type SuiObject struct {
	Object *SharedOrOwned `json:"Object"`
}

func NewImmOrOwnedObject

func NewImmOrOwnedObject(ref *SuiObjectRef) *SuiObject

func (*SuiObject) Valid

func (s *SuiObject) Valid() bool

type SuiObjectRef

type SuiObjectRef struct {
	Digest   string `json:"digest"`
	ObjectId string `json:"objectId"`
	Version  uint64 `json:"version"`
}

func (*SuiObjectRef) Check

func (s *SuiObjectRef) Check() bool

func (*SuiObjectRef) Write

func (s *SuiObjectRef) Write(buf *bytes.Buffer) error

type Type

type Type string

type WithdrawStakSuiRequest

type WithdrawStakSuiRequest struct {
	Coins     []*SuiObjectRef `json:"coins"`
	StakeCoin *SuiObjectRef   `json:"stake_coin"`
	Epoch     uint64          `json:"epoch"`
}

Jump to

Keyboard shortcuts

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