From 796f27111da54bdfac257e7250530f4ac3f50561 Mon Sep 17 00:00:00 2001 From: void-57 Date: Wed, 15 Oct 2025 22:41:07 +0530 Subject: [PATCH] Add function to convert TON addresses to UQ format with API integration --- tonBlockchainAPI.js | 64 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/tonBlockchainAPI.js b/tonBlockchainAPI.js index ee93e67..57dac7d 100644 --- a/tonBlockchainAPI.js +++ b/tonBlockchainAPI.js @@ -290,6 +290,64 @@ }); }; + tonBlockchainAPI.getUQAddress = function (address) { + return new Promise((resolve, reject) => { + try { + if (!address || typeof address !== "string") { + resolve(address); + return; + } + + const isValidTonAddress = address.match(/^[EUk]Q[A-Za-z0-9_-]{46}$/); + if (!isValidTonAddress) { + resolve(address); + return; + } + + + if (address.startsWith("UQ")) { + resolve(address); + return; + } + + + fetch( + `https://toncenter.com/api/v2/detectAddress?address=${encodeURIComponent( + address + )}`, + { + headers: { "X-API-Key": API_KEY }, + } + ) + .then((response) => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + return response.json(); + }) + .then((data) => { + if (data && data.result) { + + const uqAddress = + data.result.non_bounceable?.b64url || + data.result.non_bounceable?.b64 || + address; + resolve(uqAddress); + } else { + resolve(address); + } + }) + .catch((error) => { + console.warn("Failed to convert address using API:", error); + resolve(address); + }); + } catch (error) { + console.error("Error in getUQAddress:", error); + resolve(address); + } + }); + }; + /** * Create wallet from private key * @param {string} privHex - Private key in hexadecimal format @@ -338,7 +396,11 @@ await tonBlockchainAPI.getSenderWallet(privHex); const seqno = await wallet.methods.seqno().call(); const senderAddr = address.toString(true, true, true); - + toAddress=await tonBlockchainAPI.getUQAddress(toAddress); + + console.log( + `Sending ${amount} TON from ${senderAddr} to ${toAddress}, seqno: ${seqno}` + ); await wallet.methods .transfer({ secretKey: keyPair.secretKey,