Files
bitvid/js/utils/hex.js
T
thePR0M3TH3ANandClaude Opus 4.8 a1ed04b22c [dm] Fix NIP-17 send failing with nip17-keygen-failed (missing bytesToHex import)
Sending a NIP-17 DM builds a kind-1059 gift wrap, which needs a fresh ephemeral
keypair. generateEphemeralKeypair() hex-encodes the generated secret with
bytesToHex — but bytesToHex was never imported in client.js, so the call threw
ReferenceError. The try/catch swallowed it into privateKey="", keygen returned
null, and every NIP-17 send failed with "We couldn't create secure wrapper keys
for NIP-17 delivery." (nip17-keygen-failed). The seal (kind 13) signed fine
because it uses the active signer; only the ephemeral keygen was broken. Build
and lint don't parse JS, so the missing import wasn't caught.

- Add bytesToHex to the canonical js/utils/hex.js and import it in client.js.
- Add tests/nip17-send-gift-wrap.test.mjs: forces the NIP-17 path (useNip17 +
  relay hints, nip44-capable signer) and asserts a kind-1059 gift wrap is
  published. Reverting the import reproduces the exact nip17-keygen-failed error;
  the existing DM tests only covered the legacy kind-4/NIP-04 path (they delete
  nip44Encrypt), so this path had no coverage.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-01 09:29:43 -04:00

36 lines
900 B
JavaScript

export const HEX64_REGEX = /^[0-9a-f]{64}$/i;
export function normalizeHexString(value) {
if (typeof value !== "string") {
return "";
}
const trimmed = value.trim();
return trimmed ? trimmed.toLowerCase() : "";
}
export const normalizeHexId = normalizeHexString;
export const normalizeHexPubkey = normalizeHexString;
// Lowercase hex encoding of a byte array (e.g. a generated secret key).
export function bytesToHex(bytes) {
if (!bytes) {
return "";
}
return Array.from(bytes)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
}
export function normalizeHexHash(value) {
if (value === undefined || value === null) {
return "";
}
const stringValue = typeof value === "string" ? value : String(value);
const trimmed = stringValue.trim().toLowerCase();
if (!trimmed) {
return "";
}
return HEX64_REGEX.test(trimmed) ? trimmed : "";
}