|
1 | 1 | package common |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - |
6 | | - chainsel "github.com/smartcontractkit/chain-selectors" |
7 | | - cciptypes "github.com/smartcontractkit/chainlink-ccip/pkg/types/ccipocr3" |
| 4 | + cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3" |
8 | 5 | ) |
9 | 6 |
|
10 | | -var _ cciptypes.AddressCodec = &AddressCodec{} |
11 | | - |
12 | | -// AddressCodec is a struct that holds the chain specific address codecs and |
13 | | -// implements a superset of the cciptypes.AddressCodec interface. |
14 | | -type AddressCodec struct { |
15 | | - registeredAddressCodecMap map[string]ChainSpecificAddressCodec |
16 | | -} |
17 | | - |
18 | | -// NewAddressCodec is a constructor for NewAddressCodec |
19 | | -func NewAddressCodec(registeredMap map[string]ChainSpecificAddressCodec) AddressCodec { |
20 | | - return AddressCodec{ |
21 | | - registeredAddressCodecMap: registeredMap, |
22 | | - } |
23 | | -} |
24 | | - |
25 | | -// AddressBytesToString converts an address from bytes to string |
26 | | -func (ac AddressCodec) AddressBytesToString(addr cciptypes.UnknownAddress, chainSelector cciptypes.ChainSelector) (string, error) { |
27 | | - family, err := chainsel.GetSelectorFamily(uint64(chainSelector)) |
28 | | - if err != nil { |
29 | | - return "", fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err) |
30 | | - } |
31 | | - |
32 | | - codec, exist := ac.registeredAddressCodecMap[family] |
33 | | - if !exist { |
34 | | - return "", fmt.Errorf("unsupported family for address decode type %s", family) |
35 | | - } |
36 | | - |
37 | | - return codec.AddressBytesToString(addr) |
38 | | -} |
39 | | - |
40 | | -// TransmitterBytesToString converts a transmitter account from bytes to string |
41 | | -func (ac AddressCodec) TransmitterBytesToString(addr cciptypes.UnknownAddress, chainSelector cciptypes.ChainSelector) (string, error) { |
42 | | - family, err := chainsel.GetSelectorFamily(uint64(chainSelector)) |
43 | | - if err != nil { |
44 | | - return "", fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err) |
45 | | - } |
46 | | - |
47 | | - codec, exist := ac.registeredAddressCodecMap[family] |
48 | | - if !exist { |
49 | | - return "", fmt.Errorf("unsupported family for transmitter decode type %s", family) |
50 | | - } |
51 | | - |
52 | | - return codec.TransmitterBytesToString(addr) |
53 | | -} |
54 | | - |
55 | | -// AddressStringToBytes converts an address from string to bytes |
56 | | -func (ac AddressCodec) AddressStringToBytes(addr string, chainSelector cciptypes.ChainSelector) (cciptypes.UnknownAddress, error) { |
57 | | - family, err := chainsel.GetSelectorFamily(uint64(chainSelector)) |
58 | | - if err != nil { |
59 | | - return nil, fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err) |
60 | | - } |
61 | | - codec, exist := ac.registeredAddressCodecMap[family] |
62 | | - if !exist { |
63 | | - return nil, fmt.Errorf("unsupported family for address decode type %s", family) |
64 | | - } |
65 | | - |
66 | | - return codec.AddressStringToBytes(addr) |
67 | | -} |
68 | | - |
69 | | -// OracleIDAsAddressBytes returns valid address bytes for a given chain selector and oracle ID. |
70 | | -// Used for making nil transmitters in the OCR config valid, it just means that this oracle does not support the destination chain. |
71 | | -func (ac AddressCodec) OracleIDAsAddressBytes(oracleID uint8, chainSelector cciptypes.ChainSelector) ([]byte, error) { |
72 | | - family, err := chainsel.GetSelectorFamily(uint64(chainSelector)) |
73 | | - if err != nil { |
74 | | - return nil, fmt.Errorf("failed to get chain family for selector %d: %w", chainSelector, err) |
75 | | - } |
76 | | - codec, exist := ac.registeredAddressCodecMap[family] |
77 | | - if !exist { |
78 | | - return nil, fmt.Errorf("unsupported family for address decode type %s", family) |
79 | | - } |
80 | | - |
81 | | - return codec.OracleIDAsAddressBytes(oracleID) |
82 | | -} |
| 7 | +type AddressCodec = cciptypes.AddressCodecMap |
0 commit comments