Retry NWC requests with nip04 after nip44 timeouts

This commit is contained in:
thePR0M3TH3AN committed 2025-10-27 21:34:57 -04:00
1 parent 011d69482a
commit 37bec3c5e2
2 files changed
+95 -5

No files matched your search

+42 -5
View File
@@ -673,6 +673,45 @@ async function ensureEncryption(context) {
throw new Error("No compatible wallet encryption scheme available.");
}
function shouldRetryWithFallback(context, error, encryption, { hasRetried }) {
if (!context || !encryption || hasRetried) {
return false;
}
const code = typeof error?.code === "string" ? error.code : "";
const message = typeof error?.message === "string" ? error.message : "";
if (code === "UNSUPPORTED_ENCRYPTION" || code === "unsupported_encryption") {
return true;
}
const isTimeout =
message === "Wallet request timed out." ||
code === "timeout" ||
code === "TIMEOUT";
if (!isTimeout || encryption.scheme !== "nip44_v2") {
return false;
}
const state = ensureEncryptionState(context);
if (state.unsupported.has("nip04")) {
return false;
}
const { schemes, explicit } = getWalletSupportedEncryption(context.infoEvent);
if (explicit && schemes.includes("nip44_v2") && !schemes.includes("nip04")) {
return false;
}
return getEncryptionCandidates(context).some((candidate) => {
if (candidate.scheme !== "nip04") {
return false;
}
return !state.unsupported.has(candidate.scheme);
});
}
function finalizePendingRequest(entry) {
if (!entry || typeof entry !== "object") {
return;
@@ -1067,11 +1106,9 @@ async function sendWalletRequest(context, payload, { timeoutMs, __internalRetry
} catch (error) {
const encryption = context?.encryption || null;
if (
!__internalRetry &&
encryption &&
error &&
typeof error === "object" &&
(error.code === "UNSUPPORTED_ENCRYPTION" || error.code === "unsupported_encryption")
shouldRetryWithFallback(context, error, encryption, {
hasRetried: __internalRetry === true,
})
) {
rememberUnsupportedEncryption(context, encryption.scheme);
context.encryption = null;