DRY up the use of fromHex and put it in ethutil

This commit is contained in:
Maran
2015-03-14 11:39:35 +01:00
parent b927c29469
commit 991993357c
10 changed files with 75 additions and 90 deletions

View File

@ -1,6 +1,9 @@
package xeth
import "github.com/ethereum/go-ethereum/state"
import (
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/state"
)
type State struct {
xeth *XEth
@ -16,7 +19,7 @@ func (self *State) State() *state.StateDB {
}
func (self *State) Get(addr string) *Object {
return &Object{self.state.GetStateObject(fromHex(addr))}
return &Object{self.state.GetStateObject(ethutil.FromHex(addr))}
}
func (self *State) SafeGet(addr string) *Object {
@ -24,9 +27,9 @@ func (self *State) SafeGet(addr string) *Object {
}
func (self *State) safeGet(addr string) *state.StateObject {
object := self.state.GetStateObject(fromHex(addr))
object := self.state.GetStateObject(ethutil.FromHex(addr))
if object == nil {
object = state.NewStateObject(fromHex(addr), self.xeth.eth.StateDb())
object = state.NewStateObject(ethutil.FromHex(addr), self.xeth.eth.StateDb())
}
return object

View File

@ -17,15 +17,6 @@ import (
func toHex(b []byte) string {
return "0x" + ethutil.Bytes2Hex(b)
}
func fromHex(s string) []byte {
if len(s) > 1 {
if s[0:2] == "0x" {
s = s[2:]
}
return ethutil.Hex2Bytes(s)
}
return nil
}
type Object struct {
*state.StateObject
@ -123,7 +114,7 @@ func (self *Block) ToString() string {
}
func (self *Block) GetTransaction(hash string) *Transaction {
tx := self.ref.Transaction(fromHex(hash))
tx := self.ref.Transaction(ethutil.FromHex(hash))
if tx == nil {
return nil
}

View File

@ -5,6 +5,7 @@ import (
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/whisper"
)
@ -28,12 +29,12 @@ func (self *Whisper) Post(payload string, to, from string, topics []string, prio
ttl = 100
}
pk := crypto.ToECDSAPub(fromHex(from))
pk := crypto.ToECDSAPub(ethutil.FromHex(from))
if key := self.Whisper.GetIdentity(pk); key != nil || len(from) == 0 {
msg := whisper.NewMessage(fromHex(payload))
msg := whisper.NewMessage(ethutil.FromHex(payload))
envelope, err := msg.Seal(time.Duration(priority*100000), whisper.Opts{
Ttl: time.Duration(ttl) * time.Second,
To: crypto.ToECDSAPub(fromHex(to)),
To: crypto.ToECDSAPub(ethutil.FromHex(to)),
From: key,
Topics: whisper.TopicsFromString(topics...),
})
@ -59,13 +60,13 @@ func (self *Whisper) NewIdentity() string {
}
func (self *Whisper) HasIdentity(key string) bool {
return self.Whisper.HasIdentity(crypto.ToECDSAPub(fromHex(key)))
return self.Whisper.HasIdentity(crypto.ToECDSAPub(ethutil.FromHex(key)))
}
func (self *Whisper) Watch(opts *Options) int {
filter := whisper.Filter{
To: crypto.ToECDSAPub(fromHex(opts.To)),
From: crypto.ToECDSAPub(fromHex(opts.From)),
To: crypto.ToECDSAPub(ethutil.FromHex(opts.To)),
From: crypto.ToECDSAPub(ethutil.FromHex(opts.From)),
Topics: whisper.TopicsFromString(opts.Topics...),
}

View File

@ -116,21 +116,21 @@ func (self *XEth) State() *State { return self.state }
func (self *XEth) Whisper() *Whisper { return self.whisper }
func (self *XEth) BlockByHash(strHash string) *Block {
hash := fromHex(strHash)
hash := ethutil.FromHex(strHash)
block := self.chainManager.GetBlock(hash)
return NewBlock(block)
}
func (self *XEth) EthBlockByHash(strHash string) *types.Block {
hash := fromHex(strHash)
hash := ethutil.FromHex(strHash)
block := self.chainManager.GetBlock(hash)
return block
}
func (self *XEth) EthTransactionByHash(hash string) *types.Transaction {
data, _ := self.eth.ExtraDb().Get(fromHex(hash))
data, _ := self.eth.ExtraDb().Get(ethutil.FromHex(hash))
if len(data) != 0 {
return types.NewTransactionFromBytes(data)
}
@ -233,7 +233,7 @@ func (self *XEth) IsContract(address string) bool {
}
func (self *XEth) SecretToAddress(key string) string {
pair, err := crypto.NewKeyPairFromSec(fromHex(key))
pair, err := crypto.NewKeyPairFromSec(ethutil.FromHex(key))
if err != nil {
return ""
}
@ -273,7 +273,7 @@ func (self *XEth) FromAscii(str string) string {
str = str[2:]
}
return string(bytes.Trim(fromHex(str), "\x00"))
return string(bytes.Trim(ethutil.FromHex(str), "\x00"))
}
func (self *XEth) FromNumber(str string) string {
@ -281,11 +281,11 @@ func (self *XEth) FromNumber(str string) string {
str = str[2:]
}
return ethutil.BigD(fromHex(str)).String()
return ethutil.BigD(ethutil.FromHex(str)).String()
}
func (self *XEth) PushTx(encodedTx string) (string, error) {
tx := types.NewTransactionFromBytes(fromHex(encodedTx))
tx := types.NewTransactionFromBytes(ethutil.FromHex(encodedTx))
err := self.eth.TxPool().Add(tx)
if err != nil {
return "", err
@ -306,12 +306,12 @@ var (
func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) {
statedb := self.State().State() //self.chainManager.TransState()
msg := callmsg{
from: statedb.GetOrNewStateObject(fromHex(fromStr)),
to: fromHex(toStr),
from: statedb.GetOrNewStateObject(ethutil.FromHex(fromStr)),
to: ethutil.FromHex(toStr),
gas: ethutil.Big(gasStr),
gasPrice: ethutil.Big(gasPriceStr),
value: ethutil.Big(valueStr),
data: fromHex(dataStr),
data: ethutil.FromHex(dataStr),
}
if msg.gas.Cmp(big.NewInt(0)) == 0 {
msg.gas = defaultGas
@ -339,9 +339,9 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt
contractCreation bool
)
from = fromHex(fromStr)
data = fromHex(codeStr)
to = fromHex(toStr)
from = ethutil.FromHex(fromStr)
data = ethutil.FromHex(codeStr)
to = ethutil.FromHex(toStr)
if len(to) == 0 {
contractCreation = true
}