applesauce-common 6.2.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/README.md +1 -0
- package/dist/casts/_circular-import.d.ts +3 -0
- package/dist/casts/_circular-import.js +3 -0
- package/dist/casts/app-handler.d.ts +56 -0
- package/dist/casts/app-handler.js +105 -0
- package/dist/casts/app-recommendation.d.ts +26 -0
- package/dist/casts/app-recommendation.js +41 -0
- package/dist/casts/external-identities.d.ts +9 -0
- package/dist/casts/external-identities.js +14 -0
- package/dist/casts/forum-thread.d.ts +17 -0
- package/dist/casts/forum-thread.js +31 -0
- package/dist/casts/git-issue.d.ts +18 -0
- package/dist/casts/git-issue.js +26 -0
- package/dist/casts/git-patch.d.ts +49 -0
- package/dist/casts/git-patch.js +88 -0
- package/dist/casts/git-pull-request.d.ts +67 -0
- package/dist/casts/git-pull-request.js +122 -0
- package/dist/casts/git-repository.d.ts +9 -0
- package/dist/casts/git-repository.js +16 -0
- package/dist/casts/git-thread.d.ts +33 -0
- package/dist/casts/git-thread.js +65 -0
- package/dist/casts/index.d.ts +12 -0
- package/dist/casts/index.js +12 -0
- package/dist/casts/napplet-handlers.d.ts +39 -0
- package/dist/casts/napplet-handlers.js +91 -0
- package/dist/casts/napplet.d.ts +98 -0
- package/dist/casts/napplet.js +196 -0
- package/dist/casts/note.d.ts +8 -0
- package/dist/casts/note.js +21 -1
- package/dist/casts/nsite.d.ts +134 -0
- package/dist/casts/nsite.js +249 -0
- package/dist/casts/profile.d.ts +20 -0
- package/dist/casts/profile.js +30 -0
- package/dist/casts/reaction.d.ts +1 -1
- package/dist/casts/relay-discovery.d.ts +4 -0
- package/dist/casts/relay-discovery.js +9 -1
- package/dist/casts/report.d.ts +28 -15
- package/dist/casts/report.js +42 -19
- package/dist/casts/share.d.ts +1 -1
- package/dist/casts/torrent.d.ts +5 -0
- package/dist/casts/torrent.js +8 -1
- package/dist/casts/trusted-assertions.d.ts +2 -0
- package/dist/casts/trusted-assertions.js +19 -18
- package/dist/casts/user-status.d.ts +33 -0
- package/dist/casts/user-status.js +69 -0
- package/dist/casts/user.d.ts +7 -0
- package/dist/casts/user.js +20 -1
- package/dist/factories/app-handler.d.ts +31 -0
- package/dist/factories/app-handler.js +50 -0
- package/dist/factories/app-recommendation.d.ts +21 -0
- package/dist/factories/app-recommendation.js +28 -0
- package/dist/factories/chat-message.d.ts +37 -0
- package/dist/factories/chat-message.js +56 -0
- package/dist/factories/comment.d.ts +8 -7
- package/dist/factories/comment.js +2 -2
- package/dist/factories/external-identities.d.ts +17 -0
- package/dist/factories/external-identities.js +28 -0
- package/dist/factories/file-message.d.ts +24 -0
- package/dist/factories/file-message.js +37 -0
- package/dist/factories/forum-thread.d.ts +34 -0
- package/dist/factories/forum-thread.js +50 -0
- package/dist/factories/git-applied-status.d.ts +39 -0
- package/dist/factories/git-applied-status.js +53 -0
- package/dist/factories/git-closed-status.d.ts +18 -0
- package/dist/factories/git-closed-status.js +24 -0
- package/dist/factories/git-draft-status.d.ts +18 -0
- package/dist/factories/git-draft-status.js +24 -0
- package/dist/factories/git-issue.d.ts +19 -0
- package/dist/factories/git-issue.js +25 -0
- package/dist/factories/git-open-status.d.ts +18 -0
- package/dist/factories/git-open-status.js +22 -0
- package/dist/factories/git-patch.d.ts +59 -0
- package/dist/factories/git-patch.js +102 -0
- package/dist/factories/git-pull-request-update.d.ts +23 -0
- package/dist/factories/git-pull-request-update.js +33 -0
- package/dist/factories/git-pull-request.d.ts +39 -0
- package/dist/factories/git-pull-request.js +64 -0
- package/dist/factories/index.d.ts +24 -0
- package/dist/factories/index.js +24 -0
- package/dist/factories/internal/git-pull-request.d.ts +19 -0
- package/dist/factories/internal/git-pull-request.js +26 -0
- package/dist/factories/internal/git-status.d.ts +11 -0
- package/dist/factories/internal/git-status.js +12 -0
- package/dist/factories/internal/git-thread.d.ts +19 -0
- package/dist/factories/internal/git-thread.js +29 -0
- package/dist/factories/internal/napplet-manifest.d.ts +52 -0
- package/dist/factories/internal/napplet-manifest.js +85 -0
- package/dist/factories/internal/nsite-manifest.d.ts +45 -0
- package/dist/factories/internal/nsite-manifest.js +68 -0
- package/dist/factories/legacy-message.d.ts +2 -0
- package/dist/factories/legacy-message.js +9 -2
- package/dist/factories/named-napplet.d.ts +20 -0
- package/dist/factories/named-napplet.js +30 -0
- package/dist/factories/named-nsite.d.ts +28 -0
- package/dist/factories/named-nsite.js +53 -0
- package/dist/factories/napplet-handlers.d.ts +34 -0
- package/dist/factories/napplet-handlers.js +77 -0
- package/dist/factories/napplet-snapshot.d.ts +18 -0
- package/dist/factories/napplet-snapshot.js +38 -0
- package/dist/factories/nsite-snapshot.d.ts +18 -0
- package/dist/factories/nsite-snapshot.js +29 -0
- package/dist/factories/reaction.d.ts +3 -2
- package/dist/factories/report.d.ts +48 -0
- package/dist/factories/report.js +66 -0
- package/dist/factories/root-napplet.d.ts +18 -0
- package/dist/factories/root-napplet.js +24 -0
- package/dist/factories/root-nsite.d.ts +23 -0
- package/dist/factories/root-nsite.js +44 -0
- package/dist/factories/torrent.d.ts +24 -10
- package/dist/factories/torrent.js +48 -44
- package/dist/factories/trusted-provider-list.d.ts +23 -0
- package/dist/factories/trusted-provider-list.js +39 -0
- package/dist/factories/user-status.d.ts +26 -0
- package/dist/factories/user-status.js +52 -0
- package/dist/factories/wrapped-message.js +10 -4
- package/dist/helpers/app-data.js +15 -7
- package/dist/helpers/app-handler.d.ts +43 -6
- package/dist/helpers/app-handler.js +53 -3
- package/dist/helpers/app-recommendation.d.ts +24 -0
- package/dist/helpers/app-recommendation.js +30 -0
- package/dist/helpers/bookmark.js +5 -2
- package/dist/helpers/comment.d.ts +11 -5
- package/dist/helpers/comment.js +7 -1
- package/dist/helpers/content.d.ts +3 -1
- package/dist/helpers/emoji-pack.js +11 -6
- package/dist/helpers/emoji.d.ts +2 -2
- package/dist/helpers/encrypted-content-cache.d.ts +2 -11
- package/dist/helpers/encrypted-content-cache.js +6 -14
- package/dist/helpers/encrypted-file.d.ts +41 -0
- package/dist/helpers/encrypted-file.js +64 -0
- package/dist/helpers/external-id.js +13 -19
- package/dist/helpers/external-identities.d.ts +29 -0
- package/dist/helpers/external-identities.js +52 -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 +21 -6
- package/dist/helpers/forum-thread.d.ts +10 -0
- package/dist/helpers/forum-thread.js +10 -0
- package/dist/helpers/gift-wrap.d.ts +40 -15
- package/dist/helpers/gift-wrap.js +110 -33
- package/dist/helpers/git-issue.d.ts +13 -0
- package/dist/helpers/git-issue.js +19 -0
- package/dist/helpers/git-patch.d.ts +41 -0
- package/dist/helpers/git-patch.js +73 -0
- package/dist/helpers/git-pull-request.d.ts +57 -0
- package/dist/helpers/git-pull-request.js +111 -0
- package/dist/helpers/git-repository.d.ts +4 -0
- package/dist/helpers/git-repository.js +16 -0
- package/dist/helpers/git-status.d.ts +44 -0
- package/dist/helpers/git-status.js +93 -0
- package/dist/helpers/git.d.ts +2 -0
- package/dist/helpers/git.js +4 -0
- package/dist/helpers/groups.d.ts +5 -2
- package/dist/helpers/groups.js +40 -18
- package/dist/helpers/hashtag.d.ts +4 -2
- package/dist/helpers/index.d.ts +13 -0
- package/dist/helpers/index.js +13 -0
- package/dist/helpers/lists.js +14 -2
- package/dist/helpers/lnurl.js +1 -1
- package/dist/helpers/messages.js +3 -2
- package/dist/helpers/mute.js +20 -9
- package/dist/helpers/napplet-handlers.d.ts +79 -0
- package/dist/helpers/napplet-handlers.js +187 -0
- package/dist/helpers/napplet.d.ts +107 -0
- package/dist/helpers/napplet.js +177 -0
- package/dist/helpers/nsite.d.ts +90 -0
- package/dist/helpers/nsite.js +217 -0
- package/dist/helpers/relay-discovery.d.ts +4 -0
- package/dist/helpers/relay-discovery.js +8 -0
- package/dist/helpers/reports.d.ts +35 -0
- package/dist/helpers/reports.js +36 -5
- package/dist/helpers/threading.d.ts +4 -2
- package/dist/helpers/torrent.d.ts +9 -6
- package/dist/helpers/torrent.js +23 -15
- package/dist/helpers/trusted-assertions.d.ts +7 -0
- package/dist/helpers/trusted-assertions.js +41 -2
- package/dist/helpers/user-status.d.ts +13 -1
- package/dist/helpers/user-status.js +15 -0
- package/dist/helpers/wrapped-messages.d.ts +2 -0
- package/dist/helpers/wrapped-messages.js +6 -1
- package/dist/models/app-handler.d.ts +21 -0
- package/dist/models/app-handler.js +47 -0
- package/dist/models/git.d.ts +34 -0
- package/dist/models/git.js +144 -0
- package/dist/models/index.d.ts +4 -0
- package/dist/models/index.js +4 -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/nsite.d.ts +22 -0
- package/dist/models/nsite.js +67 -0
- package/dist/models/thread.d.ts +4 -1
- package/dist/models/thread.js +34 -5
- package/dist/models/user-status.d.ts +8 -4
- package/dist/models/user-status.js +16 -22
- package/dist/models/wrapped-messages.js +9 -7
- package/dist/observable/filter-timeline-by-mutes.d.ts +1 -1
- package/dist/observable/filter-timeline-by-mutes.js +6 -2
- package/dist/operations/app-handler.d.ts +24 -0
- package/dist/operations/app-handler.js +58 -0
- package/dist/operations/app-recommendation.d.ts +13 -0
- package/dist/operations/app-recommendation.js +40 -0
- package/dist/operations/calendar-event.js +3 -2
- package/dist/operations/chat-message.d.ts +9 -0
- package/dist/operations/chat-message.js +12 -0
- package/dist/operations/client.d.ts +1 -4
- package/dist/operations/client.js +1 -23
- package/dist/operations/comment.d.ts +9 -3
- package/dist/operations/comment.js +12 -9
- package/dist/operations/external-identities.d.ts +8 -0
- package/dist/operations/external-identities.js +23 -0
- package/dist/operations/file-message.d.ts +7 -0
- package/dist/operations/file-message.js +34 -0
- package/dist/operations/forum-thread.d.ts +3 -0
- package/dist/operations/forum-thread.js +6 -0
- package/dist/operations/gift-wrap.js +38 -14
- package/dist/operations/git-issue.d.ts +18 -0
- package/dist/operations/git-issue.js +31 -0
- package/dist/operations/git-patch.d.ts +30 -0
- package/dist/operations/git-patch.js +75 -0
- package/dist/operations/git-pull-request.d.ts +29 -0
- package/dist/operations/git-pull-request.js +74 -0
- package/dist/operations/git-repository.js +4 -4
- package/dist/operations/git-status.d.ts +28 -0
- package/dist/operations/git-status.js +116 -0
- package/dist/operations/git-thread.d.ts +13 -0
- package/dist/operations/git-thread.js +39 -0
- package/dist/operations/index.d.ts +17 -0
- package/dist/operations/index.js +17 -0
- package/dist/operations/internal/git-commits.d.ts +2 -0
- package/dist/operations/internal/git-commits.js +16 -0
- package/dist/operations/napplet-handlers.d.ts +47 -0
- package/dist/operations/napplet-handlers.js +161 -0
- package/dist/operations/napplet.d.ts +51 -0
- package/dist/operations/napplet.js +121 -0
- package/dist/operations/nsite.d.ts +51 -0
- package/dist/operations/nsite.js +112 -0
- package/dist/operations/reaction.d.ts +12 -2
- package/dist/operations/reaction.js +1 -1
- package/dist/operations/report.d.ts +33 -0
- package/dist/operations/report.js +60 -0
- package/dist/operations/stream.js +5 -4
- package/dist/operations/torrent.d.ts +1 -4
- package/dist/operations/torrent.js +24 -11
- package/dist/operations/trusted-providers.d.ts +14 -0
- package/dist/operations/trusted-providers.js +33 -0
- package/dist/operations/user-status.d.ts +11 -0
- package/dist/operations/user-status.js +29 -0
- package/dist/operations/wrapped-message.js +2 -2
- package/package.json +13 -17
|
@@ -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
|
+
}
|
|
@@ -1,23 +1,8 @@
|
|
|
1
|
+
import { serializeURL } from "applesauce-core/helpers/url";
|
|
1
2
|
/** Casts a string to a valid external pointer */
|
|
2
3
|
export function isValidExternalPointer(identifier) {
|
|
3
4
|
return parseExternalPointer(identifier) !== null;
|
|
4
5
|
}
|
|
5
|
-
/**
|
|
6
|
-
* Normalizes a URL according to NIP-73:
|
|
7
|
-
* - Removes fragment
|
|
8
|
-
* - Returns the normalized URL string
|
|
9
|
-
*/
|
|
10
|
-
function normalizeUrl(url) {
|
|
11
|
-
try {
|
|
12
|
-
const urlObj = new URL(url);
|
|
13
|
-
urlObj.hash = ""; // Remove fragment
|
|
14
|
-
return urlObj.toString();
|
|
15
|
-
}
|
|
16
|
-
catch {
|
|
17
|
-
// If URL parsing fails, return original (will be caught by validation)
|
|
18
|
-
return url;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
6
|
export function parseExternalPointer(identifier) {
|
|
22
7
|
// Check explicit prefixes first (these take precedence over URL parsing)
|
|
23
8
|
if (identifier.startsWith("#"))
|
|
@@ -49,9 +34,18 @@ export function parseExternalPointer(identifier) {
|
|
|
49
34
|
// Check for URL (must be a valid URL, normalized, no fragment)
|
|
50
35
|
// URLs don't have a prefix, so we check if it's a valid URL after checking all prefixes
|
|
51
36
|
try {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
37
|
+
let normalized;
|
|
38
|
+
if (/^(?:https?|wss?):\/\//i.test(identifier)) {
|
|
39
|
+
const serialized = serializeURL(identifier);
|
|
40
|
+
const hash = serialized.indexOf("#");
|
|
41
|
+
normalized = hash === -1 ? serialized : serialized.slice(0, hash);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
// Removing a fragment can also trim opaque paths, so retain the native setter here.
|
|
45
|
+
const url = new URL(identifier);
|
|
46
|
+
url.hash = "";
|
|
47
|
+
normalized = url.href;
|
|
48
|
+
}
|
|
55
49
|
return { kind: "web", identifier: normalized };
|
|
56
50
|
}
|
|
57
51
|
catch {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { KnownEvent, NostrEvent } from "applesauce-core/helpers/event";
|
|
2
|
+
export declare const EXTERNAL_IDENTITIES_KIND = 10011;
|
|
3
|
+
export declare const ExternalIdentitiesSymbol: unique symbol;
|
|
4
|
+
export type ExternalIdentitiesEvent = KnownEvent<typeof EXTERNAL_IDENTITIES_KIND>;
|
|
5
|
+
/** An external identity claimed by a NIP-39 "i" tag */
|
|
6
|
+
export type ExternalIdentity = {
|
|
7
|
+
/** The platform name (github, twitter, mastodon, telegram, bluesky, discord, etc) */
|
|
8
|
+
platform: string;
|
|
9
|
+
/** The identity on the platform */
|
|
10
|
+
identity: string;
|
|
11
|
+
/** A platform specific pointer to the proof of ownership */
|
|
12
|
+
proof: string;
|
|
13
|
+
};
|
|
14
|
+
/** Checks if an event is a NIP-39 external identities list */
|
|
15
|
+
export declare function isValidExternalIdentities(event?: NostrEvent): event is ExternalIdentitiesEvent;
|
|
16
|
+
/** Parses a NIP-39 ["i", "platform:identity", "proof"] tag */
|
|
17
|
+
export declare function getExternalIdentityFromTag(tag: string[]): ExternalIdentity | undefined;
|
|
18
|
+
/** Creates a NIP-39 "i" tag for an external identity */
|
|
19
|
+
export declare function createExternalIdentityTag(identity: ExternalIdentity): string[];
|
|
20
|
+
/**
|
|
21
|
+
* Returns the external identities from a NIP-39 identities list
|
|
22
|
+
* @see https://github.com/nostr-protocol/nips/blob/master/39.md
|
|
23
|
+
*/
|
|
24
|
+
export declare function getExternalIdentities(event?: NostrEvent): ExternalIdentity[];
|
|
25
|
+
/**
|
|
26
|
+
* Returns the URL of the proof for an external identity, or undefined for unknown platforms
|
|
27
|
+
* @see https://github.com/nostr-protocol/nips/blob/master/39.md
|
|
28
|
+
*/
|
|
29
|
+
export declare function getExternalIdentityProofUrl({ platform, identity, proof }: ExternalIdentity): string | undefined;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
|
|
2
|
+
import { processTags } from "applesauce-core/helpers/tags";
|
|
3
|
+
export const EXTERNAL_IDENTITIES_KIND = 10011;
|
|
4
|
+
export const ExternalIdentitiesSymbol = Symbol.for("external-identities");
|
|
5
|
+
/** Checks if an event is a NIP-39 external identities list */
|
|
6
|
+
export function isValidExternalIdentities(event) {
|
|
7
|
+
return !!event && event.kind === EXTERNAL_IDENTITIES_KIND;
|
|
8
|
+
}
|
|
9
|
+
/** Parses a NIP-39 ["i", "platform:identity", "proof"] tag */
|
|
10
|
+
export function getExternalIdentityFromTag(tag) {
|
|
11
|
+
if (tag[0] !== "i" || !tag[1] || !tag[2])
|
|
12
|
+
return undefined;
|
|
13
|
+
const index = tag[1].indexOf(":");
|
|
14
|
+
if (index <= 0 || index === tag[1].length - 1)
|
|
15
|
+
return undefined;
|
|
16
|
+
return { platform: tag[1].slice(0, index), identity: tag[1].slice(index + 1), proof: tag[2] };
|
|
17
|
+
}
|
|
18
|
+
/** Creates a NIP-39 "i" tag for an external identity */
|
|
19
|
+
export function createExternalIdentityTag(identity) {
|
|
20
|
+
return ["i", `${identity.platform}:${identity.identity}`, identity.proof];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the external identities from a NIP-39 identities list
|
|
24
|
+
* @see https://github.com/nostr-protocol/nips/blob/master/39.md
|
|
25
|
+
*/
|
|
26
|
+
export function getExternalIdentities(event) {
|
|
27
|
+
if (!isValidExternalIdentities(event))
|
|
28
|
+
return [];
|
|
29
|
+
return getOrComputeCachedValue(event, ExternalIdentitiesSymbol, () => processTags(event.tags, getExternalIdentityFromTag));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns the URL of the proof for an external identity, or undefined for unknown platforms
|
|
33
|
+
* @see https://github.com/nostr-protocol/nips/blob/master/39.md
|
|
34
|
+
*/
|
|
35
|
+
export function getExternalIdentityProofUrl({ platform, identity, proof }) {
|
|
36
|
+
switch (platform) {
|
|
37
|
+
case "github":
|
|
38
|
+
return `https://gist.github.com/${identity}/${proof}`;
|
|
39
|
+
case "twitter":
|
|
40
|
+
return `https://twitter.com/${identity}/status/${proof}`;
|
|
41
|
+
case "mastodon":
|
|
42
|
+
return `https://${identity}/${proof}`;
|
|
43
|
+
case "telegram":
|
|
44
|
+
return `https://t.me/${proof}`;
|
|
45
|
+
case "bluesky":
|
|
46
|
+
return `https://bsky.app/profile/${identity}/post/${proof}`;
|
|
47
|
+
case "discord":
|
|
48
|
+
return `https://discord.com/channels/${proof}`;
|
|
49
|
+
default:
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -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[];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
|
|
2
|
+
import { kinds } from "applesauce-core/helpers/event";
|
|
3
|
+
import { getMediaAttachments, parseFileMetadataTags } from "./file-metadata.js";
|
|
4
|
+
/** The NIP-17 file message kind */
|
|
5
|
+
export const FILE_MESSAGE_KIND = kinds.FileMessage;
|
|
6
|
+
const FileMessageMetadataSymbol = Symbol.for("file-message-metadata");
|
|
7
|
+
const MessageAttachmentsSymbol = Symbol.for("message-attachments");
|
|
8
|
+
function isURL(content) {
|
|
9
|
+
try {
|
|
10
|
+
const url = new URL(content.trim());
|
|
11
|
+
return url.protocol === "https:" || url.protocol === "http:";
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a rumor is a valid NIP-17 kind 15 file message
|
|
19
|
+
* @note requires a file-type or decryption-key tag, which also tells a file apart from a double-ratchet kind 15 receipt
|
|
20
|
+
*/
|
|
21
|
+
export function isValidFileMessage(rumor) {
|
|
22
|
+
if (!rumor || rumor.kind !== FILE_MESSAGE_KIND || !isURL(rumor.content))
|
|
23
|
+
return false;
|
|
24
|
+
return rumor.tags.some((tag) => (tag[0] === "file-type" || tag[0] === "decryption-key") && !!tag[1]);
|
|
25
|
+
}
|
|
26
|
+
export function getFileMessageMetadata(message) {
|
|
27
|
+
if (!isValidFileMessage(message))
|
|
28
|
+
return undefined;
|
|
29
|
+
return getOrComputeCachedValue(message, FileMessageMetadataSymbol, () => {
|
|
30
|
+
// Kind 15 names the MIME type "file-type" where NIP-94 uses "m".
|
|
31
|
+
const tags = message.tags.map((tag) => (tag[0] === "file-type" ? ["m", tag[1]] : tag));
|
|
32
|
+
return { ...parseFileMetadataTags(tags), url: message.content.trim() };
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** Gets the attachments of a message, from a kind 15 file message or the imeta tags of any other message */
|
|
36
|
+
export function getMessageAttachments(message) {
|
|
37
|
+
return getOrComputeCachedValue(message, MessageAttachmentsSymbol, () => {
|
|
38
|
+
if (message.kind === FILE_MESSAGE_KIND) {
|
|
39
|
+
const file = getFileMessageMetadata(message);
|
|
40
|
+
return file ? [file] : [];
|
|
41
|
+
}
|
|
42
|
+
return getMediaAttachments(message).filter((attachment) => !!attachment.url);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
@@ -7,10 +7,7 @@ export type FileMetadataFields = {
|
|
|
7
7
|
type?: string;
|
|
8
8
|
/** sha256 hash of the file */
|
|
9
9
|
sha256?: string;
|
|
10
|
-
/**
|
|
11
|
-
* The original sha256 hash before the file was transformed
|
|
12
|
-
* @deprecated
|
|
13
|
-
*/
|
|
10
|
+
/** The sha256 hash of the original file before it was transformed or encrypted */
|
|
14
11
|
originalSha256?: string;
|
|
15
12
|
/** size of the file in bytes */
|
|
16
13
|
size?: number;
|
|
@@ -32,11 +29,19 @@ export type FileMetadataFields = {
|
|
|
32
29
|
blurhash?: string;
|
|
33
30
|
/** fallback URLs */
|
|
34
31
|
fallback?: string[];
|
|
32
|
+
/** The algorithm the file is encrypted with, usually "aes-gcm" */
|
|
33
|
+
encryptionAlgorithm?: string;
|
|
34
|
+
/** The hex encoded key to decrypt the file */
|
|
35
|
+
decryptionKey?: string;
|
|
36
|
+
/** The hex encoded nonce to decrypt the file */
|
|
37
|
+
decryptionNonce?: string;
|
|
35
38
|
};
|
|
36
39
|
/** A kind 1063 file metadata event */
|
|
37
40
|
export type FileMetadataEvent = KnownEvent<1063>;
|
|
38
41
|
/** Alias for {@link FileMetadataFields} */
|
|
39
42
|
export type MediaAttachment = FileMetadataFields;
|
|
43
|
+
/** Checks if a file is encrypted and carries the key and nonce to decrypt it */
|
|
44
|
+
export declare function isEncryptedFile(file: FileMetadataFields): boolean;
|
|
40
45
|
/** Type guard for a valid kind 1063 file metadata event */
|
|
41
46
|
export declare function isValidFileMetadata(event?: NostrEvent): event is FileMetadataEvent;
|
|
42
47
|
/** Parses file metadata tags into {@link FileMetadataFields} */
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import { getURLPathname } from "applesauce-core/helpers/url";
|
|
1
2
|
import { getTagValue } from "applesauce-core/helpers/event";
|
|
2
3
|
import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
|
|
4
|
+
/** Checks if a file is encrypted and carries the key and nonce to decrypt it */
|
|
5
|
+
export function isEncryptedFile(file) {
|
|
6
|
+
return !!file.decryptionKey && !!file.decryptionNonce;
|
|
7
|
+
}
|
|
3
8
|
/** Type guard for a valid kind 1063 file metadata event */
|
|
4
9
|
export function isValidFileMetadata(event) {
|
|
5
10
|
return !!event && event.kind === 1063 && !!getTagValue(event, "url");
|
|
@@ -45,6 +50,12 @@ export function parseFileMetadataTags(tags) {
|
|
|
45
50
|
metadata.alt = fields.alt;
|
|
46
51
|
if (fields.blurhash)
|
|
47
52
|
metadata.blurhash = fields.blurhash;
|
|
53
|
+
if (fields["encryption-algorithm"])
|
|
54
|
+
metadata.encryptionAlgorithm = fields["encryption-algorithm"];
|
|
55
|
+
if (fields["decryption-key"])
|
|
56
|
+
metadata.decryptionKey = fields["decryption-key"];
|
|
57
|
+
if (fields["decryption-nonce"])
|
|
58
|
+
metadata.decryptionNonce = fields["decryption-nonce"];
|
|
48
59
|
return metadata;
|
|
49
60
|
}
|
|
50
61
|
/** Parses a imeta tag into a {@link FileMetadataFields} */
|
|
@@ -85,12 +96,10 @@ export function getFileMetadata(file) {
|
|
|
85
96
|
}
|
|
86
97
|
/** Returns the last 64 length hex string in a URL */
|
|
87
98
|
export function getSha256FromURL(url) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return hashes[hashes.length - 1][0];
|
|
93
|
-
return;
|
|
99
|
+
let hash;
|
|
100
|
+
for (const match of getURLPathname(url).matchAll(/[0-9a-f]{64}/gi))
|
|
101
|
+
hash = match[0];
|
|
102
|
+
return hash;
|
|
94
103
|
}
|
|
95
104
|
/** Creates tags for {@link FileMetadataFields} */
|
|
96
105
|
export function createFileMetadataTags(attachment) {
|
|
@@ -125,6 +134,12 @@ export function createFileMetadataTags(attachment) {
|
|
|
125
134
|
if (attachment.fallback && attachment.fallback?.length > 0)
|
|
126
135
|
for (const url of attachment.fallback)
|
|
127
136
|
add("fallback", url);
|
|
137
|
+
if (attachment.encryptionAlgorithm)
|
|
138
|
+
add("encryption-algorithm", attachment.encryptionAlgorithm);
|
|
139
|
+
if (attachment.decryptionKey)
|
|
140
|
+
add("decryption-key", attachment.decryptionKey);
|
|
141
|
+
if (attachment.decryptionNonce)
|
|
142
|
+
add("decryption-nonce", attachment.decryptionNonce);
|
|
128
143
|
return tags;
|
|
129
144
|
}
|
|
130
145
|
/** Creates an imeta tag for a media attachment */
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { KnownEvent, NostrEvent } from "applesauce-core/helpers/event";
|
|
2
|
+
/** NIP-7D forum thread kind (11) */
|
|
3
|
+
export declare const FORUM_THREAD_KIND = 11;
|
|
4
|
+
/** Type for a validated NIP-7D forum thread event */
|
|
5
|
+
export type ForumThreadEvent = KnownEvent<typeof FORUM_THREAD_KIND>;
|
|
6
|
+
/** Returns a forum thread's title, if it has one (NIP-7D `title` tag) */
|
|
7
|
+
export declare function getForumThreadTitle(thread: ForumThreadEvent): string;
|
|
8
|
+
export declare function getForumThreadTitle(thread: NostrEvent): string | undefined;
|
|
9
|
+
/** Validates that an event is a NIP-7D forum thread (kind 11) */
|
|
10
|
+
export declare function isValidForumThread(thread: NostrEvent): thread is ForumThreadEvent;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { getTagValue, kinds } from "applesauce-core/helpers/event";
|
|
2
|
+
/** NIP-7D forum thread kind (11) */
|
|
3
|
+
export const FORUM_THREAD_KIND = kinds.ForumThread;
|
|
4
|
+
export function getForumThreadTitle(thread) {
|
|
5
|
+
return getTagValue(thread, "title");
|
|
6
|
+
}
|
|
7
|
+
/** Validates that an event is a NIP-7D forum thread (kind 11) */
|
|
8
|
+
export function isValidForumThread(thread) {
|
|
9
|
+
return thread.kind === FORUM_THREAD_KIND;
|
|
10
|
+
}
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { EventMemory } from "applesauce-core/event-store";
|
|
2
2
|
import { EncryptedContentSigner } from "applesauce-core/helpers/encrypted-content";
|
|
3
|
-
import { kinds, KnownEvent, NostrEvent,
|
|
3
|
+
import { kinds, KnownEvent, NostrEvent, Rumor } from "applesauce-core/helpers/event";
|
|
4
|
+
import { GiftWrapSymbol, RumorSymbol, SealSymbol } from "applesauce-core/helpers/gift-wrap";
|
|
4
5
|
/**
|
|
5
6
|
* An internal event set to keep track of seals and rumors
|
|
6
7
|
* This is intentionally isolated from the main applications event store so to prevent seals and rumors from being leaked
|
|
7
8
|
*/
|
|
8
|
-
export declare const internalGiftWrapEvents: EventMemory
|
|
9
|
-
export type Rumor
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
/** Used to store a reference to the seal event on gift wraps (downstream) or the seal event on rumors (upstream[]) */
|
|
13
|
-
export declare const SealSymbol: unique symbol;
|
|
14
|
-
/** Used to store a reference to the rumor on seals (downstream) */
|
|
15
|
-
export declare const RumorSymbol: unique symbol;
|
|
16
|
-
/** Used to store a reference to the parent gift wrap event on seals (upstream) */
|
|
17
|
-
export declare const GiftWrapSymbol: unique symbol;
|
|
9
|
+
export declare const internalGiftWrapEvents: EventMemory<NostrEvent>;
|
|
10
|
+
export type { Rumor };
|
|
11
|
+
export { GiftWrapSymbol, RumorSymbol, SealSymbol };
|
|
18
12
|
/** A gift wrap event that knows its seal event */
|
|
19
13
|
export type UnlockedGiftWrapEvent = KnownEvent<kinds.GiftWrap> & {
|
|
20
14
|
/** Downstream seal event */
|
|
@@ -29,6 +23,20 @@ export type UnlockedSeal = KnownEvent<kinds.Seal> & {
|
|
|
29
23
|
};
|
|
30
24
|
/** Checks if an event is a rumor (normal event with "id" and no "sig") */
|
|
31
25
|
export declare function isRumor(event: any): event is Rumor;
|
|
26
|
+
/**
|
|
27
|
+
* Checks that an event is a seal and that its id and signature are valid.
|
|
28
|
+
*
|
|
29
|
+
* The seal's signature is the ONLY authorship proof in a NIP-59 envelope — the gift wrap is
|
|
30
|
+
* signed by a throwaway key that says nothing about who wrote the message, and the rumor is
|
|
31
|
+
* unsigned by definition. Everything downstream that attributes a rumor to an author (notably
|
|
32
|
+
* getSealRumor's `rumor.pubkey !== seal.pubkey` binding) rests on this check having passed.
|
|
33
|
+
*
|
|
34
|
+
* Cheap to repeat: verifyWrappedEvent -> nostr-tools' verifyEvent memoises both outcomes on the
|
|
35
|
+
* event via `verifiedSymbol`, so calls after the first are a symbol read. That is what makes it
|
|
36
|
+
* affordable to re-assert this at every boundary a seal can enter through rather than trusting
|
|
37
|
+
* the caller to have checked.
|
|
38
|
+
*/
|
|
39
|
+
export declare function isValidSeal(event: NostrEvent): event is KnownEvent<kinds.Seal>;
|
|
32
40
|
/** Returns all the parent gift wraps for a seal event */
|
|
33
41
|
export declare function getSealGiftWrap(seal: UnlockedSeal): UnlockedGiftWrapEvent;
|
|
34
42
|
export declare function getSealGiftWrap(seal: NostrEvent): UnlockedGiftWrapEvent | undefined;
|
|
@@ -36,13 +44,22 @@ export declare function getSealGiftWrap(seal: NostrEvent): UnlockedGiftWrapEvent
|
|
|
36
44
|
export declare function getRumorSeals(rumor: Rumor): UnlockedSeal[];
|
|
37
45
|
/** Returns all the parent gift wraps for a rumor event */
|
|
38
46
|
export declare function getRumorGiftWraps(rumor: Rumor): UnlockedGiftWrapEvent[];
|
|
39
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* Checks if a seal event is locked and casts it to the {@link UnlockedSeal} type
|
|
49
|
+
*
|
|
50
|
+
* The presence test is only sound because getSealRumor never writes an `undefined` RumorSymbol
|
|
51
|
+
* — see the invariant documented there. If it did, this would narrow a seal that never yielded
|
|
52
|
+
* a rumor to UnlockedSeal.
|
|
53
|
+
*/
|
|
40
54
|
export declare function isSealUnlocked(seal: NostrEvent): seal is UnlockedSeal;
|
|
41
55
|
/** Returns if a gift-wrap event or gift-wrap seal is locked */
|
|
42
56
|
export declare function isGiftWrapUnlocked(gift: NostrEvent): gift is UnlockedGiftWrapEvent;
|
|
43
57
|
/**
|
|
44
58
|
* Gets the rumor from a seal event
|
|
45
|
-
*
|
|
59
|
+
*
|
|
60
|
+
* Returns undefined for any seal that does not yield a usable rumor — an invalid seal signature,
|
|
61
|
+
* unparseable content, an invalid rumor id, or a rumor whose author does not match its seal.
|
|
62
|
+
* These are things a stranger can put in your inbox, so none of them throw.
|
|
46
63
|
*/
|
|
47
64
|
export declare function getSealRumor(seal: UnlockedSeal): Rumor;
|
|
48
65
|
export declare function getSealRumor(seal: NostrEvent): Rumor | undefined;
|
|
@@ -54,12 +71,20 @@ export declare function getGiftWrapRumor(gift: UnlockedGiftWrapEvent): Rumor;
|
|
|
54
71
|
export declare function getGiftWrapRumor(gift: NostrEvent): Rumor | undefined;
|
|
55
72
|
/**
|
|
56
73
|
* Unlocks a seal event and returns the rumor event
|
|
57
|
-
*
|
|
74
|
+
*
|
|
75
|
+
* Unlike the getters, this throws rather than returning undefined: it is an imperative operation
|
|
76
|
+
* on one caller-chosen event with a non-optional return type, not a read applied across a
|
|
77
|
+
* timeline, so there is no observable for a throw to tear down.
|
|
78
|
+
* @throws {Error} If the seal does not yield a usable rumor — including an invalid seal
|
|
79
|
+
* signature, unparseable content, or a rumor whose author does not match its seal
|
|
58
80
|
*/
|
|
59
81
|
export declare function unlockSeal(seal: NostrEvent, signer: EncryptedContentSigner): Promise<Rumor>;
|
|
60
82
|
/**
|
|
61
83
|
* Unlocks and returns the unsigned seal event in a gift-wrap
|
|
62
|
-
*
|
|
84
|
+
*
|
|
85
|
+
* Throws for the same reason unlockSeal does — see the note there.
|
|
86
|
+
* @throws {Error} If the gift wrap does not yield a usable rumor — including an invalid seal
|
|
87
|
+
* signature, unparseable content, or a rumor whose author does not match its seal
|
|
63
88
|
*/
|
|
64
89
|
export declare function unlockGiftWrap(gift: NostrEvent, signer: EncryptedContentSigner): Promise<Rumor>;
|
|
65
90
|
/** Locks a gift-wrap event and seals its seal event */
|