Appending unmarshalJSON methods to avoid panic if mal-formed arguments are passed.
This commit is contained in:
parent
44c7e0e589
commit
ff5d2e8030
@ -1,10 +1,10 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Hash [32]byte
|
||||
@ -15,6 +15,9 @@ func (h Hash) MarshalJSON() ([]byte, error) {
|
||||
|
||||
func (h *Hash) UnmarshalJSON(data []byte) error {
|
||||
d := bytes.TrimPrefix(bytes.Trim(data, `"`), []byte("0x"))
|
||||
if len(d) > 64 {
|
||||
return fmt.Errorf("argument of invalid length")
|
||||
}
|
||||
_, err := hex.Decode(h[(64-len(d))/2:], d)
|
||||
return err
|
||||
}
|
||||
@ -48,6 +51,9 @@ func (h Address) MarshalJSON() ([]byte, error) {
|
||||
|
||||
func (h *Address) UnmarshalJSON(data []byte) error {
|
||||
d := bytes.TrimPrefix(bytes.Trim(data, `"`), []byte("0x"))
|
||||
if len(d) > 40 {
|
||||
return fmt.Errorf("argument of invalid length")
|
||||
}
|
||||
_, err := hex.Decode(h[(40-len(d))/2:], d)
|
||||
return err
|
||||
}
|
||||
@ -56,7 +62,6 @@ func (h Address) String() string {
|
||||
return fmt.Sprintf("%#x", h[:])
|
||||
}
|
||||
|
||||
|
||||
func HexToAddress(data string) Address {
|
||||
h := Address{}
|
||||
b, _ := hex.DecodeString(strings.TrimPrefix(strings.Trim(data, `"`), "0x"))
|
||||
@ -70,7 +75,6 @@ func BytesToAddress(b []byte) Address {
|
||||
return h
|
||||
}
|
||||
|
||||
|
||||
type ChainEvent struct {
|
||||
Block []byte // RLP Encoded block
|
||||
Hash Hash
|
||||
@ -96,7 +100,6 @@ type API struct {
|
||||
Public bool
|
||||
}
|
||||
|
||||
|
||||
func CopyBytes(a []byte) []byte {
|
||||
b := make([]byte, len(a))
|
||||
copy(b[:], a[:])
|
||||
|
Loading…
Reference in New Issue
Block a user