Skip to content

Commit 06d4446

Browse files
committed
move AddressCodec interfaces to cl-common
1 parent 8f7d9e5 commit 06d4446

2 files changed

Lines changed: 98 additions & 27 deletions

File tree

pkg/types/ccipocr3/interfaces.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,40 @@ type AddressCodec interface {
2525
AddressStringToBytes(string, ChainSelector) (UnknownAddress, error)
2626
}
2727

28+
type AddressCodecBundle interface {
29+
AddressBytesToString(addr UnknownAddress, chainSelector ChainSelector) (string, error)
30+
AddressStringToBytes(addr string, chainSelector ChainSelector) (UnknownAddress, error)
31+
TransmitterBytesToString(addr UnknownAddress, chainSelector ChainSelector) (string, error)
32+
OracleIDAsAddressBytes(oracleID uint8, chainSelector ChainSelector) ([]byte, error)
33+
}
34+
35+
// ChainSpecificAddressCodec is an interface that defines the methods for encoding and decoding addresses for a specific chain
36+
type ChainSpecificAddressCodec interface {
37+
// AddressBytesToString converts an address from bytes to string
38+
AddressBytesToString([]byte) (string, error)
39+
// AddressStringToBytes converts an address from string to bytes
40+
AddressStringToBytes(string) ([]byte, error)
41+
// OracleIDAsAddressBytes returns a valid address for this chain family with the bytes set to the given oracle ID.
42+
OracleIDAsAddressBytes(oracleID uint8) ([]byte, error)
43+
// TransmitterBytesToString converts a transmitter account from bytes to string
44+
TransmitterBytesToString([]byte) (string, error)
45+
}
46+
47+
// ExtraDataCodecBundle is an interface that defines methods for decoding extra args and dest exec data.
48+
type ExtraDataCodecBundle interface {
49+
DecodeExtraArgs(extraArgs Bytes, sourceChainSelector ChainSelector) (map[string]any, error)
50+
DecodeTokenAmountDestExecData(destExecData Bytes, sourceChainSelector ChainSelector) (map[string]any, error)
51+
}
52+
53+
// SourceChainExtraDataCodec is an interface for decoding source chain specific extra args and dest exec data into a map[string]any representation for a specific chain
54+
// For chain A to chain B message, this interface will be the chain A specific codec
55+
type SourceChainExtraDataCodec interface {
56+
// DecodeExtraArgsToMap reformat bytes into a chain agnostic map[string]any representation for extra args
57+
DecodeExtraArgsToMap(extraArgs Bytes) (map[string]any, error)
58+
// DecodeDestExecDataToMap reformat bytes into a chain agnostic map[string]interface{} representation for dest exec data
59+
DecodeDestExecDataToMap(destExecData Bytes) (map[string]any, error)
60+
}
61+
2862
// RMNCrypto provides a chain-agnostic interface for verifying RMN signatures.
2963
// For example, on EVM, RMN reports are abi-encoded prior to being signed.
3064
// On Solana, they would be borsh encoded instead, etc.

pkg/types/ccipocr3/plugincodec.go

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,6 @@ type Codec struct {
1515
SourceChainExtraDataCodec
1616
}
1717

