Merge pull request #498 from maran/feature/drytoHex

DRY up the use of toHex in the project and move it to common
This commit is contained in:
Jeffrey Wilcke 2015-03-17 21:31:42 +01:00
commit 3ea99f989f
8 changed files with 106 additions and 111 deletions

View File

@ -65,6 +65,15 @@ func DefaultDataDir() string {
} }
} }
func ToHex(b []byte) string {
hex := Bytes2Hex(b)
// Prefer output of "0x0" instead of "0x"
if len(hex) == 0 {
hex = "0"
}
return "0x" + hex
}
func FromHex(s string) []byte { func FromHex(s string) []byte {
if len(s) > 1 { if len(s) > 1 {
if s[0:2] == "0x" { if s[0:2] == "0x" {

View File

@ -251,7 +251,7 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error)
result, _ := account.Transact(common.FromHex(args.To), common.FromHex(args.Value), common.FromHex(args.Gas), common.FromHex(args.GasPrice), common.FromHex(args.Data)) result, _ := account.Transact(common.FromHex(args.To), common.FromHex(args.Value), common.FromHex(args.Gas), common.FromHex(args.GasPrice), common.FromHex(args.Data))
if len(result) > 0 { if len(result) > 0 {
*reply = toHex(result) *reply = common.ToHex(result)
} }
} else if _, exists := p.register[args.From]; exists { } else if _, exists := p.register[args.From]; exists {
p.register[ags.From] = append(p.register[args.From], args) p.register[ags.From] = append(p.register[args.From], args)
@ -291,7 +291,7 @@ func (p *EthereumApi) GetBalance(args *GetBalanceArgs, reply *interface{}) error
return err return err
} }
state := p.getStateWithNum(args.BlockNumber).SafeGet(args.Address) state := p.getStateWithNum(args.BlockNumber).SafeGet(args.Address)
*reply = toHex(state.Balance().Bytes()) *reply = common.ToHex(state.Balance().Bytes())
return nil return nil
} }
@ -389,7 +389,7 @@ func (p *EthereumApi) NewWhisperFilter(args *WhisperFilterArgs, reply *interface
} }
id = p.xeth().Whisper().Watch(opts) id = p.xeth().Whisper().Watch(opts)
p.messages[id] = &whisperFilter{timeout: time.Now()} p.messages[id] = &whisperFilter{timeout: time.Now()}
*reply = toHex(big.NewInt(int64(id)).Bytes()) *reply = common.ToHex(big.NewInt(int64(id)).Bytes())
return nil return nil
} }
@ -485,7 +485,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
*reply = toHex(crypto.Sha3(common.FromHex(args.Data))) *reply = common.ToHex(crypto.Sha3(common.FromHex(args.Data)))
case "web3_clientVersion": case "web3_clientVersion":
*reply = p.xeth().Backend().Version() *reply = p.xeth().Backend().Version()
case "net_version": case "net_version":
@ -493,7 +493,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
case "net_listening": case "net_listening":
*reply = p.xeth().IsListening() *reply = p.xeth().IsListening()
case "net_peerCount": case "net_peerCount":
*reply = toHex(big.NewInt(int64(p.xeth().PeerCount())).Bytes()) *reply = common.ToHex(big.NewInt(int64(p.xeth().PeerCount())).Bytes())
case "eth_coinbase": case "eth_coinbase":
// TODO handling of empty coinbase due to lack of accounts // TODO handling of empty coinbase due to lack of accounts
res := p.xeth().Coinbase() res := p.xeth().Coinbase()
@ -505,11 +505,11 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
case "eth_mining": case "eth_mining":
*reply = p.xeth().IsMining() *reply = p.xeth().IsMining()
case "eth_gasPrice": case "eth_gasPrice":
*reply = toHex(defaultGasPrice.Bytes()) *reply = common.ToHex(defaultGasPrice.Bytes())
case "eth_accounts": case "eth_accounts":
*reply = p.xeth().Accounts() *reply = p.xeth().Accounts()
case "eth_blockNumber": case "eth_blockNumber":
*reply = toHex(p.xeth().Backend().ChainManager().CurrentBlock().Number().Bytes()) *reply = common.ToHex(p.xeth().Backend().ChainManager().CurrentBlock().Number().Bytes())
case "eth_getBalance": case "eth_getBalance":
args := new(GetBalanceArgs) args := new(GetBalanceArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
@ -544,7 +544,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
if err != nil { if err != nil {
return err return err
} }
*reply = toHex(big.NewInt(v).Bytes()) *reply = common.ToHex(big.NewInt(v).Bytes())
case "eth_getBlockTransactionCountByNumber": case "eth_getBlockTransactionCountByNumber":
args := new(GetBlockByNumberArgs) args := new(GetBlockByNumberArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
@ -555,7 +555,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
if err != nil { if err != nil {
return err return err
} }
*reply = toHex(big.NewInt(v).Bytes()) *reply = common.ToHex(big.NewInt(v).Bytes())
case "eth_getUncleCountByBlockHash": case "eth_getUncleCountByBlockHash":
args := new(GetBlockByHashArgs) args := new(GetBlockByHashArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
@ -566,7 +566,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
if err != nil { if err != nil {
return err return err
} }
*reply = toHex(big.NewInt(v).Bytes()) *reply = common.ToHex(big.NewInt(v).Bytes())
case "eth_getUncleCountByBlockNumber": case "eth_getUncleCountByBlockNumber":
args := new(GetBlockByNumberArgs) args := new(GetBlockByNumberArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
@ -577,7 +577,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
if err != nil { if err != nil {
return err return err
} }
*reply = toHex(big.NewInt(v).Bytes()) *reply = common.ToHex(big.NewInt(v).Bytes())
case "eth_getData", "eth_getCode": case "eth_getData", "eth_getCode":
args := new(GetDataArgs) args := new(GetDataArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
@ -668,7 +668,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return NewValidationError("Index", "does not exist") return NewValidationError("Index", "does not exist")
} }
uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false) uncle, err := p.GetBlockByHash(common.ToHex(v.Uncles[args.Index]), false)
if err != nil { if err != nil {
return err return err
} }
@ -687,7 +687,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
return NewValidationError("Index", "does not exist") return NewValidationError("Index", "does not exist")
} }
uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false) uncle, err := p.GetBlockByHash(common.ToHex(v.Uncles[args.Index]), false)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,6 +5,7 @@ import (
// "fmt" // "fmt"
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
) )
@ -56,23 +57,23 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
} }
// convert strict types to hexified strings // convert strict types to hexified strings
ext.BlockNumber = toHex(big.NewInt(b.BlockNumber).Bytes()) ext.BlockNumber = common.ToHex(big.NewInt(b.BlockNumber).Bytes())
ext.BlockHash = toHex(b.BlockHash) ext.BlockHash = common.ToHex(b.BlockHash)
ext.ParentHash = toHex(b.ParentHash) ext.ParentHash = common.ToHex(b.ParentHash)
ext.Nonce = toHex(b.Nonce) ext.Nonce = common.ToHex(b.Nonce)
ext.Sha3Uncles = toHex(b.Sha3Uncles) ext.Sha3Uncles = common.ToHex(b.Sha3Uncles)
ext.LogsBloom = toHex(b.LogsBloom) ext.LogsBloom = common.ToHex(b.LogsBloom)
ext.TransactionRoot = toHex(b.TransactionRoot) ext.TransactionRoot = common.ToHex(b.TransactionRoot)
ext.StateRoot = toHex(b.StateRoot) ext.StateRoot = common.ToHex(b.StateRoot)
ext.Miner = toHex(b.Miner) ext.Miner = common.ToHex(b.Miner)
ext.Difficulty = toHex(big.NewInt(b.Difficulty).Bytes()) ext.Difficulty = common.ToHex(big.NewInt(b.Difficulty).Bytes())
ext.TotalDifficulty = toHex(big.NewInt(b.TotalDifficulty).Bytes()) ext.TotalDifficulty = common.ToHex(big.NewInt(b.TotalDifficulty).Bytes())
ext.Size = toHex(big.NewInt(b.Size).Bytes()) ext.Size = common.ToHex(big.NewInt(b.Size).Bytes())
// ext.ExtraData = toHex(b.ExtraData) // ext.ExtraData = common.ToHex(b.ExtraData)
ext.GasLimit = toHex(big.NewInt(b.GasLimit).Bytes()) ext.GasLimit = common.ToHex(big.NewInt(b.GasLimit).Bytes())
// ext.MinGasPrice = toHex(big.NewInt(b.MinGasPrice).Bytes()) // ext.MinGasPrice = common.ToHex(big.NewInt(b.MinGasPrice).Bytes())
ext.GasUsed = toHex(big.NewInt(b.GasUsed).Bytes()) ext.GasUsed = common.ToHex(big.NewInt(b.GasUsed).Bytes())
ext.UnixTimestamp = toHex(big.NewInt(b.UnixTimestamp).Bytes()) ext.UnixTimestamp = common.ToHex(big.NewInt(b.UnixTimestamp).Bytes())
ext.Transactions = make([]interface{}, len(b.Transactions)) ext.Transactions = make([]interface{}, len(b.Transactions))
if b.fullTx { if b.fullTx {
for i, tx := range b.Transactions { for i, tx := range b.Transactions {
@ -80,12 +81,12 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
} }
} else { } else {
for i, tx := range b.Transactions { for i, tx := range b.Transactions {
ext.Transactions[i] = toHex(tx.Hash) ext.Transactions[i] = common.ToHex(tx.Hash)
} }
} }
ext.Uncles = make([]string, len(b.Uncles)) ext.Uncles = make([]string, len(b.Uncles))
for i, v := range b.Uncles { for i, v := range b.Uncles {
ext.Uncles[i] = toHex(v) ext.Uncles[i] = common.ToHex(v)
} }
return json.Marshal(ext) return json.Marshal(ext)
@ -160,17 +161,17 @@ func (t *TransactionRes) MarshalJSON() ([]byte, error) {
Input string `json:"input"` Input string `json:"input"`
} }
ext.Hash = toHex(t.Hash) ext.Hash = common.ToHex(t.Hash)
ext.Nonce = toHex(big.NewInt(t.Nonce).Bytes()) ext.Nonce = common.ToHex(big.NewInt(t.Nonce).Bytes())
ext.BlockHash = toHex(t.BlockHash) ext.BlockHash = common.ToHex(t.BlockHash)
ext.BlockNumber = toHex(big.NewInt(t.BlockNumber).Bytes()) ext.BlockNumber = common.ToHex(big.NewInt(t.BlockNumber).Bytes())
ext.TxIndex = toHex(big.NewInt(t.TxIndex).Bytes()) ext.TxIndex = common.ToHex(big.NewInt(t.TxIndex).Bytes())
ext.From = toHex(t.From) ext.From = common.ToHex(t.From)
ext.To = toHex(t.To) ext.To = common.ToHex(t.To)
ext.Value = toHex(big.NewInt(t.Value).Bytes()) ext.Value = common.ToHex(big.NewInt(t.Value).Bytes())
ext.Gas = toHex(big.NewInt(t.Gas).Bytes()) ext.Gas = common.ToHex(big.NewInt(t.Gas).Bytes())
ext.GasPrice = toHex(big.NewInt(t.GasPrice).Bytes()) ext.GasPrice = common.ToHex(big.NewInt(t.GasPrice).Bytes())
ext.Input = toHex(t.Input) ext.Input = common.ToHex(t.Input)
return json.Marshal(ext) return json.Marshal(ext)
} }

