Make Hash and Address implement JSON marshalling, add hexutils to restricted

This commit is contained in:
Austin Roberts
2021-09-10 11:33:16 -05:00
parent e94946bad0
commit 0d3400ee78
6 changed files with 1277 additions and 0 deletions
+39
View File
@@ -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