From 8678f7895cd49ec4a1a6bb0b17470c5c6e159a99 Mon Sep 17 00:00:00 2001 From: Rohit Saw Date: Mon, 15 Dec 2025 11:09:57 +0530 Subject: [PATCH] feat: address resolution function for hbarevm ticket: win-8570 --- modules/sdk-coin-evm/src/lib/utils.ts | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/modules/sdk-coin-evm/src/lib/utils.ts b/modules/sdk-coin-evm/src/lib/utils.ts index ff934e9861..0fcebe7b85 100644 --- a/modules/sdk-coin-evm/src/lib/utils.ts +++ b/modules/sdk-coin-evm/src/lib/utils.ts @@ -272,3 +272,47 @@ export function validateHederaAccountId(address: string): { valid: boolean; erro error: null, }; } + +/** + * Convert Hedera Account ID (e.g., 0.0.12345) to EVM address + */ +export async function resolveHederaAccountIdToEvmAddress( + accountId: string, + isProd: boolean +): Promise<{ address: string | null; error?: string }> { + try { + const mirrorNodeUrl = isProd + ? 'https://mainnet-public.mirrornode.hedera.com' + : 'https://testnet.mirrornode.hedera.com'; + + const response = await fetch(`${mirrorNodeUrl}/api/v1/accounts/${accountId}`); + + if (!response.ok) { + if (response.status === 404) { + return { + address: null, + error: 'Hedera Account ID not found. Please verify the account exists.', + }; + } + return { + address: null, + error: `Failed to resolve Hedera Account ID: ${response.status}`, + }; + } + + const accountData = (await response.json()) as { evm_address?: string }; + if (!accountData.evm_address) { + return { + address: null, + error: 'This Hedera account does not have an associated EVM address.', + }; + } + + return { address: accountData.evm_address }; + } catch (error) { + return { + address: null, + error: 'Failed to resolve Hedera Account ID. Please check your connection.', + }; + } +}