Skip to content

Commit

Permalink
filterForEventsTaggingId
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Jul 1, 2024
1 parent 26c7ee7 commit cbe10f7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ndk/src/subscription/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export class NDKSubscription extends EventEmitter<{

// if relayset is empty, we can't start, log it
if (!this.relayFilters || this.relayFilters.size === 0) {
this.debug(`No relays to subscribe to`, this.pool.relays.size);
this.debug(`No relays to subscribe to (%d connected relays)`, this.pool.connectedRelays().length);
return;
}

Expand Down
28 changes: 27 additions & 1 deletion ndk/src/subscription/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type NDKFilter, NDKSubscription } from ".";
import { NDK } from "../ndk";
import { generateSubId } from "./utils";
import { filterForEventsTaggingId, filterFromId, generateSubId } from "./utils";

const ndk = new NDK();

Expand Down Expand Up @@ -56,3 +56,29 @@ describe("generateSubId", () => {
expect(subId.length).toBeLessThanOrEqual(24);
});
});

describe("filterFromId", () => {
it("handles nevents", () => {
const filter = filterFromId("nevent1qgs9kqvr4dkruv3t7n2pc6e6a7v9v2s5fprmwjv4gde8c4fe5y29v0spzamhxue69uhhyetvv9ujuurjd9kkzmpwdejhgtcqype6ycavy2e9zpx9mzeuekaahgw96ken0mzkcmgz40ljccwyrn88gxv2ewr");
expect(filter).toEqual({
ids: ["73a263ac22b25104c5d8b3ccdbbdba1c5d5b337ec56c6d02abff2c61c41cce74"],
authors: ["5b0183ab6c3e322bf4d41c6b3aef98562a144847b7499543727c5539a114563e"],
});
})
})

describe("filterForEventsTaggingId", () => {
fit("handles nevents", () => {
const filter = filterForEventsTaggingId("nevent1qgs9kqvr4dkruv3t7n2pc6e6a7v9v2s5fprmwjv4gde8c4fe5y29v0spzamhxue69uhhyetvv9ujuurjd9kkzmpwdejhgtcqype6ycavy2e9zpx9mzeuekaahgw96ken0mzkcmgz40ljccwyrn88gxv2ewr");
expect(filter).toEqual({
"#e": ["73a263ac22b25104c5d8b3ccdbbdba1c5d5b337ec56c6d02abff2c61c41cce74"],
});
})

fit("handles naddr", () => {
const filter = filterForEventsTaggingId("naddr1qvzqqqr4gupzpjjwt0eqm6as279wf079c0j42jysp2t4s37u8pg5w2dfyktxgkntqqxnzde38yen2desxqmn2d3332u3ff");
expect(filter).toEqual({
"#a": ["30023:ca4e5bf20debb0578ae4bfc5c3e55548900a975847dc38514729a92596645a6b:1719357007561"],
});
})
})
40 changes: 38 additions & 2 deletions ndk/src/subscription/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,40 @@ export function generateSubId(subscriptions: NDKSubscription[], filters: NDKFilt
return subId;
}

/**
* Creates a valid nostr filter to REQ events that are tagging a NIP-19 bech32
* @param id Bech32 of the event
* @example
* const bech32 = "nevent1qgs9kqvr4dkruv3t7n2pc6e6a7v9v2s5fprmwjv4gde8c4fe5y29v0spzamhxue69uhhyetvv9ujuurjd9kkzmpwdejhgtcqype6ycavy2e9zpx9mzeuekaahgw96ken0mzkcmgz40ljccwyrn88gxv2ewr"
* const filter = filterForEventsTaggingId(bech32);
* // filter => { "#e": [<id>] }
*
* @example
* const bech32 = "naddr1qvzqqqr4gupzpjjwt0eqm6as279wf079c0j42jysp2t4s37u8pg5w2dfyktxgkntqqxnzde38yen2desxqmn2d3332u3ff";
* const filter = filterForEventsTaggingId(bech32);
* // filter => { "#a": ["30023:ca4e5bf20debb0578ae4bfc5c3e55548900a975847dc38514729a92596645a6b:1719357007561"]}
*/
export function filterForEventsTaggingId(id: string): NDKFilter | undefined {
try {
const decoded = nip19.decode(id);

switch (decoded.type) {
case 'naddr': return { "#a": [`${decoded.data.kind}:${decoded.data.pubkey}:${decoded.data.identifier}`] }
case 'nevent': return { "#e": [decoded.data.id] }
case 'note': return { "#e": [decoded.data] }
case 'nprofile': return { "#p": [decoded.data.pubkey] }
case 'npub': return { "#p": [decoded.data] }
}
} catch {}
}

/**
* Creates a valid nostr filter from an event id or a NIP-19 bech32.
*
* @example
* const bech32 = "nevent1qgs9kqvr4dkruv3t7n2pc6e6a7v9v2s5fprmwjv4gde8c4fe5y29v0spzamhxue69uhhyetvv9ujuurjd9kkzmpwdejhgtcqype6ycavy2e9zpx9mzeuekaahgw96ken0mzkcmgz40ljccwyrn88gxv2ewr"
* const filter = filterFromBech32(bech32);
* // filter => { ids: [...], authors: [...] }
*/
export function filterFromId(id: string): NDKFilter {
let decoded;
Expand All @@ -150,8 +182,12 @@ export function filterFromId(id: string): NDKFilter {
decoded = nip19.decode(id);

switch (decoded.type) {
case "nevent":
return { ids: [decoded.data.id] };
case "nevent": {
const filter: NDKFilter = { ids: [decoded.data.id] };
if (decoded.data.author) filter.authors = [decoded.data.author];
if (decoded.data.kind) filter.kinds = [decoded.data.kind];
return filter;
}
case "note":
return { ids: [decoded.data] };
case "naddr":
Expand Down

0 comments on commit cbe10f7

Please sign in to comment.