aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/messages.go
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-04-01 18:49:10 +0800
committerobscuren <geffobscura@gmail.com>2015-04-01 18:49:10 +0800
commit6afc5e762af0f81b70256ebb7e830b09caf17be0 (patch)
treefa6c2a4cff1c3799c623e4b361ac3052baa0441e /rpc/messages.go
parent4e8f8cfab701bb6c4ad2b8cf166d642f408ca398 (diff)
parent02fb83782eab5d6ad394aca58daab77a9525d5ff (diff)
downloaddexon-6afc5e762af0f81b70256ebb7e830b09caf17be0.tar
dexon-6afc5e762af0f81b70256ebb7e830b09caf17be0.tar.gz
dexon-6afc5e762af0f81b70256ebb7e830b09caf17be0.tar.bz2
dexon-6afc5e762af0f81b70256ebb7e830b09caf17be0.tar.lz
dexon-6afc5e762af0f81b70256ebb7e830b09caf17be0.tar.xz
dexon-6afc5e762af0f81b70256ebb7e830b09caf17be0.tar.zst
dexon-6afc5e762af0f81b70256ebb7e830b09caf17be0.zip
Merge branch 'hexify' of https://github.com/tgerring/go-ethereum into tgerring-hexify
Diffstat (limited to 'rpc/messages.go')
-rw-r--r--rpc/messages.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/rpc/messages.go b/rpc/messages.go
index 5c498234f..f868c5f9b 100644
--- a/rpc/messages.go
+++ b/rpc/messages.go
@@ -19,8 +19,95 @@ package rpc
import (
"encoding/json"
"fmt"
+ "math/big"
+ "strings"
+
+ "github.com/ethereum/go-ethereum/common"
)
+type hexdata struct {
+ data []byte
+}
+
+func (d *hexdata) String() string {
+ return "0x" + common.Bytes2Hex(d.data)
+}
+
+func (d *hexdata) MarshalJSON() ([]byte, error) {
+ return json.Marshal(d.String())
+}
+
+func (d *hexdata) UnmarshalJSON(b []byte) (err error) {
+ d.data = common.FromHex(string(b))
+ return nil
+}
+
+func newHexData(input interface{}) *hexdata {
+ d := new(hexdata)
+
+ switch input := input.(type) {
+ case []byte:
+ d.data = input
+ case common.Hash:
+ d.data = input.Bytes()
+ case *common.Hash:
+ d.data = input.Bytes()
+ case common.Address:
+ d.data = input.Bytes()
+ case *common.Address:
+ d.data = input.Bytes()
+ case *big.Int:
+ d.data = input.Bytes()
+ case int64:
+ d.data = big.NewInt(input).Bytes()
+ case uint64:
+ d.data = big.NewInt(int64(input)).Bytes()
+ case int:
+ d.data = big.NewInt(int64(input)).Bytes()
+ case uint:
+ d.data = big.NewInt(int64(input)).Bytes()
+ case string: // hexstring
+ d.data = common.Big(input).Bytes()
+ default:
+ d.data = nil
+ }
+
+ return d
+}
+
+type hexnum struct {
+ data []byte
+}
+
+func (d *hexnum) String() string {
+ // Get hex string from bytes
+ out := common.Bytes2Hex(d.data)
+ // Trim leading 0s
+ out = strings.Trim(out, "0")
+ // Output "0x0" when value is 0
+ if len(out) == 0 {
+ out = "0"
+ }
+ return "0x" + out
+}
+
+func (d *hexnum) MarshalJSON() ([]byte, error) {
+ return json.Marshal(d.String())
+}
+
+func (d *hexnum) UnmarshalJSON(b []byte) (err error) {
+ d.data = common.FromHex(string(b))
+ return nil
+}
+
+func newHexNum(input interface{}) *hexnum {
+ d := new(hexnum)
+
+ d.data = newHexData(input).data
+
+ return d
+}
+
type InvalidTypeError struct {
method string
msg string