From d1fe05db5272a993a9f40a2a26c6e7a19cde33db Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Tue, 19 Dec 2023 15:30:24 +0100 Subject: [PATCH 01/12] string formaters created Signed-off-by: Philip-21 --- pkg/types/hex_address.go | 16 ++++++++++++++-- pkg/types/hex_address_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 pkg/types/hex_address_test.go diff --git a/pkg/types/hex_address.go b/pkg/types/hex_address.go index 95ed86db..a46f3d8f 100644 --- a/pkg/types/hex_address.go +++ b/pkg/types/hex_address.go @@ -20,10 +20,22 @@ import "gopkg.in/yaml.v3" type HexAddress string +// Explicitly converts a string representation of a hex address to HexAddress type. +// This function assumes that hexStr is a valid hex address. +func ConvertToHexAddress(hexStr string) HexAddress { + return HexAddress(hexStr) +} + +//describe the HexValue type to be in string format +type MyType struct { + HexValue HexAddress `yaml:"hexvalue"` +} + // Explicitly quote hex addresses so that they are interpreted as string (not int) -func (h HexAddress) MarshalYAML() (interface{}, error) { +func (mt *MyType) MarshalYAML() (interface{}, error) { + hexAddr := ConvertToHexAddress(string(mt.HexValue)) return yaml.Node{ - Value: string(h), + Value: string(hexAddr), Kind: yaml.ScalarNode, Style: yaml.DoubleQuotedStyle, }, nil diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go new file mode 100644 index 00000000..373ae350 --- /dev/null +++ b/pkg/types/hex_address_test.go @@ -0,0 +1,32 @@ +package types + +import ( + "testing" +) + +func TestConvertToHexAddress(t *testing.T) { + tests := []struct { + Name string + Hexvalue string + }{ + { + Name: "TestCase1", + Hexvalue: "0x123abc", + }, + { + Name: "TestCase2", + Hexvalue: "0x549b5f43a40e1a0522864a004cfff2b0ca473a65", + }, + } + for _, tc := range tests { + t.Run(tc.Name, func(t *testing.T) { + expectedHexAddress := HexAddress(tc.Hexvalue) + result := ConvertToHexAddress(tc.Hexvalue) + + if result != expectedHexAddress { + t.Errorf("ConvertToHexAddress(%s) = %s; want %s", tc.Hexvalue, result, expectedHexAddress) + } + }) + + } +} From 9124ef8d042d2ce84c0c3e6691c426eca5e2c661 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Tue, 19 Dec 2023 16:45:12 +0100 Subject: [PATCH 02/12] hexaddress format implemented Signed-off-by: Philip-21 --- pkg/types/hex_address.go | 42 +++++++++++++++++++++++++++++------ pkg/types/hex_address_test.go | 34 ++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/pkg/types/hex_address.go b/pkg/types/hex_address.go index a46f3d8f..6f76406e 100644 --- a/pkg/types/hex_address.go +++ b/pkg/types/hex_address.go @@ -16,24 +16,52 @@ package types -import "gopkg.in/yaml.v3" +import ( + "fmt" + "regexp" + "strings" + + "gopkg.in/yaml.v3" +) type HexAddress string -// Explicitly converts a string representation of a hex address to HexAddress type. -// This function assumes that hexStr is a valid hex address. -func ConvertToHexAddress(hexStr string) HexAddress { - return HexAddress(hexStr) +// checks whether a string is a valid Hexaddress +func IsValidHex(s string) (HexAddress, error) { + //remove the 0x value if present + hexDigits := strings.TrimPrefix(s, "0x") + // Check if the remaining characters are valid hexadecimal digits + match, err := regexp.MatchString("^[0-9a-fA-F]+$", hexDigits) + if err != nil { + return "", fmt.Errorf("error in validating hex address: %v", err) + } + if match { + return HexAddress("0x" + hexDigits), nil + } else { + return "", fmt.Errorf("invalid hex address: %s", s) + } +} + +// Explicitly converts hex address to HexAddress type. +func ConvertToHexAddress(hexStr string) (HexAddress, error) { + validAddress, err := IsValidHex(hexStr) + if err != nil { + return "", err + } + return validAddress, nil } -//describe the HexValue type to be in string format +// describe the HexValue type to be in string format type MyType struct { HexValue HexAddress `yaml:"hexvalue"` } // Explicitly quote hex addresses so that they are interpreted as string (not int) func (mt *MyType) MarshalYAML() (interface{}, error) { - hexAddr := ConvertToHexAddress(string(mt.HexValue)) + hexAddr, err := ConvertToHexAddress(string(mt.HexValue)) + if err != nil { + return nil, err + } return yaml.Node{ Value: string(hexAddr), Kind: yaml.ScalarNode, diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go index 373ae350..7b045ca5 100644 --- a/pkg/types/hex_address_test.go +++ b/pkg/types/hex_address_test.go @@ -10,22 +10,42 @@ func TestConvertToHexAddress(t *testing.T) { Hexvalue string }{ { - Name: "TestCase1", + Name: "Case 24-Bits", Hexvalue: "0x123abc", }, { - Name: "TestCase2", + Name: "Case 32-Bits", + Hexvalue: "0xABCDEFFEDCBA9876543210ABCDEFFEDC", + }, + { + Name: "Case 64-Bits", + Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF", + }, + { + Name: "Case 128-Bits", + Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF", + }, + { + Name: "Case 152-Bits", Hexvalue: "0x549b5f43a40e1a0522864a004cfff2b0ca473a65", }, + { + Name: "Case 16-Bits", + Hexvalue: "0x1F2A", + }, + { + Name: "Case 65-Bits", + Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF6789ABCDEF0123456789ABCDEF012345", + }, } for _, tc := range tests { t.Run(tc.Name, func(t *testing.T) { - expectedHexAddress := HexAddress(tc.Hexvalue) - result := ConvertToHexAddress(tc.Hexvalue) - - if result != expectedHexAddress { - t.Errorf("ConvertToHexAddress(%s) = %s; want %s", tc.Hexvalue, result, expectedHexAddress) + result, err := ConvertToHexAddress(tc.Hexvalue) + if err != nil { + t.Log("error in generating result", err) } + t.Log(result) + }) } From d48c83eefb2aea426254a92d63fd4ed8176ac929 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Sat, 23 Dec 2023 19:22:02 +0100 Subject: [PATCH 03/12] adjust codes to use firefly-signer library Signed-off-by: Philip-21 --- pkg/types/hex_address.go | 42 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/pkg/types/hex_address.go b/pkg/types/hex_address.go index 6f76406e..b03d70f7 100644 --- a/pkg/types/hex_address.go +++ b/pkg/types/hex_address.go @@ -17,53 +17,37 @@ package types import ( - "fmt" - "regexp" - "strings" + "encoding/hex" + "github.com/hyperledger/firefly-signer/pkg/ethtypes" "gopkg.in/yaml.v3" ) -type HexAddress string - -// checks whether a string is a valid Hexaddress -func IsValidHex(s string) (HexAddress, error) { - //remove the 0x value if present - hexDigits := strings.TrimPrefix(s, "0x") - // Check if the remaining characters are valid hexadecimal digits - match, err := regexp.MatchString("^[0-9a-fA-F]+$", hexDigits) - if err != nil { - return "", fmt.Errorf("error in validating hex address: %v", err) - } - if match { - return HexAddress("0x" + hexDigits), nil - } else { - return "", fmt.Errorf("invalid hex address: %s", s) - } +type HexAddress struct { + ethtypes.Address0xHex } -// Explicitly converts hex address to HexAddress type. -func ConvertToHexAddress(hexStr string) (HexAddress, error) { - validAddress, err := IsValidHex(hexStr) - if err != nil { +// WrapHexAddress wraps a hex address as HexAddress +func (h *HexAddress) WrapHexAddress(addr [20]byte) (string, error) { + hexStr := "0x" + hex.EncodeToString(addr[:]) + if err := h.SetString(hexStr); err != nil { return "", err } - return validAddress, nil + return hexStr, nil } -// describe the HexValue type to be in string format -type MyType struct { +type HexType struct { HexValue HexAddress `yaml:"hexvalue"` } // Explicitly quote hex addresses so that they are interpreted as string (not int) -func (mt *MyType) MarshalYAML() (interface{}, error) { - hexAddr, err := ConvertToHexAddress(string(mt.HexValue)) +func (ht *HexType) MarshalYAML() (interface{}, error) { + hexAddr, err := ht.HexValue.WrapHexAddress(ht.HexValue.Address0xHex) if err != nil { return nil, err } return yaml.Node{ - Value: string(hexAddr), + Value: hexAddr, Kind: yaml.ScalarNode, Style: yaml.DoubleQuotedStyle, }, nil From 132346c8fcc5b7ac35f9c52e16d7843dbb5bdb6a Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Sat, 23 Dec 2023 19:22:43 +0100 Subject: [PATCH 04/12] testcases Signed-off-by: Philip-21 --- pkg/types/hex_address_test.go | 79 ++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 11 deletions(-) diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go index 7b045ca5..b92b854d 100644 --- a/pkg/types/hex_address_test.go +++ b/pkg/types/hex_address_test.go @@ -1,29 +1,32 @@ package types import ( + "encoding/hex" "testing" + + "github.com/stretchr/testify/assert" ) -func TestConvertToHexAddress(t *testing.T) { +func TestWrapHexAddress(t *testing.T) { tests := []struct { Name string - Hexvalue string + Hexvalue string `yaml:"hexvalue"` }{ { Name: "Case 24-Bits", - Hexvalue: "0x123abc", + Hexvalue: "0x123abc0000000000000000000000000000000000", }, { Name: "Case 32-Bits", - Hexvalue: "0xABCDEFFEDCBA9876543210ABCDEFFEDC", + Hexvalue: "0xabcdeffedcba9876543210abcdeffedc00000000", }, { Name: "Case 64-Bits", - Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF", + Hexvalue: "0x1234567890abcdef012345670000000000000000", }, { Name: "Case 128-Bits", - Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF", + Hexvalue: "0x1234567890abcdef0123456789abcdef00000000", }, { Name: "Case 152-Bits", @@ -31,22 +34,76 @@ func TestConvertToHexAddress(t *testing.T) { }, { Name: "Case 16-Bits", - Hexvalue: "0x1F2A", + Hexvalue: "0x1f2a000000000000000000000000000000000000", }, { Name: "Case 65-Bits", - Hexvalue: "0x1234567890ABCDEF0123456789ABCDEF6789ABCDEF0123456789ABCDEF012345", + Hexvalue: "0x1234567890abcdef0123456789abcdef6789abcd", }, } for _, tc := range tests { t.Run(tc.Name, func(t *testing.T) { - result, err := ConvertToHexAddress(tc.Hexvalue) + var hexType HexAddress + hexBytes, err := hex.DecodeString(tc.Hexvalue[2:]) + if err != nil { + t.Log("Unable to decode values:", err) + } + if len(hexBytes) != 20 { + t.Fatalf("expected 20 bytes, got %d bytes", len(hexBytes)) + } + // Copy bytes to a fixed-size array + var hexArray [20]byte + copy(hexArray[:], hexBytes) + result, err := hexType.WrapHexAddress([20]byte(hexArray)) if err != nil { t.Log("error in generating result", err) + t.Fail() + return } - t.Log(result) - + assert.Equal(t, tc.Hexvalue, result) }) } } + +//Wrong values to test the accuracy of the WrapHexAddress() +func TestFailWrapHexAddress(t *testing.T) { + testData := []struct { + Name string + HexValue string `yaml:"hexvalue"` + }{ + { + Name: "case-1", + HexValue: "px123abc0000000000000000000000000000000000", + }, + { + Name: "case-2", + HexValue: "A0234567890abcdef0123456789abcdef00000000", + }, + { + Name: "case-3", + HexValue: "06942dc1fC868aF18132C0916dA3ae4ab58142a4", + }, + } + for _, tc := range testData { + t.Run(tc.Name, func(t *testing.T) { + var hexType HexAddress + hexBytes, err := hex.DecodeString(tc.HexValue[2:]) + if err != nil { + t.Log("unable to decode hexvalue:", err) + } + if len(hexBytes) != 20 { + t.Fatalf("expected 20 bytes, but got %d bytes", len(hexBytes)) + } + var hexArray [20]byte + copy(hexArray[:], hexBytes) + result, err := hexType.WrapHexAddress([20]byte(hexArray)) + if err != nil { + t.Log("error in generating result", err) + t.Fail() + return + } + assert.Equal(t, tc.HexValue, result) + }) + } +} From b4b28cd3ec26e56718d8398e709244bb0b659d08 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Mon, 25 Dec 2023 21:04:16 +0100 Subject: [PATCH 05/12] testcases Signed-off-by: Philip-21 --- pkg/types/hex_address_test.go | 69 ++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go index b92b854d..fb944171 100644 --- a/pkg/types/hex_address_test.go +++ b/pkg/types/hex_address_test.go @@ -4,7 +4,9 @@ import ( "encoding/hex" "testing" + "github.com/hyperledger/firefly-signer/pkg/ethtypes" "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" ) func TestWrapHexAddress(t *testing.T) { @@ -12,6 +14,10 @@ func TestWrapHexAddress(t *testing.T) { Name string Hexvalue string `yaml:"hexvalue"` }{ + { + Name: "Case 16-Bits", + Hexvalue: "0x1f2a000000000000000000000000000000000000", + }, { Name: "Case 24-Bits", Hexvalue: "0x123abc0000000000000000000000000000000000", @@ -24,6 +30,10 @@ func TestWrapHexAddress(t *testing.T) { Name: "Case 64-Bits", Hexvalue: "0x1234567890abcdef012345670000000000000000", }, + { + Name: "Case 65-Bits", + Hexvalue: "0x1234567890abcdef0123456789abcdef6789abcd", + }, { Name: "Case 128-Bits", Hexvalue: "0x1234567890abcdef0123456789abcdef00000000", @@ -32,14 +42,6 @@ func TestWrapHexAddress(t *testing.T) { Name: "Case 152-Bits", Hexvalue: "0x549b5f43a40e1a0522864a004cfff2b0ca473a65", }, - { - Name: "Case 16-Bits", - Hexvalue: "0x1f2a000000000000000000000000000000000000", - }, - { - Name: "Case 65-Bits", - Hexvalue: "0x1234567890abcdef0123456789abcdef6789abcd", - }, } for _, tc := range tests { t.Run(tc.Name, func(t *testing.T) { @@ -66,7 +68,7 @@ func TestWrapHexAddress(t *testing.T) { } } -//Wrong values to test the accuracy of the WrapHexAddress() +// Wrong values to test the accuracy of the WrapHexAddress() func TestFailWrapHexAddress(t *testing.T) { testData := []struct { Name string @@ -107,3 +109,52 @@ func TestFailWrapHexAddress(t *testing.T) { }) } } + +type HexAddr struct { + ethtypes.Address0xHex +} + +func TestYamlMarshall(t *testing.T) { + testAddress := []struct { + Name string + Hexvalue string `yaml:"hexvalue"` + }{ + { + Name: "Case 16-Bits", + Hexvalue: "0x1f2a000000000000000000000000000000000000", + }, + { + Name: "Case 24-Bits", + Hexvalue: "0x123abc0000000000000000000000000000000000", + }, + { + Name: "Case 32-Bits", + Hexvalue: "0xabcdeffedcba9876543210abcdeffedc00000000", + }, + { + Name: "Case 64-Bits", + Hexvalue: "0x1234567890abcdef012345670000000000000000", + }, + } + for _, tc := range testAddress { + t.Run(tc.Name, func(t *testing.T) { + var hexType HexAddress + 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)) + if err != nil { + t.Log("unable to generate yaml string") + } + + YamlNode := yaml.Node{ + Value: YamlHex, + } + assert.YAMLEq(t, YamlNode.Value, YamlHex) + }) + + } +} From 96ec76fbc9252d15cb4d1817c65ef92c723e07a5 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Mon, 25 Dec 2023 21:06:55 +0100 Subject: [PATCH 06/12] testcases Signed-off-by: Philip-21 --- pkg/types/hex_address_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go index fb944171..e7ba9166 100644 --- a/pkg/types/hex_address_test.go +++ b/pkg/types/hex_address_test.go @@ -114,7 +114,7 @@ type HexAddr struct { ethtypes.Address0xHex } -func TestYamlMarshall(t *testing.T) { +func TestYamlMarshal(t *testing.T) { testAddress := []struct { Name string Hexvalue string `yaml:"hexvalue"` From 0bf89a08072245d02d92f00b097406332e811848 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Sat, 6 Jan 2024 07:50:43 +0100 Subject: [PATCH 07/12] 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") } From 0dc60960b88229001565bac76d2216c0e3c72f09 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Thu, 11 Jan 2024 09:38:57 +0100 Subject: [PATCH 08/12] remove failing tests Signed-off-by: Philip-21 --- pkg/types/hex_address_test.go | 42 ----------------------------------- 1 file changed, 42 deletions(-) diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go index 93cd88e9..30b9bfaa 100644 --- a/pkg/types/hex_address_test.go +++ b/pkg/types/hex_address_test.go @@ -72,48 +72,6 @@ func TestWrapHexAddress(t *testing.T) { } } -// Wrong values to test the accuracy of the WrapHexAddress() -func TestFailWrapHexAddress(t *testing.T) { - testData := []struct { - Name string - HexValue string `yaml:"hexvalue"` - }{ - { - Name: "case-1", - HexValue: "px123abc0000000000000000000000000000000000", - }, - { - Name: "case-2", - HexValue: "A0234567890abcdef0123456789abcdef00000000", - }, - { - Name: "case-3", - HexValue: "06942dc1fC868aF18132C0916dA3ae4ab58142a4", - }, - } - for _, tc := range testData { - t.Run(tc.Name, func(t *testing.T) { - var hexType HexType - hexBytes, err := hex.DecodeString(tc.HexValue[2:]) - if err != nil { - t.Log("unable to decode hexvalue:", err) - } - if len(hexBytes) != 20 { - t.Fatalf("expected 20 bytes, but got %d bytes", len(hexBytes)) - } - var hexArray [20]byte - copy(hexArray[:], hexBytes) - result, err := hexType.HexWrap.WrapHexAddress([20]byte(hexArray)) - if err != nil { - t.Log("error in generating result", err) - t.Fail() - return - } - assert.Equal(t, tc.HexValue, result) - }) - } -} - type HexAddr struct { ethtypes.Address0xHex } From 813c8267fffbc11997fb6ff3e13b58a0e6a7f3ba Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Tue, 6 Feb 2024 15:45:23 +0100 Subject: [PATCH 09/12] changes Signed-off-by: Philip-21 --- pkg/types/hex_address.go | 6 +++--- pkg/types/hex_address_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/types/hex_address.go b/pkg/types/hex_address.go index 23ec1b69..5d445a0b 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 [20]byte) (string, error) { +func (h *HexWrapper) WrapHexAddress(addr []byte) (string, error) { hexStr := "0x" + hex.EncodeToString(addr[:]) // Initialize addrStr before using it h.addrStr = new(ethtypes.Address0xHex) @@ -53,9 +53,9 @@ func (ht *HexType) MarshalYAML() (interface{}, error) { return nil, err } //copy bytes to fixed array - var hexArray [20]byte + var hexArray []byte copy(hexArray[:], hexBytes) - hexAddr, err := ht.HexWrap.WrapHexAddress([20]byte(hexArray)) + hexAddr, err := ht.HexWrap.WrapHexAddress([]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 30b9bfaa..71e26783 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 [20]byte + var hexArray []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([20]byte(hexArray)) + result, err := hexType.HexWrap.WrapHexAddress([]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 [20]byte + var hexArray []byte copy(hexArray[:], hexbyte) - YamlHex, err := hexType.HexWrap.WrapHexAddress([20]byte(hexArray)) + YamlHex, err := hexType.HexWrap.WrapHexAddress([]byte(hexArray)) if err != nil { t.Log("unable to generate yaml string") } From 678efc415e65fca37e4939f51b844147a4d06074 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Tue, 6 Feb 2024 18:59:14 +0100 Subject: [PATCH 10/12] changes Signed-off-by: Philip-21 --- pkg/types/hex_address.go | 10 +-- pkg/types/hex_address_test.go | 122 ---------------------------------- 2 files changed, 1 insertion(+), 131 deletions(-) delete mode 100644 pkg/types/hex_address_test.go diff --git a/pkg/types/hex_address.go b/pkg/types/hex_address.go index 5d445a0b..73b0c509 100644 --- a/pkg/types/hex_address.go +++ b/pkg/types/hex_address.go @@ -47,15 +47,7 @@ type HexType struct { // Explicitly quote hex addresses so that they are interpreted as string (not int) func (ht *HexType) MarshalYAML() (interface{}, error) { - //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 []byte - copy(hexArray[:], hexBytes) - hexAddr, err := ht.HexWrap.WrapHexAddress([]byte(hexArray)) + hexAddr, err := ht.HexWrap.WrapHexAddress([]byte(ht.HexValue)) if err != nil { return nil, err } diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go deleted file mode 100644 index 71e26783..00000000 --- a/pkg/types/hex_address_test.go +++ /dev/null @@ -1,122 +0,0 @@ -package types - -import ( - "encoding/hex" - "testing" - - "github.com/hyperledger/firefly-signer/pkg/ethtypes" - "github.com/stretchr/testify/assert" - "gopkg.in/yaml.v3" -) - -func TestWrapHexAddress(t *testing.T) { - tests := []struct { - Name string - Hexvalue string `yaml:"hexvalue"` - }{ - { - Name: "Case 16-Bits", - Hexvalue: "0x1f2a000000000000000000000000000000000000", - }, - { - Name: "Case 24-Bits", - Hexvalue: "0x123abc0000000000000000000000000000000000", - }, - { - Name: "Case 32-Bits", - Hexvalue: "0xabcdeffedcba9876543210abcdeffedc00000000", - }, - { - Name: "Case 64-Bits", - Hexvalue: "0x1234567890abcdef012345670000000000000000", - }, - { - Name: "Case 65-Bits", - Hexvalue: "0x1234567890abcdef0123456789abcdef6789abcd", - }, - { - Name: "Case 128-Bits", - Hexvalue: "0x1234567890abcdef0123456789abcdef00000000", - }, - { - Name: "Case 152-Bits", - Hexvalue: "0x549b5f43a40e1a0522864a004cfff2b0ca473a65", - }, - } - for _, tc := range tests { - t.Run(tc.Name, func(t *testing.T) { - 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) - } - if len(hexBytes) != 20 { - t.Fatalf("expected 20 bytes, got %d bytes", len(hexBytes)) - } - // Copy bytes to a fixed-size array - var hexArray []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)) - if err != nil { - t.Log("error in generating result", err) - t.Fail() - return - } - assert.Equal(t, tc.Hexvalue, result) - }) - - } -} - -type HexAddr struct { - ethtypes.Address0xHex -} - -func TestYamlMarshal(t *testing.T) { - testAddress := []struct { - Name string - Hexvalue string `yaml:"hexvalue"` - }{ - { - Name: "Case 16-Bits", - Hexvalue: "0x1f2a000000000000000000000000000000000000", - }, - { - Name: "Case 24-Bits", - Hexvalue: "0x123abc0000000000000000000000000000000000", - }, - { - Name: "Case 32-Bits", - Hexvalue: "0xabcdeffedcba9876543210abcdeffedc00000000", - }, - { - Name: "Case 64-Bits", - Hexvalue: "0x1234567890abcdef012345670000000000000000", - }, - } - for _, tc := range testAddress { - t.Run(tc.Name, func(t *testing.T) { - var hexType HexType - hexbyte, err := hex.DecodeString(tc.Hexvalue[2:]) - if err != nil { - t.Log("unable to decode values") - } - var hexArray []byte - copy(hexArray[:], hexbyte) - YamlHex, err := hexType.HexWrap.WrapHexAddress([]byte(hexArray)) - if err != nil { - t.Log("unable to generate yaml string") - } - - YamlNode := yaml.Node{ - Value: YamlHex, - } - assert.YAMLEq(t, YamlNode.Value, YamlHex) - }) - - } -} From 3ff3af8bab19a54412727c3bb6a64186007f1419 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Wed, 7 Feb 2024 04:47:04 +0100 Subject: [PATCH 11/12] tests Signed-off-by: Philip-21 --- pkg/types/hex_address_test.go | 122 ++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 pkg/types/hex_address_test.go diff --git a/pkg/types/hex_address_test.go b/pkg/types/hex_address_test.go new file mode 100644 index 00000000..71e26783 --- /dev/null +++ b/pkg/types/hex_address_test.go @@ -0,0 +1,122 @@ +package types + +import ( + "encoding/hex" + "testing" + + "github.com/hyperledger/firefly-signer/pkg/ethtypes" + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" +) + +func TestWrapHexAddress(t *testing.T) { + tests := []struct { + Name string + Hexvalue string `yaml:"hexvalue"` + }{ + { + Name: "Case 16-Bits", + Hexvalue: "0x1f2a000000000000000000000000000000000000", + }, + { + Name: "Case 24-Bits", + Hexvalue: "0x123abc0000000000000000000000000000000000", + }, + { + Name: "Case 32-Bits", + Hexvalue: "0xabcdeffedcba9876543210abcdeffedc00000000", + }, + { + Name: "Case 64-Bits", + Hexvalue: "0x1234567890abcdef012345670000000000000000", + }, + { + Name: "Case 65-Bits", + Hexvalue: "0x1234567890abcdef0123456789abcdef6789abcd", + }, + { + Name: "Case 128-Bits", + Hexvalue: "0x1234567890abcdef0123456789abcdef00000000", + }, + { + Name: "Case 152-Bits", + Hexvalue: "0x549b5f43a40e1a0522864a004cfff2b0ca473a65", + }, + } + for _, tc := range tests { + t.Run(tc.Name, func(t *testing.T) { + 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) + } + if len(hexBytes) != 20 { + t.Fatalf("expected 20 bytes, got %d bytes", len(hexBytes)) + } + // Copy bytes to a fixed-size array + var hexArray []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)) + if err != nil { + t.Log("error in generating result", err) + t.Fail() + return + } + assert.Equal(t, tc.Hexvalue, result) + }) + + } +} + +type HexAddr struct { + ethtypes.Address0xHex +} + +func TestYamlMarshal(t *testing.T) { + testAddress := []struct { + Name string + Hexvalue string `yaml:"hexvalue"` + }{ + { + Name: "Case 16-Bits", + Hexvalue: "0x1f2a000000000000000000000000000000000000", + }, + { + Name: "Case 24-Bits", + Hexvalue: "0x123abc0000000000000000000000000000000000", + }, + { + Name: "Case 32-Bits", + Hexvalue: "0xabcdeffedcba9876543210abcdeffedc00000000", + }, + { + Name: "Case 64-Bits", + Hexvalue: "0x1234567890abcdef012345670000000000000000", + }, + } + for _, tc := range testAddress { + t.Run(tc.Name, func(t *testing.T) { + var hexType HexType + hexbyte, err := hex.DecodeString(tc.Hexvalue[2:]) + if err != nil { + t.Log("unable to decode values") + } + var hexArray []byte + copy(hexArray[:], hexbyte) + YamlHex, err := hexType.HexWrap.WrapHexAddress([]byte(hexArray)) + if err != nil { + t.Log("unable to generate yaml string") + } + + YamlNode := yaml.Node{ + Value: YamlHex, + } + assert.YAMLEq(t, YamlNode.Value, YamlHex) + }) + + } +} From fedc52a6a51ceec6b2be6affb9b5ed496e477811 Mon Sep 17 00:00:00 2001 From: Philip-21 Date: Wed, 7 Feb 2024 05:12:55 +0100 Subject: [PATCH 12/12] 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") }