go-ethereum/xeth/hexface.go

212 lines
4.5 KiB
Go
Raw Normal View History

2014-10-31 08:30:08 -05:00
package xeth
import (
2014-08-20 06:05:26 -05:00
"bytes"
"encoding/json"
2014-12-04 03:28:02 -06:00
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
2014-10-31 06:37:43 -05:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
2014-10-31 08:43:14 -05:00
"github.com/ethereum/go-ethereum/state"
)
2015-01-28 11:14:28 -06:00
// to resolve the import cycle
type Backend interface {
BlockProcessor() *core.BlockProcessor
ChainManager() *core.ChainManager
Coinbase() []byte
KeyManager() *crypto.KeyManager
IsMining() bool
IsListening() bool
PeerCount() int
Db() ethutil.Database
TxPool() *core.TxPool
}
2014-10-31 08:30:08 -05:00
type JSXEth struct {
2015-01-28 11:14:28 -06:00
eth Backend
blockProcessor *core.BlockProcessor
chainManager *core.ChainManager
world *State
}
2015-01-28 11:14:28 -06:00
func NewJSXEth(eth Backend) *JSXEth {
xeth := &JSXEth{
eth: eth,
blockProcessor: eth.BlockProcessor(),
chainManager: eth.ChainManager(),
}
xeth.world = NewState(xeth)
return xeth
}
2015-01-28 11:14:28 -06:00
func (self *JSXEth) State() *State { return self.world }
2014-10-31 08:30:08 -05:00
func (self *JSXEth) BlockByHash(strHash string) *JSBlock {
hash := fromHex(strHash)
2015-01-28 11:14:28 -06:00
block := self.chainManager.GetBlock(hash)
return NewJSBlock(block)
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) BlockByNumber(num int32) *JSBlock {
2014-08-20 06:36:54 -05:00
if num == -1 {
2015-01-28 11:14:28 -06:00
return NewJSBlock(self.chainManager.CurrentBlock())
2014-08-20 06:36:54 -05:00
}
2015-01-28 11:14:28 -06:00
return NewJSBlock(self.chainManager.GetBlockByNumber(uint64(num)))
2014-08-20 06:36:54 -05:00
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) Block(v interface{}) *JSBlock {
2014-08-20 09:40:19 -05:00
if n, ok := v.(int32); ok {
return self.BlockByNumber(n)
} else if str, ok := v.(string); ok {
return self.BlockByHash(str)
} else if f, ok := v.(float64); ok { // Don't ask ...
return self.BlockByNumber(int32(f))
}
return nil
}
func (self *JSXEth) Accounts() []string {
2015-01-28 11:14:28 -06:00
return []string{toHex(self.eth.KeyManager().Address())}
}
2015-01-28 11:14:28 -06:00
/*
2014-10-31 08:30:08 -05:00
func (self *JSXEth) StateObject(addr string) *JSObject {
2015-01-28 11:14:28 -06:00
object := &Object{self.State().safeGet(fromHex(addr))}
return NewJSObject(object)
}
2015-01-28 11:14:28 -06:00
*/
2014-10-31 08:30:08 -05:00
func (self *JSXEth) PeerCount() int {
2015-01-28 11:14:28 -06:00
return self.eth.PeerCount()
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) IsMining() bool {
2015-01-28 11:14:28 -06:00
return self.eth.IsMining()
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) IsListening() bool {
2015-01-28 11:14:28 -06:00
return self.eth.IsListening()
}
2015-01-28 11:14:28 -06:00
func (self *JSXEth) Coinbase() string {
return toHex(self.eth.KeyManager().Address())
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) NumberToHuman(balance string) string {
2014-08-18 03:17:45 -05:00
b := ethutil.Big(balance)
return ethutil.CurrencyToString(b)
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) StorageAt(addr, storageAddr string) string {
2015-01-28 11:14:28 -06:00
storage := self.State().SafeGet(addr).StorageString(storageAddr)
2014-08-21 14:06:42 -05:00
return toHex(storage.Bytes())
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) BalanceAt(addr string) string {
2015-01-28 11:14:28 -06:00
return self.State().SafeGet(addr).Balance().String()
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) TxCountAt(address string) int {
2015-01-28 11:14:28 -06:00
return int(self.State().SafeGet(address).Nonce)
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) CodeAt(address string) string {
2015-01-28 11:14:28 -06:00
return toHex(self.State().SafeGet(address).Code)
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) IsContract(address string) bool {
2015-01-28 11:14:28 -06:00
return len(self.State().SafeGet(address).Code) > 0
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) SecretToAddress(key string) string {
pair, err := crypto.NewKeyPairFromSec(fromHex(key))
if err != nil {
return ""
}
return toHex(pair.Address())
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) Execute(addr, value, gas, price, data string) (string, error) {
2015-01-28 11:14:28 -06:00
return "", nil
}
type KeyVal struct {
Key string `json:"key"`
Value string `json:"value"`
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) EachStorage(addr string) string {
var values []KeyVal
2015-01-28 11:14:28 -06:00
object := self.State().SafeGet(addr)
2014-12-23 11:35:36 -06:00
it := object.Trie().Iterator()
for it.Next() {
values = append(values, KeyVal{toHex(it.Key), toHex(it.Value)})
2014-12-23 11:35:36 -06:00
}
valuesJson, err := json.Marshal(values)
if err != nil {
return ""
}
return string(valuesJson)
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) ToAscii(str string) string {
2014-08-20 06:05:26 -05:00
padded := ethutil.RightPadBytes([]byte(str), 32)
return "0x" + toHex(padded)
2014-08-20 06:05:26 -05:00
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) FromAscii(str string) string {
2014-08-20 06:05:26 -05:00
if ethutil.IsHex(str) {
str = str[2:]
}
return string(bytes.Trim(fromHex(str), "\x00"))
2014-08-20 06:05:26 -05:00
}
2014-10-31 08:30:08 -05:00
func (self *JSXEth) FromNumber(str string) string {
2014-08-20 06:05:26 -05:00
if ethutil.IsHex(str) {
str = str[2:]
}
return ethutil.BigD(fromHex(str)).String()
2014-08-20 06:05:26 -05:00
}
func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
2015-01-28 11:14:28 -06:00
return "", nil
}
2014-08-17 05:41:52 -05:00
2015-01-28 11:14:28 -06:00
func ToJSMessages(messages state.Messages) *ethutil.List {
var msgs []JSMessage
for _, m := range messages {
msgs = append(msgs, NewJSMessage(m))
}
2014-12-02 04:52:56 -06:00
2015-01-28 11:14:28 -06:00
return ethutil.NewList(msgs)
}
2015-01-28 11:14:28 -06:00
func (self *JSXEth) PushTx(encodedTx string) (string, error) {
tx := types.NewTransactionFromBytes(fromHex(encodedTx))
err := self.eth.TxPool().Add(tx)
2014-08-17 05:41:52 -05:00
if err != nil {
2015-01-28 11:14:28 -06:00
return "", err
2014-08-17 05:41:52 -05:00
}
2015-01-28 11:14:28 -06:00
if tx.To() == nil {
addr := core.AddressFromMessage(tx)
return toHex(addr), nil
2014-08-18 03:17:45 -05:00
}
2015-01-28 11:14:28 -06:00
return toHex(tx.Hash()), nil
2014-08-20 02:59:09 -05:00
}