-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bot/modules/nostr: convert to TS #627
Changes from all commits
0e0b0b4
d98cb39
af78cbb
10c646c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,21 @@ | ||||||||||||||||||||||||||||||||||||||
import { generateSecretKey, getPublicKey as nostrGetPublicKey } from 'nostr-tools'; | ||||||||||||||||||||||||||||||||||||||
import { SimplePool } from 'nostr-tools'; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
const nostrSkEnvVar = process.env.NOSTR_SK; | ||||||||||||||||||||||||||||||||||||||
const sk = nostrSkEnvVar ? Buffer.from(nostrSkEnvVar, 'hex') : generateSecretKey(); | ||||||||||||||||||||||||||||||||||||||
const pk = nostrGetPublicKey(sk); | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export const getPrivateKey = () => sk; | ||||||||||||||||||||||||||||||||||||||
export const getPublicKey = () => pk; | ||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export const pool = new SimplePool(); | ||||||||||||||||||||||||||||||||||||||
const relays = (env => { | ||||||||||||||||||||||||||||||||||||||
if (!env.RELAYS) return []; | ||||||||||||||||||||||||||||||||||||||
return env.RELAYS.split(','); | ||||||||||||||||||||||||||||||||||||||
})(process.env); | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+11
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add type annotation for relays array and validate relay URLs The relays array should be typed and URLs should be validated. export const pool = new SimplePool();
+const isValidRelayUrl = (url: string): boolean => {
+ try {
+ return new URL(url).protocol === 'wss:';
+ } catch {
+ return false;
+ }
+};
+
+const relays: string[] = ((env: NodeJS.ProcessEnv) => {
if (!env.RELAYS) return [];
- return env.RELAYS.split(',');
+ return env.RELAYS.split(',').filter(isValidRelayUrl);
})(process.env); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||
export const addRelay = (relay: string) => { | ||||||||||||||||||||||||||||||||||||||
relays.push(relay); | ||||||||||||||||||||||||||||||||||||||
relays.map(relay => pool.ensureRelay(relay)); | ||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||
Comment on lines
+17
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve relay management The current implementation has potential issues:
-export const addRelay = (relay: string) => {
+export const addRelay = (relay: string): boolean => {
+ if (!isValidRelayUrl(relay) || relays.includes(relay)) {
+ return false;
+ }
relays.push(relay);
- relays.map(relay => pool.ensureRelay(relay));
+ pool.ensureRelay(relay);
+ return true;
};
|
||||||||||||||||||||||||||||||||||||||
export const getRelays = () => relays; |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,11 @@ | ||||||||
import { nip19 } from 'nostr-tools'; | ||||||||
|
||||||||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add logger import Import the logger module to support error logging in the decodeNpub function. import { nip19 } from 'nostr-tools';
+import { logger } from '../../../logger'; 📝 Committable suggestion
Suggested change
|
||||||||
export const decodeNpub = (npub: string) => { | ||||||||
try { | ||||||||
const { type, data } = nip19.decode(npub); | ||||||||
if (type === 'npub') return data; | ||||||||
} catch (err) {} | ||||||||
}; | ||||||||
export const encodeNpub = (hex: string) => { | ||||||||
return nip19.npubEncode(hex); | ||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add validation for NOSTR_SK environment variable
The hex format of NOSTR_SK should be validated before usage.
📝 Committable suggestion