zkspace

package module
v0.0.0-...-3a227ed Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: MIT Imports: 7 Imported by: 0

README

zkspace-sdk

Zkspace SDK is used to interact with the Zkspace 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/okx/go-wallet-sdk/coins/zkspace

Usage

New address
pri := "559376194bb4c9a9dfb33fde4a2ab15daa8a899a3f43dee787046f57d5f7b10a"
addr, err := GetAddress(pri)
Change Pubkey
l1PrivateKeyBytes, _ := hex.DecodeString(l1PrivateKey[2:])
ethSigner, _ := core.NewOkEthSignerFromPrivBytes(l1PrivateKeyBytes)
zkSigner, _ := NewZkSignerFromEthSigner(ethSigner, core.ChainIdMainnet)
const nonce = 6
const accountId = 11573
newPkHash := zkSigner.GetPublicKeyHash()
nonceStr := "0x" + fmt.Sprintf("%08x", nonce)
accountIdStr := "0x" + fmt.Sprintf("%08x", accountId)
message := fmt.Sprintf("Register ZKSwap pubkey:\n\n%s\nnonce: %s\naccount id: %s\n\nOnly sign this message for a trusted client!",
zkSigner.GetPublicKeyHash()[5:], nonceStr, accountIdStr)
ethSignature, err := ethSigner.SignMessage([]byte(message))
from := ethSigner.GetAddress()
tx := CreateChangePubKeyTx(accountId, from, newPkHash, nonce, ethSignature)
Transfer
l1PrivateKeyBytes, _ := hex.DecodeString(l1PrivateKey[2:])
ethSigner, _ := core.NewOkEthSignerFromPrivBytes(l1PrivateKeyBytes)
zkSigner, _ := NewZkSignerFromEthSigner(ethSigner, core.ChainIdMainnet)
from := ethSigner.GetAddress()
const nonce = 7
const accountId = 11573
const chainId = 13
const to = "0x21dceed765c30b2abea933a161479aea4702e433"
const tokenId = 1
const tokenSymbol = "ZKS"
const decimals = 18
token := &core.Token{
    Id:       1,
    Symbol:   tokenSymbol,
    Decimals: decimals,
}
amount, _ := big.NewInt(0).SetString("5000000000000000000", 10)
readableAmount := token.ToDecimalString(amount)
// calculate fee
const feeUSDT = "0.5"
const feeTokenId = 1
const feeTokenSymbol = "ZKS"
const feeDecimals = 18
const price = "0.0593863548182511"
feeToken := &core.Token{
    Id:       1,
    Symbol:   feeTokenSymbol,
    Decimals: feeDecimals,
}
fee, _ := big.NewInt(0).SetString("8410000000000000000", 10)
readableFee := feeToken.ToDecimalString(fee)

// prepare for l1 signature
message := fmt.Sprintf("Transfer %s %s\nTo: %s\nChain Id: %d\nNonce: %d\nFee: %s %s\nAccount Id: %d",
readableAmount, tokenSymbol, strings.ToLower(to), chainId, nonce, readableFee, feeTokenSymbol, accountId)

ethSignature, err := ethSigner.SignMessage([]byte(message)) // 0x549dd4788ef9abb59240d6ee0952e789df02b98890f89abf30987291b89270a73b363ddc69e9da9165cba1e7e95d23576372bd38761c4e713473d336638fd55e1b
ethereumSignature := &core.EthSignature{
    Type:      "EthereumSignature",
    Signature: "0x" + hex.EncodeToString(ethSignature),
}
tx := CreateTransferTx(accountId, from, to, tokenId, amount, feeTokenId, fee, chainId, nonce)
signature, err := zkSigner.SignTransfer(&tx)
tx.Signature = signature
transferTx := CreateSignTransferTx(&tx)
signedTransaction := &SignedTransaction{
    Transaction:       transferTx,
    EthereumSignature: ethereumSignature,
} 

License

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

Documentation

Index

Constants

View Source
const (
	TransactionTypeChangePubKeyOnchain core.TransactionType = "Onchain"
	TransactionTypeChangePubKeyECDSA   core.TransactionType = "ECDSA"
	TransactionTypeChangePubKeyCREATE2 core.TransactionType = "CREATE2"
)
View Source
const (
	Message                 = "Access ZKSwap account.\n\nOnly sign this message for a trusted client!"
	TransactionVersion byte = 0x01
)
View Source
const (
	TransactionTypeTransfer core.TransactionType = "Transfer"
)

Variables

This section is empty.

Functions

func GetAddress

func GetAddress(ethPrivKeyHex string) (string, error)

func GetPubKeyHash

func GetPubKeyHash(ethPrivKeyHex string, chainId int) (string, error)

Types

type ChangePubKey

