improve address conversion logic

This commit is contained in:
void-57 2025-10-14 01:36:32 +05:30
parent 0048864281
commit e4f0e8bba4

View File

@ -16,8 +16,7 @@
new TonWeb.HttpProvider("https://toncenter.com/api/v2/jsonRPC"), new TonWeb.HttpProvider("https://toncenter.com/api/v2/jsonRPC"),
{ {
headers: { headers: {
"X-API-Key": "X-API-Key": API_KEY,
API_KEY,
}, },
} }
); );
@ -151,7 +150,7 @@
.then((data) => { .then((data) => {
if (data) { if (data) {
const friendly = const friendly =
data?.result?.non_bounceable?.b64url || data?.result?.bounceable?.b64url ||
rawAddr.replace(":retry:", ""); rawAddr.replace(":retry:", "");
const cleanAddr = rawAddr.replace(":retry:", ""); const cleanAddr = rawAddr.replace(":retry:", "");
addrCache.set(cleanAddr, friendly); addrCache.set(cleanAddr, friendly);
@ -172,13 +171,13 @@
} }
/** /**
* Convert raw address to b64 format with rate limiting * Convert address to b64 format with rate limiting
* @param {string} rawAddr - The raw address to convert * @param {string} rawAddr - The address to convert (raw, EQ, UQ formats)
* @returns {Promise} Promise object that resolves with user-friendly address * @returns {Promise} Promise object that resolves with user-friendly address
*/ */
tonBlockchainAPI.convertTob64 = function (rawAddr) { tonBlockchainAPI.convertTob64 = function (rawAddr) {
return new Promise((resolve) => { return new Promise((resolve) => {
// if it doesn't look like a raw address, return as-is // if it doesn't look like an address, return as-is
if (!rawAddr || typeof rawAddr !== "string" || rawAddr === "Unknown") { if (!rawAddr || typeof rawAddr !== "string" || rawAddr === "Unknown") {
resolve(rawAddr); resolve(rawAddr);
return; return;
@ -193,8 +192,9 @@
const isRawAddress = const isRawAddress =
rawAddr.includes(":") && rawAddr.match(/^-?\d+:[a-fA-F0-9]{64}$/); rawAddr.includes(":") && rawAddr.match(/^-?\d+:[a-fA-F0-9]{64}$/);
if (!isRawAddress) { const isFriendlyAddress = rawAddr.match(/^[EUk]Q[A-Za-z0-9_-]{46}$/);
// Already user-friendly or not a TON address
if (!isRawAddress && !isFriendlyAddress) {
resolve(rawAddr); resolve(rawAddr);
return; return;
} }
@ -205,7 +205,7 @@
return; return;
} }
// Add to queue for conversion // Add to queue for conversion (works for both raw and friendly addresses)
requestQueue.push({ rawAddr, resolve }); requestQueue.push({ rawAddr, resolve });
processRequestQueue(); processRequestQueue();
}); });