feat: Allow HBAR transfers to Account IDs by converting to EVM addresses and update UI prompts.

This commit is contained in:
void-57 2025-12-09 14:29:54 +05:30
parent eac1457f0a
commit 649f05dc6c
2 changed files with 31 additions and 13 deletions

View File

@ -290,7 +290,7 @@
/** /**
* Send HBAR using JSON-RPC Relay (EVM-compatible) * Send HBAR using JSON-RPC Relay (EVM-compatible)
* @param {string} fromPrivateKey - Sender's private key (hex format) * @param {string} fromPrivateKey - Sender's private key (hex format)
* @param {string} toAddress - Recipient's EVM address * @param {string} toAddress - Recipient's EVM address (0x...) - Account IDs should be converted to EVM addresses before calling
* @param {number} amount - Amount in HBAR * @param {number} amount - Amount in HBAR
* @param {string} memo - Optional memo * @param {string} memo - Optional memo
* @returns {Promise<Object>} - Transaction result * @returns {Promise<Object>} - Transaction result

View File

@ -355,7 +355,7 @@
<i class="fas fa-times"></i> <i class="fas fa-times"></i>
</button> </button>
</div> </div>
<div class="form-text">Enter an HBAR address or BTC/FLO/HBAR private key to view transactions</div> <div class="form-text">Enter EVM address, Account ID or BTC/FLO/HBAR private key to view transactions</div>
</div> </div>
</div> </div>
@ -562,12 +562,12 @@
<div class="form-group"> <div class="form-group">
<label for="recipientAddress"><i class="fas fa-user"></i> Recipient Address</label> <label for="recipientAddress"><i class="fas fa-user"></i> Recipient Address</label>
<div class="input-with-actions"> <div class="input-with-actions">
<input type="text" id="recipientAddress" class="form-input" placeholder="Enter recipient's EVM address (0x...)" /> <input type="text" id="recipientAddress" class="form-input" placeholder="Enter recipient's EVM address (0x...) or Account ID (0.0.xxxx)" />
<button type="button" class="input-action-btn clear-btn" onclick="clearInput('recipientAddress')"> <button type="button" class="input-action-btn clear-btn" onclick="clearInput('recipientAddress')">
<i class="fas fa-times"></i> <i class="fas fa-times"></i>
</button> </button>
</div> </div>
<div class="form-text">EVM address format (0x...)</div> <div class="form-text">EVM address (0x...) or Account ID (0.0.xxxx)</div>
</div> </div>
<div class="form-group"> <div class="form-group">
@ -1597,21 +1597,37 @@
return; return;
} }
if (!recipient.startsWith('0x') || recipient.length !== 42) { // Validate recipient address format (EVM or Account ID)
showNotification('⚠️ Invalid recipient address. Expected EVM address (0x...)', 'error'); const validation = hederaAPI.validateAddress(recipient);
if (!validation.valid) {
showNotification('⚠️ Invalid recipient address. Expected EVM address (0x...) or Account ID (0.0.xxxx)', 'error');
return; return;
} }
// If Account ID is provided, convert it to EVM address
let recipientEvmAddress = recipient;
if (validation.type === 'accountId') {
try {
showNotification('🔄 Converting Account ID to EVM address...', 'info');
const accountData = await hederaAPI.getBalance(recipient);
recipientEvmAddress = accountData.evmAddress;
console.log(`Converted Account ID ${recipient} to EVM address ${recipientEvmAddress}`);
} catch (error) {
showNotification('⚠️ Could not find account. Please verify the Account ID.', 'error');
return;
}
}
if (!amount || amount <= 0) { if (!amount || amount <= 0) {
showNotification('⚠️ Please enter a valid amount', 'warning'); showNotification('⚠️ Please enter a valid amount', 'warning');
return; return;
} }
try { try {
// Check if recipient account exists // Check if recipient account exists (using EVM address)
let recipientExists = false; let recipientExists = false;
try { try {
const recipientBalance = await hederaAPI.getBalance(recipient); const recipientBalance = await hederaAPI.getBalance(recipientEvmAddress);
recipientExists = true; recipientExists = true;
console.log('Recipient account exists with balance:', recipientBalance.balance); console.log('Recipient account exists with balance:', recipientBalance.balance);
} catch (error) { } catch (error) {
@ -1643,11 +1659,13 @@
const gasPrice = await web3.eth.getGasPrice(); const gasPrice = await web3.eth.getGasPrice();
console.log('Current gas price:', gasPrice, 'wei'); console.log('Current gas price:', gasPrice, 'wei');
// Estimate gas for this transaction // Estimate gas for this transaction (use EVM address)
const amountInWei = web3.utils.toWei(amount.toString(), 'ether');
const amountString = amount.toFixed(18);
const amountInWei = web3.utils.toWei(amountString, 'ether');
const estimatedGas = await web3.eth.estimateGas({ const estimatedGas = await web3.eth.estimateGas({
from: senderAddress, from: senderAddress,
to: recipient, to: recipientEvmAddress,
value: amountInWei value: amountInWei
}); });
console.log('Estimated gas units:', estimatedGas); console.log('Estimated gas units:', estimatedGas);
@ -1678,12 +1696,12 @@
return; return;
} }
// Store pending transaction // Store pending transaction (use EVM address)
const estimatedFee = estimatedGasFee; const estimatedFee = estimatedGasFee;
pendingTx = { pendingTx = {
privateKey: hexPrivateKey, // Use hex format for API privateKey: hexPrivateKey, // Use hex format for API
from: senderAddress, from: senderAddress,
to: recipient, to: recipientEvmAddress, // Use EVM address for transaction
amount: amount, amount: amount,
fee: estimatedFee, fee: estimatedFee,
total: amount + estimatedFee total: amount + estimatedFee