Prefer nip04 encryption fallback for implicit wallets

This commit is contained in:
thePR0M3TH3AN committed 2025-10-27 20:35:14 -04:00
1 parent 2c5213aa17
commit 011d69482a
2 files changed
+124 -1

No files matched your search

+15 -1
View File
@@ -627,7 +627,8 @@ async function ensureEncryption(context) {
}
}
const { schemes: walletSchemes } = getWalletSupportedEncryption(infoEvent);
const { schemes: walletSchemes, explicit: walletSchemesExplicit } =
getWalletSupportedEncryption(infoEvent);
const candidates = getEncryptionCandidates(context).filter((candidate) => {
return !state.unsupported.has(candidate.scheme);
});
@@ -636,6 +637,19 @@ async function ensureEncryption(context) {
? candidates.filter((candidate) => walletSchemes.includes(candidate.scheme))
: candidates;
if (
compatibleCandidates.length > 1 &&
(!walletSchemesExplicit || !walletSchemes.includes("nip44_v2"))
) {
const nip04Index = compatibleCandidates.findIndex(
(candidate) => candidate.scheme === "nip04"
);
if (nip04Index > 0) {
const [nip04Candidate] = compatibleCandidates.splice(nip04Index, 1);
compatibleCandidates.unshift(nip04Candidate);
}
}
if (compatibleCandidates.length) {
const selected = compatibleCandidates[0];
context.encryption = selected;
+109
View File
@@ -65,6 +65,14 @@ await (async () => {
},
},
},
nip04: {
async encrypt() {
return "nip04";
},
async decrypt() {
return "nip04";
},
},
};
const walletPubkey = "b".repeat(64);
@@ -105,6 +113,107 @@ await (async () => {
resetWalletClient();
})();
await (async () => {
resetWalletClient();
window.NostrTools = {
getPublicKey() {
return "c".repeat(64);
},
nip44: {
v2: {
async encrypt(plaintext, key) {
return `nip44:${key}:${plaintext}`;
},
async decrypt(ciphertext, key) {
return `nip44:${key}:${ciphertext}`;
},
utils: {
getConversationKey(secretBytes, pubkey) {
return `key-${pubkey}-${secretBytes.length}`;
},
},
},
},
nip04: {
async encrypt() {
return "nip04";
},
async decrypt() {
return "nip04";
},
},
};
const walletPubkey = "b".repeat(64);
const secretKey = "a".repeat(64);
const relays = ["wss://relay.one.example"];
const uri =
`nostr+walletconnect://${walletPubkey}` +
`?relay=${encodeURIComponent(relays[0])}` +
`&secret=${secretKey}`;
const context = ensureActiveState({ nwcUri: uri });
context.infoEvent = { kind: 13194, tags: [] };
const encryption = await __TESTING__.ensureEncryptionForContext(context);
assert.equal(encryption.scheme, "nip04");
resetWalletClient();
})();
await (async () => {
resetWalletClient();
window.NostrTools = {
getPublicKey() {
return "c".repeat(64);
},
nip44: {
v2: {
async encrypt(plaintext, key) {
return `nip44:${key}:${plaintext}`;
},
async decrypt(ciphertext, key) {
return `nip44:${key}:${ciphertext}`;
},
utils: {
getConversationKey(secretBytes, pubkey) {
return `key-${pubkey}-${secretBytes.length}`;
},
},
},
},
nip04: {
async encrypt() {
return "nip04";
},
async decrypt() {
return "nip04";
},
},
};
const walletPubkey = "b".repeat(64);
const secretKey = "a".repeat(64);
const relays = ["wss://relay.one.example"];
const uri =
`nostr+walletconnect://${walletPubkey}` +
`?relay=${encodeURIComponent(relays[0])}` +
`&secret=${secretKey}`;
const context = ensureActiveState({ nwcUri: uri });
context.infoEvent = {
kind: 13194,
tags: [["encryption", "nip04"]],
};
const encryption = await __TESTING__.ensureEncryptionForContext(context);
assert.equal(encryption.scheme, "nip04");
resetWalletClient();
})();
await (async () => {
resetWalletClient();