Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions popup/popup-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Pure formatting and validation helpers for the Podkey popup UI.
* Kept DOM- and chrome-free so Node tests can verify every edge case directly.
*/

import { hexToNpub, normalizeSecretKeyToHex } from '../src/keyformat.js';

export const MIN_PASSPHRASE_LENGTH = 8;

/**
* Format a 64-char hex public key in the requested display format.
* Defaults to raw 64-char hex Nostr pubkey while also supporting npub and did:nostr.
* @param {string} publicKeyHex
* @param {'hex'|'npub'|'did'} format
* @returns {{ value: string, copyLabel: string }}
*/
export function formatIdentity (publicKeyHex, format = 'hex') {
if (typeof publicKeyHex !== 'string' || !/^[0-9a-fA-F]{64}$/.test(publicKeyHex)) {
return { value: '', copyLabel: 'Copy public key' };
}
const hex = publicKeyHex.toLowerCase();
if (format === 'did') {
return { value: `did:nostr:${hex}`, copyLabel: 'Copy DID' };
}
if (format === 'npub') {
return { value: hexToNpub(hex), copyLabel: 'Copy npub' };
}
return { value: hex, copyLabel: 'Copy public key' };
}

/**
* Evaluate a passphrase + confirmation pair for inline UI feedback.
* @param {string} passphrase
* @param {string} confirm
* @returns {{ lengthOk: boolean, matchOk: boolean, valid: boolean, error: string|null }}
*/
export function evaluatePassphrasePair (passphrase = '', confirm = '') {
const lengthOk = passphrase.length >= MIN_PASSPHRASE_LENGTH;
const matchOk = passphrase.length > 0 && passphrase === confirm;
let error = null;
if (!lengthOk) {
error = `Passphrase must be at least ${MIN_PASSPHRASE_LENGTH} characters.`;
} else if (!matchOk) {
error = 'Passphrases do not match.';
}
return { lengthOk, matchOk, valid: lengthOk && matchOk, error };
}

/**
* Validate a pasted private key (64-char hex or NIP-19 nsec1…) without throwing.
* @param {string} raw
* @returns {{ valid: boolean, format: 'hex'|'nsec'|null, error: string|null }}
*/
export function evaluatePrivateKeyInput (raw = '') {
const trimmed = typeof raw === 'string' ? raw.trim() : '';
if (!trimmed) {
return { valid: false, format: null, error: 'Enter your nsec or 64-character hex private key.' };
}
try {
normalizeSecretKeyToHex(trimmed);
const format = /^nsec1/i.test(trimmed) ? 'nsec' : 'hex';
return { valid: true, format, error: null };
} catch {
return { valid: false, format: null, error: 'Key must be a valid nsec1… or 64-character hex private key.' };
}
}

/**
* Extract an HTTP(S) origin from a browser tab URL, or null for internal/non-web pages.
* @param {string|undefined} tabUrl
* @returns {string|null}
*/
export function extractWebOrigin (tabUrl) {
if (typeof tabUrl !== 'string' || !tabUrl) return null;
try {
const parsed = new URL(tabUrl);
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null;
return parsed.origin;
} catch {
return null;
}
}
Loading
Loading