Add function to convert TON addresses to UQ format with API integration
This commit is contained in:
parent
c40e09a794
commit
796f27111d
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user