From fedc52a6a51ceec6b2be6affb9b5ed496e477811 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Wed, 7 Feb 2024 05:12:55 +0100 Subject: [PATCH] ensuring all values are wrapped in 20 bytes type based on firefly signer Signed-off-by: Philip-21 --- pkg/types/hex_address.go | 12 ++++++++++-- pkg/types/hex_address_test.go | 8 ++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pkg/types/hex_address.go b/pkg/types/hex_address.go index 73b0c509..23ec1b69 100644 --- a/pkg/types/hex_address.go +++ b/pkg/types/hex_address.go @@ -30,7 +30,7 @@ type HexWrapper struct { } // WrapHexAddress wraps a hex address as HexAddress -func (h *HexWrapper) WrapHexAddress(addr []byte) (string, error) { +func (h *HexWrapper) WrapHexAddress(addr [20]byte) (string, error) { hexStr := "0x" + hex.EncodeToString(addr[:]) // Initialize addrStr before using it h.addrStr = new(ethtypes.Address0xHex) @@ -47,7 +47,15 @@ type HexType struct { // Explicitly quote hex addresses so that they are interpreted as string (not int) func (ht *HexType) MarshalYAML() (interface{}, error) { - hexAddr, err := ht.HexWrap.WrapHexAddress([]byte(ht.HexValue)) + //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 71e26783..30b9bfaa 100644 --- a/pkg/types/hex_address_test.go +++ b/pkg/types/hex_address_test.go @@ -56,11 +56,11 @@ func TestWrapHexAddress(t *testing.T) { t.Fatalf("expected 20 bytes, got %d bytes", len(hexBytes)) } // Copy bytes to a fixed-size array - var hexArray []byte + var hexArray [20]byte copy(hexArray[:], hexBytes) //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([]byte(hexArray)) + result, err := hexType.HexWrap.WrapHexAddress([20]byte(hexArray)) if err != nil { t.Log("error in generating result", err) t.Fail() @@ -105,9 +105,9 @@ func TestYamlMarshal(t *testing.T) { if err != nil { t.Log("unable to decode values") } - var hexArray []byte + var hexArray [20]byte copy(hexArray[:], hexbyte) - YamlHex, err := hexType.HexWrap.WrapHexAddress([]byte(hexArray)) + YamlHex, err := hexType.HexWrap.WrapHexAddress([20]byte(hexArray)) if err != nil { t.Log("unable to generate yaml string") }