|
1 | 1 | import { CHAIN_TYPE } from "../config/types"; |
2 | 2 | import bs58 from "bs58"; |
3 | 3 | import type { HardhatRuntimeEnvironment } from "hardhat/types"; |
| 4 | +import { configData } from "../config"; |
4 | 5 |
|
5 | 6 | /** |
6 | 7 | * Error thrown when an invalid address is provided for a specific chain type |
@@ -228,3 +229,98 @@ export function convertChainAddressToHex( |
228 | 229 | export function isValidChainType(chainType: string): chainType is CHAIN_TYPE { |
229 | 230 | return chainType === "evm" || chainType === "svm"; |
230 | 231 | } |
| 232 | + |
| 233 | +/** |
| 234 | + * Interface for chain configuration lookup result |
| 235 | + */ |
| 236 | +export interface ChainInfo { |
| 237 | + name: string; |
| 238 | + chainType: CHAIN_TYPE; |
| 239 | + chainSelector: string; |
| 240 | + config: any; |
| 241 | +} |
| 242 | + |
| 243 | +/** |
| 244 | + * Looks up chain information by chain selector |
| 245 | + * @param chainSelector - The chain selector to look up |
| 246 | + * @returns Chain information or undefined if not found |
| 247 | + */ |
| 248 | +export function getChainInfoBySelector( |
| 249 | + chainSelector: string | bigint |
| 250 | +): ChainInfo | undefined { |
| 251 | + const selectorString = chainSelector.toString(); |
| 252 | + |
| 253 | + const chainName = Object.keys(configData).find( |
| 254 | + (key) => |
| 255 | + configData[key as keyof typeof configData]?.chainSelector?.toString() === |
| 256 | + selectorString |
| 257 | + ); |
| 258 | + |
| 259 | + if (!chainName) { |
| 260 | + return undefined; |
| 261 | + } |
| 262 | + |
| 263 | + const config = configData[chainName as keyof typeof configData]; |
| 264 | + const chainType = config.chainType as CHAIN_TYPE; |
| 265 | + |
| 266 | + return { |
| 267 | + name: chainName, |
| 268 | + chainType, |
| 269 | + chainSelector: selectorString, |
| 270 | + config, |
| 271 | + }; |
| 272 | +} |
| 273 | + |
| 274 | +/** |
| 275 | + * Decodes an encoded address based on the chain type |
| 276 | + * @param encodedAddress - The encoded address data |
| 277 | + * @param chainType - The chain type (evm or svm) |
| 278 | + * @param hre - Hardhat runtime environment (optional, required for EVM) |
| 279 | + * @returns The decoded address string |
| 280 | + * @throws Error if decoding fails |
| 281 | + */ |
| 282 | +export function decodeChainAddress( |
| 283 | + encodedAddress: string, |
| 284 | + chainType: CHAIN_TYPE, |
| 285 | + hre?: HardhatRuntimeEnvironment |
| 286 | +): string { |
| 287 | + if (chainType === "svm") { |
| 288 | + // For Solana, the encoded address is a hex string (32 bytes) |
| 289 | + const hexString = encodedAddress.startsWith("0x") |
| 290 | + ? encodedAddress.slice(2) |
| 291 | + : encodedAddress; |
| 292 | + const bytes = Buffer.from(hexString, "hex"); |
| 293 | + return bs58.encode(bytes); |
| 294 | + } else if (chainType === "evm") { |
| 295 | + if (!hre) { |
| 296 | + throw new Error("HardhatRuntimeEnvironment is required for EVM address decoding"); |
| 297 | + } |
| 298 | + // For EVM chains, use AbiCoder |
| 299 | + return new hre.ethers.AbiCoder().decode(["address"], encodedAddress)[0]; |
| 300 | + } else { |
| 301 | + throw new UnsupportedChainTypeError(chainType); |
| 302 | + } |
| 303 | +} |
| 304 | + |
| 305 | +/** |
| 306 | + * Decodes an address by first looking up the chain type from the selector |
| 307 | + * @param encodedAddress - The encoded address data |
| 308 | + * @param chainSelector - The chain selector |
| 309 | + * @param hre - Hardhat runtime environment |
| 310 | + * @returns The decoded address string or "UNKNOWN_CHAIN" if chain not found |
| 311 | + */ |
| 312 | +export function decodeAddressByChainSelector( |
| 313 | + encodedAddress: string, |
| 314 | + chainSelector: string | bigint, |
| 315 | + hre: HardhatRuntimeEnvironment |
| 316 | +): string { |
| 317 | + try { |
| 318 | + const chainInfo = getChainInfoBySelector(chainSelector); |
| 319 | + if (!chainInfo) { |
| 320 | + return "UNKNOWN_CHAIN"; |
| 321 | + } |
| 322 | + return decodeChainAddress(encodedAddress, chainInfo.chainType, hre); |
| 323 | + } catch (error) { |
| 324 | + return "DECODE_ERROR"; |
| 325 | + } |
| 326 | +} |
0 commit comments