type ChangePubKey struct {
	Type         string `json:"type"`
	AccountId    uint32 `json:"accountId"`
	Account      string `json:"account"`
	NewPkHash    string `json:"newPkHash"`
	Nonce        uint32 `json:"nonce"`
	EthSignature string `json:"ethSignature"`
}

func CreateChangePubKeyTx

func CreateChangePubKeyTx(accountId uint32, from string, newPkHash string, nonce uint32, ethSignature []byte) ChangePubKey

func (*ChangePubKey) GetTxHash

func (t *ChangePubKey) GetTxHash() (string, error)

type ChangePubKeyAuthType

type ChangePubKeyAuthType string
const (
	ChangePubKeyAuthTypeOnchain ChangePubKeyAuthType = `Onchain`
	ChangePubKeyAuthTypeECDSA   ChangePubKeyAuthType = `ECDSA`
	ChangePubKeyAuthTypeCREATE2 ChangePubKeyAuthType = `CREATE2`
)

type ChangePubKeyCREATE2

type ChangePubKeyCREATE2 struct {
	Type           ChangePubKeyAuthType `json:"type"`
	CreatorAddress string               `json:"creatorAddress"`
	SaltArg        string               `json:"saltArg"`
	CodeHash       string               `json:"codeHash"`
}

type ChangePubKeyECDSA

type ChangePubKeyECDSA struct {
	Type         ChangePubKeyAuthType `json:"type"`
	EthSignature string               `json:"ethSignature"`
	BatchHash    string               `json:"batchHash"`
}

type ChangePubKeyOnchain

type ChangePubKeyOnchain struct {
	Type ChangePubKeyAuthType `json:"type"`
}

type ChangePubKeyVariant

type ChangePubKeyVariant interface {
	// contains filtered or unexported methods
}

type SignTransfer

type SignTransfer struct {
	Type       string     `json:"type"`
	AccountId  uint32     `json:"accountId"`
	From       string     `json:"from"`
	To         string     `json:"to"`
	TokenId    uint16     `json:"token"`
	Amount     string     `json:"amount"`
	FeeTokenId uint8      `json:"feeToken"`
	Fee        string     `json:"fee"`
	ChainId    uint8      `json:"chainId"`
	Nonce      uint32     `json:"nonce"`
	Signature  *Signature `json:"signature"`
}

func CreateSignTransferTx

func CreateSignTransferTx(tx *Transfer) SignTransfer

type Signature

type Signature struct {
	PubKey    string `json:"pubKey"`
	Signature string `json:"signature"`
}

type SignedTransaction

type SignedTransaction struct {
	Transaction       SignTransfer       `json:"tx"`
	EthereumSignature *core.EthSignature `json:"signature"`
}

type TransactionTypeChangePubKey

type TransactionTypeChangePubKey struct {
	ChangePubKey string `json:"ChangePubKey"`
}

type Transfer

type Transfer struct {
	Type       string     `json:"type"`
	AccountId  uint32     `json:"accountId"`
	From       string     `json:"from"`
	To         string     `json:"to"`
	TokenId    uint16     `json:"token"`
	Amount     *big.Int   `json:"amount"`
	FeeTokenId uint8      `json:"feeToken"`
	Fee        *big.Int   `json:"fee"`
	ChainId    uint8      `json:"chainId"`
	Nonce      uint32     `json:"nonce"`
	Signature  *Signature `json:"signature"`
}

func CreateTransferTx

func CreateTransferTx(accountId uint32, from string, to string, tokenId uint16, amount *big.Int, feeTokenId uint8, fee *big.Int,
	chainId uint8, nonce uint32) Transfer

func (*Transfer) GetTxHash

func (t *Transfer) GetTxHash() (string, error)

type ZkSigner

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

func NewZkSignerFromEthSigner

func NewZkSignerFromEthSigner(es core.EthSigner, cid core.ChainId) (*ZkSigner, error)

func NewZkSignerFromRawPrivateKey

func NewZkSignerFromRawPrivateKey(rawPk []byte) (*ZkSigner, error)

func NewZkSignerFromSeed

func NewZkSignerFromSeed(seed []byte) (*ZkSigner, error)

func (*ZkSigner) GetPrivateKey

func (s *ZkSigner) GetPrivateKey() string

func (*ZkSigner) GetPublicKey

func (s *ZkSigner) GetPublicKey() string

func (*ZkSigner) GetPublicKeyHash

func (s *ZkSigner) GetPublicKeyHash() string

func (*ZkSigner) Sign

func (s *ZkSigner) Sign(message []byte) (*zkscrypto.Signature, error)

func (*ZkSigner) SignTransfer

func (s *ZkSigner) SignTransfer(txData *Transfer) (*Signature, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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