From 0bf89a08072245d02d92f00b097406332e811848 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Sat, 6 Jan 2024 07:50:43 +0100 Subject: [PATCH] adjusting hexaddress.go to tackle errors Signed-off-by: Philip-21 --- pkg/types/hex_address.go | 23 ++++++++++++++++++----- pkg/types/hex_address_test.go | 16 ++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/pkg/types/hex_address.go b/pkg/types/hex_address.go index b03d70f7..23ec1b69 100644 --- a/pkg/types/hex_address.go +++ b/pkg/types/hex_address.go @@ -23,14 +23,18 @@ 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 @@ -38,11 +42,20 @@ func (h *HexAddress) WrapHexAddress(addr [20]byte) (string, error) { 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 } diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go index e7ba9166..93cd88e9 100644 --- a/pkg/types/hex_address_test.go +++ b/pkg/types/hex_address_test.go @@ -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) @@ -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() @@ -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) @@ -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() @@ -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") }