Skip to content

Commit

Permalink
Fixed bug with json marshal on Address
Browse files Browse the repository at this point in the history
  • Loading branch information
jdowning100 committed Feb 24, 2023
1 parent 9c1ea9e commit 58b5f7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"

Expand Down Expand Up @@ -135,7 +136,7 @@ func (a Address) Format(s fmt.State, c rune) {
// MarshalText returns the hex representation of a.
func (a Address) MarshalText() ([]byte, error) {
if a.inner == nil {
return []byte{}, nil
return hexutil.Bytes(ZeroInternal[:]).MarshalText()
}
return a.inner.MarshalText()
}
Expand All @@ -150,10 +151,22 @@ func (a *Address) UnmarshalText(input []byte) error {
return nil
}

// MarshalJSON marshals a subscription as its ID.
func (a *Address) MarshalJSON() ([]byte, error) {
if a.inner == nil {
return json.Marshal(ZeroAddr)
}
return json.Marshal(a.inner)
}

// UnmarshalJSON parses a hash in hex syntax.
func (a *Address) UnmarshalJSON(input []byte) error {
var temp [AddressLength]byte
if err := hexutil.UnmarshalFixedJSON(reflect.TypeOf(InternalAddress{}), input, temp[:]); err != nil {
if len(input) == 0 {
a.inner = Bytes20ToAddress(ZeroInternal).inner
return nil
}
return err
}
a.inner = Bytes20ToAddress(temp).inner
Expand Down
2 changes: 1 addition & 1 deletion common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var (
var (
hashT = reflect.TypeOf(Hash{})
// The zero address (0x0)
ZeroInternal = InternalAddress{0x0000000000000000000000000000000000000000}
ZeroInternal = InternalAddress{}
ZeroAddr = Address{&ZeroInternal}
ErrInvalidScope = errors.New("address is not in scope")
)
Expand Down

0 comments on commit 58b5f7e

Please sign in to comment.