18-
// ChainSpecificAddressCodec is an interface that defines the methods for encoding and decoding addresses for a specific chain
19-
type ChainSpecificAddressCodec interface {
20-
// AddressBytesToString converts an address from bytes to string
21-
AddressBytesToString([]byte) (string, error)
22-
// AddressStringToBytes converts an address from string to bytes
23-
AddressStringToBytes(string) ([]byte, error)
24-
// OracleIDAsAddressBytes returns a valid address for this chain family with the bytes set to the given oracle ID.
25-
OracleIDAsAddressBytes(oracleID uint8) ([]byte, error)
26-
// TransmitterBytesToString converts a transmitter account from bytes to string
27-
TransmitterBytesToString([]byte) (string, error)
28-
}
29-
30-
// SourceChainExtraDataCodec is an interface for decoding source chain specific extra args and dest exec data into a map[string]any representation for a specific chain
31-
// For chain A to chain B message, this interface will be the chain A specific codec
32-
type SourceChainExtraDataCodec interface {
33-
// DecodeExtraArgsToMap reformat bytes into a chain agnostic map[string]any representation for extra args
34-
DecodeExtraArgsToMap(extraArgs Bytes) (map[string]any, error)
35-
// DecodeDestExecDataToMap reformat bytes into a chain agnostic map[string]interface{} representation for dest exec data
36-
DecodeDestExecDataToMap(destExecData Bytes) (map[string]any, error)
37-
}
38-
39-
// ExtraDataCodecBundle is an interface that defines methods for decoding extra args and dest exec data.
40-
type ExtraDataCodecBundle interface {
41-
DecodeExtraArgs(extraArgs Bytes, sourceChainSelector ChainSelector) (map[string]any, error)
42-
DecodeTokenAmountDestExecData(destExecData Bytes, sourceChainSelector ChainSelector) (map[string]any, error)
43-
}
44-
4518
// ExtraDataCodecMap is a map of chain family to SourceChainExtraDataCodec
4619
type ExtraDataCodecMap map[string]SourceChainExtraDataCodec
4720

@@ -86,3 +59,67 @@ func (c ExtraDataCodecMap) DecodeTokenAmountDestExecData(destExecData Bytes, sou
8659

8760
return codec.DecodeDestExecDataToMap(destExecData)
8861
}
62+
63+
// AddressCodecMap is a map of chain family to ChainSpecificAddressCodec
64+
type AddressCodecMap map[string]ChainSpecificAddressCodec
65+
66+
var _ AddressCodecBundle = AddressCodecMap{}
67+
68+
// AddressBytesToString converts an address from bytes to string
69+
func (ac AddressCodecMap) AddressBytesToString(addr UnknownAddress, chainSelector ChainSelector) (string, error) {
70+
family, err := chainsel.GetSelectorFamily(uint64(chainSelector))
71+
if err != nil {
72+
return "", fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err)
73+
}
74+
75+
codec, exist := ac[family]
76+
if !exist {
77+
return "", fmt.Errorf("unsupported family for address decode type %s", family)
78+
}
79+
80+
return codec.AddressBytesToString(addr)
81+
}
82+
83+
// TransmitterBytesToString converts a transmitter account from bytes to string
84+
func (ac AddressCodecMap) TransmitterBytesToString(addr UnknownAddress, chainSelector ChainSelector) (string, error) {
85+
family, err := chainsel.GetSelectorFamily(uint64(chainSelector))
86+
if err != nil {
87+
return "", fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err)
88+
}
89+
90+
codec, exist := ac[family]
91+
if !exist {
92+
return "", fmt.Errorf("unsupported family for transmitter decode type %s", family)
93+
}
94+
95+
return codec.TransmitterBytesToString(addr)
96+
}
97+
98+
// AddressStringToBytes converts an address from string to bytes
99+
func (ac AddressCodecMap) AddressStringToBytes(addr string, chainSelector ChainSelector) (UnknownAddress, error) {
100+
family, err := chainsel.GetSelectorFamily(uint64(chainSelector))
101+
if err != nil {
102+
return nil, fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err)
103+
}
104+
codec, exist := ac[family]
105+
if !exist {
106+
return nil, fmt.Errorf("unsupported family for address decode type %s", family)
107+
}
108+
109+
return codec.AddressStringToBytes(addr)
110+
}
111+
112+
// OracleIDAsAddressBytes returns valid address bytes for a given chain selector and oracle ID.
113+
// Used for making nil transmitters in the OCR config valid, it just means that this oracle does not support the destination chain.
114+
func (ac AddressCodecMap) OracleIDAsAddressBytes(oracleID uint8, chainSelector ChainSelector) ([]byte, error) {
115+
family, err := chainsel.GetSelectorFamily(uint64(chainSelector))
116+
if err != nil {
117+
return nil, fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err)
118+
}
119+
codec, exist := ac[family]
120+
if !exist {
121+
return nil, fmt.Errorf("unsupported family for address decode type %s", family)
122+
}
123+
124+
return codec.OracleIDAsAddressBytes(oracleID)
125+
}

0 commit comments

Comments
 (0)