feat(utils): add preimage and payment hash helper#524
feat(utils): add preimage and payment hash helper#524naveenkumar29052006 wants to merge 4 commits intogetAlby:masterfrom
Conversation
📝 WalkthroughWalkthroughExtracts preimage/payment-hash generation into a new async utility in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/utils.ts`:
- Around line 5-13: Add an explicit return type to
generatePreimageAndPaymentHash (async function generatePreimageAndPaymentHash():
Promise<{ preimage: string; paymentHash: string }>) and make the function
Node-compatible by importing Node's crypto module (import crypto from 'crypto')
and switching from browser APIs to Node equivalents: use crypto.randomBytes(32)
for the preimage bytes and
crypto.createHash('sha256').update(preimageBytes).digest() for the payment hash,
then convert both byte arrays to hex via the existing toHexString helper.
|
@rolznz sir could u plz check here |
|
@naveenkumar29052006 looks good, thanks! can you update the PR description to reference the issue ( |
|
@rolznz Done Sir |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In @.vscode/settings.json:
- Around line 5-7: Remove the TypeScript-specific formatter override that sets
"editor.defaultFormatter": "vscode.typescript-language-features" under the
"[typescript]" VSCode settings so TypeScript files use the project's canonical
formatter (e.g., Prettier) and avoid inconsistent formatting; update or delete
the "[typescript]" block in .vscode/settings.json so it no longer enforces that
formatter.
🧹 Nitpick comments (3)
src/nwc/NWCClient.test.ts (1)
54-71: Tests look good; consider asserting specific error messages.The new validation tests correctly cover the two error scenarios. For improved test robustness, consider using
toThrow(/expected message pattern/)to verify the exact error is thrown, preventing false positives if a different error occurs.test("throws when no relay is provided", () => { expect(() => NWCClient.parseWalletConnectUrl( "nostr+walletconnect://abc123", ), - ).toThrow(); + ).toThrow(/no relay/i); }); test("throws when secret is required but missing", () => { expect(() => new NWCClient({ nostrWalletConnectUrl: "nostr+walletconnect://abc123?relay=wss://relay.example.com", }), - ).toThrow(); + ).toThrow(/missing secret/i); });src/nwc/NWCClient.ts (2)
141-142: Good integration; note that existing relay check is now unreachable.The validation call is correctly placed. However, the check on lines 125-127 is now effectively dead code since:
url.searchParams.getAll("relay")returns an empty array (notnull/undefined) when no relay params existvalidateWalletConnectUrlon line 141 throws for emptyrelayUrlsbefore returningConsider removing lines 125-127 to avoid confusion.
147-154: Double validation is redundant.
parseWalletConnectUrl(line 147-149) already callsvalidateWalletConnectUrl(options)internally before returning. Line 150 then callsvalidateWalletConnectUrl(parsed, true)again, re-running all the same checks plus the secret requirement.Consider either:
- Adding an optional
requireSecretparameter toparseWalletConnectUrl, or- Having
parseWalletConnectUrlskip validation and let callers validate as neededOption 1: Add parameter to parseWalletConnectUrl
- static parseWalletConnectUrl(walletConnectUrl: string): NWCOptions { + static parseWalletConnectUrl(walletConnectUrl: string, requireSecret = false): NWCOptions { // ... parsing logic ... - NWCClient.validateWalletConnectUrl(options); + NWCClient.validateWalletConnectUrl(options, requireSecret); return options; }Then in the constructor:
if (options && options.nostrWalletConnectUrl) { const parsed = NWCClient.parseWalletConnectUrl( options.nostrWalletConnectUrl, + true, // requireSecret ); - NWCClient.validateWalletConnectUrl(parsed, true); options = {
This PR adds a small utility helper to generate a random 32-byte preimage and its corresponding SHA-256 payment hash.
That fixes issue Fixes #523
The goal is to avoid duplicating Web Crypto and hex-encoding logic in multiple places (e.g. hold invoice flows and examples) and keep the implementation consistent and easy to reuse.
No behavior changes — this is a pure DX and maintainability improvement.

Summary by CodeRabbit