zillean

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

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

Go to latest
Published: Mar 6, 2019 License: GPL-3.0 Imports: 12 Imported by: 2

README

Zilliqa Golang SDK

GoDoc Go Report Card Build Status

A Golang implementation of Zilliqa API.

The project is still under development. Please note that specifications may change greatly.

Installation

go get -u github.com/GincoInc/zillean
go get -u github.com/GincoInc/go-crypto

Getting started

cd $GOPATH/src/github.com/GincoInc/zillean/example
go run main.go

Example

package main

import (
	"fmt"

	"github.com/GincoInc/zillean"
)

func main() {
	// initialize the Zillean
	zil := zillean.NewZillean("https://api.zilliqa.com")

	// generate a private key
	privKey := zil.GeneratePrivateKey()
	fmt.Printf("private key: %s\n", privKey) // private key: b6c00064b10d33c4a9fadb5b473d834b1995f132acdbe4b831ab5343702c174e

	// get a public key
	pubKey, _ := zil.GetPublicKeyFromPrivateKey(privKey)
	fmt.Printf("public key: %s\n", pubKey) // public key: 03dcb21aaaa918f91a708858dc271343b4bee059e53202ce0358b68effa7e64378

	// get a address
	address, _ := zil.GetAddressFromPrivateKey(privKey)
	fmt.Printf("address: %s\n", address) // address: 5f0e26adf701bb6a4535f0485fe3400e6e90c9ae

	// sign the transaction
	rawTx := zillean.RawTransaction{
		Version:  0,
		Nonce:    2,
		To:       "to address",
		Amount:   "1000000000000",
		PubKey:   pubKey,
		GasPrice: big.NewInt(1000000000),
		GasLimit: 1,
	}
	signature, _ := zil.SignTransaction(rawTx, privKey)
	txID, _ := zil.RPC.CreateTransaction(rawTx, signature)
	fmt.Printf("txID: %s\n", txID)
}

Supports

Wallet API
  • GeneratePrivateKey
  • VerifyPrivateKey
  • GetPublicKeyFromPrivateKey
  • IsPublicjKey
  • GetAddressFromPrivateKey
  • GetAddressFromPublicKey
  • IsAddress
  • SignTransaction
  • VerifySignature
JSON-RPC API
  • GetNetworkId
  • GetBlockchainInfo
  • GetShardingStructure
  • GetDsBlock
  • GetLatestDsBlock
  • GetNumDSBlocks
  • GetDSBlockRate
  • DSBlockListing
  • GetTxBlock
  • GetLatestTxBlock
  • GetNumTxBlocks
  • GetTxBlockRate
  • TxBlockListing
  • GetNumTransactions
  • GetTransactionRate
  • GetCurrentMiniEpoch
  • GetCurrentDSEpoch
  • GetPrevDifficulty
  • GetPrevDSDifficulty
  • CreateTransaction
  • GetTransaction
  • GetRecentTransactions
  • GetTransactionsForTxBlock
  • GetNumTxnsTxEpoch
  • GetNumTxnsDSEpoch
  • GetMinimumGasPrice
  • GetSmartContractCode
  • GetSmartContractInit
  • GetSmartContractState
  • GetSmartContracts
  • GetContractAddressFromTransactionID
  • GetBalance

Documentation

Overview

Package zillean is golang library for Zilliqa blockchain.

Example
package main

import (
	"fmt"
	"math/big"
)

const endpoint = "https://api.zilliqa.com"

