zcnbridge

package
v1.10.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 42 Imported by: 1

README

Bridge SDK documentation

This bridge SDK comprises 3 types of client and following available methods

  1. Bridge deployer/owner for Authorizer Smart Contract
  2. Customer/client to execute Bridge Smart contract burn and mint transactions
  3. Token bridge authorizer for signing payload for 0Chain and Ethereum chain

Bridge owner SDK

Deployer or Owner SDK is required to Add or Remove Authorizers from Authorizers Smart Contract

Configuration file: ~/.zcn/owner.yaml to initialize owner

owner:
    # Address of Ethereum authorizers contract
    AuthorizersAddress: "0xFE20Ce9fBe514397427d20C91CB657a4478A0FFa"
    OwnerEthereumMnemonic: "add here the owner mnemonic that has deployed auth and bridge contracts"

Authorizes config: ~/.zcn/authorizers.yaml to add authorizers

Example:

transaction, err := owner.AddEthereumAuthorizer(context.TODO(), wallet.Address)
if err != nil {
    fmt.Println(err)
}

status, err := zcnbridge.ConfirmEthereumTransaction(transaction.Hash().String(), 5, time.Second*5)
if err != nil {
    fmt.Println(err)
}

Client SDK

This SDK is used to execute mint and burn transaction on both chains, Ethereum and 0Chain

Configuration file: ~/.zcn/bridge.yaml to initialize the client

Functionality:

  • Burn (0Chain and Ethereum)
  • Mint (0Chain and Ethereum)

For more detailed information please find documentation here

Token Bridge SDK

Functionality:

  1. Used by token bridge to sign payloads for 0Chain and Ethereum chains
  2. Checks transactions status and give the client payload to

Documentation

Index

Constants

View Source
const (
	ZChainsClientConfigName  = "config.yaml"
	ZChainWalletConfigName   = "wallet.json"
	EthereumWalletStorageDir = "wallets"
)

Variables

View Source
var (
	DefaultClientIDEncoder = func(id string) []byte {
		result, err := hex.DecodeString(id)
		if err != nil {
			Logger.Fatal(err)
		}
		return result
	}
)

Functions

func AccountExists added in v1.3.5

func AccountExists(homedir, address string) bool

AccountExists checks if account exists

func ConfirmEthereumTransaction

func ConfirmEthereumTransaction(hash string, times int, duration time.Duration) (int, error)

func CreateKeyStorage added in v1.3.5

func CreateKeyStorage(homedir, password string) error

CreateKeyStorage create, restore or unlock key storage

func CreateSignedTransaction added in v1.3.5

func CreateSignedTransaction(
	chainID *big.Int,
	client *ethclient.Client,
	fromAddress common.Address,
	privateKey *ecdsa.PrivateKey,
	gasLimitUnits uint64,
) *bind.TransactOpts

_allowances[owner][spender] = amount; as a spender, ERC20 WZCN token must increase allowance for the bridge to make burn on behalf of WZCN owner

func DeleteAccount added in v1.4.7

func DeleteAccount(homedir, address string) bool

DeleteAccount deletes account from wallet

func GetAuthorizer added in v1.7.1

func GetAuthorizer(id string, cb zcncore.GetInfoCallback) (err error)

GetAuthorizer returned authorizer by ID

func GetAuthorizers

func GetAuthorizers(active bool, cb zcncore.GetInfoCallback) (err error)

GetAuthorizers Returns all or only active authorizers

func GetGlobalConfig added in v1.7.1

func GetGlobalConfig(cb zcncore.GetInfoCallback) (err error)

GetGlobalConfig Returns global config

func GetTransactionStatus

func GetTransactionStatus(hash string) (int, error)

func ImportAccount added in v1.3.5

func ImportAccount(homedir, mnemonic, password string) (string, error)

ImportAccount imports account using mnemonic

func ListStorageAccounts added in v1.3.5

func ListStorageAccounts(homedir string) []common.Address

ListStorageAccounts List available accounts

Types

type AuthorizerNode

type AuthorizerNode struct {
	ID  string `json:"id"`
	URL string `json:"url"`
}

type AuthorizerNodesResponse added in v1.7.1

type AuthorizerNodesResponse struct {
	Nodes []*AuthorizerNode `json:"nodes"`
}

type AuthorizerResponse added in v1.7.1

type AuthorizerResponse struct {
	AuthorizerID string `json:"id"`
	URL          string `json:"url"`

	// Configuration
	Fee common.Balance `json:"fee"`

	// Geolocation
	Latitude  float64 `json:"latitude"`
	Longitude float64 `json:"longitude"`

	// Stats
	LastHealthCheck int64 `json:"last_health_check"`

	// stake_pool_settings
	DelegateWallet string         `json:"delegate_wallet"`
	MinStake       common.Balance `json:"min_stake"`
	MaxStake       common.Balance `json:"max_stake"`
	NumDelegates   int            `json:"num_delegates"`
	ServiceCharge  float64        `json:"service_charge"`
}

