applesauce-common 7.1.0 → 7.1.2

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.
@@ -1,9 +1,15 @@
1
1
  import { EventFactory } from "applesauce-core/factories";
2
2
  import { kinds, KnownEventTemplate, NostrEvent } from "applesauce-core/helpers";
3
+ import { type TextContentOptions } from "applesauce-core/operations/content";
3
4
  export type LegacyMessageTemplate = KnownEventTemplate<kinds.EncryptedDirectMessage>;
4
5
  export declare class LegacyMessageFactory extends EventFactory<kinds.EncryptedDirectMessage, LegacyMessageTemplate> {
5
- static create(recipient: string, message: string): LegacyMessageFactory;
6
- static reply(parent: NostrEvent, recipient: string, message: string): LegacyMessageFactory;
6
+ /** Creates an encrypted message with optional public emoji tags */
7
+ static create(recipient: string, message: string, options?: Pick<TextContentOptions, "emojis">): LegacyMessageFactory;
8
+ /**
9
+ * Creates an encrypted reply with optional public emoji tags
10
+ * @throws if the parent is not a legacy message
11
+ */
12
+ static reply(parent: NostrEvent, recipient: string, message: string, options?: Pick<TextContentOptions, "emojis">): LegacyMessageFactory;
7
13
  /** Sets the "p" tag of the user the message is addressed to */
8
14
  recipient(pubkey: string): this;
9
15
  /** Sets the parent message "e" tag for NIP-04 reply threading */
@@ -1,19 +1,24 @@
1
1
  import { EventFactory, blankEventTemplate } from "applesauce-core/factories";
2
2
  import { kinds } from "applesauce-core/helpers";
3
+ import { includeEmojis, setContent } from "applesauce-core/operations/content";
3
4
  import { setMessageAddress, setMessageParent } from "../operations/legacy-message.js";
4
5
  export class LegacyMessageFactory extends EventFactory {
5
- static create(recipient, message) {
6
+ /** Creates an encrypted message with optional public emoji tags */
7
+ static create(recipient, message, options) {
6
8
  return new LegacyMessageFactory((res) => res(blankEventTemplate(kinds.EncryptedDirectMessage)))
7
9
  .recipient(recipient)
10
+ .chain(setContent(message))
11
+ .chain(includeEmojis(options?.emojis))
8
12
  .encryptedContent(recipient, message, "nip04");
9
13
  }
10
- static reply(parent, recipient, message) {
14
+ /**
15
+ * Creates an encrypted reply with optional public emoji tags
16
+ * @throws if the parent is not a legacy message
17
+ */
18
+ static reply(parent, recipient, message, options) {
11
19
  if (parent.kind !== kinds.EncryptedDirectMessage)
12
20
  throw new Error("Parent must be a legacy message (kind 4)");
13
- return new LegacyMessageFactory((res) => res(blankEventTemplate(kinds.EncryptedDirectMessage)))
14
- .recipient(recipient)
15
- .encryptedContent(recipient, message, "nip04")
16
- .replyTo(parent);
21
+ return LegacyMessageFactory.create(recipient, message, options).replyTo(parent);
17
22
  }
18
23
  /** Sets the "p" tag of the user the message is addressed to */
19
24
  recipient(pubkey) {
@@ -11,7 +11,7 @@ export declare const ZapReceiverSymbol: unique symbol;
11
11
  export declare const ZapInvoiceSymbol: unique symbol;
12
12
  export declare const ZapEventPointerSymbol: unique symbol;
13
13
  export declare const ZapAddressPointerSymbol: unique symbol;
14
- /** Returns the senders pubkey */
14
+ /** Returns the senders pubkey from the signed zap request */
15
15
  export declare function getZapSender(zap: ZapEvent): string;
16
16
  export declare function getZapSender(zap: NostrEvent): string | undefined;
17
17
  /** Gets the receivers pubkey */
@@ -38,8 +38,9 @@ export declare function getZapRequest(zap: NostrEvent): ZapRequestEvent | undefi
38
38
  */
39
39
  export declare function isValidZapRequest(event?: NostrEvent): event is ZapRequestEvent;
40
40
  /**
41
- * Checks if a zap event is valid (not missing fields)
42
- * DOES NOT validate LNURL address
41
+ * Checks if a zap receipt is valid and its sender, recipient and amount match the signed zap request
42
+ * @note DOES NOT validate that the receipt was signed by the recipient's LNURL server
43
+ * @see https://github.com/nostr-protocol/nips/blob/master/57.md
43
44
  */
44
45
  export declare function isValidZap(zap?: NostrEvent): zap is ZapEvent;
45
46
  export type ZapSplit = {
@@ -13,7 +13,8 @@ export const ZapEventPointerSymbol = Symbol.for("zap-event-pointer");
13
13
  export const ZapAddressPointerSymbol = Symbol.for("zap-address-pointer");
14
14
  export function getZapSender(zap) {
15
15
  return getOrComputeCachedValue(zap, ZapSenderSymbol, () => {
16
- return getTagValue(zap, "P") || getZapRequest(zap)?.pubkey;
16
+ // Use the signature-verified zap request author, the receipt P tag is only a claim made by the LNURL server
17
+ return getZapRequest(zap)?.pubkey;
17
18
  });
18
19
  }
19
20
  export function getZapRecipient(zap) {
@@ -97,8 +98,9 @@ export function isValidZapRequest(event) {
97
98
  return true;
98
99
  }
99
100
  /**
100
- * Checks if a zap event is valid (not missing fields)
101
- * DOES NOT validate LNURL address
101
+ * Checks if a zap receipt is valid and its sender, recipient and amount match the signed zap request
102
+ * @note DOES NOT validate that the receipt was signed by the recipient's LNURL server
103
+ * @see https://github.com/nostr-protocol/nips/blob/master/57.md
102
104
  */
103
105
  export function isValidZap(zap) {
104
106
  if (!zap)
@@ -106,13 +108,22 @@ export function isValidZap(zap) {
106
108
  if (zap.kind !== kinds.Zap)
107
109
  return false;
108
110
  // Is not a valid zap kind if any of these is undefined
109
- if (getZapPayment(zap) === undefined)
111
+ const payment = getZapPayment(zap);
112
+ const request = getZapRequest(zap);
113
+ const recipient = getZapRecipient(zap);
114
+ if (!payment || !request || !recipient)
110
115
  return false;
111
- if (getZapRequest(zap) === undefined)
116
+ // The optional P tag must match the zap request author (anonymous zaps use the same ephemeral key for both)
117
+ const senderTag = getTagValue(zap, "P");
118
+ if (senderTag && senderTag !== request.pubkey)
112
119
  return false;
113
- if (getZapRecipient(zap) === undefined)
120
+ // The receipt p tag must match the recipient of the zap request
121
+ const requestRecipient = getTagValue(request, "p");
122
+ if (requestRecipient !== recipient)
114
123
  return false;
115
- if (getZapSender(zap) === undefined)
124
+ // The invoice amount must match the amount requested by the sender
125
+ const requestAmount = getTagValue(request, "amount");
126
+ if (requestAmount && parseInt(requestAmount) !== payment.amount)
116
127
  return false;
117
128
  return true;
118
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-common",
3
- "version": "7.1.0",
3
+ "version": "7.1.2",
4
4
  "description": "Common NIP-specific helpers and models for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",