-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BCFR-147][core] - Add codec chain agnostic modifier for converting b…
…yte array address to string (#14620) * most of iface tests working * fix codec tests * evm tests passing * renamings * cleanups * rename codec to commoncodec * bump common * bump common * lint and changeset * [Bot] Update changeset file with jira issues * chain agnostic modifier injected through relayer * common bump * addressing comments * bump common * compatibility with job spec * contract generate * bump common * inject modifier in runtime * bump common * bump common * general name * bump common * fix comment * bump common * fix common ref * update solana ref * Bump common refs * fix lint --------- Co-authored-by: app-token-issuer-infra-releng[bot] <120227048+app-token-issuer-infra-releng[bot]@users.noreply.github.com> Co-authored-by: ilija <pavlovicilija42@gmail.com>
- Loading branch information
1 parent
f3e66b2
commit 09145ba
Showing
19 changed files
with
243 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"chainlink": minor | ||
--- | ||
|
||
add address bytes to string modifier to chainReader. #internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
'@chainlink/contracts': minor | ||
--- | ||
|
||
add accountStr to chainReader test contract struct in order to test the new address bytes to string modifier. #internal | ||
|
||
|
||
PR issue: BCFR-147 | ||
|
||
Solidity Review issue: BCFR-957 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 26 additions & 24 deletions
50
core/gethwrappers/generated/chain_reader_tester/chain_reader_tester.go
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package codec | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
|
||
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" | ||
) | ||
|
||
// EVMAddressModifier implements the AddressModifier interface for Ethereum addresses. | ||
// It handles encoding and decoding Ethereum addresses with EIP-55 checksums and hex encoding. | ||
type EVMAddressModifier struct{} | ||
|
||
func (e EVMAddressModifier) EncodeAddress(bytes []byte) (string, error) { | ||
if len(bytes) != e.Length() { | ||
return "", fmt.Errorf("%w: got length %d, expected 20 for bytes %x", commontypes.ErrInvalidType, len(bytes), bytes) | ||
} | ||
|
||
return common.BytesToAddress(bytes).Hex(), nil | ||
} | ||
|
||
// DecodeAddress takes an EIP-55 encoded Ethereum address (e.g., "0x...") and decodes it to a 20-byte array. | ||
func (e EVMAddressModifier) DecodeAddress(str string) ([]byte, error) { | ||
str = strings.TrimPrefix(str, "0x") | ||
if len(str) != 40 { | ||
return nil, fmt.Errorf("%w: got length %d, expected 40 for address %s", commontypes.ErrInvalidType, len(str), str) | ||
} | ||
|
||
address := common.HexToAddress(str) | ||
if address == (common.Address{}) { | ||
return nil, fmt.Errorf("%w: address is zero", commontypes.ErrInvalidType) | ||
} | ||
|
||
return address.Bytes(), nil | ||
} | ||
|
||
// Length returns the expected length of an Ethereum address in bytes (20 bytes). | ||
func (e EVMAddressModifier) Length() int { | ||
return common.AddressLength | ||
} |
54 changes: 54 additions & 0 deletions
54
core/services/relay/evm/codec/byte_string_modifier_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package codec_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
commontypes "github.com/smartcontractkit/chainlink-common/pkg/types" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/codec" | ||
) | ||
|
||
func TestEVMAddressModifier(t *testing.T) { | ||
modifier := codec.EVMAddressModifier{} | ||
validAddressStr := "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4" | ||
validAddressBytes := common.HexToAddress(validAddressStr).Bytes() | ||
invalidLengthAddressStr := "0xabcdef1234567890abcdef" | ||
|
||
t.Run("EncodeAddress encodes valid Ethereum address bytes", func(t *testing.T) { | ||
encoded, err := modifier.EncodeAddress(validAddressBytes) | ||
require.NoError(t, err) | ||
assert.Equal(t, validAddressStr, encoded) | ||
}) | ||
|
||
t.Run("EncodeAddress returns error for invalid byte length", func(t *testing.T) { | ||
invalidBytes := []byte(invalidLengthAddressStr) | ||
_, err := modifier.EncodeAddress(invalidBytes) | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), commontypes.ErrInvalidType) | ||
}) | ||
|
||
t.Run("DecodeAddress decodes valid Ethereum address", func(t *testing.T) { | ||
decodedBytes, err := modifier.DecodeAddress(validAddressStr) | ||
require.NoError(t, err) | ||
assert.Equal(t, validAddressBytes, decodedBytes) | ||
}) | ||
|
||
t.Run("DecodeAddress returns error for invalid address length", func(t *testing.T) { | ||
_, err := modifier.DecodeAddress(invalidLengthAddressStr) | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), commontypes.ErrInvalidType) | ||
}) | ||
|
||
t.Run("DecodeAddress returns error for zero-value address", func(t *testing.T) { | ||
_, err := modifier.DecodeAddress(common.Address{}.Hex()) | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), commontypes.ErrInvalidType) | ||
}) | ||
|
||
t.Run("Length returns 20 for Ethereum addresses", func(t *testing.T) { | ||
assert.Equal(t, common.AddressLength, modifier.Length()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.