type BridgeClient added in v1.3.5

type BridgeClient struct {
	BridgeAddress,
	TokenAddress,
	AuthorizersAddress,
	EthereumAddress,
	Password,
	EthereumNodeURL,
	Homedir string

	ConsensusThreshold float64
	GasLimit           uint64
}

func CreateBridgeClient added in v1.3.5

func CreateBridgeClient(cfg *viper.Viper) *BridgeClient

func SetupBridgeClientSDK added in v1.3.5

func SetupBridgeClientSDK(cfg *BridgeSDKConfig) *BridgeClient

SetupBridgeClientSDK Use this from standalone application 0Chain SDK initialization is required

func (*BridgeClient) AddEthereumAuthorizer added in v1.8.17

func (b *BridgeClient) AddEthereumAuthorizer(ctx context.Context, address common.Address) (*types.Transaction, error)

AddEthereumAuthorizer Adds authorizer to Ethereum bridge. Only contract deployer can call this method

func (*BridgeClient) AddEthereumAuthorizers added in v1.8.17

func (b *BridgeClient) AddEthereumAuthorizers(configDir string)

func (*BridgeClient) BurnWZCN added in v1.3.5

func (b *BridgeClient) BurnWZCN(ctx context.Context, amountTokens uint64) (*types.Transaction, error)

BurnWZCN Burns WZCN tokens on behalf of the 0ZCN client amountTokens - ZCN tokens clientID - 0ZCN client ERC20 signature: "burn(uint256,bytes)"

func (*BridgeClient) BurnZCN added in v1.3.5

func (b *BridgeClient) BurnZCN(ctx context.Context, amount, txnfee uint64) (*transaction.Transaction, error)

BurnZCN burns ZCN tokens before conversion from ZCN to WZCN as a first step

func (*BridgeClient) CreateEthClient added in v1.8.17

func (b *BridgeClient) CreateEthClient() (*ethclient.Client, error)

func (*BridgeClient) CreateSignedTransactionFromKeyStore added in v1.8.17

func (b *BridgeClient) CreateSignedTransactionFromKeyStore(client *ethclient.Client, gasLimitUnits uint64) *bind.TransactOpts

func (*BridgeClient) GetBalance added in v1.3.5

func (b *BridgeClient) GetBalance() (*big.Int, error)

GetBalance returns balance of the current client

func (*BridgeClient) GetUserNonceMinted added in v1.8.11

func (b *BridgeClient) GetUserNonceMinted(ctx context.Context, rawEthereumAddress string) (*big.Int, error)

GetUserNonceMinted Returns nonce for a specified Ethereum address

func (*BridgeClient) IncreaseBurnerAllowance added in v1.3.5

func (b *BridgeClient) IncreaseBurnerAllowance(ctx context.Context, amountWei Wei) (*types.Transaction, error)

IncreaseBurnerAllowance Increases allowance for bridge contract address to transfer WZCN tokens on behalf of the token owner to the Burn TokenPool During the burn the script transfers amount from token owner to the bridge burn token pool Example: owner wants to burn some amount. The contract will transfer some amount from owner address to the pool. So the owner must call IncreaseAllowance of the WZCN token with 2 parameters: spender address which is the bridge contract and amount to be burned (transferred) ERC20 signature: "increaseAllowance(address,uint256)"

func (*BridgeClient) MintWZCN added in v1.3.5

func (b *BridgeClient) MintWZCN(ctx context.Context, payload *ethereum.MintPayload) (*types.Transaction, error)

MintWZCN Mint ZCN tokens on behalf of the 0ZCN client payload: received from authorizers

func (*BridgeClient) MintZCN added in v1.3.5

func (b *BridgeClient) MintZCN(ctx context.Context, payload *zcnsc.MintPayload) (string, error)

MintZCN mints ZCN tokens after receiving proof-of-burn of WZCN tokens

func (*BridgeClient) QueryEthereumBurnEvents added in v1.8.16

func (b *BridgeClient) QueryEthereumBurnEvents(startNonce string) ([]*ethereum.BurnEvent, error)

QueryEthereumBurnEvents gets ethereum burn events

func (*BridgeClient) QueryEthereumMintPayload added in v1.3.5

func (b *BridgeClient) QueryEthereumMintPayload(zchainBurnHash string) (*ethereum.MintPayload, error)

QueryEthereumMintPayload gets burn ticket and creates mint payload to be minted in the Ethereum chain zchainBurnHash - Ethereum burn transaction hash

func (*BridgeClient) QueryZChainMintPayload added in v1.3.5

func (b *BridgeClient) QueryZChainMintPayload(ethBurnHash string) (*zcnsc.MintPayload, error)

QueryZChainMintPayload gets burn ticket and creates mint payload to be minted in the ZChain ethBurnHash - Ethereum burn transaction hash

func (*BridgeClient) RemoveEthereumAuthorizer added in v1.8.17

