Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions modules/sdk-coin-evm/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
};
}
}