aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
authorTaylor Gerring <taylor.gerring@gmail.com>2015-03-31 23:39:58 +0800
committerTaylor Gerring <taylor.gerring@gmail.com>2015-03-31 23:39:58 +0800
commit3a948b2dbaf22409507a2d3244032751b76432bb (patch)
tree69fd9d572ce89cdfcee48bc9da60d9da0b031320 /rpc
parent8b51582b534b501ad0cb628e0913b990efa09f74 (diff)
downloaddexon-3a948b2dbaf22409507a2d3244032751b76432bb.tar
dexon-3a948b2dbaf22409507a2d3244032751b76432bb.tar.gz
dexon-3a948b2dbaf22409507a2d3244032751b76432bb.tar.bz2
dexon-3a948b2dbaf22409507a2d3244032751b76432bb.tar.lz
dexon-3a948b2dbaf22409507a2d3244032751b76432bb.tar.xz
dexon-3a948b2dbaf22409507a2d3244032751b76432bb.tar.zst
dexon-3a948b2dbaf22409507a2d3244032751b76432bb.zip
Add hexdata and hexnum types
Diffstat (limited to 'rpc')
-rw-r--r--rpc/messages.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/rpc/messages.go b/rpc/messages.go
index 5c498234f..108a07ed8 100644
--- a/rpc/messages.go
+++ b/rpc/messages.go
@@ -19,8 +19,84 @@ package rpc
import (
"encoding/json"
"fmt"
+ "math/big"
+ "strings"
+
+ "github.com/ethereum/go-ethereum/common"
)
+type hexdata struct {
+ data []byte
+}
+
+func (d *hexdata) MarshalJSON() ([]byte, error) {
+ v := common.Bytes2Hex(d.data)
+ return json.Marshal("0x" + v)
+}
+
+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.(type) {
+ case []byte:
+ d.data = input.([]byte)
+ case common.Hash:
+ d.data = input.(common.Hash).Bytes()
+ case common.Address:
+ d.data = input.(common.Address).Bytes()
+ case *big.Int:
+ d.data = input.(*big.Int).Bytes()
+ case int64:
+ d.data = big.NewInt(input.(int64)).Bytes()
+ case uint64:
+ d.data = big.NewInt(int64(input.(uint64))).Bytes()
+ case int:
+ d.data = big.NewInt(int64(input.(int))).Bytes()
+ case uint:
+ d.data = big.NewInt(int64(input.(uint))).Bytes()
+ case string:
+ d.data = common.Big(input.(string)).Bytes()
+ default:
+ d.data = nil
+ }
+
+ return d
+}
+
+type hexnum struct {
+ data []byte
+}
+
+func (d *hexnum) MarshalJSON() ([]byte, error) {
+ // 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 json.Marshal("0x" + out)
+}
+
+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