func (b *BridgeClient) RemoveEthereumAuthorizer(ctx context.Context, address common.Address) (*types.Transaction, error)

RemoveEthereumAuthorizer Removes authorizer from Ethereum bridge. Only contract deployer can call this method

func (*BridgeClient) SignWithEthereumChain added in v1.3.5

func (b *BridgeClient) SignWithEthereumChain(message string) ([]byte, error)

SignWithEthereumChain signs the digest with Ethereum chain signer taking key from the current user key storage

func (*BridgeClient) VerifyZCNTransaction added in v1.3.5

func (b *BridgeClient) VerifyZCNTransaction(ctx context.Context, hash string) (*transaction.Transaction, error)

VerifyZCNTransaction verifies 0CHain transaction

type BridgeSDKConfig added in v1.3.5

type BridgeSDKConfig struct {
	LogLevel        *string
	LogPath         *string
	ConfigChainFile *string
	ConfigDir       *string
	Development     *bool
}

type EthereumBurnEvents added in v1.8.16

type EthereumBurnEvents struct {
	AuthorizerID string `json:"authorizer_id,omitempty"`
	BurnEvents   []struct {
		Nonce           int64  `json:"nonce"`
		TransactionHash string `json:"transaction_hash"`
	} `json:"burn_events"`
}

EthereumBurnEvents represents burn events returned by authorizers

func (*EthereumBurnEvents) Data added in v1.8.16

func (r *EthereumBurnEvents) Data() interface{}

func (*EthereumBurnEvents) Error added in v1.8.16

func (r *EthereumBurnEvents) Error() error

func (*EthereumBurnEvents) GetAuthorizerID added in v1.8.16

func (r *EthereumBurnEvents) GetAuthorizerID() string

func (*EthereumBurnEvents) SetAuthorizerID added in v1.8.16

func (r *EthereumBurnEvents) SetAuthorizerID(id string)

type JobError

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

JobError result of internal request wrapped in authorizer job

func (*JobError) MarshalJSON

func (e *JobError) MarshalJSON() ([]byte, error)

func (*JobError) UnmarshalJSON

func (e *JobError) UnmarshalJSON(buf []byte) error

type JobResult

type JobResult interface {
	// Error = Status of Authorizer job on authorizer server
	Error() error
	// Data returns the actual result
	Data() interface{}
	// SetAuthorizerID Assigns authorizer ID to the Job
	SetAuthorizerID(ID string)
	// GetAuthorizerID returns authorizer ID
	GetAuthorizerID() string
}

JobResult = Authorizer task result, it wraps actual result of the query inside authorizer

type JobStatus

type JobStatus uint

JobStatus = Ethereum transaction status

type ProofEthereumBurn

type ProofEthereumBurn struct {
	TxnID             string `json:"ethereum_txn_id"`
	Nonce             int64  `json:"nonce"`
	Amount            int64  `json:"amount"`
	ReceivingClientID string `json:"receiving_client_id"` // 0ZCN address
	Signature         string `json:"signature"`
}

ProofEthereumBurn Authorizer returns this type for Ethereum transaction

type ProofZCNBurn

type ProofZCNBurn struct {
	AuthorizerID string `json:"authorizer_id,omitempty"`
	TxnID        string `json:"0chain_txn_id"`
	To           string `json:"to"`
	Nonce        int64  `json:"nonce"`
	Amount       int64  `json:"amount"`
	Signature    []byte `json:"signature"`
}

ProofZCNBurn Authorizer returns this type for ZCN transaction

func (*ProofZCNBurn) Data

func (r *ProofZCNBurn) Data() interface{}

func (*ProofZCNBurn) Error

func (r *ProofZCNBurn) Error() error

func (*ProofZCNBurn) GetAuthorizerID

func (r *ProofZCNBurn) GetAuthorizerID() string

func (*ProofZCNBurn) SetAuthorizerID

func (r *ProofZCNBurn) SetAuthorizerID(id string)

type WZCNBurnEvent

type WZCNBurnEvent struct {
	// 	AuthorizerID Authorizer ID
	AuthorizerID string `json:"authorizer_id,omitempty"`
	// BurnTicket Returns burn ticket
	BurnTicket *ProofEthereumBurn `json:"ticket,omitempty"`
	// Err gives error of job on server side
	Err *JobError `json:"err,omitempty"`
	// Status gives job status on server side (authoriser)
	Status JobStatus `json:"status,omitempty"`
}

WZCNBurnEvent returned from burn ticket handler of: /v1/ether/burnticket/get

func (*WZCNBurnEvent) Data

func (r *WZCNBurnEvent) Data() interface{}

func (*WZCNBurnEvent) Error

func (r *WZCNBurnEvent) Error() error

func (*WZCNBurnEvent) GetAuthorizerID

func (r *WZCNBurnEvent) GetAuthorizerID() string

func (*WZCNBurnEvent) SetAuthorizerID

func (r *WZCNBurnEvent) SetAuthorizerID(id string)

type Wei added in v1.3.3

type Wei int64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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