This commit is contained in:
Péter Szilágyi 2016-03-30 07:44:33 +00:00
commit 2d51e2b554
21 changed files with 177 additions and 110 deletions

View file

@ -396,7 +396,7 @@ multiply7 = Multiply7.at(contractaddress);
if sol != nil && solcVersion != sol.Version() { if sol != nil && solcVersion != sol.Version() {
modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"`+sol.Version()+`"`)) modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"`+sol.Version()+`"`))
fmt.Printf("modified contractinfo:\n%s\n", modContractInfo) fmt.Printf("modified contractinfo:\n%s\n", modContractInfo)
contentHash = `"` + common.ToHex(crypto.Keccak256([]byte(modContractInfo))) + `"` contentHash = `"` + common.BytesToHex(crypto.Keccak256([]byte(modContractInfo))) + `"`
} }
if checkEvalJSON(t, repl, `filename = "/tmp/info.json"`, `"/tmp/info.json"`) != nil { if checkEvalJSON(t, repl, `filename = "/tmp/info.json"`, `"/tmp/info.json"`) != nil {
return return

View file

@ -23,30 +23,39 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"math/big" "math/big"
"regexp"
"strings" "strings"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
) )
func ToHex(b []byte) string { // BytesToHex converts a byte slice to hexadecimal notation, prefixed with 0x.
hex := Bytes2Hex(b) func BytesToHex(blob []byte) string {
// Prefer output of "0x0" instead of "0x" return "0x" + hex.EncodeToString(blob)
if len(hex) == 0 {
hex = "0"
}
return "0x" + hex
} }
// NumberToHex converts a byte slice to hexadecimal notation, prefixed with 0x,
// with the added rule that the empty slice is encoded as the zero value.
func NumberToHex(blob []byte) string {
if len(blob) == 0 {
return "0x0"
}
return "0x" + hex.EncodeToString(blob)
}
// FromHex parses a hex string into a byte slice.
func FromHex(s string) []byte { func FromHex(s string) []byte {
if len(s) > 1 { // Cut off and optional 0x or 0X prefix
if s[0:2] == "0x" { if len(s) > 2 && (s[:2] == "0x" || s[:2] == "0X") {
s = s[2:] s = s[2:]
} }
// Pad odd length strings to even ones
if len(s)%2 == 1 { if len(s)%2 == 1 {
s = "0" + s s = "0" + s
} }
return Hex2Bytes(s) return Hex2Bytes(s)
} }
return nil
}
// Number to bytes // Number to bytes
// //
@ -117,28 +126,29 @@ func CopyBytes(b []byte) (copiedBytes []byte) {
return return
} }
func HasHexPrefix(str string) bool { // isHexPattern is a regular expression to verify whether a string represents an
l := len(str) // abirarilly long hex number with or without the "0x" prefix.
return l >= 2 && str[0:2] == "0x" var isHexPattern = regexp.MustCompile("^0(x|X)([0-9a-fA-F]{2})+$")
}
// IsHex checks whether a string is a valid hexadecimal number, consisting of
// only hex digits and prefixed with 0x or 0X.
func IsHex(str string) bool { func IsHex(str string) bool {
l := len(str) return isHexPattern.MatchString(str)
return l >= 4 && l%2 == 0 && str[0:2] == "0x"
}
func Bytes2Hex(d []byte) string {
return hex.EncodeToString(d)
} }
func Hex2Bytes(str string) []byte { func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str) h, err := hex.DecodeString(str)
if err != nil {
glog.V(logger.Error).Infof("Invalid hex string to decode: %s: %v", str, err)
}
return h return h
} }
func Hex2BytesFixed(str string, flen int) []byte { func Hex2BytesFixed(str string, flen int) []byte {
h, _ := hex.DecodeString(str) h, err := hex.DecodeString(str)
if err != nil {
glog.V(logger.Error).Infof("Invalid hex string to decode: %s: %v", str, err)
}
if len(h) == flen { if len(h) == flen {
return h return h
} else { } else {
@ -153,7 +163,7 @@ func Hex2BytesFixed(str string, flen int) []byte {
} }
func StringToByteFunc(str string, cb func(str string) []byte) (ret []byte) { func StringToByteFunc(str string, cb func(str string) []byte) (ret []byte) {
if len(str) > 1 && str[0:2] == "0x" && !strings.Contains(str, "\n") { if len(str) > 1 && (str[:2] == "0x" || str[:2] == "0X") && !strings.Contains(str, "\n") {
ret = Hex2Bytes(str[2:]) ret = Hex2Bytes(str[2:])
} else { } else {
ret = cb(str) ret = cb(str)
@ -170,12 +180,11 @@ func FormatData(data string) []byte {
d := new(big.Int) d := new(big.Int)
if data[0:1] == "\"" && data[len(data)-1:] == "\"" { if data[0:1] == "\"" && data[len(data)-1:] == "\"" {
return RightPadBytes([]byte(data[1:len(data)-1]), 32) return RightPadBytes([]byte(data[1:len(data)-1]), 32)
} else if len(data) > 1 && data[:2] == "0x" { } else if len(data) > 1 && (data[:2] == "0x" || data[:2] == "0X") {
d.SetBytes(Hex2Bytes(data[2:])) d.SetBytes(Hex2Bytes(data[2:]))
} else { } else {
d.SetString(data, 0) d.SetString(data, 0)
} }
return BigToBytes(d, 256) return BigToBytes(d, 256)
} }
@ -225,22 +234,14 @@ func LeftPadString(str string, l int) string {
if l < len(str) { if l < len(str) {
return str return str
} }
return hex.EncodeToString(make([]byte, (l-len(str))/2)) + str
zeros := Bytes2Hex(make([]byte, (l-len(str))/2))
return zeros + str
} }
func RightPadString(str string, l int) string { func RightPadString(str string, l int) string {
if l < len(str) { if l < len(str) {
return str return str
} }
return str + hex.EncodeToString(make([]byte, (l-len(str))/2))
zeros := Bytes2Hex(make([]byte, (l-len(str))/2))
return str + zeros
} }
func ToAddress(slice []byte) (addr []byte) { func ToAddress(slice []byte) (addr []byte) {

View file

@ -83,16 +83,51 @@ func (s *BytesSuite) TestCopyBytes(c *checker.C) {
} }
func (s *BytesSuite) TestIsHex(c *checker.C) { func (s *BytesSuite) TestIsHex(c *checker.C) {
data1 := "a9e67e" tests := []struct {
exp1 := false data string
res1 := IsHex(data1) hex bool
c.Assert(res1, checker.DeepEquals, exp1) }{
// Ensure minimum length requirements pass
{"", false},
{"0", false},
{"00", false},
{"0x", false},
{"0x0", false},
{"0x00", true},
data2 := "0xa9e67e00" // Ensure prefix is enforced
exp2 := true {"1234", false},
res2 := IsHex(data2) {"abcdef", false},
c.Assert(res2, checker.DeepEquals, exp2) {"0_1234", false},
{"0_abcdef", false},
{"0x1234", true},
{"0xabcdef", true},
// Ensure even number of digits
{"0x333", false},
{"0x4444", true},
{"0x55555", false},
{"0x666666", true},
// Ensure bad digits are caught
{"0xhi", false},
{"0xhelloworld", false},
{"0x0x00", false},
// Ensure both lower as well as upper case are accepted
{"0X1234", true},
{"0Xabcdef", true},
{"0xaCcDeF", true},
{"0XaCcDeF", true},
// Random tests from previous suite
{"a9e67e", false},
{"0xa9e67e00", true},
}
for _, tt := range tests {
c.Assert(IsHex(tt.data), checker.DeepEquals, tt.hex)
}
} }
func (s *BytesSuite) TestParseDataString(c *checker.C) { func (s *BytesSuite) TestParseDataString(c *checker.C) {

View file

@ -111,11 +111,12 @@ func FetchDocsForContract(contractAddress string, xeth *xeth.XEth, client *httpc
codehex := xeth.CodeAt(contractAddress) codehex := xeth.CodeAt(contractAddress)
codeb := xeth.CodeAtBytes(contractAddress) codeb := xeth.CodeAtBytes(contractAddress)
if codehex == "0x" { if codehex == "0x" || codehex == "0X" {
err = fmt.Errorf("contract (%v) not found", contractAddress) err = fmt.Errorf("contract (%v) not found", contractAddress)
return return
} }
codehash := common.BytesToHash(crypto.Keccak256(codeb)) codehash := common.BytesToHash(crypto.Keccak256(codeb))
// set up nameresolver with natspecreg + urlhint contract addresses // set up nameresolver with natspecreg + urlhint contract addresses
reg := registrar.New(xeth) reg := registrar.New(xeth)

View file

@ -56,11 +56,11 @@ func FileExist(filePath string) bool {
return true return true
} }
func AbsolutePath(Datadir string, filename string) string { func AbsolutePath(datadir string, filename string) string {
if filepath.IsAbs(filename) { if filepath.IsAbs(filename) {
return filename return filename
} }
return filepath.Join(Datadir, filename) return filepath.Join(datadir, filename)
} }
func HomeDir() string { func HomeDir() string {

View file

@ -183,7 +183,7 @@ func (be *registryAPIBackend) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr
gp := new(core.GasPool).AddGas(common.MaxBig) gp := new(core.GasPool).AddGas(common.MaxBig)
res, gas, err := core.ApplyMessage(vmenv, msg, gp) res, gas, err := core.ApplyMessage(vmenv, msg, gp)
return common.ToHex(res), gas.String(), err return common.NumberToHex(res), gas.String(), err
} }
// StorageAt returns the data stores in the state for the given address and location. // StorageAt returns the data stores in the state for the given address and location.
@ -199,7 +199,7 @@ func (be *registryAPIBackend) StorageAt(addr string, storageAddr string) string
// Transact forms a transaction from the given arguments and submits it to the // Transact forms a transaction from the given arguments and submits it to the
// transactio pool for execution. // transactio pool for execution.
func (be *registryAPIBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { func (be *registryAPIBackend) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
if len(toStr) > 0 && toStr != "0x" && !common.IsHexAddress(toStr) { if len(toStr) > 0 && toStr != "0x" && toStr != "0X" && !common.IsHexAddress(toStr) {
return "", errors.New("invalid address") return "", errors.New("invalid address")
} }

View file

@ -18,6 +18,7 @@ package registrar
import ( import (
"encoding/binary" "encoding/binary"
"encoding/hex"
"fmt" "fmt"
"math/big" "math/big"
"regexp" "regexp"
@ -68,7 +69,7 @@ const (
) )
func abiSignature(s string) string { func abiSignature(s string) string {
return common.ToHex(crypto.Keccak256([]byte(s))[:4]) return common.BytesToHex(crypto.Keccak256([]byte(s))[:4])
} }
var ( var (
@ -282,8 +283,8 @@ func (self *Registrar) SetHashToHash(address common.Address, codehash, dochash c
if err != nil { if err != nil {
return return
} }
codehex := common.Bytes2Hex(codehash[:]) codehex := hex.EncodeToString(codehash[:])
dochex := common.Bytes2Hex(dochash[:]) dochex := hex.EncodeToString(dochash[:])
data := registerContentHashAbi + codehex + dochex data := registerContentHashAbi + codehex + dochex
glog.V(logger.Detail).Infof("SetHashToHash data: %s sent to %v\n", data, HashRegAddr) glog.V(logger.Detail).Infof("SetHashToHash data: %s sent to %v\n", data, HashRegAddr)
@ -305,7 +306,7 @@ func (self *Registrar) SetUrlToHash(address common.Address, hash common.Hash, ur
return "", fmt.Errorf("UrlHint address is not set") return "", fmt.Errorf("UrlHint address is not set")
} }
hashHex := common.Bytes2Hex(hash[:]) hashHex := hex.EncodeToString(hash[:])
var urlHex string var urlHex string
urlb := []byte(url) urlb := []byte(url)
var cnt byte var cnt byte
@ -315,15 +316,15 @@ func (self *Registrar) SetUrlToHash(address common.Address, hash common.Hash, ur
if n > 32 { if n > 32 {
n = 32 n = 32
} }
urlHex = common.Bytes2Hex(urlb[:n]) urlHex = hex.EncodeToString(urlb[:n])
urlb = urlb[n:] urlb = urlb[n:]
n = len(urlb) n = len(urlb)
bcnt := make([]byte, 32) bcnt := make([]byte, 32)
bcnt[31] = cnt bcnt[31] = cnt
data := registerUrlAbi + data := registerUrlAbi +
hashHex + hashHex +
common.Bytes2Hex(bcnt) + hex.EncodeToString(bcnt) +
common.Bytes2Hex(common.Hex2BytesFixed(urlHex, 32)) hex.EncodeToString(common.Hex2BytesFixed(urlHex, 32))
txh, err = self.backend.Transact( txh, err = self.backend.Transact(
address.Hex(), address.Hex(),
UrlHintAddr, UrlHintAddr,
@ -420,7 +421,7 @@ func storageFixedArray(addr, idx []byte) []byte {
} }
func storageAddress(addr []byte) string { func storageAddress(addr []byte) string {
return common.ToHex(addr) return common.BytesToHex(addr)
} }
func encodeAddress(address common.Address) string { func encodeAddress(address common.Address) string {
@ -428,7 +429,7 @@ func encodeAddress(address common.Address) string {
} }
func encodeName(name string, index uint8) (string, string) { func encodeName(name string, index uint8) (string, string) {
extra := common.Bytes2Hex([]byte(name)) extra := hex.EncodeToString([]byte(name))
if len(name) > 32 { if len(name) > 32 {
return fmt.Sprintf("%064x", index), extra return fmt.Sprintf("%064x", index), extra
} }

View file

@ -52,7 +52,7 @@ func (self *testBackend) initUrlHint() {
mapaddr := storageMapping(storageIdx2Addr(1), hash[:]) mapaddr := storageMapping(storageIdx2Addr(1), hash[:])
key := storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0))) key := storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(0)))
self.contracts[UrlHintAddr[2:]][key] = common.ToHex([]byte(url)) self.contracts[UrlHintAddr[2:]][key] = common.NumberToHex([]byte(url))
key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1))) key = storageAddress(storageFixedArray(mapaddr, storageIdx2Addr(1)))
self.contracts[UrlHintAddr[2:]][key] = "0x0" self.contracts[UrlHintAddr[2:]][key] = "0x0"
} }

View file

@ -50,7 +50,7 @@ func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) }
func (h Hash) Str() string { return string(h[:]) } func (h Hash) Str() string { return string(h[:]) }
func (h Hash) Bytes() []byte { return h[:] } func (h Hash) Bytes() []byte { return h[:] }
func (h Hash) Big() *big.Int { return Bytes2Big(h[:]) } func (h Hash) Big() *big.Int { return Bytes2Big(h[:]) }
func (h Hash) Hex() string { return "0x" + Bytes2Hex(h[:]) } func (h Hash) Hex() string { return BytesToHex(h[:]) }
// UnmarshalJSON parses a hash in its hex from to a hash. // UnmarshalJSON parses a hash in its hex from to a hash.
func (h *Hash) UnmarshalJSON(input []byte) error { func (h *Hash) UnmarshalJSON(input []byte) error {
@ -110,7 +110,8 @@ func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) } func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) }
// IsHexAddress verifies whether a string can represent a valid hex-encoded // IsHexAddress verifies whether a string can represent a valid hex-encoded
// Ethereum address or not. // Ethereum address or not. The accepted format is a 40 hex-character string,
// optionally prefixed by 0x or 0X.
func IsHexAddress(s string) bool { func IsHexAddress(s string) bool {
if len(s) == 2+2*AddressLength && IsHex(s) { if len(s) == 2+2*AddressLength && IsHex(s) {
return true return true
@ -126,7 +127,7 @@ func (a Address) Str() string { return string(a[:]) }
func (a Address) Bytes() []byte { return a[:] } func (a Address) Bytes() []byte { return a[:] }
func (a Address) Big() *big.Int { return Bytes2Big(a[:]) } func (a Address) Big() *big.Int { return Bytes2Big(a[:]) }
func (a Address) Hash() Hash { return BytesToHash(a[:]) } func (a Address) Hash() Hash { return BytesToHash(a[:]) }
func (a Address) Hex() string { return "0x" + Bytes2Hex(a[:]) } func (a Address) Hex() string { return BytesToHex(a[:]) }
// Sets the address to the value of b. If b is larger than len(a) it will panic // Sets the address to the value of b. If b is larger than len(a) it will panic
func (a *Address) SetBytes(b []byte) { func (a *Address) SetBytes(b []byte) {
@ -182,8 +183,7 @@ func (a *Address) UnmarshalJSON(data []byte) error {
// hex(value[:4])...(hex[len(value)-4:]) // hex(value[:4])...(hex[len(value)-4:])
func PP(value []byte) string { func PP(value []byte) string {
if len(value) <= 8 { if len(value) <= 8 {
return Bytes2Hex(value) return hex.EncodeToString(value)
} }
return fmt.Sprintf("%x...%x", value[:4], value[len(value)-4]) return fmt.Sprintf("%x...%x", value[:4], value[len(value)-4])
} }

View file

@ -29,3 +29,38 @@ func TestBytesConversion(t *testing.T) {
t.Errorf("expected %x got %x", exp, hash) t.Errorf("expected %x got %x", exp, hash)
} }
} }
// Tests whether addresses are correctly matched against allowed form and data
// content.
func TestIsHexAddress(t *testing.T) {
tests := []struct {
address string
valid bool
}{
{"", false}, // Empty, without optional 0x prefix
{"0x", false}, // Empty, with optional 0x prefix
{"00", false}, // Too short, without optional 0x prefix
{"0x00", false}, // Too short, with optional 0x prefix
{"00000000000000000000000000000000000000", false}, // Too short (even), without optional 0x prefix
{"0x00000000000000000000000000000000000000", false}, // Too short (even), with optional 0x prefix
{"000000000000000000000000000000000000000", false}, // Too short (odd), without optional 0x prefix
{"0x000000000000000000000000000000000000000", false}, // Too short (odd), with optional 0x prefix
{"0000000000000000000000000000000000000000", true}, // Valid, without optional 0x prefix
{"0x0000000000000000000000000000000000000000", true}, // Valid, with optional 0x prefix
{"0x00000000000000000000000000000000000000", false}, // Length / prefix combo invalidity
{"00x0000000000000000000000000000000000000", false}, // Invalid content, without optional 0x prefix
{"0x0x00000000000000000000000000000000000000", false}, // Invalid content, with optional 0x prefix
{"abcdefghijklmnopqrstuvwxyz0123456789xxxx", false}, // Invalid content, without optional 0x prefix
{"0xabcdefghijklmnopqrstuvwxyz0123456789xxxx", false}, // Invalid content, with optional 0x prefix
{"00000000000000000000000000000000000000000", false}, // Too long (odd), without optional 0x prefix
{"0x00000000000000000000000000000000000000000", false}, // Too long (odd), with optional 0x prefix
{"000000000000000000000000000000000000000000", false}, // Too long (even), without optional 0x prefix
{"0x000000000000000000000000000000000000000000", false}, // Too long (even), with optional 0x prefix
}
for i, tt := range tests {
if valid := IsHexAddress(tt.address); valid != tt.valid {
t.Errorf("test %d: address validity mismatch: have %v, want %v", i, valid, tt.valid)
}
}
}

View file

@ -18,11 +18,11 @@ package core
import ( import (
"bytes" "bytes"
"encoding/hex"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
@ -43,7 +43,7 @@ func ReportBlock(block *types.Block, err error) {
blockRlp, _ := rlp.EncodeToBytes(block) blockRlp, _ := rlp.EncodeToBytes(block)
data := map[string]interface{}{ data := map[string]interface{}{
"block": common.Bytes2Hex(blockRlp), "block": hex.EncodeToString(blockRlp),
"errortype": err.Error(), "errortype": err.Error(),
"hints": map[string]interface{}{ "hints": map[string]interface{}{
"receipts": "NYI", "receipts": "NYI",

View file

@ -17,6 +17,7 @@
package state package state
import ( import (
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -39,7 +40,7 @@ type World struct {
func (self *StateDB) RawDump() World { func (self *StateDB) RawDump() World {
world := World{ world := World{
Root: common.Bytes2Hex(self.trie.Root()), Root: hex.EncodeToString(self.trie.Root()),
Accounts: make(map[string]Account), Accounts: make(map[string]Account),
} }
@ -48,14 +49,14 @@ func (self *StateDB) RawDump() World {
addr := self.trie.GetKey(it.Key) addr := self.trie.GetKey(it.Key)
stateObject, _ := DecodeObject(common.BytesToAddress(addr), self.db, it.Value) stateObject, _ := DecodeObject(common.BytesToAddress(addr), self.db, it.Value)
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash), Code: common.Bytes2Hex(stateObject.Code())} account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: hex.EncodeToString(stateObject.Root()), CodeHash: hex.EncodeToString(stateObject.codeHash), Code: hex.EncodeToString(stateObject.Code())}
account.Storage = make(map[string]string) account.Storage = make(map[string]string)
storageIt := stateObject.trie.Iterator() storageIt := stateObject.trie.Iterator()
for storageIt.Next() { for storageIt.Next() {
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value) account.Storage[hex.EncodeToString(self.trie.GetKey(storageIt.Key))] = hex.EncodeToString(storageIt.Value)
} }
world.Accounts[common.Bytes2Hex(addr)] = account world.Accounts[hex.EncodeToString(addr)] = account
} }
return world return world
} }

View file

@ -17,6 +17,7 @@
package core package core
import ( import (
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
@ -288,14 +289,14 @@ func (self *TxPool) add(tx *types.Transaction) error {
if glog.V(logger.Debug) { if glog.V(logger.Debug) {
var toname string var toname string
if to := tx.To(); to != nil { if to := tx.To(); to != nil {
toname = common.Bytes2Hex(to[:4]) toname = hex.EncodeToString(to[:4])
} else { } else {
toname = "[NEW_CONTRACT]" toname = "[NEW_CONTRACT]"
} }
// we can ignore the error here because From is // we can ignore the error here because From is
// verified in ValidateTransaction. // verified in ValidateTransaction.
f, _ := tx.From() f, _ := tx.From()
from := common.Bytes2Hex(f[:4]) from := hex.EncodeToString(f[:4])
glog.Infof("(t) %x => %s (%v) %x\n", from, toname, tx.Value, hash) glog.Infof("(t) %x => %s (%v) %x\n", from, toname, tx.Value, hash)
} }

View file

@ -585,11 +585,7 @@ func (s *PublicBlockChainAPI) GetCode(address common.Address, blockNr rpc.BlockN
if state == nil || err != nil { if state == nil || err != nil {
return "", err return "", err
} }
res := state.GetCode(address) return common.BytesToHex(state.GetCode(address)), nil
if len(res) == 0 { // backwards compatibility
return "0x", nil
}
return common.ToHex(res), nil
} }
// GetStorageAt returns the storage from the state at the given address, key and // GetStorageAt returns the storage from the state at the given address, key and
@ -674,10 +670,7 @@ func (s *PublicBlockChainAPI) doCall(args CallArgs, blockNr rpc.BlockNumber) (st
gp := new(core.GasPool).AddGas(common.MaxBig) gp := new(core.GasPool).AddGas(common.MaxBig)
res, gas, err := core.ApplyMessage(vmenv, msg, gp) res, gas, err := core.ApplyMessage(vmenv, msg, gp)
if len(res) == 0 { // backwards compatibility return common.BytesToHex(res), gas, err
return "0x", gas, err
}
return common.ToHex(res), gas, err
} }
// Call executes the given transaction on the state for the given block number. // Call executes the given transaction on the state for the given block number.
@ -1106,7 +1099,7 @@ func (s *PublicTransactionPoolAPI) SendRawTransaction(encodedTx string) (string,
// be unlocked. // be unlocked.
func (s *PublicTransactionPoolAPI) Sign(address common.Address, data string) (string, error) { func (s *PublicTransactionPoolAPI) Sign(address common.Address, data string) (string, error) {
signature, error := s.am.Sign(accounts.Account{Address: address}, common.HexToHash(data).Bytes()) signature, error := s.am.Sign(accounts.Account{Address: address}, common.HexToHash(data).Bytes())
return common.ToHex(signature), error return common.BytesToHex(signature), error
} }
type SignTransactionArgs struct { type SignTransactionArgs struct {
@ -1198,7 +1191,7 @@ func newTx(t *types.Transaction) *Tx {
From: from, From: from,
Value: rpc.NewHexNumber(t.Value()), Value: rpc.NewHexNumber(t.Value()),
Nonce: rpc.NewHexNumber(t.Nonce()), Nonce: rpc.NewHexNumber(t.Nonce()),
Data: "0x" + common.Bytes2Hex(t.Data()), Data: common.BytesToHex(t.Data()),
GasLimit: rpc.NewHexNumber(t.Gas()), GasLimit: rpc.NewHexNumber(t.Gas()),
GasPrice: rpc.NewHexNumber(t.GasPrice()), GasPrice: rpc.NewHexNumber(t.GasPrice()),
Hash: t.Hash(), Hash: t.Hash(),
@ -1245,7 +1238,7 @@ func (s *PublicTransactionPoolAPI) SignTransaction(args *SignTransactionArgs) (*
return nil, err return nil, err
} }
return &SignTransactionResult{"0x" + common.Bytes2Hex(data), newTx(tx)}, nil return &SignTransactionResult{common.BytesToHex(data), newTx(tx)}, nil
} }
// PendingTransactions returns the transactions that are in the transaction pool and have a from address that is one of // PendingTransactions returns the transactions that are in the transaction pool and have a from address that is one of

View file

@ -573,7 +573,7 @@ func newFilterId() (string, error) {
if n != 16 { if n != 16 {
return "", errors.New("Unable to generate filter id") return "", errors.New("Unable to generate filter id")
} }
return "0x" + hex.EncodeToString(subid[:]), nil return common.BytesToHex(subid[:]), nil
} }
// toRPCLogs is a helper that will convert a vm.Logs array to an structure which // toRPCLogs is a helper that will convert a vm.Logs array to an structure which

View file

@ -21,12 +21,14 @@ import (
"io" "io"
"log" "log"
"os" "os"
"path/filepath"
"github.com/ethereum/go-ethereum/common"
) )
func openLogFile(datadir string, filename string) *os.File { func openLogFile(datadir string, filename string) *os.File {
path := common.AbsolutePath(datadir, filename) path := filename
if !filepath.IsAbs(path) {
path = filepath.Join(datadir, filename)
}
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {
panic(fmt.Sprintf("error opening log file '%s': %v", filename, err)) panic(fmt.Sprintf("error opening log file '%s': %v", filename, err))

View file

@ -269,5 +269,5 @@ func (s *PublicWeb3API) ClientVersion() string {
// Sha3 applies the ethereum sha3 implementation on the input. // Sha3 applies the ethereum sha3 implementation on the input.
// It assumes the input is hex encoded. // It assumes the input is hex encoded.
func (s *PublicWeb3API) Sha3(input string) string { func (s *PublicWeb3API) Sha3(input string) string {
return common.ToHex(crypto.Keccak256(common.FromHex(input))) return common.BytesToHex(crypto.Keccak256(common.FromHex(input)))
} }

View file

@ -24,6 +24,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"gopkg.in/fatih/set.v0" "gopkg.in/fatih/set.v0"
) )
@ -220,11 +221,7 @@ func (h *HexNumber) UnmarshalJSON(input []byte) error {
// MarshalJSON serialize the hex number instance to a hex representation. // MarshalJSON serialize the hex number instance to a hex representation.
func (h *HexNumber) MarshalJSON() ([]byte, error) { func (h *HexNumber) MarshalJSON() ([]byte, error) {
if h != nil { if h != nil {
hn := (*big.Int)(h) return []byte(`"` + common.NumberToHex((*big.Int)(h).Bytes()) + `"`), nil
if hn.BitLen() == 0 {
return []byte(`"0x0"`), nil
}
return []byte(fmt.Sprintf(`"0x%x"`, hn)), nil
} }
return nil, nil return nil, nil
} }

View file

@ -18,7 +18,6 @@ package rpc
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
@ -26,6 +25,7 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -211,7 +211,7 @@ func newSubscriptionId() (string, error) {
if n != 16 { if n != 16 {
return "", errors.New("Unable to generate subscription id") return "", errors.New("Unable to generate subscription id")
} }
return "0x" + hex.EncodeToString(subid[:]), nil return common.BytesToHex(subid[:]), nil
} }
// SupportedModules returns the collection of API's that the RPC server offers // SupportedModules returns the collection of API's that the RPC server offers

View file

@ -437,7 +437,7 @@ func (test *BlockTest) ValidateImportedHeaders(cm *core.BlockChain, validBlocks
// all blocks have been processed by ChainManager, as they may not // all blocks have been processed by ChainManager, as they may not
// be part of the longest chain until last block is imported. // be part of the longest chain until last block is imported.
for b := cm.CurrentBlock(); b != nil && b.NumberU64() != 0; b = cm.GetBlock(b.Header().ParentHash) { for b := cm.CurrentBlock(); b != nil && b.NumberU64() != 0; b = cm.GetBlock(b.Header().ParentHash) {
bHash := common.Bytes2Hex(b.Hash().Bytes()) // hex without 0x prefix bHash := hex.EncodeToString(b.Hash().Bytes()) // hex without 0x prefix
if err := validateHeader(bmap[bHash].BlockHeader, b.Header()); err != nil { if err := validateHeader(bmap[bHash].BlockHeader, b.Header()); err != nil {
return fmt.Errorf("Imported block header validation failed: %v", err) return fmt.Errorf("Imported block header validation failed: %v", err)
} }

View file

@ -74,7 +74,7 @@ func (s *PublicWhisperAPI) NewIdentity() (string, error) {
} }
identity := s.w.NewIdentity() identity := s.w.NewIdentity()
return common.ToHex(crypto.FromECDSAPub(&identity.PublicKey)), nil return common.BytesToHex(crypto.FromECDSAPub(&identity.PublicKey)), nil
} }
type NewFilterArgs struct { type NewFilterArgs struct {
@ -403,11 +403,11 @@ func NewWhisperMessage(message *Message) WhisperMessage {
return WhisperMessage{ return WhisperMessage{
ref: message, ref: message,
Payload: common.ToHex(message.Payload), Payload: common.BytesToHex(message.Payload),
From: common.ToHex(crypto.FromECDSAPub(message.Recover())), From: common.BytesToHex(crypto.FromECDSAPub(message.Recover())),
To: common.ToHex(crypto.FromECDSAPub(message.To)), To: common.BytesToHex(crypto.FromECDSAPub(message.To)),
Sent: message.Sent.Unix(), Sent: message.Sent.Unix(),
TTL: int64(message.TTL / time.Second), TTL: int64(message.TTL / time.Second),
Hash: common.ToHex(message.Hash.Bytes()), Hash: common.BytesToHex(message.Hash.Bytes()),
} }
} }