From 991993357c902eaab56726bef97e7494674aa5e5 Mon Sep 17 00:00:00 2001
From: Maran <maran.hidskes@gmail.com>
Date: Sat, 14 Mar 2015 11:39:35 +0100
Subject: DRY up the use of fromHex and put it in ethutil

---
 rpc/api.go       | 14 +++++++-------
 rpc/util.go      | 13 -------------
 rpc/util_test.go | 25 -------------------------
 3 files changed, 7 insertions(+), 45 deletions(-)
 delete mode 100644 rpc/util_test.go

(limited to 'rpc')

diff --git a/rpc/api.go b/rpc/api.go
index af9f10530..78e6a8136 100644
--- a/rpc/api.go
+++ b/rpc/api.go
@@ -241,7 +241,7 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error)
 	//	p.register[args.From] = append(p.register[args.From], args)
 	//} else {
 	/*
-		account := accounts.Get(fromHex(args.From))
+		account := accounts.Get(ethutil.FromHex(args.From))
 		if account != nil {
 			if account.Unlocked() {
 				if !unlockAccount(account) {
@@ -249,7 +249,7 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error)
 				}
 			}
 
-			result, _ := account.Transact(fromHex(args.To), fromHex(args.Value), fromHex(args.Gas), fromHex(args.GasPrice), fromHex(args.Data))
+			result, _ := account.Transact(ethutil.FromHex(args.To), ethutil.FromHex(args.Value), ethutil.FromHex(args.Gas), ethutil.FromHex(args.GasPrice), ethutil.FromHex(args.Data))
 			if len(result) > 0 {
 				*reply = toHex(result)
 			}
@@ -480,7 +480,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
 		if err := json.Unmarshal(req.Params, &args); err != nil {
 			return err
 		}
-		*reply = toHex(crypto.Sha3(fromHex(args.Data)))
+		*reply = toHex(crypto.Sha3(ethutil.FromHex(args.Data)))
 	case "web3_clientVersion":
 		*reply = p.xeth().Backend().Version()
 	case "net_version":
@@ -815,12 +815,12 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions {
 
 	// Convert optional address slice/string to byte slice
 	if str, ok := options.Address.(string); ok {
-		opts.Address = [][]byte{fromHex(str)}
+		opts.Address = [][]byte{ethutil.FromHex(str)}
 	} else if slice, ok := options.Address.([]interface{}); ok {
 		bslice := make([][]byte, len(slice))
 		for i, addr := range slice {
 			if saddr, ok := addr.(string); ok {
-				bslice[i] = fromHex(saddr)
+				bslice[i] = ethutil.FromHex(saddr)
 			}
 		}
 		opts.Address = bslice
@@ -834,11 +834,11 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions {
 		if slice, ok := topicDat.([]interface{}); ok {
 			topics[i] = make([][]byte, len(slice))
 			for j, topic := range slice {
-				topics[i][j] = fromHex(topic.(string))
+				topics[i][j] = ethutil.FromHex(topic.(string))
 			}
 		} else if str, ok := topicDat.(string); ok {
 			topics[i] = make([][]byte, 1)
-			topics[i][0] = fromHex(str)
+			topics[i][0] = ethutil.FromHex(str)
 		}
 	}
 	opts.Topics = topics
diff --git a/rpc/util.go b/rpc/util.go
index 573190e59..4acd90284 100644
--- a/rpc/util.go
+++ b/rpc/util.go
@@ -128,19 +128,6 @@ func toHex(b []byte) string {
 	return "0x" + hex
 }
 
-func fromHex(s string) []byte {
-	if len(s) > 1 {
-		if s[0:2] == "0x" {
-			s = s[2:]
-		}
-		if len(s)%2 == 1 {
-			s = "0" + s
-		}
-		return ethutil.Hex2Bytes(s)
-	}
-	return nil
-}
-
 func i2hex(n int) string {
 	return toHex(big.NewInt(int64(n)).Bytes())
 }
diff --git a/rpc/util_test.go b/rpc/util_test.go
deleted file mode 100644
index b0a4979b5..000000000
--- a/rpc/util_test.go
+++ /dev/null
@@ -1,25 +0,0 @@
-package rpc
-
-import (
-	"bytes"
-	"testing"
-)
-
-//fromHex
-func TestFromHex(t *testing.T) {
-	input := "0x01"
-	expected := []byte{1}
-	result := fromHex(input)
-	if bytes.Compare(expected, result) != 0 {
-		t.Errorf("Expected % x got % x", expected, result)
-	}
-}
-
-func TestFromHexOddLength(t *testing.T) {
-	input := "0x1"
-	expected := []byte{1}
-	result := fromHex(input)
-	if bytes.Compare(expected, result) != 0 {
-		t.Errorf("Expected % x got % x", expected, result)
-	}
-}
-- 
cgit v1.2.3