mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-28 08:22:24 +00:00
- Updated `js/nostr/adapters/nsecAdapter.js` to ensure private keys are `Uint8Array` bytes before passing to `getPublicKey`, fixing compatibility with `nostr-tools` v2. - Updated `tests/nostr/nip46Client.test.js` to use byte arrays for keys in crypto operations. - Implemented missing `setupModalMorePopover`, `setupModalSharePopover`, and `refreshModalMoreMenuPanel` in `VideoModal.js`. - Fixed a bug in `VideoModal.js` where `setCommentComposerState` was overriding `CommentsController` send button logic; it now delegates to the controller if available. Co-authored-by: PR0M3TH3AN <[email protected]>
108 lines
3.2 KiB
JavaScript
108 lines
3.2 KiB
JavaScript
// js/nostr/adapters/nsecAdapter.js
|
|
|
|
import { signEventWithPrivateKey } from "../publishHelpers.js";
|
|
import { createPrivateKeyCipherClosures } from "../signerHelpers.js";
|
|
import { ensureNostrTools, getCachedNostrTools } from "../toolkit.js";
|
|
import { normalizeActorKey } from "../watchHistory.js";
|
|
import { HEX64_REGEX } from "../../utils/hex.js";
|
|
|
|
function normalizePrivateKey(privateKey) {
|
|
if (typeof privateKey !== "string") {
|
|
return "";
|
|
}
|
|
const trimmed = privateKey.trim();
|
|
if (!trimmed || !HEX64_REGEX.test(trimmed)) {
|
|
return "";
|
|
}
|
|
return trimmed.toLowerCase();
|
|
}
|
|
|
|
async function resolvePublicKey(privateKey, pubkey) {
|
|
const normalizedPubkey = normalizeActorKey(pubkey);
|
|
if (normalizedPubkey && HEX64_REGEX.test(normalizedPubkey)) {
|
|
return normalizedPubkey;
|
|
}
|
|
|
|
if (!privateKey) {
|
|
return "";
|
|
}
|
|
|
|
const tools = (await ensureNostrTools()) || getCachedNostrTools();
|
|
if (tools?.getPublicKey && typeof tools.getPublicKey === "function") {
|
|
try {
|
|
// nostr-tools v2 requires Uint8Array
|
|
let keyInput = privateKey;
|
|
if (typeof privateKey === "string" && tools.utils?.hexToBytes) {
|
|
keyInput = tools.utils.hexToBytes(privateKey);
|
|
}
|
|
const derived = tools.getPublicKey(keyInput);
|
|
const normalized = normalizeActorKey(derived);
|
|
if (normalized && HEX64_REGEX.test(normalized)) {
|
|
return normalized;
|
|
}
|
|
} catch (e) {
|
|
// Fallback for v1 or other errors
|
|
try {
|
|
const derived = tools.getPublicKey(privateKey);
|
|
const normalized = normalizeActorKey(derived);
|
|
if (normalized && HEX64_REGEX.test(normalized)) {
|
|
return normalized;
|
|
}
|
|
} catch (e2) {
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
|
|
return "";
|
|
}
|
|
|
|
export async function createNsecAdapter({ privateKey, pubkey } = {}) {
|
|
const normalizedPrivateKey = normalizePrivateKey(privateKey);
|
|
const resolvedPubkey = await resolvePublicKey(normalizedPrivateKey, pubkey);
|
|
const cipherClosures = await createPrivateKeyCipherClosures(normalizedPrivateKey);
|
|
|
|
const signer = {
|
|
type: "nsec",
|
|
pubkey: resolvedPubkey,
|
|
metadata: async () => null,
|
|
relays: async () => null,
|
|
signEvent: async (event) => {
|
|
if (!normalizedPrivateKey) {
|
|
throw new Error("Missing private key for signing.");
|
|
}
|
|
return signEventWithPrivateKey(event, normalizedPrivateKey);
|
|
},
|
|
requestPermissions: async () => ({ ok: true }),
|
|
destroy: async () => {},
|
|
canSign: () => Boolean(normalizedPrivateKey),
|
|
capabilities: {
|
|
sign: Boolean(normalizedPrivateKey),
|
|
nip44: false,
|
|
nip04: false,
|
|
},
|
|
};
|
|
|
|
if (typeof cipherClosures.nip04Encrypt === "function") {
|
|
signer.nip04Encrypt = cipherClosures.nip04Encrypt;
|
|
}
|
|
if (typeof cipherClosures.nip04Decrypt === "function") {
|
|
signer.nip04Decrypt = cipherClosures.nip04Decrypt;
|
|
}
|
|
if (typeof cipherClosures.nip44Encrypt === "function") {
|
|
signer.nip44Encrypt = cipherClosures.nip44Encrypt;
|
|
}
|
|
if (typeof cipherClosures.nip44Decrypt === "function") {
|
|
signer.nip44Decrypt = cipherClosures.nip44Decrypt;
|
|
}
|
|
|
|
signer.capabilities.nip04 = Boolean(
|
|
signer.nip04Encrypt || signer.nip04Decrypt
|
|
);
|
|
signer.capabilities.nip44 = Boolean(
|
|
signer.nip44Encrypt || signer.nip44Decrypt
|
|
);
|
|
|
|
return signer;
|
|
}
|