View File

@ -124,17 +124,8 @@ func (self JsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error)
return reqParsed, nil return reqParsed, nil
} }
func toHex(b []byte) string {
hex := common.Bytes2Hex(b)
// Prefer output of "0x0" instead of "0x"
if len(hex) == 0 {
hex = "0"
}
return "0x" + hex
}
func i2hex(n int) string { func i2hex(n int) string {
return toHex(big.NewInt(int64(n)).Bytes()) return common.ToHex(big.NewInt(int64(n)).Bytes())
} }
type RpcServer interface { type RpcServer interface {
@ -155,11 +146,11 @@ func toLogs(logs state.Logs) (ls []Log) {
for i, log := range logs { for i, log := range logs {
var l Log var l Log
l.Topic = make([]string, len(log.Topics())) l.Topic = make([]string, len(log.Topics()))
l.Address = toHex(log.Address()) l.Address = common.ToHex(log.Address())
l.Data = toHex(log.Data()) l.Data = common.ToHex(log.Data())
l.Number = log.Number() l.Number = log.Number()
for j, topic := range log.Topics() { for j, topic := range log.Topics() {
l.Topic[j] = toHex(topic) l.Topic[j] = common.ToHex(topic)
} }
ls[i] = l ls[i] = l
} }

View File

@ -4,8 +4,8 @@ package qwhisper
import ( import (
"time" "time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/whisper" "github.com/ethereum/go-ethereum/whisper"
"github.com/obscuren/qml" "github.com/obscuren/qml"
@ -13,8 +13,6 @@ import (
var qlogger = logger.NewLogger("QSHH") var qlogger = logger.NewLogger("QSHH")
func toHex(b []byte) string { return "0x" + common.Bytes2Hex(b) }
type Whisper struct { type Whisper struct {
*whisper.Whisper *whisper.Whisper
view qml.Object view qml.Object
@ -66,7 +64,7 @@ func (self *Whisper) Post(payload []string, to, from string, topics []string, pr
func (self *Whisper) NewIdentity() string { func (self *Whisper) NewIdentity() string {
key := self.Whisper.NewIdentity() key := self.Whisper.NewIdentity()
return toHex(crypto.FromECDSAPub(&key.PublicKey)) return common.ToHex(crypto.FromECDSAPub(&key.PublicKey))
} }
func (self *Whisper) HasIdentity(key string) bool { func (self *Whisper) HasIdentity(key string) bool {

View File

@ -5,19 +5,15 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
) )
func toHex(b []byte) string {
return "0x" + common.Bytes2Hex(b)
}
type Object struct { type Object struct {
*state.StateObject *state.StateObject
} }
@ -49,7 +45,7 @@ func (self *Object) Storage() (storage map[string]string) {
for it.Next() { for it.Next() {
var data []byte var data []byte
rlp.Decode(bytes.NewReader(it.Value), &data) rlp.Decode(bytes.NewReader(it.Value), &data)
storage[toHex(it.Key)] = toHex(data) storage[common.ToHex(it.Key)] = common.ToHex(data)
} }
return return
@ -95,12 +91,12 @@ func NewBlock(block *types.Block) *Block {
return &Block{ return &Block{
ref: block, Size: block.Size().String(), ref: block, Size: block.Size().String(),
Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(), Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(),
GasLimit: block.GasLimit().String(), Hash: toHex(block.Hash()), GasLimit: block.GasLimit().String(), Hash: common.ToHex(block.Hash()),
Transactions: txlist, Uncles: ulist, Transactions: txlist, Uncles: ulist,
Time: block.Time(), Time: block.Time(),
Coinbase: toHex(block.Coinbase()), Coinbase: common.ToHex(block.Coinbase()),
PrevHash: toHex(block.ParentHash()), PrevHash: common.ToHex(block.ParentHash()),
Bloom: toHex(block.Bloom()), Bloom: common.ToHex(block.Bloom()),
Raw: block.String(), Raw: block.String(),
} }
} }
@ -139,22 +135,22 @@ type Transaction struct {
} }
func NewTx(tx *types.Transaction) *Transaction { func NewTx(tx *types.Transaction) *Transaction {
hash := toHex(tx.Hash()) hash := common.ToHex(tx.Hash())
receiver := toHex(tx.To()) receiver := common.ToHex(tx.To())
if len(receiver) == 0 { if len(receiver) == 0 {
receiver = toHex(core.AddressFromMessage(tx)) receiver = common.ToHex(core.AddressFromMessage(tx))
} }
sender := toHex(tx.From()) sender := common.ToHex(tx.From())
createsContract := core.MessageCreatesContract(tx) createsContract := core.MessageCreatesContract(tx)
var data string var data string
if createsContract { if createsContract {
data = strings.Join(core.Disassemble(tx.Data()), "\n") data = strings.Join(core.Disassemble(tx.Data()), "\n")
} else { } else {
data = toHex(tx.Data()) data = common.ToHex(tx.Data())
} }
return &Transaction{ref: tx, Hash: hash, Value: common.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: toHex(tx.Data())} return &Transaction{ref: tx, Hash: hash, Value: common.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: common.ToHex(tx.Data())}
} }
func (self *Transaction) ToString() string { func (self *Transaction) ToString() string {
@ -168,7 +164,7 @@ type Key struct {
} }
func NewKey(key *crypto.KeyPair) *Key { func NewKey(key *crypto.KeyPair) *Key {
return &Key{toHex(key.Address()), toHex(key.PrivateKey), toHex(key.PublicKey)} return &Key{common.ToHex(key.Address()), common.ToHex(key.PrivateKey), common.ToHex(key.PublicKey)}
} }
type PReceipt struct { type PReceipt struct {
@ -181,9 +177,9 @@ type PReceipt struct {
func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt { func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt {
return &PReceipt{ return &PReceipt{
contractCreation, contractCreation,
toHex(creationAddress), common.ToHex(creationAddress),
toHex(hash), common.ToHex(hash),
toHex(address), common.ToHex(address),
} }
} }
@ -220,8 +216,8 @@ type Receipt struct {
func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt { func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt {
return &Receipt{ return &Receipt{
contractCreation, contractCreation,
toHex(creationAddress), common.ToHex(creationAddress),
toHex(hash), common.ToHex(hash),
toHex(address), common.ToHex(address),
} }
} }

View File

@ -56,7 +56,7 @@ func (self *Whisper) Post(payload string, to, from string, topics []string, prio
func (self *Whisper) NewIdentity() string { func (self *Whisper) NewIdentity() string {
key := self.Whisper.NewIdentity() key := self.Whisper.NewIdentity()
return toHex(crypto.FromECDSAPub(&key.PublicKey)) return common.ToHex(crypto.FromECDSAPub(&key.PublicKey))
} }
func (self *Whisper) HasIdentity(key string) bool { func (self *Whisper) HasIdentity(key string) bool {
@ -112,9 +112,9 @@ type WhisperMessage struct {
func NewWhisperMessage(msg *whisper.Message) WhisperMessage { func NewWhisperMessage(msg *whisper.Message) WhisperMessage {
return WhisperMessage{ return WhisperMessage{
ref: msg, ref: msg,
Payload: toHex(msg.Payload), Payload: common.ToHex(msg.Payload),
From: toHex(crypto.FromECDSAPub(msg.Recover())), From: common.ToHex(crypto.FromECDSAPub(msg.Recover())),
To: toHex(crypto.FromECDSAPub(msg.To)), To: common.ToHex(crypto.FromECDSAPub(msg.To)),
Sent: msg.Sent, Sent: msg.Sent,
} }
} }

View File

@ -8,10 +8,10 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
@ -170,7 +170,7 @@ func (self *XEth) Accounts() []string {
accounts, _ := self.eth.AccountManager().Accounts() accounts, _ := self.eth.AccountManager().Accounts()
accountAddresses := make([]string, len(accounts)) accountAddresses := make([]string, len(accounts))
for i, ac := range accounts { for i, ac := range accounts {
accountAddresses[i] = toHex(ac.Address) accountAddresses[i] = common.ToHex(ac.Address)
} }
return accountAddresses return accountAddresses
} }
@ -201,7 +201,7 @@ func (self *XEth) IsListening() bool {
func (self *XEth) Coinbase() string { func (self *XEth) Coinbase() string {
cb, _ := self.eth.AccountManager().Coinbase() cb, _ := self.eth.AccountManager().Coinbase()
return toHex(cb) return common.ToHex(cb)
} }
func (self *XEth) NumberToHuman(balance string) string { func (self *XEth) NumberToHuman(balance string) string {
@ -213,7 +213,7 @@ func (self *XEth) NumberToHuman(balance string) string {
func (self *XEth) StorageAt(addr, storageAddr string) string { func (self *XEth) StorageAt(addr, storageAddr string) string {
storage := self.State().SafeGet(addr).StorageString(storageAddr) storage := self.State().SafeGet(addr).StorageString(storageAddr)
return toHex(storage.Bytes()) return common.ToHex(storage.Bytes())
} }
func (self *XEth) BalanceAt(addr string) string { func (self *XEth) BalanceAt(addr string) string {
@ -225,7 +225,7 @@ func (self *XEth) TxCountAt(address string) int {
} }
func (self *XEth) CodeAt(address string) string { func (self *XEth) CodeAt(address string) string {
return toHex(self.State().SafeGet(address).Code()) return common.ToHex(self.State().SafeGet(address).Code())
} }
func (self *XEth) IsContract(address string) bool { func (self *XEth) IsContract(address string) bool {
@ -238,7 +238,7 @@ func (self *XEth) SecretToAddress(key string) string {
return "" return ""
} }
return toHex(pair.Address()) return common.ToHex(pair.Address())
} }
type KeyVal struct { type KeyVal struct {
@ -251,7 +251,7 @@ func (self *XEth) EachStorage(addr string) string {
object := self.State().SafeGet(addr) object := self.State().SafeGet(addr)
it := object.Trie().Iterator() it := object.Trie().Iterator()
for it.Next() { for it.Next() {
values = append(values, KeyVal{toHex(it.Key), toHex(it.Value)}) values = append(values, KeyVal{common.ToHex(it.Key), common.ToHex(it.Value)})
} }
valuesJson, err := json.Marshal(values) valuesJson, err := json.Marshal(values)
@ -265,7 +265,7 @@ func (self *XEth) EachStorage(addr string) string {
func (self *XEth) ToAscii(str string) string { func (self *XEth) ToAscii(str string) string {
padded := common.RightPadBytes([]byte(str), 32) padded := common.RightPadBytes([]byte(str), 32)
return "0x" + toHex(padded) return "0x" + common.ToHex(padded)
} }
func (self *XEth) FromAscii(str string) string { func (self *XEth) FromAscii(str string) string {
@ -293,9 +293,9 @@ func (self *XEth) PushTx(encodedTx string) (string, error) {
if tx.To() == nil { if tx.To() == nil {
addr := core.AddressFromMessage(tx) addr := core.AddressFromMessage(tx)
return toHex(addr), nil return common.ToHex(addr), nil
} }
return toHex(tx.Hash()), nil return common.ToHex(tx.Hash()), nil
} }
var ( var (
@ -325,7 +325,7 @@ func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr st
vmenv := core.NewEnv(statedb, self.chainManager, msg, block) vmenv := core.NewEnv(statedb, self.chainManager, msg, block)
res, err := vmenv.Call(msg.from, msg.to, msg.data, msg.gas, msg.gasPrice, msg.value) res, err := vmenv.Call(msg.from, msg.to, msg.data, msg.gas, msg.gasPrice, msg.value)
return toHex(res), err return common.ToHex(res), err
} }
func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
@ -371,9 +371,9 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt
} }
if types.IsContractAddr(to) { if types.IsContractAddr(to) {
return toHex(core.AddressFromMessage(tx)), nil return common.ToHex(core.AddressFromMessage(tx)), nil
} }
return toHex(tx.Hash()), nil return common.ToHex(tx.Hash()), nil
} }
func (self *XEth) sign(tx *types.Transaction, from []byte, didUnlock bool) error { func (self *XEth) sign(tx *types.Transaction, from []byte, didUnlock bool) error {