diff options
author | Wei-Ning Huang <w@dexon.org> | 2019-03-17 09:12:50 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:27:23 +0800 |
commit | 9493109f2be4507605e6b17e406bf8fd147ab3c8 (patch) | |
tree | 84fee3d1fb2095ff5ba793bdfccba89970bc8f89 /vendor/github.com/onrik | |
parent | 9ee92eff068b09246ab6446efa39abfc0c7bd8a8 (diff) | |
download | go-tangerine-9493109f2be4507605e6b17e406bf8fd147ab3c8.tar go-tangerine-9493109f2be4507605e6b17e406bf8fd147ab3c8.tar.gz go-tangerine-9493109f2be4507605e6b17e406bf8fd147ab3c8.tar.bz2 go-tangerine-9493109f2be4507605e6b17e406bf8fd147ab3c8.tar.lz go-tangerine-9493109f2be4507605e6b17e406bf8fd147ab3c8.tar.xz go-tangerine-9493109f2be4507605e6b17e406bf8fd147ab3c8.tar.zst go-tangerine-9493109f2be4507605e6b17e406bf8fd147ab3c8.zip |
dex: implement recovery mechanism (#258)
* dex: implement recovery mechanism
The DEXON recovery protocol allows us to use the Ethereum blockchain as a
fallback consensus chain to coordinate recovery.
* fix
Diffstat (limited to 'vendor/github.com/onrik')
-rw-r--r-- | vendor/github.com/onrik/ethrpc/LICENSE | 21 | ||||
-rw-r--r-- | vendor/github.com/onrik/ethrpc/README.md | 103 | ||||
-rw-r--r-- | vendor/github.com/onrik/ethrpc/ethrpc.go | 514 | ||||
-rw-r--r-- | vendor/github.com/onrik/ethrpc/go.mod | 1 | ||||
-rw-r--r-- | vendor/github.com/onrik/ethrpc/helpers.go | 40 | ||||
-rw-r--r-- | vendor/github.com/onrik/ethrpc/interface.go | 50 | ||||
-rw-r--r-- | vendor/github.com/onrik/ethrpc/options.go | 35 | ||||
-rw-r--r-- | vendor/github.com/onrik/ethrpc/types.go | 322 |
8 files changed, 1086 insertions, 0 deletions
diff --git a/vendor/github.com/onrik/ethrpc/LICENSE b/vendor/github.com/onrik/ethrpc/LICENSE new file mode 100644 index 000000000..c8162bd91 --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Andrey + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/onrik/ethrpc/README.md b/vendor/github.com/onrik/ethrpc/README.md new file mode 100644 index 000000000..c273e8931 --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/README.md @@ -0,0 +1,103 @@ +# Ethrpc +[![Build Status](https://travis-ci.org/onrik/ethrpc.svg?branch=master)](https://travis-ci.org/onrik/ethrpc) +[![Coverage Status](https://coveralls.io/repos/github/onrik/ethrpc/badge.svg?branch=master)](https://coveralls.io/github/onrik/ethrpc?branch=master) +[![Go Report Card](https://goreportcard.com/badge/github.com/onrik/ethrpc)](https://goreportcard.com/report/github.com/onrik/ethrpc) +[![GoDoc](https://godoc.org/github.com/onrik/ethrpc?status.svg)](https://godoc.org/github.com/onrik/ethrpc) +[![Donate with Ethereum](https://en.cryptobadges.io/badge/micro/0xf4144308d6D67A1F00a61A596c0eB7B08411344a)](https://en.cryptobadges.io/donate/0xf4144308d6D67A1F00a61A596c0eB7B08411344a) + +Golang client for ethereum [JSON RPC API](https://github.com/ethereum/wiki/wiki/JSON-RPC). + +- [x] web3_clientVersion +- [x] web3_sha3 +- [x] net_version +- [x] net_peerCount +- [x] net_listening +- [x] eth_protocolVersion +- [x] eth_syncing +- [x] eth_coinbase +- [x] eth_mining +- [x] eth_hashrate +- [x] eth_gasPrice +- [x] eth_accounts +- [x] eth_blockNumber +- [x] eth_getBalance +- [x] eth_getStorageAt +- [x] eth_getTransactionCount +- [x] eth_getBlockTransactionCountByHash +- [x] eth_getBlockTransactionCountByNumber +- [x] eth_getUncleCountByBlockHash +- [x] eth_getUncleCountByBlockNumber +- [x] eth_getCode +- [x] eth_sign +- [x] eth_sendTransaction +- [x] eth_sendRawTransaction +- [x] eth_call +- [x] eth_estimateGas +- [x] eth_getBlockByHash +- [x] eth_getBlockByNumber +- [x] eth_getTransactionByHash +- [x] eth_getTransactionByBlockHashAndIndex +- [x] eth_getTransactionByBlockNumberAndIndex +- [x] eth_getTransactionReceipt +- [ ] eth_getUncleByBlockHashAndIndex +- [ ] eth_getUncleByBlockNumberAndIndex +- [x] eth_getCompilers +- [ ] eth_compileLLL +- [ ] eth_compileSolidity +- [ ] eth_compileSerpent +- [x] eth_newFilter +- [x] eth_newBlockFilter +- [x] eth_newPendingTransactionFilter +- [x] eth_uninstallFilter +- [x] eth_getFilterChanges +- [x] eth_getFilterLogs +- [x] eth_getLogs +- [ ] eth_getWork +- [ ] eth_submitWork +- [ ] eth_submitHashrate +- [ ] shh_post +- [ ] shh_version +- [ ] shh_newIdentity +- [ ] shh_hasIdentity +- [ ] shh_newGroup +- [ ] shh_addToGroup +- [ ] shh_newFilter +- [ ] shh_uninstallFilter +- [ ] shh_getFilterChanges +- [ ] shh_getMessages + +##### Usage: +```go +package main + +import ( + "fmt" + "log" + + "github.com/onrik/ethrpc" +) + +func main() { + client := ethrpc.New("http://127.0.0.1:8545") + + version, err := client.Web3ClientVersion() + if err != nil { + log.Fatal(err) + } + fmt.Println(version) + + // Send 1 eth + txid, err := client.EthSendTransaction(ethrpc.T{ + From: "0x6247cf0412c6462da2a51d05139e2a3c6c630f0a", + To: "0xcfa202c4268749fbb5136f2b68f7402984ed444b", + Value: ethrpc.Eth1(), + }) + if err != nil { + log.Fatal(err) + } + fmt.Println(txid) +} +``` + +[![Donate with Ethereum](https://en.cryptobadges.io/badge/big/0xf4144308d6D67A1F00a61A596c0eB7B08411344a?showBalance=true)](https://en.cryptobadges.io/donate/0xf4144308d6D67A1F00a61A596c0eB7B08411344a) + diff --git a/vendor/github.com/onrik/ethrpc/ethrpc.go b/vendor/github.com/onrik/ethrpc/ethrpc.go new file mode 100644 index 000000000..5118b425d --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/ethrpc.go @@ -0,0 +1,514 @@ +package ethrpc + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "math/big" + "net/http" + "os" +) + +// EthError - ethereum error +type EthError struct { + Code int `json:"code"` + Message string `json:"message"` +} + +func (err EthError) Error() string { + return fmt.Sprintf("Error %d (%s)", err.Code, err.Message) +} + +type ethResponse struct { + ID int `json:"id"` + JSONRPC string `json:"jsonrpc"` + Result json.RawMessage `json:"result"` + Error *EthError `json:"error"` +} + +type ethRequest struct { + ID int `json:"id"` + JSONRPC string `json:"jsonrpc"` + Method string `json:"method"` + Params []interface{} `json:"params"` +} + +// EthRPC - Ethereum rpc client +type EthRPC struct { + url string + client httpClient + log logger + Debug bool +} + +// New create new rpc client with given url +func New(url string, options ...func(rpc *EthRPC)) *EthRPC { + rpc := &EthRPC{ + url: url, + client: http.DefaultClient, + log: log.New(os.Stderr, "", log.LstdFlags), + } + for _, option := range options { + option(rpc) + } + + return rpc +} + +// NewEthRPC create new rpc client with given url +func NewEthRPC(url string, options ...func(rpc *EthRPC)) *EthRPC { + return New(url, options...) +} + +func (rpc *EthRPC) call(method string, target interface{}, params ...interface{}) error { + result, err := rpc.Call(method, params...) + if err != nil { + return err + } + + if target == nil { + return nil + } + + return json.Unmarshal(result, target) +} + +// URL returns client url +func (rpc *EthRPC) URL() string { + return rpc.url +} + +// Call returns raw response of method call +func (rpc *EthRPC) Call(method string, params ...interface{}) (json.RawMessage, error) { + request := ethRequest{ + ID: 1, + JSONRPC: "2.0", + Method: method, + Params: params, + } + + body, err := json.Marshal(request) + if err != nil { + return nil, err + } + + response, err := rpc.client.Post(rpc.url, "application/json", bytes.NewBuffer(body)) + if response != nil { + defer response.Body.Close() + } + if err != nil { + return nil, err + } + + data, err := ioutil.ReadAll(response.Body) + if err != nil { + return nil, err + } + + if rpc.Debug { + rpc.log.Println(fmt.Sprintf("%s\nRequest: %s\nResponse: %s\n", method, body, data)) + } + + resp := new(ethResponse) + if err := json.Unmarshal(data, resp); err != nil { + return nil, err + } + + if resp.Error != nil { + return nil, *resp.Error + } + + return resp.Result, nil + +} + +// RawCall returns raw response of method call (Deprecated) +func (rpc *EthRPC) RawCall(method string, params ...interface{}) (json.RawMessage, error) { + return rpc.Call(method, params...) +} + +// Web3ClientVersion returns the current client version. +func (rpc *EthRPC) Web3ClientVersion() (string, error) { + var clientVersion string + + err := rpc.call("web3_clientVersion", &clientVersion) + return clientVersion, err +} + +// Web3Sha3 returns Keccak-256 (not the standardized SHA3-256) of the given data. +func (rpc *EthRPC) Web3Sha3(data []byte) (string, error) { + var hash string + + err := rpc.call("web3_sha3", &hash, fmt.Sprintf("0x%x", data)) + return hash, err +} + +// NetVersion returns the current network protocol version. +func (rpc *EthRPC) NetVersion() (string, error) { + var version string + + err := rpc.call("net_version", &version) + return version, err +} + +// NetListening returns true if client is actively listening for network connections. +func (rpc *EthRPC) NetListening() (bool, error) { + var listening bool + + err := rpc.call("net_listening", &listening) + return listening, err +} + +// NetPeerCount returns number of peers currently connected to the client. +func (rpc *EthRPC) NetPeerCount() (int, error) { + var response string + if err := rpc.call("net_peerCount", &response); err != nil { + return 0, err + } + + return ParseInt(response) +} + +// EthProtocolVersion returns the current ethereum protocol version. +func (rpc *EthRPC) EthProtocolVersion() (string, error) { + var protocolVersion string + + err := rpc.call("eth_protocolVersion", &protocolVersion) + return protocolVersion, err +} + +// EthSyncing returns an object with data about the sync status or false. +func (rpc *EthRPC) EthSyncing() (*Syncing, error) { + result, err := rpc.RawCall("eth_syncing") + if err != nil { + return nil, err + } + syncing := new(Syncing) + if bytes.Equal(result, []byte("false")) { + return syncing, nil + } + err = json.Unmarshal(result, syncing) + return syncing, err +} + +// EthCoinbase returns the client coinbase address +func (rpc *EthRPC) EthCoinbase() (string, error) { + var address string + + err := rpc.call("eth_coinbase", &address) + return address, err +} + +// EthMining returns true if client is actively mining new blocks. +func (rpc *EthRPC) EthMining() (bool, error) { + var mining bool + + err := rpc.call("eth_mining", &mining) + return mining, err +} + +// EthHashrate returns the number of hashes per second that the node is mining with. +func (rpc *EthRPC) EthHashrate() (int, error) { + var response string + + if err := rpc.call("eth_hashrate", &response); err != nil { + return 0, err + } + + return ParseInt(response) +} + +// EthGasPrice returns the current price per gas in wei. +func (rpc *EthRPC) EthGasPrice() (big.Int, error) { + var response string + if err := rpc.call("eth_gasPrice", &response); err != nil { + return big.Int{}, err + } + + return ParseBigInt(response) +} + +// EthAccounts returns a list of addresses owned by client. +func (rpc *EthRPC) EthAccounts() ([]string, error) { + accounts := []string{} + + err := rpc.call("eth_accounts", &accounts) + return accounts, err +} + +// EthBlockNumber returns the number of most recent block. +func (rpc *EthRPC) EthBlockNumber() (int, error) { + var response string + if err := rpc.call("eth_blockNumber", &response); err != nil { + return 0, err + } + + return ParseInt(response) +} + +// EthGetBalance returns the balance of the account of given address in wei. +func (rpc *EthRPC) EthGetBalance(address, block string) (big.Int, error) { + var response string + if err := rpc.call("eth_getBalance", &response, address, block); err != nil { + return big.Int{}, err + } + + return ParseBigInt(response) +} + +// EthGetStorageAt returns the value from a storage position at a given address. +func (rpc *EthRPC) EthGetStorageAt(data string, position int, tag string) (string, error) { + var result string + + err := rpc.call("eth_getStorageAt", &result, data, IntToHex(position), tag) + return result, err +} + +// EthGetTransactionCount returns the number of transactions sent from an address. +func (rpc *EthRPC) EthGetTransactionCount(address, block string) (int, error) { + var response string + + if err := rpc.call("eth_getTransactionCount", &response, address, block); err != nil { + return 0, err + } + + return ParseInt(response) +} + +// EthGetBlockTransactionCountByHash returns the number of transactions in a block from a block matching the given block hash. +func (rpc *EthRPC) EthGetBlockTransactionCountByHash(hash string) (int, error) { + var response string + + if err := rpc.call("eth_getBlockTransactionCountByHash", &response, hash); err != nil { + return 0, err + } + + return ParseInt(response) +} + +// EthGetBlockTransactionCountByNumber returns the number of transactions in a block from a block matching the given block +func (rpc *EthRPC) EthGetBlockTransactionCountByNumber(number int) (int, error) { + var response string + + if err := rpc.call("eth_getBlockTransactionCountByNumber", &response, IntToHex(number)); err != nil { + return 0, err + } + + return ParseInt(response) +} + +// EthGetUncleCountByBlockHash returns the number of uncles in a block from a block matching the given block hash. +func (rpc *EthRPC) EthGetUncleCountByBlockHash(hash string) (int, error) { + var response string + + if err := rpc.call("eth_getUncleCountByBlockHash", &response, hash); err != nil { + return 0, err + } + + return ParseInt(response) +} + +// EthGetUncleCountByBlockNumber returns the number of uncles in a block from a block matching the given block number. +func (rpc *EthRPC) EthGetUncleCountByBlockNumber(number int) (int, error) { + var response string + + if err := rpc.call("eth_getUncleCountByBlockNumber", &response, IntToHex(number)); err != nil { + return 0, err + } + + return ParseInt(response) +} + +// EthGetCode returns code at a given address. +func (rpc *EthRPC) EthGetCode(address, block string) (string, error) { + var code string + + err := rpc.call("eth_getCode", &code, address, block) + return code, err +} + +// EthSign signs data with a given address. +// Calculates an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))) +func (rpc *EthRPC) EthSign(address, data string) (string, error) { + var signature string + + err := rpc.call("eth_sign", &signature, address, data) + return signature, err +} + +// EthSendTransaction creates new message call transaction or a contract creation, if the data field contains code. +func (rpc *EthRPC) EthSendTransaction(transaction T) (string, error) { + var hash string + + err := rpc.call("eth_sendTransaction", &hash, transaction) + return hash, err +} + +// EthSendRawTransaction creates new message call transaction or a contract creation for signed transactions. +func (rpc *EthRPC) EthSendRawTransaction(data string) (string, error) { + var hash string + + err := rpc.call("eth_sendRawTransaction", &hash, data) + return hash, err +} + +// EthCall executes a new message call immediately without creating a transaction on the block chain. +func (rpc *EthRPC) EthCall(transaction T, tag string) (string, error) { + var data string + + err := rpc.call("eth_call", &data, transaction, tag) + return data, err +} + +// EthEstimateGas makes a call or transaction, which won't be added to the blockchain and returns the used gas, which can be used for estimating the used gas. +func (rpc *EthRPC) EthEstimateGas(transaction T) (int, error) { + var response string + + err := rpc.call("eth_estimateGas", &response, transaction) + if err != nil { + return 0, err + } + + return ParseInt(response) +} + +func (rpc *EthRPC) getBlock(method string, withTransactions bool, params ...interface{}) (*Block, error) { + result, err := rpc.RawCall(method, params...) + if err != nil { + return nil, err + } + if bytes.Equal(result, []byte("null")) { + return nil, nil + } + + var response proxyBlock + if withTransactions { + response = new(proxyBlockWithTransactions) + } else { + response = new(proxyBlockWithoutTransactions) + } + + err = json.Unmarshal(result, response) + if err != nil { + return nil, err + } + + block := response.toBlock() + return &block, nil +} + +// EthGetBlockByHash returns information about a block by hash. +func (rpc *EthRPC) EthGetBlockByHash(hash string, withTransactions bool) (*Block, error) { + return rpc.getBlock("eth_getBlockByHash", withTransactions, hash, withTransactions) +} + +// EthGetBlockByNumber returns information about a block by block number. +func (rpc *EthRPC) EthGetBlockByNumber(number int, withTransactions bool) (*Block, error) { + return rpc.getBlock("eth_getBlockByNumber", withTransactions, IntToHex(number), withTransactions) +} + +func (rpc *EthRPC) getTransaction(method string, params ...interface{}) (*Transaction, error) { + transaction := new(Transaction) + + err := rpc.call(method, transaction, params...) + return transaction, err +} + +// EthGetTransactionByHash returns the information about a transaction requested by transaction hash. +func (rpc *EthRPC) EthGetTransactionByHash(hash string) (*Transaction, error) { + return rpc.getTransaction("eth_getTransactionByHash", hash) +} + +// EthGetTransactionByBlockHashAndIndex returns information about a transaction by block hash and transaction index position. +func (rpc *EthRPC) EthGetTransactionByBlockHashAndIndex(blockHash string, transactionIndex int) (*Transaction, error) { + return rpc.getTransaction("eth_getTransactionByBlockHashAndIndex", blockHash, IntToHex(transactionIndex)) +} + +// EthGetTransactionByBlockNumberAndIndex returns information about a transaction by block number and transaction index position. +func (rpc *EthRPC) EthGetTransactionByBlockNumberAndIndex(blockNumber, transactionIndex int) (*Transaction, error) { + return rpc.getTransaction("eth_getTransactionByBlockNumberAndIndex", IntToHex(blockNumber), IntToHex(transactionIndex)) +} + +// EthGetTransactionReceipt returns the receipt of a transaction by transaction hash. +// Note That the receipt is not available for pending transactions. +func (rpc *EthRPC) EthGetTransactionReceipt(hash string) (*TransactionReceipt, error) { + transactionReceipt := new(TransactionReceipt) + + err := rpc.call("eth_getTransactionReceipt", transactionReceipt, hash) + if err != nil { + return nil, err + } + + return transactionReceipt, nil +} + +// EthGetCompilers returns a list of available compilers in the client. +func (rpc *EthRPC) EthGetCompilers() ([]string, error) { + compilers := []string{} + + err := rpc.call("eth_getCompilers", &compilers) + return compilers, err +} + +// EthNewFilter creates a new filter object. +func (rpc *EthRPC) EthNewFilter(params FilterParams) (string, error) { + var filterID string + err := rpc.call("eth_newFilter", &filterID, params) + return filterID, err +} + +// EthNewBlockFilter creates a filter in the node, to notify when a new block arrives. +// To check if the state has changed, call EthGetFilterChanges. +func (rpc *EthRPC) EthNewBlockFilter() (string, error) { + var filterID string + err := rpc.call("eth_newBlockFilter", &filterID) + return filterID, err +} + +// EthNewPendingTransactionFilter creates a filter in the node, to notify when new pending transactions arrive. +// To check if the state has changed, call EthGetFilterChanges. +func (rpc *EthRPC) EthNewPendingTransactionFilter() (string, error) { + var filterID string + err := rpc.call("eth_newPendingTransactionFilter", &filterID) + return filterID, err +} + +// EthUninstallFilter uninstalls a filter with given id. +func (rpc *EthRPC) EthUninstallFilter(filterID string) (bool, error) { + var res bool + err := rpc.call("eth_uninstallFilter", &res, filterID) + return res, err +} + +// EthGetFilterChanges polling method for a filter, which returns an array of logs which occurred since last poll. +func (rpc *EthRPC) EthGetFilterChanges(filterID string) ([]Log, error) { + var logs = []Log{} + err := rpc.call("eth_getFilterChanges", &logs, filterID) + return logs, err +} + +// EthGetFilterLogs returns an array of all logs matching filter with given id. +func (rpc *EthRPC) EthGetFilterLogs(filterID string) ([]Log, error) { + var logs = []Log{} + err := rpc.call("eth_getFilterLogs", &logs, filterID) + return logs, err +} + +// EthGetLogs returns an array of all logs matching a given filter object. +func (rpc *EthRPC) EthGetLogs(params FilterParams) ([]Log, error) { + var logs = []Log{} + err := rpc.call("eth_getLogs", &logs, params) + return logs, err +} + +// Eth1 returns 1 ethereum value (10^18 wei) +func (rpc *EthRPC) Eth1() *big.Int { + return Eth1() +} + +// Eth1 returns 1 ethereum value (10^18 wei) +func Eth1() *big.Int { + return big.NewInt(1000000000000000000) +} diff --git a/vendor/github.com/onrik/ethrpc/go.mod b/vendor/github.com/onrik/ethrpc/go.mod new file mode 100644 index 000000000..8f047b1d3 --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/go.mod @@ -0,0 +1 @@ +module github.com/onrik/ethrpc diff --git a/vendor/github.com/onrik/ethrpc/helpers.go b/vendor/github.com/onrik/ethrpc/helpers.go new file mode 100644 index 000000000..e98030055 --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/helpers.go @@ -0,0 +1,40 @@ +package ethrpc + +import ( + "fmt" + "math/big" + "strconv" + "strings" +) + +// ParseInt parse hex string value to int +func ParseInt(value string) (int, error) { + i, err := strconv.ParseInt(strings.TrimPrefix(value, "0x"), 16, 64) + if err != nil { + return 0, err + } + + return int(i), nil +} + +// ParseBigInt parse hex string value to big.Int +func ParseBigInt(value string) (big.Int, error) { + i := big.Int{} + _, err := fmt.Sscan(value, &i) + + return i, err +} + +// IntToHex convert int to hexadecimal representation +func IntToHex(i int) string { + return fmt.Sprintf("0x%x", i) +} + +// BigToHex covert big.Int to hexadecimal representation +func BigToHex(bigInt big.Int) string { + if bigInt.BitLen() == 0 { + return "0x0" + } + + return "0x" + strings.TrimPrefix(fmt.Sprintf("%x", bigInt.Bytes()), "0") +} diff --git a/vendor/github.com/onrik/ethrpc/interface.go b/vendor/github.com/onrik/ethrpc/interface.go new file mode 100644 index 000000000..2e3021d1b --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/interface.go @@ -0,0 +1,50 @@ +package ethrpc + +import ( + "math/big" +) + +type EthereumAPI interface { + Web3ClientVersion() (string, error) + Web3Sha3(data []byte) (string, error) + NetVersion() (string, error) + NetListening() (bool, error) + NetPeerCount() (int, error) + EthProtocolVersion() (string, error) + EthSyncing() (*Syncing, error) + EthCoinbase() (string, error) + EthMining() (bool, error) + EthHashrate() (int, error) + EthGasPrice() (big.Int, error) + EthAccounts() ([]string, error) + EthBlockNumber() (int, error) + EthGetBalance(address, block string) (big.Int, error) + EthGetStorageAt(data string, position int, tag string) (string, error) + EthGetTransactionCount(address, block string) (int, error) + EthGetBlockTransactionCountByHash(hash string) (int, error) + EthGetBlockTransactionCountByNumber(number int) (int, error) + EthGetUncleCountByBlockHash(hash string) (int, error) + EthGetUncleCountByBlockNumber(number int) (int, error) + EthGetCode(address, block string) (string, error) + EthSign(address, data string) (string, error) + EthSendTransaction(transaction T) (string, error) + EthSendRawTransaction(data string) (string, error) + EthCall(transaction T, tag string) (string, error) + EthEstimateGas(transaction T) (int, error) + EthGetBlockByHash(hash string, withTransactions bool) (*Block, error) + EthGetBlockByNumber(number int, withTransactions bool) (*Block, error) + EthGetTransactionByHash(hash string) (*Transaction, error) + EthGetTransactionByBlockHashAndIndex(blockHash string, transactionIndex int) (*Transaction, error) + EthGetTransactionByBlockNumberAndIndex(blockNumber, transactionIndex int) (*Transaction, error) + EthGetTransactionReceipt(hash string) (*TransactionReceipt, error) + EthGetCompilers() ([]string, error) + EthNewFilter(params FilterParams) (string, error) + EthNewBlockFilter() (string, error) + EthNewPendingTransactionFilter() (string, error) + EthUninstallFilter(filterID string) (bool, error) + EthGetFilterChanges(filterID string) ([]Log, error) + EthGetFilterLogs(filterID string) ([]Log, error) + EthGetLogs(params FilterParams) ([]Log, error) +} + +var _ EthereumAPI = (*EthRPC)(nil) diff --git a/vendor/github.com/onrik/ethrpc/options.go b/vendor/github.com/onrik/ethrpc/options.go new file mode 100644 index 000000000..72ab39879 --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/options.go @@ -0,0 +1,35 @@ +package ethrpc + +import ( + "io" + "net/http" +) + +type httpClient interface { + Post(url string, contentType string, body io.Reader) (*http.Response, error) +} + +type logger interface { + Println(v ...interface{}) +} + +// WithHttpClient set custom http client +func WithHttpClient(client httpClient) func(rpc *EthRPC) { + return func(rpc *EthRPC) { + rpc.client = client + } +} + +// WithLogger set custom logger +func WithLogger(l logger) func(rpc *EthRPC) { + return func(rpc *EthRPC) { + rpc.log = l + } +} + +// WithDebug set debug flag +func WithDebug(enabled bool) func(rpc *EthRPC) { + return func(rpc *EthRPC) { + rpc.Debug = enabled + } +} diff --git a/vendor/github.com/onrik/ethrpc/types.go b/vendor/github.com/onrik/ethrpc/types.go new file mode 100644 index 000000000..b90baeef0 --- /dev/null +++ b/vendor/github.com/onrik/ethrpc/types.go @@ -0,0 +1,322 @@ +package ethrpc + +import ( + "bytes" + "encoding/json" + "math/big" + "unsafe" +) + +// Syncing - object with syncing data info +type Syncing struct { + IsSyncing bool + StartingBlock int + CurrentBlock int + HighestBlock int +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (s *Syncing) UnmarshalJSON(data []byte) error { + proxy := new(proxySyncing) + if err := json.Unmarshal(data, proxy); err != nil { + return err + } + + proxy.IsSyncing = true + *s = *(*Syncing)(unsafe.Pointer(proxy)) + + return nil +} + +// T - input transaction object +type T struct { + From string + To string + Gas int + GasPrice *big.Int + Value *big.Int + Data string + Nonce int +} + +// MarshalJSON implements the json.Unmarshaler interface. +func (t T) MarshalJSON() ([]byte, error) { + params := map[string]interface{}{ + "from": t.From, + } + if t.To != "" { + params["to"] = t.To + } + if t.Gas > 0 { + params["gas"] = IntToHex(t.Gas) + } + if t.GasPrice != nil { + params["gasPrice"] = BigToHex(*t.GasPrice) + } + if t.Value != nil { + params["value"] = BigToHex(*t.Value) + } + if t.Data != "" { + params["data"] = t.Data + } + if t.Nonce > 0 { + params["nonce"] = IntToHex(t.Nonce) + } + + return json.Marshal(params) +} + +// Transaction - transaction object +type Transaction struct { + Hash string + Nonce int + BlockHash string + BlockNumber *int + TransactionIndex *int + From string + To string + Value big.Int + Gas int + GasPrice big.Int + Input string +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (t *Transaction) UnmarshalJSON(data []byte) error { + proxy := new(proxyTransaction) + if err := json.Unmarshal(data, proxy); err != nil { + return err + } + + *t = *(*Transaction)(unsafe.Pointer(proxy)) + + return nil +} + +// Log - log object +type Log struct { + Removed bool + LogIndex int + TransactionIndex int + TransactionHash string + BlockNumber int + BlockHash string + Address string + Data string + Topics []string +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (log *Log) UnmarshalJSON(data []byte) error { + proxy := new(proxyLog) + if err := json.Unmarshal(data, proxy); err != nil { + return err + } + + *log = *(*Log)(unsafe.Pointer(proxy)) + + return nil +} + +// FilterParams - Filter parameters object +type FilterParams struct { + FromBlock string `json:"fromBlock,omitempty"` + ToBlock string `json:"toBlock,omitempty"` + Address []string `json:"address,omitempty"` + Topics [][]string `json:"topics,omitempty"` +} + +// TransactionReceipt - transaction receipt object +type TransactionReceipt struct { + TransactionHash string + TransactionIndex int + BlockHash string + BlockNumber int + CumulativeGasUsed int + GasUsed int + ContractAddress string + Logs []Log + LogsBloom string + Root string + Status string +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (t *TransactionReceipt) UnmarshalJSON(data []byte) error { + proxy := new(proxyTransactionReceipt) + if err := json.Unmarshal(data, proxy); err != nil { + return err + } + + *t = *(*TransactionReceipt)(unsafe.Pointer(proxy)) + + return nil +} + +// Block - block object +type Block struct { + Number int + Hash string + ParentHash string + Nonce string + Sha3Uncles string + LogsBloom string + TransactionsRoot string + StateRoot string + Miner string + Difficulty big.Int + TotalDifficulty big.Int + ExtraData string + Size int + GasLimit int + GasUsed int + Timestamp int + Uncles []string + Transactions []Transaction +} + +type proxySyncing struct { + IsSyncing bool `json:"-"` + StartingBlock hexInt `json:"startingBlock"` + CurrentBlock hexInt `json:"currentBlock"` + HighestBlock hexInt `json:"highestBlock"` +} + +type proxyTransaction struct { + Hash string `json:"hash"` + Nonce hexInt `json:"nonce"` + BlockHash string `json:"blockHash"` + BlockNumber *hexInt `json:"blockNumber"` + TransactionIndex *hexInt `json:"transactionIndex"` + From string `json:"from"` + To string `json:"to"` + Value hexBig `json:"value"` + Gas hexInt `json:"gas"` + GasPrice hexBig `json:"gasPrice"` + Input string `json:"input"` +} + +type proxyLog struct { + Removed bool `json:"removed"` + LogIndex hexInt `json:"logIndex"` + TransactionIndex hexInt `json:"transactionIndex"` + TransactionHash string `json:"transactionHash"` + BlockNumber hexInt `json:"blockNumber"` + BlockHash string `json:"blockHash"` + Address string `json:"address"` + Data string `json:"data"` + Topics []string `json:"topics"` +} + +type proxyTransactionReceipt struct { + TransactionHash string `json:"transactionHash"` + TransactionIndex hexInt `json:"transactionIndex"` + BlockHash string `json:"blockHash"` + BlockNumber hexInt `json:"blockNumber"` + CumulativeGasUsed hexInt `json:"cumulativeGasUsed"` + GasUsed hexInt `json:"gasUsed"` + ContractAddress string `json:"contractAddress,omitempty"` + Logs []Log `json:"logs"` + LogsBloom string `json:"logsBloom"` + Root string `json:"root"` + Status string `json:"status,omitempty"` +} + +type hexInt int + +func (i *hexInt) UnmarshalJSON(data []byte) error { + result, err := ParseInt(string(bytes.Trim(data, `"`))) + *i = hexInt(result) + + return err +} + +type hexBig big.Int + +func (i *hexBig) UnmarshalJSON(data []byte) error { + result, err := ParseBigInt(string(bytes.Trim(data, `"`))) + *i = hexBig(result) + + return err +} + +type proxyBlock interface { + toBlock() Block +} + +type proxyBlockWithTransactions struct { + Number hexInt `json:"number"` + Hash string `json:"hash"` + ParentHash string `json:"parentHash"` + Nonce string `json:"nonce"` + Sha3Uncles string `json:"sha3Uncles"` + LogsBloom string `json:"logsBloom"` + TransactionsRoot string `json:"transactionsRoot"` + StateRoot string `json:"stateRoot"` + Miner string `json:"miner"` + Difficulty hexBig `json:"difficulty"` + TotalDifficulty hexBig `json:"totalDifficulty"` + ExtraData string `json:"extraData"` + Size hexInt `json:"size"` + GasLimit hexInt `json:"gasLimit"` + GasUsed hexInt `json:"gasUsed"` + Timestamp hexInt `json:"timestamp"` + Uncles []string `json:"uncles"` + Transactions []proxyTransaction `json:"transactions"` +} + +func (proxy *proxyBlockWithTransactions) toBlock() Block { + return *(*Block)(unsafe.Pointer(proxy)) +} + +type proxyBlockWithoutTransactions struct { + Number hexInt `json:"number"` + Hash string `json:"hash"` + ParentHash string `json:"parentHash"` + Nonce string `json:"nonce"` + Sha3Uncles string `json:"sha3Uncles"` + LogsBloom string `json:"logsBloom"` + TransactionsRoot string `json:"transactionsRoot"` + StateRoot string `json:"stateRoot"` + Miner string `json:"miner"` + Difficulty hexBig `json:"difficulty"` + TotalDifficulty hexBig `json:"totalDifficulty"` + ExtraData string `json:"extraData"` + Size hexInt `json:"size"` + GasLimit hexInt `json:"gasLimit"` + GasUsed hexInt `json:"gasUsed"` + Timestamp hexInt `json:"timestamp"` + Uncles []string `json:"uncles"` + Transactions []string `json:"transactions"` +} + +func (proxy *proxyBlockWithoutTransactions) toBlock() Block { + block := Block{ + Number: int(proxy.Number), + Hash: proxy.Hash, + ParentHash: proxy.ParentHash, + Nonce: proxy.Nonce, + Sha3Uncles: proxy.Sha3Uncles, + LogsBloom: proxy.LogsBloom, + TransactionsRoot: proxy.TransactionsRoot, + StateRoot: proxy.StateRoot, + Miner: proxy.Miner, + Difficulty: big.Int(proxy.Difficulty), + TotalDifficulty: big.Int(proxy.TotalDifficulty), + ExtraData: proxy.ExtraData, + Size: int(proxy.Size), + GasLimit: int(proxy.GasLimit), + GasUsed: int(proxy.GasUsed), + Timestamp: int(proxy.Timestamp), + Uncles: proxy.Uncles, + } + + block.Transactions = make([]Transaction, len(proxy.Transactions)) + for i := range proxy.Transactions { + block.Transactions[i] = Transaction{ + Hash: proxy.Transactions[i], + } + } + + return block +} |