From b26f5e0bb7e8922c80bc3513d1ebce2c99a081f5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 31 May 2015 15:53:17 +0200 Subject: types: block json unmarshal method added --- core/types/block.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'core/types') diff --git a/core/types/block.go b/core/types/block.go index c93452fa7..d7963981e 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -1,7 +1,9 @@ package types import ( + "bytes" "encoding/binary" + "encoding/json" "fmt" "io" "math/big" @@ -80,6 +82,28 @@ func (self *Header) RlpData() interface{} { return self.rlpData(true) } +func (h *Header) UnmarshalJSON(data []byte) error { + var ext struct { + ParentHash string + Coinbase string + Difficulty string + GasLimit string + Time uint64 + Extra string + } + dec := json.NewDecoder(bytes.NewReader(data)) + if err := dec.Decode(&ext); err != nil { + return err + } + + h.ParentHash = common.HexToHash(ext.ParentHash) + h.Coinbase = common.HexToAddress(ext.Coinbase) + h.Difficulty = common.String2Big(ext.Difficulty) + h.Time = ext.Time + h.Extra = []byte(ext.Extra) + return nil +} + func rlpHash(x interface{}) (h common.Hash) { hw := sha3.NewKeccak256() rlp.Encode(hw, x) -- cgit v1.2.3