1
- import { EventTemplate , kinds , NostrEvent } from "nostr-tools" ;
1
+ import { EventTemplate , NostrEvent } from "nostr-tools" ;
2
2
3
- import { GROUPS_LIST_KIND } from "./groups.js" ;
4
3
import { EventStore } from "../event-store/event-store.js" ;
5
4
import { unixNow } from "./time.js" ;
6
5
import { isEvent } from "./event.js" ;
7
-
8
- export type HiddenTagsSigner = {
9
- nip04 ?: {
10
- encrypt : ( pubkey : string , plaintext : string ) => Promise < string > | string ;
11
- decrypt : ( pubkey : string , ciphertext : string ) => Promise < string > | string ;
12
- } ;
13
- nip44 ?: {
14
- encrypt : ( pubkey : string , plaintext : string ) => Promise < string > | string ;
15
- decrypt : ( pubkey : string , ciphertext : string ) => Promise < string > | string ;
16
- } ;
17
- } ;
6
+ import {
7
+ canHaveHiddenContent ,
8
+ getHiddenContentEncryptionMethods ,
9
+ HiddenContentSigner ,
10
+ unlockHiddenContent ,
11
+ } from "./hidden-content.js" ;
18
12
19
13
export const HiddenTagsSymbol = Symbol . for ( "hidden-tags" ) ;
20
14
21
- /** Various event kinds that can have encrypted tags in their content and which encryption method they use */
22
- export const EventEncryptionMethod : Record < number , "nip04" | "nip44" > = {
23
- // NIP-60 wallet
24
- 17375 : "nip44" ,
25
-
26
- // NIP-51 lists
27
- [ kinds . BookmarkList ] : "nip04" ,
28
- [ kinds . InterestsList ] : "nip04" ,
29
- [ kinds . Mutelist ] : "nip04" ,
30
- [ kinds . CommunitiesList ] : "nip04" ,
31
- [ kinds . PublicChatsList ] : "nip04" ,
32
- [ kinds . SearchRelaysList ] : "nip04" ,
33
- [ GROUPS_LIST_KIND ] : "nip04" ,
34
-
35
- // NIP-51 sets
36
- [ kinds . Bookmarksets ] : "nip04" ,
37
- [ kinds . Relaysets ] : "nip04" ,
38
- [ kinds . Followsets ] : "nip04" ,
39
- [ kinds . Curationsets ] : "nip04" ,
40
- [ kinds . Interestsets ] : "nip04" ,
41
- } ;
42
-
43
15
/** Checks if an event can have hidden tags */
44
16
export function canHaveHiddenTags ( kind : number ) : boolean {
45
- return EventEncryptionMethod [ kind ] !== undefined ;
17
+ return canHaveHiddenContent ( kind ) ;
46
18
}
47
19
48
20
/** Checks if an event has hidden tags */
@@ -61,12 +33,8 @@ export function isHiddenTagsLocked(event: NostrEvent): boolean {
61
33
}
62
34
63
35
/** Returns either nip04 or nip44 encryption method depending on list kind */
64
- export function getListEncryptionMethods ( kind : number , signer : HiddenTagsSigner ) {
65
- const method = EventEncryptionMethod [ kind ] ;
66
- const encryption = signer [ method ] ;
67
- if ( ! encryption ) throw new Error ( `Signer does not support ${ method } encryption` ) ;
68
-
69
- return encryption ;
36
+ export function getListEncryptionMethods ( kind : number , signer : HiddenContentSigner ) {
37
+ return getHiddenContentEncryptionMethods ( kind , signer ) ;
70
38
}
71
39
72
40
/**
@@ -78,12 +46,11 @@ export function getListEncryptionMethods(kind: number, signer: HiddenTagsSigner)
78
46
*/
79
47
export async function unlockHiddenTags < T extends { kind : number ; pubkey : string ; content : string } > (
80
48
event : T ,
81
- signer : HiddenTagsSigner ,
49
+ signer : HiddenContentSigner ,
82
50
store ?: EventStore ,
83
51
) : Promise < T > {
84
52
if ( ! canHaveHiddenTags ( event . kind ) ) throw new Error ( "Event kind does not support hidden tags" ) ;
85
- const encryption = getListEncryptionMethods ( event . kind , signer ) ;
86
- const plaintext = await encryption . decrypt ( event . pubkey , event . content ) ;
53
+ const plaintext = await unlockHiddenContent ( event , signer ) ;
87
54
88
55
const parsed = JSON . parse ( plaintext ) as string [ ] [ ] ;
89
56
if ( ! Array . isArray ( parsed ) ) throw new Error ( "Content is not an array of tags" ) ;
@@ -100,12 +67,13 @@ export async function unlockHiddenTags<T extends { kind: number; pubkey: string;
100
67
101
68
/**
102
69
* Override the hidden tags in an event
70
+ * @deprecated use EventFactory to create draft events
103
71
* @throws
104
72
*/
105
73
export async function overrideHiddenTags (
106
74
event : NostrEvent ,
107
75
hidden : string [ ] [ ] ,
108
- signer : HiddenTagsSigner ,
76
+ signer : HiddenContentSigner ,
109
77
) : Promise < EventTemplate > {
110
78
if ( ! canHaveHiddenTags ( event . kind ) ) throw new Error ( "Event kind does not support hidden tags" ) ;
111
79
const encryption = getListEncryptionMethods ( event . kind , signer ) ;
0 commit comments