Make Hash and Address implement JSON marshalling, add hexutils to restricted
This commit is contained in:
@@ -1,9 +1,48 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
type Hash [32]byte
|
||||
|
||||
func (h Hash) MarshalJSON() ([]byte, error) {
|
||||
return []byte(fmt.Sprintf(`"%#x"`, h[:])), nil
|
||||
}
|
||||
|
||||
func (h Hash) UnmarshalJSON(data []byte) error {
|
||||
_, err := hex.Decode(h[32-len(data):], bytes.TrimPrefix(bytes.Trim(data, `"`), []byte("0x")))
|
||||
return err
|
||||
}
|
||||
|
||||
func HexToHash(data string) Hash {
|
||||
h := Hash{}
|
||||
b, _ := hex.DecodeString(strings.TrimPrefix(strings.Trim(data, `"`), "0x"))
|
||||
copy(h[32-len(data):], b)
|
||||
return h
|
||||
}
|
||||
|
||||
type Address [20]byte
|
||||
|
||||
func (h Address) MarshalJSON() ([]byte, error) {
|
||||
return []byte(fmt.Sprintf(`"%#x"`, h[:])), nil
|
||||
}
|
||||
|
||||
func (h Address) UnmarshalJSON(data []byte) error {
|
||||
_, err := hex.Decode(h[20-len(data):], bytes.TrimPrefix(bytes.Trim(data, `"`), []byte("0x")))
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
func HexToAddress(data string) Address {
|
||||
h := Address{}
|
||||
b, _ := hex.DecodeString(strings.TrimPrefix(strings.Trim(data, `"`), "0x"))
|
||||
copy(h[20-len(data):], b)
|
||||
return h
|
||||
}
|
||||
|
||||
type ChainEvent struct {
|
||||
Block []byte // RLP Encoded block
|
||||
|
||||
Reference in New Issue
Block a user