From 7330c97b5b00255db9dc1ffaff942992f80fb7d1 Mon Sep 17 00:00:00 2001 From: Maran Date: Mon, 16 Mar 2015 15:17:19 +0100 Subject: DRY up the use of toHex in the project and move it to common --- xeth/types.go | 64 +++++++++++++++++++++++++++------------------------------ xeth/whisper.go | 10 ++++----- xeth/xeth.go | 26 +++++++++++------------ 3 files changed, 48 insertions(+), 52 deletions(-) (limited to 'xeth') diff --git a/xeth/types.go b/xeth/types.go index e15305481..6b3b6d29f 100644 --- a/xeth/types.go +++ b/xeth/types.go @@ -5,19 +5,15 @@ import ( "fmt" "strings" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/state" ) -func toHex(b []byte) string { - return "0x" + common.Bytes2Hex(b) -} - type Object struct { *state.StateObject } @@ -49,7 +45,7 @@ func (self *Object) Storage() (storage map[string]string) { for it.Next() { var data []byte rlp.Decode(bytes.NewReader(it.Value), &data) - storage[toHex(it.Key)] = toHex(data) + storage[common.ToHex(it.Key)] = common.ToHex(data) } return @@ -59,19 +55,19 @@ func (self *Object) Storage() (storage map[string]string) { type Block struct { //Transactions string `json:"transactions"` ref *types.Block - Size string `json:"size"` - Number int `json:"number"` - Hash string `json:"hash"` + Size string `json:"size"` + Number int `json:"number"` + Hash string `json:"hash"` Transactions *common.List `json:"transactions"` Uncles *common.List `json:"uncles"` - Time int64 `json:"time"` - Coinbase string `json:"coinbase"` - Name string `json:"name"` - GasLimit string `json:"gasLimit"` - GasUsed string `json:"gasUsed"` - PrevHash string `json:"prevHash"` - Bloom string `json:"bloom"` - Raw string `json:"raw"` + Time int64 `json:"time"` + Coinbase string `json:"coinbase"` + Name string `json:"name"` + GasLimit string `json:"gasLimit"` + GasUsed string `json:"gasUsed"` + PrevHash string `json:"prevHash"` + Bloom string `json:"bloom"` + Raw string `json:"raw"` } // Creates a new QML Block from a chain block @@ -95,12 +91,12 @@ func NewBlock(block *types.Block) *Block { return &Block{ ref: block, Size: block.Size().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, Time: block.Time(), - Coinbase: toHex(block.Coinbase()), - PrevHash: toHex(block.ParentHash()), - Bloom: toHex(block.Bloom()), + Coinbase: common.ToHex(block.Coinbase()), + PrevHash: common.ToHex(block.ParentHash()), + Bloom: common.ToHex(block.Bloom()), Raw: block.String(), } } @@ -139,22 +135,22 @@ type Transaction struct { } func NewTx(tx *types.Transaction) *Transaction { - hash := toHex(tx.Hash()) - receiver := toHex(tx.To()) + hash := common.ToHex(tx.Hash()) + receiver := common.ToHex(tx.To()) 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) var data string if createsContract { data = strings.Join(core.Disassemble(tx.Data()), "\n") } 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 { @@ -168,7 +164,7 @@ type Key struct { } 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 { @@ -181,9 +177,9 @@ type PReceipt struct { func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt { return &PReceipt{ contractCreation, - toHex(creationAddress), - toHex(hash), - toHex(address), + common.ToHex(creationAddress), + common.ToHex(hash), + common.ToHex(address), } } @@ -220,8 +216,8 @@ type Receipt struct { func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt { return &Receipt{ contractCreation, - toHex(creationAddress), - toHex(hash), - toHex(address), + common.ToHex(creationAddress), + common.ToHex(hash), + common.ToHex(address), } } diff --git a/xeth/whisper.go b/xeth/whisper.go index 76bf8012a..c43eb4388 100644 --- a/xeth/whisper.go +++ b/xeth/whisper.go @@ -4,8 +4,8 @@ import ( "errors" "time" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/whisper" ) @@ -56,7 +56,7 @@ func (self *Whisper) Post(payload string, to, from string, topics []string, prio func (self *Whisper) NewIdentity() string { key := self.Whisper.NewIdentity() - return toHex(crypto.FromECDSAPub(&key.PublicKey)) + return common.ToHex(crypto.FromECDSAPub(&key.PublicKey)) } func (self *Whisper) HasIdentity(key string) bool { @@ -108,9 +108,9 @@ type WhisperMessage struct { func NewWhisperMessage(msg *whisper.Message) WhisperMessage { return WhisperMessage{ ref: msg, - Payload: toHex(msg.Payload), - From: toHex(crypto.FromECDSAPub(msg.Recover())), - To: toHex(crypto.FromECDSAPub(msg.To)), + Payload: common.ToHex(msg.Payload), + From: common.ToHex(crypto.FromECDSAPub(msg.Recover())), + To: common.ToHex(crypto.FromECDSAPub(msg.To)), Sent: msg.Sent, } } diff --git a/xeth/xeth.go b/xeth/xeth.go index 6c7a26c04..115bd787a 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -8,10 +8,10 @@ import ( "math/big" "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" @@ -170,7 +170,7 @@ func (self *XEth) Accounts() []string { accounts, _ := self.eth.AccountManager().Accounts() accountAddresses := make([]string, len(accounts)) for i, ac := range accounts { - accountAddresses[i] = toHex(ac.Address) + accountAddresses[i] = common.ToHex(ac.Address) } return accountAddresses } @@ -201,7 +201,7 @@ func (self *XEth) IsListening() bool { func (self *XEth) Coinbase() string { cb, _ := self.eth.AccountManager().Coinbase() - return toHex(cb) + return common.ToHex(cb) } 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 { storage := self.State().SafeGet(addr).StorageString(storageAddr) - return toHex(storage.Bytes()) + return common.ToHex(storage.Bytes()) } 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 { - return toHex(self.State().SafeGet(address).Code()) + return common.ToHex(self.State().SafeGet(address).Code()) } func (self *XEth) IsContract(address string) bool { @@ -238,7 +238,7 @@ func (self *XEth) SecretToAddress(key string) string { return "" } - return toHex(pair.Address()) + return common.ToHex(pair.Address()) } type KeyVal struct { @@ -251,7 +251,7 @@ func (self *XEth) EachStorage(addr string) string { object := self.State().SafeGet(addr) it := object.Trie().Iterator() 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) @@ -265,7 +265,7 @@ func (self *XEth) EachStorage(addr string) string { func (self *XEth) ToAscii(str string) string { padded := common.RightPadBytes([]byte(str), 32) - return "0x" + toHex(padded) + return "0x" + common.ToHex(padded) } func (self *XEth) FromAscii(str string) string { @@ -293,9 +293,9 @@ func (self *XEth) PushTx(encodedTx string) (string, error) { if tx.To() == nil { 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 ( @@ -325,7 +325,7 @@ func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr st vmenv := core.NewEnv(statedb, self.chainManager, msg, block) 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) { @@ -371,9 +371,9 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt } 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 { -- cgit v1.2.3