Files
bitvid/js/nostr/adapters/nip46Adapter.js
T
google-labs-jules[bot]andPR0M3TH3AN e92c95995f feat: prioritize profile and feed data sync
- Implements a priority queue for NIP-07 (browser extension) requests to allow critical tasks (blocklist/prefs decryption) to jump the line.
- Updates NIP-46 client and adapters to support priority overrides for encryption/decryption.
- Updates UserBlockListManager, SubscriptionsManager, and HashtagPreferencesService to request HIGH priority when decrypting data.
- Ensures strict serialization for NIP-07 to prevent extension errors while optimizing latency for critical data.

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-01-28 15:00:15 +00:00

93 lines
2.8 KiB
JavaScript

// js/nostr/adapters/nip46Adapter.js
import { Nip46RpcClient } from "../nip46Client.js";
import { normalizeActorKey } from "../watchHistory.js";
function resolveClient(client) {
if (client instanceof Nip46RpcClient) {
return client;
}
return client?.nip46Client instanceof Nip46RpcClient ? client.nip46Client : null;
}
export async function createNip46Adapter(client) {
const resolvedClient = resolveClient(client);
if (!resolvedClient) {
return {
type: "nip46",
pubkey: "",
metadata: async () => null,
relays: async () => null,
signEvent: async () => {
throw new Error("NIP-46 client unavailable.");
},
requestPermissions: async () => ({
ok: false,
error: new Error("client-unavailable"),
}),
destroy: async () => {},
canSign: () => false,
capabilities: {
sign: false,
nip44: false,
nip04: false,
},
};
}
let pubkey = normalizeActorKey(resolvedClient.userPubkey);
if (!pubkey) {
pubkey = normalizeActorKey(await resolvedClient.getUserPubkey());
}
const signer = {
type: "nip46",
pubkey,
metadata: async () =>
resolvedClient.metadata && typeof resolvedClient.metadata === "object"
? { ...resolvedClient.metadata }
: null,
relays: async () => (Array.isArray(resolvedClient.relays) ? [...resolvedClient.relays] : null),
signEvent: async (event) => resolvedClient.signEvent(event),
requestPermissions: async () => ({ ok: true }),
destroy: async () => {
resolvedClient.destroy();
},
canSign: () =>
!resolvedClient.destroyed && typeof resolvedClient.signEvent === "function",
capabilities: {
sign:
!resolvedClient.destroyed &&
typeof resolvedClient.signEvent === "function",
nip44: false,
nip04: false,
},
};
if (typeof resolvedClient.nip04Encrypt === "function") {
signer.nip04Encrypt = (pubkey, plaintext, options) =>
resolvedClient.nip04Encrypt(pubkey, plaintext, options);
}
if (typeof resolvedClient.nip04Decrypt === "function") {
signer.nip04Decrypt = (pubkey, ciphertext, options) =>
resolvedClient.nip04Decrypt(pubkey, ciphertext, options);
}
if (typeof resolvedClient.nip44Encrypt === "function") {
signer.nip44Encrypt = (pubkey, plaintext, options) =>
resolvedClient.nip44Encrypt(pubkey, plaintext, options);
}
if (typeof resolvedClient.nip44Decrypt === "function") {
signer.nip44Decrypt = (pubkey, ciphertext, options) =>
resolvedClient.nip44Decrypt(pubkey, ciphertext, options);
}
signer.capabilities.nip04 = Boolean(
signer.nip04Encrypt || signer.nip04Decrypt
);
signer.capabilities.nip44 = Boolean(
signer.nip44Encrypt || signer.nip44Decrypt
);
return signer;
}