func main() {
	// generate a new account
	toZil := NewZillean(endpoint)
	toPrivKey := toZil.GeneratePrivateKey()
	toPubKey, _ := toZil.GetPublicKeyFromPrivateKey(toPrivKey)
	toAddress, _ := toZil.GetAddressFromPrivateKey(toPrivKey)
	fmt.Printf("private key: %s\n", toPrivKey)
	fmt.Printf("public key: %s\n", toPubKey)
	fmt.Printf("address: %s\n", toAddress)

	// use an existing account
	fromZil := NewZillean(endpoint)
	fromPrivKey := "AAFD338492962FAD674EE3BD6EBC57C8373B2C9BADBAC8806D890F1FE8C571DF"
	fromPubKey, _ := fromZil.GetPublicKeyFromPrivateKey(fromPrivKey)

	// create a transaction
	rawTx := RawTransaction{
		Version:  0,
		Nonce:    1,
		To:       toAddress,
		Amount:   "1000000000000",
		PubKey:   fromPubKey,
		GasPrice: big.NewInt(1000000000),
		GasLimit: 1,
	}
	signature, _ := fromZil.SignTransaction(rawTx, fromPrivKey)
	txID, _ := fromZil.RPC.CreateTransaction(rawTx, signature)
	fmt.Printf("txID: %s\n", txID)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balance

type Balance struct {
	Balance string `json:"balance"`
	Nonce   int64  `json:"nonce"`
}

Balance describes the balance for an account.

type BlockchainInfo

type BlockchainInfo struct {
	CurrentDSEpoch    string            `json:"CurrentDSEpoch"`
	CurrentMiniEpoch  string            `json:"CurrentMiniEpoch"`
	DSBlockRate       float64           `json:"DSBlockRate"`
	NumDSBlocks       string            `json:"NumDSBlocks"`
	NumPeers          int64             `json:"NumPeers"`
	NumTransactions   string            `json:"NumTransactions"`
	NumTxBlocks       string            `json:"NumTxBlocks"`
	NumTxnsDSEpoch    string            `json:"NumTxnsDSEpoch"`
	NumTxnsTxEpoch    int64             `json:"NumTxnsTxEpoch"`
	ShardingStructure ShardingStructure `json:"ShardingStructure"`
	TransactionRate   int64             `json:"TransactionRate"`
	TxBlockRate       float64           `json:"TxBlockRate"`
}

BlockchainInfo describes the information about Zilliqa blockchain.

type DsBlock

type DsBlock struct {
	Header struct {
		BlockNum     string   `json:"blockNum"`
		Difficulty   int64    `json:"difficulty"`
		DifficultyDS int64    `json:"difficultyDS"`
		GasPrice     string   `json:"gasPrice"`
		LeaderPubKey string   `json:"leaderPubKey"`
		PoWWinners   []string `json:"powWinners"`
		Prevhash     string   `json:"prevhash"`
		Timestamp    string   `json:"timestamp"`
	} `json:"header"`
	Signature string `json:"signature"`
}

DsBlock describes a DS-Block.

type ECSchnorr

type ECSchnorr struct {
	Curve elliptic.Curve
}

ECSchnorr represents a EC-Schnorr object.

See https://www.bsi.bund.de/SharedDocs/Downloads/EN/BSI/Publications/TechGuidelines/TR03111/BSI-TR-03111_pdf.pdf?__blob=publicationFile&v=1 for more information about the EC-Schnorr signature implementation.

func NewECSchnorr

func NewECSchnorr() *ECSchnorr

NewECSchnorr returns a new ECSchnorr.

func (*ECSchnorr) GeneratePrivateKey

func (ecs *ECSchnorr) GeneratePrivateKey() []byte

GeneratePrivateKey generates a new private key for Schnorr signature.

func (*ECSchnorr) GetPublicKey

func (ecs *ECSchnorr) GetPublicKey(privKey []byte, compress bool) []byte

GetPublicKey returns the public key of a corresponding private key.

func (*ECSchnorr) Sign

func (ecs *ECSchnorr) Sign(privKey, pubKey, msg []byte) ([]byte, []byte)

Sign returns the signature (r, s) on a given message.

func (*ECSchnorr) Verify

func (ecs *ECSchnorr) Verify(r, s, pubKey, msg []byte) bool

Verify returns a boolean that implies whether a given signature is successfully verified or not. The algorithm takes the following steps: 1. Compute Q = sG + r * pubKey 2. r' = H(Q, kpub, m) 3. return r' == r

type ListedBlocks

type ListedBlocks struct {
	Data []struct {
		BlockNum int64  `json:"BlockNum"`
		Hash     string `json:"Hash"`
	} `json:"data"`
	MaxPages int64 `json:"maxPages"`
}

ListedBlocks contains the paginated list of Blocks. This can be used for both DS-Blocks and TX-Blocks.

type RPC

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

RPC represents a JSON-RPC API client object.

func NewRPC

func NewRPC(endpoint string) *RPC

NewRPC returns a new RPC object.

func (*RPC) CreateTransaction

func (r *RPC) CreateTransaction(rawTx RawTransaction, signature string) (string, error)

CreateTransaction create a new Transaction. See https://github.com/Zilliqa/Zilliqa-JavaScript-Library/#createtransactionjson in javascript for an example of how to construct the transaction object.

func (*RPC) DSBlockListing

func (r *RPC) DSBlockListing(pageNumber int64) (*ListedBlocks, error)

DSBlockListing returns a paginated list of Directory Service blocks. Pass in page number as parameter. Returns a maxPages variable that specifies the max number of pages. 1 - latest blocks, maxPages - oldest blocks.

func (*RPC) GetBalance

func (r *RPC) GetBalance(address string) (*Balance, error)

GetBalance returns the balance and nonce of a given address.

func (*RPC) GetBlockchainInfo

func (r *RPC) GetBlockchainInfo() (*BlockchainInfo, error)

GetBlockchainInfo returns statistics about the specified zilliqa node.

func (*RPC) GetContractAddressFromTransactionID

func (r *RPC) GetContractAddressFromTransactionID(txHash string) (string, error)

GetContractAddressFromTransactionID returns a smart contract address of 20 bytes from a transaction ID, represented as a String .

func (*RPC) GetCurrentDSEpoch

func (r *RPC) GetCurrentDSEpoch() (string, error)

GetCurrentDSEpoch returns the number of DS epochs in the network so far represented as String.

func (*RPC) GetCurrentMiniEpoch

func (r *RPC) GetCurrentMiniEpoch() (string, error)

GetCurrentMiniEpoch returns the number of TX epochs in the network so far represented as String.

func (*RPC) GetDSBlockRate

func (r *RPC) GetDSBlockRate() (float64, error)

GetDSBlockRate returns the current Directory Service blockrate per second.

func (*RPC) GetDsBlock

func (r *RPC) GetDsBlock(blockNumber string) (*DsBlock, error)

GetDsBlock returns details of a Directory Service block by block number.

func (*RPC) GetLatestDsBlock

func (r *RPC) GetLatestDsBlock() (*DsBlock, error)

GetLatestDsBlock returns details of the most recent Directory Service block.

func (*RPC) GetLatestTxBlock

func (r *RPC) GetLatestTxBlock() (*TxBlock, error)

GetLatestTxBlock returns details of the most recent Transaction block.

func (*RPC) GetMinimumGasPrice

func (r *RPC) GetMinimumGasPrice() (string, error)

GetMinimumGasPrice returns the minimum gas price of the last DS epoch represented as String. This is measured in the smallest price unit Qa (10^-12 Zil) in Zilliqa.

func (*RPC) GetNetworkID

func (r *RPC) GetNetworkID() (string, error)

GetNetworkID returns the network ID of the specified zilliqa node.

func (*RPC) GetNumDSBlocks

func (r *RPC) GetNumDSBlocks() (string, error)

GetNumDSBlocks returns the number of Directory Service blocks in the network so far. This is represented as a String.

func (*RPC) GetNumTransactions

func (r *RPC) GetNumTransactions() (string, error)

GetNumTransactions returns the number of Transactions validated in the network so far. This is represented as a String.

func (*RPC) GetNumTxBlocks

func (r *RPC) GetNumTxBlocks() (string, error)

GetNumTxBlocks returns the number of Transaction blocks in the network so far, this is represented as String.

func (*RPC) GetNumTxnsDSEpoch

func (r *RPC) GetNumTxnsDSEpoch() (string, error)

GetNumTxnsDSEpoch returns the number of transactions in this Directory Service epoch, this is represented as String.

func (*RPC) GetNumTxnsTxEpoch

func (r *RPC) GetNumTxnsTxEpoch() (string, error)

GetNumTxnsTxEpoch returns the number of transactions in this Transaction epoch, this is represented as String.

func (*RPC) GetPrevDSDifficulty

func (r *RPC) GetPrevDSDifficulty() (int64, error)

GetPrevDSDifficulty returns the minimum DS difficulty of the previous block, this is represented as an Number.

func (*RPC) GetPrevDifficulty

func (r *RPC) GetPrevDifficulty() (int64, error)

GetPrevDifficulty returns the minimum shard difficulty of the previous block, this is represented as an Number.

func (*RPC) GetRecentTransactions

func (r *RPC) GetRecentTransactions() (*RecentTransactions, error)

GetRecentTransactions returns the most recent transactions (upto 100) accepted by the specified zilliqa node.

func (*RPC) GetShardingStructure

func (r *RPC) GetShardingStructure() (*ShardingStructure, error)

GetShardingStructure returns the current sharding structure of the network from the specified network's lookup node.

func (*RPC) GetSmartContractCode

func (r *RPC) GetSmartContractCode(contractAddress string) (string, error)

GetSmartContractCode returns the Scilla code of a smart contract address.

func (*RPC) GetSmartContractInit

func (r *RPC) GetSmartContractInit(contractAddress string) ([]SmartContractState, error)

GetSmartContractInit returns the initialization parameters (immutable) of a given smart contract address.

func (*RPC) GetSmartContractState

func (r *RPC) GetSmartContractState(contractAddress string) ([]SmartContractState, error)

GetSmartContractState returns the state variables (mutable) of a smart contract address.

func (*RPC) GetSmartContracts

func (r *RPC) GetSmartContracts(address string) ([]SmartContract, error)

GetSmartContracts returns the list of smart contracts created by an address.

func (*RPC) GetTransaction

func (r *RPC) GetTransaction(txHash string) (*Transaction, error)

GetTransaction returns details of a Transaction by its hash.

func (*RPC) GetTransactionRate

func (r *RPC) GetTransactionRate() (float64, error)

GetTransactionRate returns the current Transaction rate of the network.

func (*RPC) GetTransactionsForTxBlock

func (r *RPC) GetTransactionsForTxBlock(blockNumber string) ([][]string, error)

GetTransactionsForTxBlock returns the transactions included within a micro-block created by a specific shard.

func (*RPC) GetTxBlock

func (r *RPC) GetTxBlock(blockNumber string) (*TxBlock, error)

GetTxBlock returns details of a Transaction block by block number.

func (*RPC) GetTxBlockRate

func (r *RPC) GetTxBlockRate() (float64, error)

GetTxBlockRate returns the current Transaction blockrate per second.

func (*RPC) TxBlockListing

func (r *RPC) TxBlockListing(pageNumber int64) (*ListedBlocks, error)

TxBlockListing returns a paginated list of Transaction blocks. Pass in page number as parameter. Returns a maxPages variable that specifies the max number of pages. 1 - latest blocks, maxPages - oldest blocks.

type RawTransaction

type RawTransaction struct {
	Version   uint32   `json:"version"`
	Nonce     uint64   `json:"nonce"`
	To        string   `json:"toAddr"`
	Amount    string   `json:"amount"`
	PubKey    string   `json:"pubKey"`
	GasPrice  *big.Int `json:"gasPrice"`
	GasLimit  uint64   `json:"gasLimit"`
	Code      string   `json:"code"`
	Data      string   `json:"data"`
	Signature string   `json:"signature"`
}

RawTransaction describes a raw transaction object, which can be used in creating a new transaction.

type RecentTransactions

type RecentTransactions struct {
	TxnHashes []string `json:"TxnHashes"`
	Number    int64    `json:"number"`
}

RecentTransactions contains the most recent transactions (up to 100).

type ShardingStructure

type ShardingStructure struct {
	NumPeers []int64 `json:"NumPeers"`
}

ShardingStructure contains the number of peers in each shard.

type SmartContract

type SmartContract struct {
	Address string               `json:"address"`
	State   []SmartContractState `json:"state"`
}

SmartContract describes the smart contracts created by an address.

type SmartContractState

type SmartContractState struct {
	Type  string `json:"type"`
	Value string `json:"value"`
	Vname string `json:"vname"`
}

SmartContractState describes the state of a smart contract.

type Transaction

type Transaction struct {
	ID       string `json:"ID"`
	Amount   string `json:"amount"`
	GasLimit string `json:"gasLimit"`
	GasPrice string `json:"gasPrice"`
	Nonce    string `json:"nonce"`
	Receipt  struct {
		CumulativeGas string `json:"cumulative_gas"`
		EpochNum      string `json:"epoch_num"`
		Success       bool   `json:"success"`
	} `json:"receipt"`
	SenderPubKey string `json:"senderPubKey"`
	Signature    string `json:"signature"`
	ToAddr       string `json:"toAddr"`
	Version      string `json:"version"`
}

Transaction describes a transaction object.

type TxBlock

type TxBlock struct {
	Body struct {
		HeaderSign      string `json:"HeaderSign"`
		MicroBlockInfos []struct {
			MicroBlockHash        string `json:"MicroBlockHash"`
			MicroBlockShardID     int64  `json:"MicroBlockShardId"`
			MicroBlockTxnRootHash string `json:"MicroBlockTxnRootHash"`
		} `json:"MicroBlockInfos"`
	} `json:"body"`
	Header struct {
		BlockNum       string `json:"BlockNum"`
		DsBlockNum     string `json:"DSBlockNum"`
		GasLimit       string `json:"GasLimit"`
		GasUsed        string `json:"GasUsed"`
		MbInfoHash     string `json:"MbInfoHash"`
		MinerPubKey    string `json:"MinerPubKey"`
		NumMicroBlocks int64  `json:"NumMicroBlocks"`
		NumTxns        int64  `json:"NumTxns"`
		PrevBlockHash  string `json:"PrevBlockHash"`
		Rewards        string `json:"Rewards"`
		StateDeltaHash string `json:"StateDeltaHash"`
		StateRootHash  string `json:"StateRootHash"`
		Timestamp      string `json:"Timestamp"`
		TxnHash        string `json:"TxnHash"`
		Version        int64  `json:"version"`
	} `json:"header"`
}

TxBlock describes a TX-Block.

type Zillean

type Zillean struct {
	ECS *ECSchnorr
	RPC *RPC
}

Zillean represents the zillean object.

func NewZillean

func NewZillean(endpoint string) *Zillean

NewZillean returns a new Zillean.

func (*Zillean) GeneratePrivateKey

func (z *Zillean) GeneratePrivateKey() string

GeneratePrivateKey returns string which represents a generated private key.

func (*Zillean) GetAddressFromPrivateKey

func (z *Zillean) GetAddressFromPrivateKey(privateKey string) (string, error)

GetAddressFromPrivateKey returns the address derived from a private key.

func (*Zillean) GetAddressFromPublicKey

func (z *Zillean) GetAddressFromPublicKey(publicKey string) (string, error)

GetAddressFromPublicKey returns the address derived from a public key.

func (*Zillean) GetPublicKeyFromPrivateKey

func (z *Zillean) GetPublicKeyFromPrivateKey(privateKey string) (string, error)

GetPublicKeyFromPrivateKey returns the public key derived from a private key.

func (*Zillean) IsAddress

func (z *Zillean) IsAddress(address string) bool

IsAddress checks whether a given string is an address or not.

func (*Zillean) IsPublicKey

func (z *Zillean) IsPublicKey(publicKey string) bool

IsPublicKey checks whether a given string is a public key or not.

func (*Zillean) SignTransaction

func (z *Zillean) SignTransaction(rawTx RawTransaction, privateKey string) (string, error)

SignTransaction returns the EC-Schnorr signature on a raw transaction.

func (*Zillean) VerifyPrivateKey

func (z *Zillean) VerifyPrivateKey(privateKey string) (bool, error)

VerifyPrivateKey verifies a EC-Schnorr private key.

func (*Zillean) VerifySignature

func (z *Zillean) VerifySignature(r, s, publicKey, msg []byte) bool

VerifySignature verifies a signature on a message.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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