Skip to content

Commit

Permalink
ensuring all values are wrapped in 20 bytes type based on firefly signer
Browse files Browse the repository at this point in the history
Signed-off-by: Philip-21 <[email protected]>
  • Loading branch information
Philip-21 committed Feb 7, 2024
1 parent 3ff3af8 commit fedc52a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions pkg/types/hex_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/types/hex_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit fedc52a

Please sign in to comment.