|
| 1 | +package changesets |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "fmt" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + |
| 9 | + gethcommon "github.com/ethereum/go-ethereum/common" |
| 10 | + gethcrypto "github.com/ethereum/go-ethereum/crypto" |
| 11 | + |
| 12 | + chainsel "github.com/smartcontractkit/chain-selectors" |
| 13 | +) |
| 14 | + |
| 15 | +// This mirrors the cross-family signer translation in |
| 16 | +// chainlink-ccip/deployment/v2_0_0/changesets/lane_signing_helpers.go (added in #2169 |
| 17 | +// "fix cross-family signer lookup"). That change fixed the *forward* path — deriving a |
| 18 | +// NOP's signer address for a destination family when the lane changeset writes it |
| 19 | +// on-chain. This is the *inverse* path: when reconstructing committee membership from |
| 20 | +// on-chain state (CommitteeInputFromState -> NOPIdentities.AliasForSigner), an on-chain |
| 21 | +// signer recorded in the verifier chain's family (e.g. a Canton verifier's EVM address on |
| 22 | +// an EVM committee verifier) must resolve back to the NOP even though JD only knows that |
| 23 | +// NOP under its own family (canton). Without the same translation the reconstruction |
| 24 | +// fails with "on-chain signer ... has no JD-known NOP". |
| 25 | + |
| 26 | +// rawPubKeySourceFamily is a synthetic "family" tag used only as a translation source, |
| 27 | +// standing for a NOP's raw public key fetched directly from JD rather than derived from |
| 28 | +// any one chain family's own address representation. It is never a real chain family and |
| 29 | +// must never be passed as a translation target. |
| 30 | +const rawPubKeySourceFamily = "xxx_notarealfamily_raw_pubkey" |
| 31 | + |
| 32 | +// addressClassFamilies are chain families whose signer identity is a 20-byte address |
| 33 | +// derived via secp256k1 -> keccak256 (EVM-style). Members encode identical bytes and |
| 34 | +// differ only in string formatting, so they are freely interconvertible in both |
| 35 | +// directions. |
| 36 | +var addressClassFamilies = map[string]bool{ |
| 37 | + chainsel.FamilyEVM: true, |
| 38 | + chainsel.FamilySolana: true, |
| 39 | +} |
| 40 | + |
| 41 | +// rawPubKeyClassFamilies are chain families (plus the synthetic rawPubKeySourceFamily) |
| 42 | +// whose signer identity is the full uncompressed secp256k1 public key, hex-encoded with |
| 43 | +// no per-family formatting differences. Members are interchangeable as-is. |
| 44 | +var rawPubKeyClassFamilies = map[string]bool{ |
| 45 | + chainsel.FamilyAptos: true, |
| 46 | + chainsel.FamilyStellar: true, |
| 47 | + chainsel.FamilyCanton: true, |
| 48 | + rawPubKeySourceFamily: true, |
| 49 | +} |
| 50 | + |
| 51 | +// translatableSignerFamilies is the set of real chain families the inverse index is |
| 52 | +// populated for. When a NOP has a signer identity known for one family, its address in |
| 53 | +// every other family in this set that can be derived is precomputed, so AliasForSigner |
| 54 | +// resolves an on-chain signer regardless of which family it was recorded in. |
| 55 | +var translatableSignerFamilies = []string{ |
| 56 | + chainsel.FamilyEVM, |
| 57 | + chainsel.FamilySolana, |
| 58 | + chainsel.FamilyAptos, |
| 59 | + chainsel.FamilyStellar, |
| 60 | + chainsel.FamilyCanton, |
| 61 | +} |
| 62 | + |
| 63 | +// signerAddressForFamily derives the signer address a NOP would present on targetFamily |
| 64 | +// from whatever identities it already has: its per-family signer addresses (JD- or |
| 65 | +// state-sourced) and/or its raw public key. It returns ok=false when no candidate can be |
| 66 | +// translated into targetFamily (e.g. only an address-class family is known but the target |
| 67 | +// needs a raw public key, which a hashed address cannot yield). |
| 68 | +func signerAddressForFamily(directByFamily map[string]string, rawPubKey, targetFamily string) (string, bool) { |
| 69 | + candidates := make(map[string]string, len(directByFamily)+1) |
| 70 | + for family, addr := range directByFamily { |
| 71 | + if addr != "" { |
| 72 | + candidates[family] = addr |
| 73 | + } |
| 74 | + } |
| 75 | + if rawPubKey != "" { |
| 76 | + if _, exists := candidates[rawPubKeySourceFamily]; !exists { |
| 77 | + candidates[rawPubKeySourceFamily] = rawPubKey |
| 78 | + } |
| 79 | + } |
| 80 | + delete(candidates, targetFamily) |
| 81 | + |
| 82 | + families := make([]string, 0, len(candidates)) |
| 83 | + for family := range candidates { |
| 84 | + families = append(families, family) |
| 85 | + } |
| 86 | + sort.Strings(families) // deterministic when more than one source family is available |
| 87 | + |
| 88 | + for _, family := range families { |
| 89 | + if translated, err := translateSignerAddress(family, candidates[family], targetFamily); err == nil { |
| 90 | + return translated, true |
| 91 | + } |
| 92 | + } |
| 93 | + return "", false |
| 94 | +} |
| 95 | + |
| 96 | +// translateSignerAddress converts a signer identity stored under sourceFamily into the |
| 97 | +// representation targetFamily expects, valid only when both families sign with the same |
| 98 | +// underlying secp256k1 key. |
| 99 | +// |
| 100 | +// Address-class families (evm, solana) store the same 20 bytes and translate in both |
| 101 | +// directions. Raw-pubkey-class families (aptos, stellar, canton) store the same |
| 102 | +// hex-encoded public key and translate in both directions. Deriving an address-class |
| 103 | +// value from a raw public key is one-directional: an address is a keccak256 hash of the |
| 104 | +// public key, so recovering the public key from an address is not possible — that |
| 105 | +// direction returns an error instead of a wrong answer. |
| 106 | +func translateSignerAddress(sourceFamily, value, targetFamily string) (string, error) { |
| 107 | + switch { |
| 108 | + case addressClassFamilies[sourceFamily] && addressClassFamilies[targetFamily]: |
| 109 | + addrBytes, err := decodeAddressHex(value) |
| 110 | + if err != nil { |
| 111 | + return "", fmt.Errorf("decode %s signer address: %w", sourceFamily, err) |
| 112 | + } |
| 113 | + return formatAddressForFamily(addrBytes, targetFamily) |
| 114 | + case rawPubKeyClassFamilies[sourceFamily] && rawPubKeyClassFamilies[targetFamily]: |
| 115 | + return strings.ToLower(strings.TrimPrefix(value, "0x")), nil |
| 116 | + case rawPubKeyClassFamilies[sourceFamily] && addressClassFamilies[targetFamily]: |
| 117 | + pubKeyBytes, err := hex.DecodeString(strings.TrimPrefix(strings.ToLower(value), "0x")) |
| 118 | + if err != nil { |
| 119 | + return "", fmt.Errorf("decode %s raw public key: %w", sourceFamily, err) |
| 120 | + } |
| 121 | + pubKey, err := gethcrypto.UnmarshalPubkey(pubKeyBytes) |
| 122 | + if err != nil { |
| 123 | + return "", fmt.Errorf("unmarshal %s public key: %w", sourceFamily, err) |
| 124 | + } |
| 125 | + return formatAddressForFamily(gethcrypto.PubkeyToAddress(*pubKey).Bytes(), targetFamily) |
| 126 | + case addressClassFamilies[sourceFamily] && rawPubKeyClassFamilies[targetFamily]: |
| 127 | + return "", fmt.Errorf( |
| 128 | + "cannot derive a %s raw public key from a %s address: address derivation (keccak256) is not "+ |
| 129 | + "reversible; register the node's raw public key for family %s directly", |
| 130 | + targetFamily, sourceFamily, targetFamily) |
| 131 | + default: |
| 132 | + return "", fmt.Errorf("no signer address translation defined from family %s to family %s", sourceFamily, targetFamily) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// decodeAddressHex parses a 20-byte address from hex, tolerating an optional 0x prefix |
| 137 | +// and either case. |
| 138 | +func decodeAddressHex(s string) ([]byte, error) { |
| 139 | + s = strings.TrimPrefix(strings.TrimPrefix(s, "0x"), "0X") |
| 140 | + b, err := hex.DecodeString(s) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + if len(b) != 20 { |
| 145 | + return nil, fmt.Errorf("expected 20-byte address, got %d bytes", len(b)) |
| 146 | + } |
| 147 | + return b, nil |
| 148 | +} |
| 149 | + |
| 150 | +// formatAddressForFamily renders a 20-byte address per family convention: EIP-55 |
| 151 | +// checksummed with 0x for evm, lowercase without 0x for solana. |
| 152 | +func formatAddressForFamily(addr []byte, family string) (string, error) { |
| 153 | + switch family { |
| 154 | + case chainsel.FamilyEVM: |
| 155 | + return gethcommon.BytesToAddress(addr).Hex(), nil |
| 156 | + case chainsel.FamilySolana: |
| 157 | + return strings.ToLower(hex.EncodeToString(addr)), nil |
| 158 | + default: |
| 159 | + return "", fmt.Errorf("unsupported address-class family %q", family) |
| 160 | + } |
| 161 | +} |
0 commit comments