zillean

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

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

Go to latest
Published: Dec 26, 2018 License: GPL-3.0 Imports: 12 Imported by: 0

README

Zilliqa Golang SDK

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("http://127.0.0.1:4200")

	// 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:   "1",
		PubKey:   pubKey,
		GasPrice: 1,
		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
  • GetBalance
  • GetDsBlock
  • GetTxBlock
  • GetLatestDsBlock
  • GetLatestTxBlock
  • GetTransaction
  • CreateTransaction
  • GetSmartContracts
  • GetSmartContractState
  • GetSmartContractCode
  • GetSmartContractInit
  • GetBlockchainInfo
  • GetNetworkId
  • GetRecentTransactions
  • GetDSBlockListing
  • GetTxBlockListing

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,
	}
	k, _ := GenerateDRN(EncodeTransaction(rawTx))
	signature, _ := fromZil.SignTransaction(k, 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

func EncodeTransaction

func EncodeTransaction(rawTx RawTransaction) []byte

EncodeTransaction encodes raw transaction using protobuf.

func GenerateDRN

func GenerateDRN(nonce []byte) ([]byte, error)

GenerateDRN generates a random k for Schnorr signature.

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 struct {
		NumPeers []int64 `json:"NumPeers"`
	}
	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, k, msg []byte) ([]byte, []byte, error)

Sign returns the signature (r, s) on a given message. The algorithm takes the following step: 1. Take a radom k as an input 2. Compute the commitment Q = kG, where G is the base point 3. Compute the challenge r = H(Q, pubKey, msg) 4. Compute s = k - r * privKey mod n 5. Signature on m is (r, s)

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) 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) GetNetworkID

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

GetNetworkID returns the network ID of the specified zilliqa node.

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) 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) GetTxBlock

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

GetTxBlock returns details of a Transaction block by block number.

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 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"`
		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"`
		Type           int64  `json:"type"`
		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(k []byte, 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