Skip to content

Commit

Permalink
adjusting hexaddress.go to tackle errors
Browse files Browse the repository at this point in the history
Signed-off-by: Philip-21 <[email protected]>
  • Loading branch information
Philip-21 committed Jan 6, 2024
1 parent bc29d72 commit 0bf89a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
23 changes: 18 additions & 5 deletions pkg/types/hex_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,39 @@ import (
"gopkg.in/yaml.v3"
)

type HexAddress struct {
ethtypes.Address0xHex
type HexAddress string

type HexWrapper struct {
addrStr *ethtypes.Address0xHex
}

// WrapHexAddress wraps a hex address as HexAddress
func (h *HexAddress) WrapHexAddress(addr [20]byte) (string, error) {
func (h *HexWrapper) WrapHexAddress(addr [20]byte) (string, error) {
hexStr := "0x" + hex.EncodeToString(addr[:])
if err := h.SetString(hexStr); err != nil {
// Initialize addrStr before using it
h.addrStr = new(ethtypes.Address0xHex)
if err := h.addrStr.SetString(hexStr); err != nil {
return "", err
}
return hexStr, nil
}

type HexType struct {
HexValue HexAddress `yaml:"hexvalue"`
HexWrap HexWrapper
}

// Explicitly quote hex addresses so that they are interpreted as string (not int)
func (ht *HexType) MarshalYAML() (interface{}, error) {
hexAddr, err := ht.HexValue.WrapHexAddress(ht.HexValue.Address0xHex)
//convert to byte type
hexBytes, err := hex.DecodeString(string(ht.HexValue[2:]))
if err != nil {
return nil, err
}
//copy bytes to fixed array
var hexArray [20]byte
copy(hexArray[:], hexBytes)
hexAddr, err := ht.HexWrap.WrapHexAddress([20]byte(hexArray))
if err != nil {
return nil, err
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/types/hex_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ func TestWrapHexAddress(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
var hexType HexAddress
var hexType HexType
//[2:] is used to skip the "0x" prefix before decoding the hexadecimal string into a byte slice.
//because x isnt a valid hexadecimal digit
hexBytes, err := hex.DecodeString(tc.Hexvalue[2:])
if err != nil {
t.Log("Unable to decode values:", err)
Expand All @@ -56,7 +58,9 @@ func TestWrapHexAddress(t *testing.T) {
// Copy bytes to a fixed-size array
var hexArray [20]byte
copy(hexArray[:], hexBytes)
result, err := hexType.WrapHexAddress([20]byte(hexArray))
//encodes the decoded values to hexadecimal and returns string
//Ethereum convention for representing hexadecimal values, the prefix must have "0x"
result, err := hexType.HexWrap.WrapHexAddress([20]byte(hexArray))
if err != nil {
t.Log("error in generating result", err)
t.Fail()
Expand Down Expand Up @@ -89,7 +93,7 @@ func TestFailWrapHexAddress(t *testing.T) {
}
for _, tc := range testData {
t.Run(tc.Name, func(t *testing.T) {
var hexType HexAddress
var hexType HexType
hexBytes, err := hex.DecodeString(tc.HexValue[2:])
if err != nil {
t.Log("unable to decode hexvalue:", err)
Expand All @@ -99,7 +103,7 @@ func TestFailWrapHexAddress(t *testing.T) {
}
var hexArray [20]byte
copy(hexArray[:], hexBytes)
result, err := hexType.WrapHexAddress([20]byte(hexArray))
result, err := hexType.HexWrap.WrapHexAddress([20]byte(hexArray))
if err != nil {
t.Log("error in generating result", err)
t.Fail()
Expand Down Expand Up @@ -138,14 +142,14 @@ func TestYamlMarshal(t *testing.T) {
}
for _, tc := range testAddress {
t.Run(tc.Name, func(t *testing.T) {
var hexType HexAddress
var hexType HexType
hexbyte, err := hex.DecodeString(tc.Hexvalue[2:])
if err != nil {
t.Log("unable to decode values")
}
var hexArray [20]byte
copy(hexArray[:], hexbyte)
YamlHex, err := hexType.WrapHexAddress([20]byte(hexArray))
YamlHex, err := hexType.HexWrap.WrapHexAddress([20]byte(hexArray))
if err != nil {
t.Log("unable to generate yaml string")
}
Expand Down

0 comments on commit 0bf89a0

Please sign in to comment.