Enhance private key validation for wallet recovery and improve error messaging

This commit is contained in:
void-57 2025-10-14 13:31:24 +05:30
parent bb30be548a
commit c40e09a794

View File

@ -2345,11 +2345,18 @@
}
// Check for TON/FLO/BTC private key format (WIF - Wallet Import Format)
const base58Regex = /^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/;
if (base58Regex.test(input) && input.length >= 51 && input.length <= 56) {
const base58Regex =
/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/;
if (
base58Regex.test(input) &&
input.length >= 51 &&
input.length <= 56
) {
// Only accept private keys with specific prefixes that can be converted to TON
const validPrivateKeyPrefixes = ['R',, 'K', 'L', 'T'];
if (validPrivateKeyPrefixes.some(prefix => input.startsWith(prefix))) {
const validPrivateKeyPrefixes = ["R", "K", "L", "T"];
if (
validPrivateKeyPrefixes.some((prefix) => input.startsWith(prefix))
) {
return true;
}
return false; // Reject other Base58 strings (like BTC addresses)
@ -2898,6 +2905,34 @@
}
}
// Validation function for recover wallet (only private keys,)
function isValidRecoverPrivateKey(input) {
// Check if it's a hex private key (64 or 128 characters) - TON format
const hexOnly = /^[0-9a-fA-F]+$/.test(input);
if (hexOnly && (input.length === 64 || input.length === 128)) {
return true;
}
// Check for TON/FLO/BTC private key format (WIF - Wallet Import Format)
const base58Regex =
/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/;
if (
base58Regex.test(input) &&
input.length >= 51 &&
input.length <= 56
) {
// Only accept private keys with specific prefixes for TON/FLO/BTC
const validPrivateKeyPrefixes = ["R", "K", "L", "T"];
if (
validPrivateKeyPrefixes.some((prefix) => input.startsWith(prefix))
) {
return true;
}
}
return false;
}
// Recover wallet functionality
async function recoverWallet() {
const privateKeyInput = document
@ -2913,6 +2948,27 @@
return;
}
// Validate private key format
if (!isValidRecoverPrivateKey(privateKeyInput)) {
output.innerHTML = `
<div class="error-state">
<div class="error-icon">
<i class="fas fa-exclamation-triangle"></i>
</div>
<div class="error-message">
<h3>Invalid Private Key Format</h3>
<p>Please enter a valid private key (TON/FLO/BTC format).</p>
<p>Addresses and other formats are not supported.</p>
</div>
</div>
`;
showNotification(
"Invalid private key format - only TON/FLO/BTC private keys supported",
"error"
);
return;
}
// Show loading state
const originalHTML = button.innerHTML;
button.disabled = true;
@ -2921,7 +2977,7 @@
try {
let wallet;
if (typeof tonCrypto !== "undefined" ) {
if (typeof tonCrypto !== "undefined") {
wallet = await tonCrypto.recoverFromInput(privateKeyInput);
}
const tonData = wallet.TON || wallet;
@ -3045,11 +3101,15 @@
</div>
<div class="error-message">
<h3>Recovery Failed</h3>
<p>Please check that you've entered a valid private key in the correct format.</p>
<p>Unable to recover address from the provided private key. Please ensure you've entered a valid TON/FLO/BTC private key.</p>
</div>
</div>
`;
showNotification("Failed to recover ", "error");
showNotification(
"Failed to recover address from private key",
"error"
);
} finally {
button.disabled = false;
button.innerHTML = originalHTML;