#14904: fix(nostr): handle string return from nip19.decode in normalizePubkey
channel: nostr
size: XS
trusted-contributor
experienced-contributor
Cluster:
Network Error Handling Improvements
## Summary
- Fixes #8570
- `nostr-tools` ≥2.23 changed `nip19.decode()` to return a hex string for `npub` keys instead of `Uint8Array`
- `normalizePubkey()` used `Array.from(decoded.data)` which splits a string into individual characters, producing garbage hex output (128 chars of per-byte hex instead of 64)
- Added a `typeof` check before the `Uint8Array` conversion to handle both string (≥2.23) and `Uint8Array` (older) return types
## Test plan
- [x] Added 2 new tests for `normalizePubkey` npub format handling (converts npub to hex, roundtrips with `pubkeyToNpub`)
- [x] Added 2 new tests for `validatePrivateKey` nsec format (accepts valid nsec, returns same bytes as hex equivalent)
- [x] Fixed pre-existing invalid `TEST_NSEC` constant (bad checksum) by dynamically encoding from `TEST_HEX_KEY`
- [x] All 4 new tests fail before the fix, pass after
- [x] All 36 tests pass
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates the Nostr extension’s `normalizePubkey()` helper to correctly handle `nostr-tools`’ `nip19.decode()` behavior change for `npub` keys (returning a hex `string` instead of a `Uint8Array`), preventing incorrect hex normalization.
It also adds test coverage for:
- `normalizePubkey` npub handling (npub → 64-char lowercase hex, roundtrip with `pubkeyToNpub`)
- `validatePrivateKey` nsec handling (accepts a valid nsec, returns expected bytes)
No merge-blocking issues were found in the changeset.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The change is narrowly scoped (type guard on `decoded.data`), preserves previous behavior for older `nostr-tools` versions, and is covered by targeted new tests for both npub and nsec flows. No regressions or incorrect assumptions were identified within the repository code.
- No files require special attention
<sub>Last reviewed commit: d8dd20e</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#4878: fix: string/type handling and API fixes (#4537, #4380, #4373, #4547...
by lailoo · 2026-01-30
71.9%
#9825: feat(nostr): upgrade to NIP-17 with NIP-65 relay discovery
by nash-the-ai · 2026-02-05
71.7%
#20496: test(utils): add comprehensive unit tests for utility functions
by masifislamm · 2026-02-19
71.3%
#12665: fix(tools): recognise tool groups that expand to plugin tools in al...
by mcaxtr · 2026-02-09
71.3%
#10257: fix(security): anchor MIME sanitization regex and block fullwidth b...
by nu-gui · 2026-02-06
71.3%
#11880: fix: guard decodeURIComponent against malformed percent-encoding in...
by Yida-Dev · 2026-02-08
71.0%
#21979: chore: add knip dependency check and remove unused dependencies
by chansuke · 2026-02-20
70.3%
#16507: feat(nostr): refactor channel dispatch logic (AI-assisted)
by tanujbhaud · 2026-02-14
70.2%
#17900: refactor(security): extract shared normalizeAllowFromList into audi...
by iyoda · 2026-02-16
70.1%
#23772: fix(ci): unblock repo-wide check gate type regressions
by SmithLabsLLC · 2026-02-22
69.8%