applesauce-common 7.0.0 → 7.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/casts/index.d.ts +1 -0
- package/dist/casts/index.js +1 -0
- package/dist/casts/napplet-handlers.d.ts +39 -0
- package/dist/casts/napplet-handlers.js +91 -0
- package/dist/factories/file-message.d.ts +24 -0
- package/dist/factories/file-message.js +37 -0
- package/dist/factories/index.d.ts +2 -0
- package/dist/factories/index.js +2 -0
- package/dist/factories/napplet-handlers.d.ts +34 -0
- package/dist/factories/napplet-handlers.js +77 -0
- package/dist/factories/wrapped-message.js +10 -4
- package/dist/helpers/encrypted-content-cache.d.ts +2 -11
- package/dist/helpers/encrypted-content-cache.js +6 -31
- package/dist/helpers/encrypted-file.d.ts +41 -0
- package/dist/helpers/encrypted-file.js +64 -0
- package/dist/helpers/file-message.d.ts +18 -0
- package/dist/helpers/file-message.js +44 -0
- package/dist/helpers/file-metadata.d.ts +9 -4
- package/dist/helpers/file-metadata.js +16 -0
- package/dist/helpers/index.d.ts +3 -0
- package/dist/helpers/index.js +3 -0
- package/dist/helpers/messages.js +3 -2
- package/dist/helpers/mute.js +2 -2
- package/dist/helpers/napplet-handlers.d.ts +79 -0
- package/dist/helpers/napplet-handlers.js +187 -0
- package/dist/helpers/napplet.d.ts +18 -0
- package/dist/helpers/napplet.js +33 -0
- package/dist/helpers/wrapped-messages.d.ts +2 -0
- package/dist/helpers/wrapped-messages.js +6 -1
- package/dist/models/index.d.ts +1 -0
- package/dist/models/index.js +1 -0
- package/dist/models/legacy-messages.js +6 -1
- package/dist/models/napplet-handlers.d.ts +16 -0
- package/dist/models/napplet-handlers.js +45 -0
- package/dist/models/wrapped-messages.js +9 -7
- package/dist/observable/filter-timeline-by-mutes.js +1 -1
- package/dist/operations/file-message.d.ts +7 -0
- package/dist/operations/file-message.js +34 -0
- package/dist/operations/index.d.ts +2 -0
- package/dist/operations/index.js +2 -0
- package/dist/operations/napplet-handlers.d.ts +47 -0
- package/dist/operations/napplet-handlers.js +161 -0
- package/dist/operations/wrapped-message.js +2 -2
- package/package.json +12 -16
package/dist/casts/index.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export * from "./git-thread.js";
|
|
|
20
20
|
export * from "./groups.js";
|
|
21
21
|
export * from "./mutes.js";
|
|
22
22
|
export * from "./napplet.js";
|
|
23
|
+
export * from "./napplet-handlers.js";
|
|
23
24
|
export * from "./note.js";
|
|
24
25
|
export * from "./profile.js";
|
|
25
26
|
export * from "./profile-badges.js";
|
package/dist/casts/index.js
CHANGED
|
@@ -20,6 +20,7 @@ export * from "./git-thread.js";
|
|
|
20
20
|
export * from "./groups.js";
|
|
21
21
|
export * from "./mutes.js";
|
|
22
22
|
export * from "./napplet.js";
|
|
23
|
+
export * from "./napplet-handlers.js";
|
|
23
24
|
export * from "./note.js";
|
|
24
25
|
export * from "./profile.js";
|
|
25
26
|
export * from "./profile-badges.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ChainableObservable } from "applesauce-core";
|
|
2
|
+
import { NostrEvent } from "applesauce-core/helpers/event";
|
|
3
|
+
import { HiddenContentSigner } from "applesauce-core/helpers/hidden-content";
|
|
4
|
+
import { NappletHandlerCandidate, NappletHandlersEvent } from "../helpers/napplet-handlers.js";
|
|
5
|
+
import { CastRefEventStore, EventCast } from "./cast.js";
|
|
6
|
+
import { Napplet } from "./napplet.js";
|
|
7
|
+
/** Cast for napplet handler preferences (kind 31129) */
|
|
8
|
+
export declare class NappletHandlers extends EventCast<NappletHandlersEvent> {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(event: NostrEvent, store: CastRefEventStore);
|
|
11
|
+
/** The archetype the preferences apply to */
|
|
12
|
+
get archetype(): string;
|
|
13
|
+
/** The public entries, which other users read as recommendations */
|
|
14
|
+
get publicEntries(): import("../helpers/napplet-handlers.js").NappletHandlerEntries;
|
|
15
|
+
/** The private entries if unlocked, otherwise undefined */
|
|
16
|
+
get hiddenEntries(): import("../helpers/napplet-handlers.js").NappletHandlerEntries | undefined;
|
|
17
|
+
/** An observable of the private entries that emits undefined until they are unlocked */
|
|
18
|
+
get hiddenEntries$(): ChainableObservable<import("../helpers/napplet-handlers.js").NappletHandlerEntries | undefined>;
|
|
19
|
+
/** Whether the event has encrypted private entries */
|
|
20
|
+
get hasHidden(): boolean;
|
|
21
|
+
/** Whether the private entries are unlocked */
|
|
22
|
+
get unlocked(): boolean;
|
|
23
|
+
/** Decrypts the private entries */
|
|
24
|
+
unlock(signer: HiddenContentSigner): Promise<import("../helpers/napplet-handlers.js").NappletHandlerEntries>;
|
|
25
|
+
/** Returns the handlers to try for an intent name or convention identity, in resolution order */
|
|
26
|
+
candidates(intent: string): NappletHandlerCandidate[];
|
|
27
|
+
/** Subscribes to the archetype-wide handlers, private entries first once unlocked */
|
|
28
|
+
get handlers$(): ChainableObservable<(Napplet | undefined)[]>;
|
|
29
|
+
/** Subscribes to the handler candidates for an intent name or convention identity, in resolution order */
|
|
30
|
+
candidates$(intent: string): ChainableObservable<(Napplet | undefined)[]>;
|
|
31
|
+
}
|
|
32
|
+
declare module "applesauce-core/casts" {
|
|
33
|
+
interface User {
|
|
34
|
+
/** Subscribes to the user's napplet handler preferences for every archetype */
|
|
35
|
+
get nappletHandlers$(): ChainableObservable<NappletHandlers[]>;
|
|
36
|
+
/** Subscribes to the user's own napplet handler preferences for an archetype */
|
|
37
|
+
nappletHandlers(archetype: string): ChainableObservable<NappletHandlers | undefined>;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { User } from "applesauce-core/casts/user";
|
|
2
|
+
import { hasHiddenTags } from "applesauce-core/helpers/hidden-tags";
|
|
3
|
+
import { watchEventUpdates } from "applesauce-core/observable";
|
|
4
|
+
import { combineLatest, map, of, switchMap } from "rxjs";
|
|
5
|
+
import { getHiddenNappletHandlers, getNappletHandlerCandidates, getNappletHandlersArchetype, getPublicNappletHandlers, isHiddenNappletHandlersUnlocked, isValidNappletHandlers, NAPPLET_HANDLERS_KIND, unlockHiddenNappletHandlers, } from "../helpers/napplet-handlers.js";
|
|
6
|
+
import { createNappletConvention } from "../helpers/napplet.js";
|
|
7
|
+
import { castEventStream, castTimelineStream } from "../observable/cast-stream.js";
|
|
8
|
+
import { EventCast } from "./cast.js";
|
|
9
|
+
import { Napplet, NappletSnapshot } from "./napplet.js";
|
|
10
|
+
/** Cast for napplet handler preferences (kind 31129) */
|
|
11
|
+
export class NappletHandlers extends EventCast {
|
|
12
|
+
constructor(event, store) {
|
|
13
|
+
if (!isValidNappletHandlers(event))
|
|
14
|
+
throw new Error("Invalid napplet handler preferences");
|
|
15
|
+
super(event, store);
|
|
16
|
+
}
|
|
17
|
+
/** The archetype the preferences apply to */
|
|
18
|
+
get archetype() {
|
|
19
|
+
return getNappletHandlersArchetype(this.event);
|
|
20
|
+
}
|
|
21
|
+
/** The public entries, which other users read as recommendations */
|
|
22
|
+
get publicEntries() {
|
|
23
|
+
return getPublicNappletHandlers(this.event);
|
|
24
|
+
}
|
|
25
|
+
/** The private entries if unlocked, otherwise undefined */
|
|
26
|
+
get hiddenEntries() {
|
|
27
|
+
return getHiddenNappletHandlers(this.event);
|
|
28
|
+
}
|
|
29
|
+
/** An observable of the private entries that emits undefined until they are unlocked */
|
|
30
|
+
get hiddenEntries$() {
|
|
31
|
+
return this.$$ref("hiddenEntries$", (store) => of(this.event).pipe(watchEventUpdates(store), map((event) => (event ? getHiddenNappletHandlers(event) : undefined))));
|
|
32
|
+
}
|
|
33
|
+
/** Whether the event has encrypted private entries */
|
|
34
|
+
get hasHidden() {
|
|
35
|
+
return hasHiddenTags(this.event);
|
|
36
|
+
}
|
|
37
|
+
/** Whether the private entries are unlocked */
|
|
38
|
+
get unlocked() {
|
|
39
|
+
return isHiddenNappletHandlersUnlocked(this.event);
|
|
40
|
+
}
|
|
41
|
+
/** Decrypts the private entries */
|
|
42
|
+
unlock(signer) {
|
|
43
|
+
return unlockHiddenNappletHandlers(this.event, signer);
|
|
44
|
+
}
|
|
45
|
+
/** Returns the handlers to try for an intent name or convention identity, in resolution order */
|
|
46
|
+
candidates(intent) {
|
|
47
|
+
return getNappletHandlerCandidates(this.event, this.#identity(intent));
|
|
48
|
+
}
|
|
49
|
+
/** Subscribes to the archetype-wide handlers, private entries first once unlocked */
|
|
50
|
+
get handlers$() {
|
|
51
|
+
return this.$$ref("handlers$", (store) => this.hiddenEntries$.pipe(map((hidden) => [...(hidden?.handlers ?? []), ...this.publicEntries.handlers]), switchMap((refs) => this.#napplets$(refs, store))));
|
|
52
|
+
}
|
|
53
|
+
/** Subscribes to the handler candidates for an intent name or convention identity, in resolution order */
|
|
54
|
+
candidates$(intent) {
|
|
55
|
+
const identity = this.#identity(intent);
|
|
56
|
+
return this.$$ref(`candidates$:${identity}`, (store) => this.hiddenEntries$.pipe(map(() => getNappletHandlerCandidates(this.event, identity).map((candidate) => candidate.ref)), switchMap((refs) => this.#napplets$(refs, store))));
|
|
57
|
+
}
|
|
58
|
+
/** Expands a bare intent name into a convention identity for this archetype */
|
|
59
|
+
#identity(intent) {
|
|
60
|
+
return intent.startsWith("napplet:") ? intent : createNappletConvention(this.archetype, intent);
|
|
61
|
+
}
|
|
62
|
+
/** Subscribes to the napplets for a list of references, in the same order */
|
|
63
|
+
#napplets$(refs, store) {
|
|
64
|
+
if (refs.length === 0)
|
|
65
|
+
return of([]);
|
|
66
|
+
return combineLatest(refs.map((ref) =>
|
|
67
|
+
// A pin only resolves to a snapshot, never to the napplet it derives from
|
|
68
|
+
ref.type === "snapshot"
|
|
69
|
+
? store.event(ref.pointer).pipe(castEventStream(NappletSnapshot, store))
|
|
70
|
+
: store.replaceable(ref.pointer).pipe(castEventStream(Napplet, store))));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
Object.defineProperty(User.prototype, "nappletHandlers$", {
|
|
74
|
+
get() {
|
|
75
|
+
return this.$$ref("nappletHandlers$", (store) => store
|
|
76
|
+
.timeline({ kinds: [NAPPLET_HANDLERS_KIND], authors: [this.pubkey] })
|
|
77
|
+
.pipe(castTimelineStream(NappletHandlers, store)));
|
|
78
|
+
},
|
|
79
|
+
configurable: true,
|
|
80
|
+
enumerable: false,
|
|
81
|
+
});
|
|
82
|
+
Object.defineProperty(User.prototype, "nappletHandlers", {
|
|
83
|
+
value(archetype) {
|
|
84
|
+
return this.$$ref(`nappletHandlers:${archetype}`, (store) => store
|
|
85
|
+
.addressable({ kind: NAPPLET_HANDLERS_KIND, pubkey: this.pubkey, identifier: archetype })
|
|
86
|
+
.pipe(castEventStream(NappletHandlers, store)));
|
|
87
|
+
},
|
|
88
|
+
configurable: true,
|
|
89
|
+
enumerable: false,
|
|
90
|
+
writable: true,
|
|
91
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { EventFactory } from "applesauce-core/factories";
|
|
2
|
+
import { kinds, KnownEventTemplate } from "applesauce-core/helpers";
|
|
3
|
+
import { FileMetadataFields } from "../helpers/file-metadata.js";
|
|
4
|
+
import { Rumor } from "../helpers/gift-wrap.js";
|
|
5
|
+
export type FileMessageTemplate = KnownEventTemplate<kinds.FileMessage>;
|
|
6
|
+
/** A factory for NIP-17 kind 15 file messages, which carry the URL and decryption key of an encrypted file */
|
|
7
|
+
export declare class FileMessageFactory extends EventFactory<kinds.FileMessage, FileMessageTemplate> {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new file message
|
|
10
|
+
* @param recipients A single recipient pubkey or an array of recipient pubkeys
|
|
11
|
+
* @param file The uploaded file's URL, type, hashes, and decryption fields
|
|
12
|
+
* @throws if the file has no URL
|
|
13
|
+
*/
|
|
14
|
+
static create(recipients: string | string[], file: FileMetadataFields): FileMessageFactory;
|
|
15
|
+
/**
|
|
16
|
+
* Sets the file URL and file tags
|
|
17
|
+
* @throws if the file has no URL
|
|
18
|
+
*/
|
|
19
|
+
file(file: FileMetadataFields): this;
|
|
20
|
+
/** Sets the parent message "e" tag for NIP-17 reply threading */
|
|
21
|
+
replyTo(parent: Rumor | string): this;
|
|
22
|
+
/** Sets the subject tag for the message thread */
|
|
23
|
+
subject(subject: string): this;
|
|
24
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { blankEventTemplate, EventFactory } from "applesauce-core/factories";
|
|
2
|
+
import { kinds } from "applesauce-core/helpers";
|
|
3
|
+
import { includeNameValueTag } from "applesauce-core/operations/tags";
|
|
4
|
+
import { setFileMessage } from "../operations/file-message.js";
|
|
5
|
+
import { setParent, setSubject } from "../operations/wrapped-message.js";
|
|
6
|
+
/** A factory for NIP-17 kind 15 file messages, which carry the URL and decryption key of an encrypted file */
|
|
7
|
+
export class FileMessageFactory extends EventFactory {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new file message
|
|
10
|
+
* @param recipients A single recipient pubkey or an array of recipient pubkeys
|
|
11
|
+
* @param file The uploaded file's URL, type, hashes, and decryption fields
|
|
12
|
+
* @throws if the file has no URL
|
|
13
|
+
*/
|
|
14
|
+
static create(recipients, file) {
|
|
15
|
+
const recipientList = typeof recipients === "string" ? [recipients] : recipients;
|
|
16
|
+
let factory = new FileMessageFactory((res) => res(blankEventTemplate(kinds.FileMessage))).file(file);
|
|
17
|
+
for (const recipient of recipientList) {
|
|
18
|
+
factory = factory.chain(includeNameValueTag(["p", recipient]));
|
|
19
|
+
}
|
|
20
|
+
return factory;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Sets the file URL and file tags
|
|
24
|
+
* @throws if the file has no URL
|
|
25
|
+
*/
|
|
26
|
+
file(file) {
|
|
27
|
+
return this.chain(setFileMessage(file));
|
|
28
|
+
}
|
|
29
|
+
/** Sets the parent message "e" tag for NIP-17 reply threading */
|
|
30
|
+
replyTo(parent) {
|
|
31
|
+
return this.chain(setParent(parent));
|
|
32
|
+
}
|
|
33
|
+
/** Sets the subject tag for the message thread */
|
|
34
|
+
subject(subject) {
|
|
35
|
+
return this.chain(setSubject(subject));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -14,6 +14,7 @@ export * from "./group.js";
|
|
|
14
14
|
export * from "./group-management.js";
|
|
15
15
|
export * from "./legacy-message.js";
|
|
16
16
|
export * from "./wrapped-message.js";
|
|
17
|
+
export * from "./file-message.js";
|
|
17
18
|
export * from "./app-data.js";
|
|
18
19
|
export * from "./zap-goal.js";
|
|
19
20
|
export * from "./zap-request.js";
|
|
@@ -55,6 +56,7 @@ export * from "./nostr-web-token.js";
|
|
|
55
56
|
export * from "./root-napplet.js";
|
|
56
57
|
export * from "./named-napplet.js";
|
|
57
58
|
export * from "./napplet-snapshot.js";
|
|
59
|
+
export * from "./napplet-handlers.js";
|
|
58
60
|
export * from "./app-handler.js";
|
|
59
61
|
export * from "./app-recommendation.js";
|
|
60
62
|
export * from "./root-nsite.js";
|
package/dist/factories/index.js
CHANGED
|
@@ -14,6 +14,7 @@ export * from "./group.js";
|
|
|
14
14
|
export * from "./group-management.js";
|
|
15
15
|
export * from "./legacy-message.js";
|
|
16
16
|
export * from "./wrapped-message.js";
|
|
17
|
+
export * from "./file-message.js";
|
|
17
18
|
export * from "./app-data.js";
|
|
18
19
|
export * from "./zap-goal.js";
|
|
19
20
|
export * from "./zap-request.js";
|
|
@@ -55,6 +56,7 @@ export * from "./nostr-web-token.js";
|
|
|
55
56
|
export * from "./root-napplet.js";
|
|
56
57
|
export * from "./named-napplet.js";
|
|
57
58
|
export * from "./napplet-snapshot.js";
|
|
59
|
+
export * from "./napplet-handlers.js";
|
|
58
60
|
export * from "./app-handler.js";
|
|
59
61
|
export * from "./app-recommendation.js";
|
|
60
62
|
export * from "./root-nsite.js";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { EventFactory, TagOperation } from "applesauce-core/factories";
|
|
2
|
+
import { KnownEventTemplate, NostrEvent } from "applesauce-core/helpers/event";
|
|
3
|
+
import { NAPPLET_HANDLERS_KIND } from "../helpers/napplet-handlers.js";
|
|
4
|
+
import { NappletHandlerInput } from "../operations/napplet-handlers.js";
|
|
5
|
+
/** An event template for napplet handler preferences */
|
|
6
|
+
export type NappletHandlersTemplate = KnownEventTemplate<typeof NAPPLET_HANDLERS_KIND>;
|
|
7
|
+
/** A factory for napplet handler preferences (kind 31129) */
|
|
8
|
+
export declare class NappletHandlersFactory extends EventFactory<typeof NAPPLET_HANDLERS_KIND, NappletHandlersTemplate> {
|
|
9
|
+
/** Creates handler preferences for an archetype */
|
|
10
|
+
static create(archetype: string): NappletHandlersFactory;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a factory configured to modify existing handler preferences
|
|
13
|
+
* @throws if the event is not a valid handler preferences event
|
|
14
|
+
*/
|
|
15
|
+
static modify(event: NostrEvent): NappletHandlersFactory;
|
|
16
|
+
/** Applies a tag operation to the public or private tags */
|
|
17
|
+
protected modifyHandlerTags(operation: TagOperation, hidden: boolean): this;
|
|
18
|
+
/** Applies an intent tag operation once the archetype is known from the draft's `d` tag */
|
|
19
|
+
protected modifyIntentTags(intent: string, hidden: boolean, build: (identity: string) => TagOperation): this;
|
|
20
|
+
/** Adds an archetype-wide handler as the last fallback */
|
|
21
|
+
handler(input: NappletHandlerInput, hidden?: boolean): this;
|
|
22
|
+
/** Makes an archetype-wide handler the preferred one */
|
|
23
|
+
preferHandler(input: NappletHandlerInput, hidden?: boolean): this;
|
|
24
|
+
/** Removes an archetype-wide handler */
|
|
25
|
+
removeHandler(input: NappletHandlerInput, hidden?: boolean): this;
|
|
26
|
+
/** Adds a handler for one intent (`"edit"` or `"napplet:<archetype>/edit"`) as the last fallback */
|
|
27
|
+
intentHandler(intent: string, input: NappletHandlerInput, hidden?: boolean): this;
|
|
28
|
+
/** Makes a handler the preferred one for an intent */
|
|
29
|
+
preferIntentHandler(intent: string, input: NappletHandlerInput, hidden?: boolean): this;
|
|
30
|
+
/** Removes one handler for an intent, or every handler for it when no input is given */
|
|
31
|
+
removeIntentHandler(intent: string, input?: NappletHandlerInput, hidden?: boolean): this;
|
|
32
|
+
/** Removes every public and private entry, leaving an empty preferences event */
|
|
33
|
+
clear(): this;
|
|
34
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { blankEventTemplate, EventFactory, toEventTemplate } from "applesauce-core/factories";
|
|
2
|
+
import { getTagValue } from "applesauce-core/helpers/event";
|
|
3
|
+
import { modifyHiddenTags, modifyPublicTags } from "applesauce-core/operations/tags";
|
|
4
|
+
import { includeSingletonTag } from "applesauce-core/operations/tags";
|
|
5
|
+
import { setContent } from "applesauce-core/operations/content";
|
|
6
|
+
import { isValidNappletHandlers, NAPPLET_HANDLERS_KIND } from "../helpers/napplet-handlers.js";
|
|
7
|
+
import { createNappletConvention, parseNappletConvention } from "../helpers/napplet.js";
|
|
8
|
+
import { addNappletHandlerTag, addNappletIntentHandlerTag, clearNappletHandlerTags, preferNappletHandlerTag, preferNappletIntentHandlerTag, removeNappletHandlerTag, removeNappletIntentHandlerTag, } from "../operations/napplet-handlers.js";
|
|
9
|
+
/**
|
|
10
|
+
* Returns the convention identity for an intent name or identity within an archetype
|
|
11
|
+
* @throws if the identity is invalid or belongs to another archetype
|
|
12
|
+
*/
|
|
13
|
+
function getConventionIdentity(archetype, intent) {
|
|
14
|
+
if (!intent.startsWith("napplet:"))
|
|
15
|
+
return createNappletConvention(archetype, intent);
|
|
16
|
+
// Shells ignore `i` tags for another archetype, so refuse to write one
|
|
17
|
+
const convention = parseNappletConvention(intent);
|
|
18
|
+
if (convention?.archetype !== archetype)
|
|
19
|
+
throw new Error(`Intent ${intent} is not for archetype ${archetype}`);
|
|
20
|
+
return intent;
|
|
21
|
+
}
|
|
22
|
+
/** A factory for napplet handler preferences (kind 31129) */
|
|
23
|
+
export class NappletHandlersFactory extends EventFactory {
|
|
24
|
+
/** Creates handler preferences for an archetype */
|
|
25
|
+
static create(archetype) {
|
|
26
|
+
return new NappletHandlersFactory((res) => res(blankEventTemplate(NAPPLET_HANDLERS_KIND))).chain(includeSingletonTag(["d", archetype], true));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a factory configured to modify existing handler preferences
|
|
30
|
+
* @throws if the event is not a valid handler preferences event
|
|
31
|
+
*/
|
|
32
|
+
static modify(event) {
|
|
33
|
+
if (!isValidNappletHandlers(event))
|
|
34
|
+
throw new Error("Expected a napplet handler preferences event");
|
|
35
|
+
return new NappletHandlersFactory((res) => res(toEventTemplate(event)));
|
|
36
|
+
}
|
|
37
|
+
/** Applies a tag operation to the public or private tags */
|
|
38
|
+
modifyHandlerTags(operation, hidden) {
|
|
39
|
+
return hidden ? this.modifyHiddenTags(operation) : this.modifyPublicTags(operation);
|
|
40
|
+
}
|
|
41
|
+
/** Applies an intent tag operation once the archetype is known from the draft's `d` tag */
|
|
42
|
+
modifyIntentTags(intent, hidden, build) {
|
|
43
|
+
return this.chain(async (draft) => {
|
|
44
|
+
const identity = getConventionIdentity(getTagValue(draft, "d") ?? "", intent);
|
|
45
|
+
const operation = build(identity);
|
|
46
|
+
return hidden ? modifyHiddenTags(this.signer, operation)(draft) : modifyPublicTags(operation)(draft);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** Adds an archetype-wide handler as the last fallback */
|
|
50
|
+
handler(input, hidden = false) {
|
|
51
|
+
return this.modifyHandlerTags(addNappletHandlerTag(input), hidden);
|
|
52
|
+
}
|
|
53
|
+
/** Makes an archetype-wide handler the preferred one */
|
|
54
|
+
preferHandler(input, hidden = false) {
|
|
55
|
+
return this.modifyHandlerTags(preferNappletHandlerTag(input), hidden);
|
|
56
|
+
}
|
|
57
|
+
/** Removes an archetype-wide handler */
|
|
58
|
+
removeHandler(input, hidden = false) {
|
|
59
|
+
return this.modifyHandlerTags(removeNappletHandlerTag(input), hidden);
|
|
60
|
+
}
|
|
61
|
+
/** Adds a handler for one intent (`"edit"` or `"napplet:<archetype>/edit"`) as the last fallback */
|
|
62
|
+
intentHandler(intent, input, hidden = false) {
|
|
63
|
+
return this.modifyIntentTags(intent, hidden, (identity) => addNappletIntentHandlerTag(identity, input));
|
|
64
|
+
}
|
|
65
|
+
/** Makes a handler the preferred one for an intent */
|
|
66
|
+
preferIntentHandler(intent, input, hidden = false) {
|
|
67
|
+
return this.modifyIntentTags(intent, hidden, (identity) => preferNappletIntentHandlerTag(identity, input));
|
|
68
|
+
}
|
|
69
|
+
/** Removes one handler for an intent, or every handler for it when no input is given */
|
|
70
|
+
removeIntentHandler(intent, input, hidden = false) {
|
|
71
|
+
return this.modifyIntentTags(intent, hidden, (identity) => removeNappletIntentHandlerTag(identity, input));
|
|
72
|
+
}
|
|
73
|
+
/** Removes every public and private entry, leaving an empty preferences event */
|
|
74
|
+
clear() {
|
|
75
|
+
return this.modifyPublicTags(clearNappletHandlerTags()).chain(setContent(""));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { blankEventTemplate, EventFactory } from "applesauce-core/factories";
|
|
2
2
|
import { kinds } from "applesauce-core/helpers";
|
|
3
3
|
import { includeNameValueTag } from "applesauce-core/operations/tags";
|
|
4
|
-
import {
|
|
4
|
+
import { includeEmojis, setContent, setContentWarning } from "applesauce-core/operations/content";
|
|
5
5
|
import { setParent, setSubject } from "../operations/wrapped-message.js";
|
|
6
6
|
export class WrappedMessageFactory extends EventFactory {
|
|
7
7
|
/**
|
|
@@ -11,7 +11,7 @@ export class WrappedMessageFactory extends EventFactory {
|
|
|
11
11
|
*/
|
|
12
12
|
static create(recipients, message) {
|
|
13
13
|
const recipientList = typeof recipients === "string" ? [recipients] : recipients;
|
|
14
|
-
let factory = new WrappedMessageFactory((res) => res(blankEventTemplate(kinds.PrivateDirectMessage))).chain(
|
|
14
|
+
let factory = new WrappedMessageFactory((res) => res(blankEventTemplate(kinds.PrivateDirectMessage))).chain(setContent(message));
|
|
15
15
|
for (const recipient of recipientList) {
|
|
16
16
|
factory = factory.chain(includeNameValueTag(["p", recipient]));
|
|
17
17
|
}
|
|
@@ -20,13 +20,19 @@ export class WrappedMessageFactory extends EventFactory {
|
|
|
20
20
|
/** Creates a reply to a wrapped message */
|
|
21
21
|
static reply(parent, recipient, message) {
|
|
22
22
|
return new WrappedMessageFactory((res) => res(blankEventTemplate(kinds.PrivateDirectMessage)))
|
|
23
|
-
.chain(
|
|
23
|
+
.chain(setContent(message))
|
|
24
24
|
.chain(includeNameValueTag(["p", recipient]))
|
|
25
25
|
.replyTo(parent);
|
|
26
26
|
}
|
|
27
27
|
/** Sets the text content of the message */
|
|
28
28
|
text(content, options) {
|
|
29
|
-
|
|
29
|
+
// In NIP-17 every p tag is a recipient, including tags inferred from mentions.
|
|
30
|
+
let factory = this.chain(setContent(content));
|
|
31
|
+
if (options?.emojis)
|
|
32
|
+
factory = factory.chain(includeEmojis(options.emojis));
|
|
33
|
+
if (options?.contentWarning !== undefined)
|
|
34
|
+
factory = factory.chain(setContentWarning(options.contentWarning));
|
|
35
|
+
return factory;
|
|
30
36
|
}
|
|
31
37
|
/** Sets the parent message "e" tag for NIP-17 reply threading */
|
|
32
38
|
replyTo(parent) {
|
|
@@ -1,17 +1,8 @@
|
|
|
1
1
|
import { IEventStoreStreams } from "applesauce-core/event-store";
|
|
2
|
+
import { EncryptedContentCache } from "applesauce-core/helpers/encrypted-content-cache";
|
|
2
3
|
import { NostrEvent } from "applesauce-core/helpers/event";
|
|
3
4
|
import { Observable } from "rxjs";
|
|
4
|
-
|
|
5
|
-
export declare const EncryptedContentFromCacheSymbol: unique symbol;
|
|
6
|
-
/** An interface that is used to cache encrypted content on events */
|
|
7
|
-
export interface EncryptedContentCache {
|
|
8
|
-
getItem: (key: string) => Promise<string | null>;
|
|
9
|
-
setItem: (key: string, value: string) => Promise<any>;
|
|
10
|
-
}
|
|
11
|
-
/** Marks the encrypted content as being from a cache */
|
|
12
|
-
export declare function markEncryptedContentFromCache<T extends object>(event: T): void;
|
|
13
|
-
/** Checks if the encrypted content is from a cache */
|
|
14
|
-
export declare function isEncryptedContentFromCache<T extends object>(event: T): boolean;
|
|
5
|
+
export * from "applesauce-core/helpers/encrypted-content-cache";
|
|
15
6
|
/**
|
|
16
7
|
* Starts a process that persists and restores all encrypted content
|
|
17
8
|
* @param eventStore - The event store to listen to
|
|
@@ -1,35 +1,10 @@
|
|
|
1
1
|
import { logger } from "applesauce-core";
|
|
2
|
-
import {
|
|
2
|
+
import { isEncryptedContentFromCache, markEncryptedContentFromCache, } from "applesauce-core/helpers/encrypted-content-cache";
|
|
3
3
|
import { canHaveEncryptedContent, getEncryptedContent, isEncryptedContentUnlocked, setEncryptedContentCache, } from "applesauce-core/helpers/encrypted-content";
|
|
4
4
|
import { kinds, notifyEventUpdate } from "applesauce-core/helpers/event";
|
|
5
5
|
import { catchError, combineLatest, combineLatestWith, distinct, EMPTY, filter, isObservable, map, merge, mergeMap, of, switchMap, } from "rxjs";
|
|
6
6
|
import { getGiftWrapSeal, getSealGiftWrap, getSealRumor, isGiftWrapUnlocked } from "./gift-wrap.js";
|
|
7
|
-
|
|
8
|
-
export const EncryptedContentFromCacheSymbol = Symbol.for("encrypted-content-from-cache");
|
|
9
|
-
/** Marks the encrypted content as being from a cache */
|
|
10
|
-
export function markEncryptedContentFromCache(event) {
|
|
11
|
-
// A restore-provenance flag that is instance-local — unlike FromCacheSymbol, it is NOT a member
|
|
12
|
-
// of EventStore.copySymbolsToDuplicateEvent's merge list (see cache.ts taxonomy), so a
|
|
13
|
-
// duplicate-delivery merge never copies it onto another object. persistEncryptedContent's
|
|
14
|
-
// `persist` and `persistSeals` subscriptions both filter on isEncryptedContentFromCache(event)
|
|
15
|
-
// === false to avoid re-persisting content that was just restored from the cache: if a
|
|
16
|
-
// logically-identical event ever reached that filter as a distinct object lacking this flag, the
|
|
17
|
-
// filter would fail open and re-persist already-cached content. Whether that is reachable is
|
|
18
|
-
// unverified here: EventMemory.add keeps the originally-tracked object for a same-id duplicate
|
|
19
|
-
// and discards the newcomer entirely, so the ordinary EventStore.add duplicate-delivery path does
|
|
20
|
-
// not appear to replace the tracked instance — but replaceable-history and cross-store paths were
|
|
21
|
-
// not traced as part of this comment-only fix.
|
|
22
|
-
//
|
|
23
|
-
// Written non-enumerable via setCachedValue (05.1-09) so a plain spread drops it instead of
|
|
24
|
-
// carrying a stale provenance flag forward. isEncryptedContentFromCache's Reflect.has reader is
|
|
25
|
-
// enumerability-blind and still sees this write (WR-11's separate === true tightening stays
|
|
26
|
-
// deferred — D-10 — and is out of scope here).
|
|
27
|
-
setCachedValue(event, EncryptedContentFromCacheSymbol, true);
|
|
28
|
-
}
|
|
29
|
-
/** Checks if the encrypted content is from a cache */
|
|
30
|
-
export function isEncryptedContentFromCache(event) {
|
|
31
|
-
return Reflect.has(event, EncryptedContentFromCacheSymbol);
|
|
32
|
-
}
|
|
7
|
+
export * from "applesauce-core/helpers/encrypted-content-cache";
|
|
33
8
|
const log = logger.extend("EncryptedContentCache");
|
|
34
9
|
/**
|
|
35
10
|
* Starts a process that persists and restores all encrypted content
|
|
@@ -42,7 +17,7 @@ export function persistEncryptedContent(eventStore, storage, fallback) {
|
|
|
42
17
|
const storage$ = isObservable(storage) ? storage : of(storage);
|
|
43
18
|
// Get the encrypted content from storage or call the fallback
|
|
44
19
|
const getItem = async (storage, event) => {
|
|
45
|
-
return (await storage.getItem(event.id))
|
|
20
|
+
return (await storage.getItem(event.id)) ?? (fallback ? await fallback(event) : null);
|
|
46
21
|
};
|
|
47
22
|
// Restore encrypted content when it is inserted
|
|
48
23
|
const restore = eventStore.insert$
|
|
@@ -88,7 +63,7 @@ export function persistEncryptedContent(eventStore, storage, fallback) {
|
|
|
88
63
|
return EMPTY;
|
|
89
64
|
}))))
|
|
90
65
|
.subscribe(async ([seal, content]) => {
|
|
91
|
-
if (!seal ||
|
|
66
|
+
if (!seal || typeof content !== "string")
|
|
92
67
|
return;
|
|
93
68
|
markEncryptedContentFromCache(seal);
|
|
94
69
|
setEncryptedContentCache(seal, content);
|
|
@@ -114,7 +89,7 @@ export function persistEncryptedContent(eventStore, storage, fallback) {
|
|
|
114
89
|
.subscribe(async ([event, storage]) => {
|
|
115
90
|
try {
|
|
116
91
|
const content = getEncryptedContent(event);
|
|
117
|
-
if (content) {
|
|
92
|
+
if (content !== undefined) {
|
|
118
93
|
await storage.setItem(event.id, content);
|
|
119
94
|
log(`Persisted encrypted content for ${event.id}`);
|
|
120
95
|
}
|
|
@@ -134,7 +109,7 @@ export function persistEncryptedContent(eventStore, storage, fallback) {
|
|
|
134
109
|
return;
|
|
135
110
|
try {
|
|
136
111
|
const content = getEncryptedContent(seal);
|
|
137
|
-
if (content) {
|
|
112
|
+
if (content !== undefined) {
|
|
138
113
|
await storage.setItem(seal.id, content);
|
|
139
114
|
log(`Persisted encrypted content for ${seal.id}`);
|
|
140
115
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** The file encryption algorithm used by NIP-17 file messages */
|
|
2
|
+
export declare const ENCRYPTED_FILE_ALGORITHM = "aes-gcm";
|
|
3
|
+
/** Raw file data accepted by {@link encryptFile} and {@link decryptFile} */
|
|
4
|
+
export type FileData = Blob | ArrayBuffer | Uint8Array;
|
|
5
|
+
/** The result of encrypting a file */
|
|
6
|
+
export type EncryptedFile = {
|
|
7
|
+
/** The encrypted bytes to upload */
|
|
8
|
+
ciphertext: Uint8Array;
|
|
9
|
+
/** The encryption algorithm, always {@link ENCRYPTED_FILE_ALGORITHM} */
|
|
10
|
+
algorithm: typeof ENCRYPTED_FILE_ALGORITHM;
|
|
11
|
+
/** The hex encoded AES key */
|
|
12
|
+
key: string;
|
|
13
|
+
/** The hex encoded AES-GCM nonce */
|
|
14
|
+
nonce: string;
|
|
15
|
+
/** The sha256 of the encrypted bytes */
|
|
16
|
+
sha256: string;
|
|
17
|
+
/** The sha256 of the original bytes */
|
|
18
|
+
originalSha256: string;
|
|
19
|
+
/** The size of the encrypted bytes */
|
|
20
|
+
size: number;
|
|
21
|
+
};
|
|
22
|
+
/** The fields needed to decrypt a file */
|
|
23
|
+
export type FileDecryptionFields = {
|
|
24
|
+
/** The encryption algorithm, defaults to {@link ENCRYPTED_FILE_ALGORITHM} */
|
|
25
|
+
encryptionAlgorithm?: string;
|
|
26
|
+
/** The hex encoded AES key */
|
|
27
|
+
decryptionKey?: string;
|
|
28
|
+
/** The hex encoded AES-GCM nonce */
|
|
29
|
+
decryptionNonce?: string;
|
|
30
|
+
/** The expected sha256 of the encrypted bytes */
|
|
31
|
+
sha256?: string;
|
|
32
|
+
/** The expected sha256 of the decrypted bytes */
|
|
33
|
+
originalSha256?: string;
|
|
34
|
+
};
|
|
35
|
+
/** Encrypts a file with a random AES-GCM key and nonce for upload to a public file server */
|
|
36
|
+
export declare function encryptFile(data: FileData): Promise<EncryptedFile>;
|
|
37
|
+
/**
|
|
38
|
+
* Decrypts a file encrypted with {@link encryptFile} or another NIP-17 client
|
|
39
|
+
* @throws if the algorithm is unsupported, the key or nonce is missing, a hash does not match, or decryption fails
|
|
40
|
+
*/
|
|
41
|
+
export declare function decryptFile(data: FileData, fields: FileDecryptionFields): Promise<Uint8Array>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
3
|
+
/** The file encryption algorithm used by NIP-17 file messages */
|
|
4
|
+
export const ENCRYPTED_FILE_ALGORITHM = "aes-gcm";
|
|
5
|
+
async function toBytes(data) {
|
|
6
|
+
if (data instanceof Uint8Array)
|
|
7
|
+
return data;
|
|
8
|
+
if (data instanceof ArrayBuffer)
|
|
9
|
+
return new Uint8Array(data);
|
|
10
|
+
return new Uint8Array(await data.arrayBuffer());
|
|
11
|
+
}
|
|
12
|
+
/** Returns the hex encoded sha256 of some bytes */
|
|
13
|
+
function hashHex(bytes) {
|
|
14
|
+
return bytesToHex(sha256(bytes));
|
|
15
|
+
}
|
|
16
|
+
/** Copies bytes into a fresh ArrayBuffer, which Web Crypto requires */
|
|
17
|
+
function buffer(bytes) {
|
|
18
|
+
return bytes.slice().buffer;
|
|
19
|
+
}
|
|
20
|
+
/** Encrypts a file with a random AES-GCM key and nonce for upload to a public file server */
|
|
21
|
+
export async function encryptFile(data) {
|
|
22
|
+
const plaintext = await toBytes(data);
|
|
23
|
+
const key = crypto.getRandomValues(new Uint8Array(32));
|
|
24
|
+
const nonce = crypto.getRandomValues(new Uint8Array(12));
|
|
25
|
+
const cryptoKey = await crypto.subtle.importKey("raw", buffer(key), "AES-GCM", false, ["encrypt"]);
|
|
26
|
+
const encrypted = await crypto.subtle.encrypt({ name: "AES-GCM", iv: buffer(nonce) }, cryptoKey, buffer(plaintext));
|
|
27
|
+
const ciphertext = new Uint8Array(encrypted);
|
|
28
|
+
return {
|
|
29
|
+
ciphertext,
|
|
30
|
+
algorithm: ENCRYPTED_FILE_ALGORITHM,
|
|
31
|
+
key: bytesToHex(key),
|
|
32
|
+
nonce: bytesToHex(nonce),
|
|
33
|
+
sha256: hashHex(ciphertext),
|
|
34
|
+
originalSha256: hashHex(plaintext),
|
|
35
|
+
size: ciphertext.length,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Decrypts a file encrypted with {@link encryptFile} or another NIP-17 client
|
|
40
|
+
* @throws if the algorithm is unsupported, the key or nonce is missing, a hash does not match, or decryption fails
|
|
41
|
+
*/
|
|
42
|
+
export async function decryptFile(data, fields) {
|
|
43
|
+
const algorithm = fields.encryptionAlgorithm ?? ENCRYPTED_FILE_ALGORITHM;
|
|
44
|
+
if (algorithm.toLowerCase() !== ENCRYPTED_FILE_ALGORITHM)
|
|
45
|
+
throw new Error(`Unsupported encryption algorithm ${algorithm}`);
|
|
46
|
+
if (!fields.decryptionKey || !fields.decryptionNonce)
|
|
47
|
+
throw new Error("Missing decryption key or nonce");
|
|
48
|
+
const ciphertext = await toBytes(data);
|
|
49
|
+
if (fields.sha256 && hashHex(ciphertext) !== fields.sha256.toLowerCase())
|
|
50
|
+
throw new Error("Encrypted file hash does not match");
|
|
51
|
+
// Clients disagree on the nonce length (12 or 16 bytes), and AES-GCM accepts both.
|
|
52
|
+
const key = hexToBytes(fields.decryptionKey);
|
|
53
|
+
const nonce = hexToBytes(fields.decryptionNonce);
|
|
54
|
+
const cryptoKey = await crypto.subtle.importKey("raw", buffer(key), "AES-GCM", false, ["decrypt"]);
|
|
55
|
+
const decrypted = await crypto.subtle
|
|
56
|
+
.decrypt({ name: "AES-GCM", iv: buffer(nonce) }, cryptoKey, buffer(ciphertext))
|
|
57
|
+
.catch(() => {
|
|
58
|
+
throw new Error("Failed to decrypt file");
|
|
59
|
+
});
|
|
60
|
+
const plaintext = new Uint8Array(decrypted);
|
|
61
|
+
if (fields.originalSha256 && hashHex(plaintext) !== fields.originalSha256.toLowerCase())
|
|
62
|
+
throw new Error("Decrypted file hash does not match");
|
|
63
|
+
return plaintext;
|
|
64
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { NostrEvent, Rumor } from "applesauce-core/helpers/event";
|
|
2
|
+
import { FileMetadataFields } from "./file-metadata.js";
|
|
3
|
+
/** The NIP-17 file message kind */
|
|
4
|
+
export declare const FILE_MESSAGE_KIND = 15;
|
|
5
|
+
/** A NIP-17 kind 15 file message rumor */
|
|
6
|
+
export type FileMessage = Rumor & {
|
|
7
|
+
kind: typeof FILE_MESSAGE_KIND;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Checks if a rumor is a valid NIP-17 kind 15 file message
|
|
11
|
+
* @note requires a file-type or decryption-key tag, which also tells a file apart from a double-ratchet kind 15 receipt
|
|
12
|
+
*/
|
|
13
|
+
export declare function isValidFileMessage(rumor?: Rumor | NostrEvent): rumor is FileMessage;
|
|
14
|
+
/** Gets the file URL, type, hashes, and decryption fields of a kind 15 file message */
|
|
15
|
+
export declare function getFileMessageMetadata(message: FileMessage): FileMetadataFields;
|
|
16
|
+
export declare function getFileMessageMetadata(message?: Rumor | NostrEvent): FileMetadataFields | undefined;
|
|
17
|
+
/** Gets the attachments of a message, from a kind 15 file message or the imeta tags of any other message */
|
|
18
|
+
export declare function getMessageAttachments(message: Rumor | NostrEvent): FileMetadataFields[];
|