nostr-tools 2.23.3 → 2.23.5
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/lib/cjs/abstract-pool.js +5 -1
- package/lib/cjs/abstract-pool.js.map +2 -2
- package/lib/cjs/abstract-relay.js +5 -1
- package/lib/cjs/abstract-relay.js.map +2 -2
- package/lib/cjs/index.js +180 -16
- package/lib/cjs/index.js.map +4 -4
- package/lib/cjs/nip17.js +31 -9
- package/lib/cjs/nip17.js.map +2 -2
- package/lib/cjs/nip22.js +154 -0
- package/lib/cjs/nip22.js.map +7 -0
- package/lib/cjs/nip44.js +34 -10
- package/lib/cjs/nip44.js.map +2 -2
- package/lib/cjs/nip46.js +39 -11
- package/lib/cjs/nip46.js.map +2 -2
- package/lib/cjs/nip47.js +3 -3
- package/lib/cjs/nip47.js.map +2 -2
- package/lib/cjs/nip59.js +31 -9
- package/lib/cjs/nip59.js.map +2 -2
- package/lib/cjs/pool.js +5 -1
- package/lib/cjs/pool.js.map +2 -2
- package/lib/cjs/relay.js +5 -1
- package/lib/cjs/relay.js.map +2 -2
- package/lib/esm/abstract-pool.js +5 -1
- package/lib/esm/abstract-pool.js.map +2 -2
- package/lib/esm/abstract-relay.js +5 -1
- package/lib/esm/abstract-relay.js.map +2 -2
- package/lib/esm/index.js +180 -16
- package/lib/esm/index.js.map +4 -4
- package/lib/esm/nip17.js +31 -9
- package/lib/esm/nip17.js.map +2 -2
- package/lib/esm/nip22.js +133 -0
- package/lib/esm/nip22.js.map +7 -0
- package/lib/esm/nip44.js +34 -10
- package/lib/esm/nip44.js.map +2 -2
- package/lib/esm/nip46.js +39 -11
- package/lib/esm/nip46.js.map +2 -2
- package/lib/esm/nip47.js +3 -3
- package/lib/esm/nip47.js.map +2 -2
- package/lib/esm/nip59.js +31 -9
- package/lib/esm/nip59.js.map +2 -2
- package/lib/esm/pool.js +5 -1
- package/lib/esm/pool.js.map +2 -2
- package/lib/esm/relay.js +5 -1
- package/lib/esm/relay.js.map +2 -2
- package/lib/nostr.bundle.js +180 -16
- package/lib/nostr.bundle.js.map +4 -4
- package/lib/types/index.d.ts +1 -0
- package/lib/types/nip22.d.ts +36 -0
- package/lib/types/nip22.test.d.ts +1 -0
- package/lib/types/nip44.d.ts +5 -0
- package/lib/types/nip46.d.ts +2 -0
- package/lib/types/nip47.d.ts +2 -0
- package/package.json +8 -2
package/lib/cjs/nip17.js
CHANGED
|
@@ -133,7 +133,8 @@ var import_sha22 = require("@noble/hashes/sha2.js");
|
|
|
133
133
|
var import_utils5 = require("@noble/hashes/utils.js");
|
|
134
134
|
var import_base = require("@scure/base");
|
|
135
135
|
var minPlaintextSize = 1;
|
|
136
|
-
var maxPlaintextSize =
|
|
136
|
+
var maxPlaintextSize = 4294967295;
|
|
137
|
+
var extendedPrefixThreshold = 65536;
|
|
137
138
|
function getConversationKey(privkeyA, pubkeyB) {
|
|
138
139
|
const sharedX = import_secp256k12.secp256k1.getSharedSecret(privkeyA, (0, import_utils5.hexToBytes)("02" + pubkeyB)).subarray(1, 33);
|
|
139
140
|
return (0, import_hkdf.extract)(import_sha22.sha256, sharedX, utf8Encoder.encode("nip44-v2"));
|
|
@@ -151,28 +152,49 @@ function calcPaddedLen(len) {
|
|
|
151
152
|
throw new Error("expected positive integer");
|
|
152
153
|
if (len <= 32)
|
|
153
154
|
return 32;
|
|
154
|
-
const nextPower =
|
|
155
|
+
const nextPower = 2 ** (Math.floor(Math.log2(len - 1)) + 1);
|
|
155
156
|
const chunk = nextPower <= 256 ? 32 : nextPower / 8;
|
|
156
157
|
return chunk * (Math.floor((len - 1) / chunk) + 1);
|
|
157
158
|
}
|
|
158
159
|
function writeU16BE(num) {
|
|
159
|
-
if (!Number.isSafeInteger(num) || num < minPlaintextSize || num >
|
|
160
|
+
if (!Number.isSafeInteger(num) || num < minPlaintextSize || num > 65535)
|
|
160
161
|
throw new Error("invalid plaintext size: must be between 1 and 65535 bytes");
|
|
161
162
|
const arr = new Uint8Array(2);
|
|
162
163
|
new DataView(arr.buffer).setUint16(0, num, false);
|
|
163
164
|
return arr;
|
|
164
165
|
}
|
|
166
|
+
function writeU32BE(num) {
|
|
167
|
+
if (!Number.isSafeInteger(num) || num < extendedPrefixThreshold || num > maxPlaintextSize)
|
|
168
|
+
throw new Error("invalid plaintext size: must be between 65536 and 4294967295 bytes");
|
|
169
|
+
const arr = new Uint8Array(4);
|
|
170
|
+
new DataView(arr.buffer).setUint32(0, num, false);
|
|
171
|
+
return arr;
|
|
172
|
+
}
|
|
165
173
|
function pad(plaintext) {
|
|
166
174
|
const unpadded = utf8Encoder.encode(plaintext);
|
|
167
175
|
const unpaddedLen = unpadded.length;
|
|
168
|
-
|
|
176
|
+
if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize)
|
|
177
|
+
throw new Error("invalid plaintext size: must be between 1 and 4294967295 bytes");
|
|
178
|
+
const prefix = unpaddedLen >= extendedPrefixThreshold ? (0, import_utils5.concatBytes)(new Uint8Array([0, 0]), writeU32BE(unpaddedLen)) : writeU16BE(unpaddedLen);
|
|
169
179
|
const suffix = new Uint8Array(calcPaddedLen(unpaddedLen) - unpaddedLen);
|
|
170
180
|
return (0, import_utils5.concatBytes)(prefix, unpadded, suffix);
|
|
171
181
|
}
|
|
172
182
|
function unpad(padded) {
|
|
173
|
-
const
|
|
174
|
-
const
|
|
175
|
-
|
|
183
|
+
const dv = new DataView(padded.buffer, padded.byteOffset, padded.byteLength);
|
|
184
|
+
const firstTwo = dv.getUint16(0);
|
|
185
|
+
let unpaddedLen;
|
|
186
|
+
let prefixLen;
|
|
187
|
+
if (firstTwo === 0) {
|
|
188
|
+
unpaddedLen = dv.getUint32(2);
|
|
189
|
+
if (unpaddedLen < extendedPrefixThreshold)
|
|
190
|
+
throw new Error("invalid padding");
|
|
191
|
+
prefixLen = 6;
|
|
192
|
+
} else {
|
|
193
|
+
unpaddedLen = firstTwo;
|
|
194
|
+
prefixLen = 2;
|
|
195
|
+
}
|
|
196
|
+
const unpadded = padded.subarray(prefixLen, prefixLen + unpaddedLen);
|
|
197
|
+
if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize || unpadded.length !== unpaddedLen || padded.length !== prefixLen + calcPaddedLen(unpaddedLen))
|
|
176
198
|
throw new Error("invalid padding");
|
|
177
199
|
return utf8Decoder.decode(unpadded);
|
|
178
200
|
}
|
|
@@ -186,7 +208,7 @@ function decodePayload(payload) {
|
|
|
186
208
|
if (typeof payload !== "string")
|
|
187
209
|
throw new Error("payload must be a valid string");
|
|
188
210
|
const plen = payload.length;
|
|
189
|
-
if (plen < 132
|
|
211
|
+
if (plen < 132)
|
|
190
212
|
throw new Error("invalid payload length: " + plen);
|
|
191
213
|
if (payload[0] === "#")
|
|
192
214
|
throw new Error("unknown encryption version");
|
|
@@ -197,7 +219,7 @@ function decodePayload(payload) {
|
|
|
197
219
|
throw new Error("invalid base64: " + error.message);
|
|
198
220
|
}
|
|
199
221
|
const dlen = data.length;
|
|
200
|
-
if (dlen < 99
|
|
222
|
+
if (dlen < 99)
|
|
201
223
|
throw new Error("invalid data length: " + dlen);
|
|
202
224
|
const vers = data[0];
|
|
203
225
|
if (vers !== 2)
|
package/lib/cjs/nip17.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../nip17.ts", "../../pure.ts", "../../core.ts", "../../utils.ts", "../../kinds.ts", "../../nip44.ts", "../../nip59.ts"],
|
|
4
|
-
"sourcesContent": ["import { PrivateDirectMessage } from './kinds.ts'\nimport { EventTemplate, NostrEvent, getPublicKey } from './pure.ts'\nimport * as nip59 from './nip59.ts'\n\ntype Recipient = {\n publicKey: string\n relayUrl?: string\n}\n\ntype ReplyTo = {\n eventId: string\n relayUrl?: string\n}\n\nfunction createEvent(\n recipients: Recipient | Recipient[],\n message: string,\n conversationTitle?: string,\n replyTo?: ReplyTo,\n): EventTemplate {\n const baseEvent: EventTemplate = {\n created_at: Math.ceil(Date.now() / 1000),\n kind: PrivateDirectMessage,\n tags: [],\n content: message,\n }\n\n const recipientsArray = Array.isArray(recipients) ? recipients : [recipients]\n\n recipientsArray.forEach(({ publicKey, relayUrl }) => {\n baseEvent.tags.push(relayUrl ? ['p', publicKey, relayUrl] : ['p', publicKey])\n })\n\n if (replyTo) {\n baseEvent.tags.push(['e', replyTo.eventId, replyTo.relayUrl || '', 'reply'])\n }\n\n if (conversationTitle) {\n baseEvent.tags.push(['subject', conversationTitle])\n }\n\n return baseEvent\n}\n\nexport function wrapEvent(\n senderPrivateKey: Uint8Array,\n recipient: Recipient,\n message: string,\n conversationTitle?: string,\n replyTo?: ReplyTo,\n): NostrEvent {\n const event = createEvent(recipient, message, conversationTitle, replyTo)\n return nip59.wrapEvent(event, senderPrivateKey, recipient.publicKey)\n}\n\nexport function wrapManyEvents(\n senderPrivateKey: Uint8Array,\n recipients: Recipient[],\n message: string,\n conversationTitle?: string,\n replyTo?: ReplyTo,\n): NostrEvent[] {\n if (!recipients || recipients.length === 0) {\n throw new Error('At least one recipient is required.')\n }\n\n const senderPublicKey = getPublicKey(senderPrivateKey)\n\n // wrap the event for the sender and then for each recipient\n return [{ publicKey: senderPublicKey }, ...recipients].map(recipient =>\n wrapEvent(senderPrivateKey, recipient, message, conversationTitle, replyTo),\n )\n}\n\nexport const unwrapEvent = nip59.unwrapEvent\n\nexport const unwrapManyEvents = nip59.unwrapManyEvents\n", "import { schnorr } from '@noble/curves/secp256k1.js'\nimport { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'\nimport { Nostr, Event, EventTemplate, UnsignedEvent, VerifiedEvent, verifiedSymbol, validateEvent } from './core.ts'\nimport { sha256 } from '@noble/hashes/sha2.js'\n\nimport { utf8Encoder } from './utils.ts'\n\nclass JS implements Nostr {\n generateSecretKey(): Uint8Array {\n return schnorr.utils.randomSecretKey()\n }\n getPublicKey(secretKey: Uint8Array): string {\n return bytesToHex(schnorr.getPublicKey(secretKey))\n }\n finalizeEvent(t: EventTemplate, secretKey: Uint8Array): VerifiedEvent {\n const event = t as VerifiedEvent\n event.pubkey = bytesToHex(schnorr.getPublicKey(secretKey))\n event.id = getEventHash(event)\n event.sig = bytesToHex(schnorr.sign(hexToBytes(getEventHash(event)), secretKey))\n event[verifiedSymbol] = true\n return event\n }\n verifyEvent(event: Event): event is VerifiedEvent {\n if (typeof event[verifiedSymbol] === 'boolean') return event[verifiedSymbol]\n\n try {\n const hash = getEventHash(event)\n if (hash !== event.id) {\n event[verifiedSymbol] = false\n return false\n }\n\n const valid = schnorr.verify(hexToBytes(event.sig), hexToBytes(hash), hexToBytes(event.pubkey))\n event[verifiedSymbol] = valid\n return valid\n } catch (err) {\n event[verifiedSymbol] = false\n return false\n }\n }\n}\n\nexport function serializeEvent(evt: UnsignedEvent): string {\n if (!validateEvent(evt)) throw new Error(\"can't serialize event with wrong or missing properties\")\n return JSON.stringify([0, evt.pubkey, evt.created_at, evt.kind, evt.tags, evt.content])\n}\n\nexport function getEventHash(event: UnsignedEvent): string {\n let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))\n return bytesToHex(eventHash)\n}\n\nconst i: JS = new JS()\n\nexport const generateSecretKey = i.generateSecretKey\nexport const getPublicKey = i.getPublicKey\nexport const finalizeEvent = i.finalizeEvent\nexport const verifyEvent = i.verifyEvent\nexport * from './core.ts'\n", "export interface Nostr {\n generateSecretKey(): Uint8Array\n getPublicKey(secretKey: Uint8Array): string\n finalizeEvent(event: EventTemplate, secretKey: Uint8Array): VerifiedEvent\n verifyEvent(event: Event): event is VerifiedEvent\n}\n\n/** Designates a verified event signature. */\nexport const verifiedSymbol = Symbol('verified')\n\nexport type NostrEvent = {\n kind: number\n tags: string[][]\n content: string\n created_at: number\n pubkey: string\n id: string\n sig: string\n [verifiedSymbol]?: boolean\n}\n\nexport type Event = NostrEvent\nexport type EventTemplate = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at'>\nexport type UnsignedEvent = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at' | 'pubkey'>\n\n/** An event whose signature has been verified. */\nexport interface VerifiedEvent extends Event {\n [verifiedSymbol]: true\n}\n\nconst isRecord = (obj: unknown): obj is Record<string, unknown> => obj instanceof Object\n\nexport function validateEvent<T>(event: T): event is T & UnsignedEvent {\n if (!isRecord(event)) return false\n if (typeof event.kind !== 'number') return false\n if (typeof event.content !== 'string') return false\n if (typeof event.created_at !== 'number') return false\n if (typeof event.pubkey !== 'string') return false\n if (!event.pubkey.match(/^[a-f0-9]{64}$/)) return false\n\n if (!Array.isArray(event.tags)) return false\n for (let i = 0; i < event.tags.length; i++) {\n let tag = event.tags[i]\n if (!Array.isArray(tag)) return false\n for (let j = 0; j < tag.length; j++) {\n if (typeof tag[j] !== 'string') return false\n }\n }\n\n return true\n}\n\n/**\n * Sort events in reverse-chronological order by the `created_at` timestamp,\n * and then by the event `id` (lexicographically) in case of ties.\n * This mutates the array.\n */\nexport function sortEvents(events: Event[]): Event[] {\n return events.sort((a: NostrEvent, b: NostrEvent): number => {\n if (a.created_at !== b.created_at) {\n return b.created_at - a.created_at\n }\n return a.id.localeCompare(b.id)\n })\n}\n", "import type { NostrEvent } from './core.ts'\n\nexport const utf8Decoder: TextDecoder = new TextDecoder('utf-8')\nexport const utf8Encoder: TextEncoder = new TextEncoder()\n\nexport { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'\n\nexport function normalizeURL(url: string): string {\n try {\n if (url.indexOf('://') === -1) url = 'wss://' + url\n let p = new URL(url)\n if (p.protocol === 'http:') p.protocol = 'ws:'\n else if (p.protocol === 'https:') p.protocol = 'wss:'\n p.pathname = p.pathname.replace(/\\/+/g, '/')\n if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)\n if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''\n p.searchParams.sort()\n p.hash = ''\n return p.toString()\n } catch (e) {\n throw new Error(`Invalid URL: ${url}`)\n }\n}\n\nexport function insertEventIntoDescendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {\n const [idx, found] = binarySearch(sortedArray, b => {\n if (event.id === b.id) return 0\n if (event.created_at === b.created_at) return -1\n return b.created_at - event.created_at\n })\n if (!found) {\n sortedArray.splice(idx, 0, event)\n }\n return sortedArray\n}\n\nexport function insertEventIntoAscendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {\n const [idx, found] = binarySearch(sortedArray, b => {\n if (event.id === b.id) return 0\n if (event.created_at === b.created_at) return -1\n return event.created_at - b.created_at\n })\n if (!found) {\n sortedArray.splice(idx, 0, event)\n }\n return sortedArray\n}\n\nexport function binarySearch<T>(arr: T[], compare: (b: T) => number): [number, boolean] {\n let start = 0\n let end = arr.length - 1\n\n while (start <= end) {\n const mid = Math.floor((start + end) / 2)\n const cmp = compare(arr[mid])\n\n if (cmp === 0) {\n return [mid, true]\n }\n\n if (cmp < 0) {\n end = mid - 1\n } else {\n start = mid + 1\n }\n }\n\n return [start, false]\n}\n\nexport function mergeReverseSortedLists(list1: NostrEvent[], list2: NostrEvent[]): NostrEvent[] {\n const result: NostrEvent[] = new Array(list1.length + list2.length)\n result.length = 0\n let i1 = 0\n let i2 = 0\n let sameTimestampIds: string[] = []\n\n while (i1 < list1.length && i2 < list2.length) {\n let next: NostrEvent\n if (list1[i1]?.created_at > list2[i2]?.created_at) {\n next = list1[i1]\n i1++\n } else {\n next = list2[i2]\n i2++\n }\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n while (i1 < list1.length) {\n const next = list1[i1]\n i1++\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n while (i2 < list2.length) {\n const next = list2[i2]\n i2++\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n return result\n}\n", "import { NostrEvent, validateEvent } from './pure.ts'\n\n/** Events are **regular**, which means they're all expected to be stored by relays. */\nexport function isRegularKind(kind: number): boolean {\n return kind < 10000 && kind !== 0 && kind !== 3\n}\n\n/** Events are **replaceable**, which means that, for each combination of `pubkey` and `kind`, only the latest event is expected to (SHOULD) be stored by relays, older versions are expected to be discarded. */\nexport function isReplaceableKind(kind: number): boolean {\n return kind === 0 || kind === 3 || (10000 <= kind && kind < 20000)\n}\n\n/** Events are **ephemeral**, which means they are not expected to be stored by relays. */\nexport function isEphemeralKind(kind: number): boolean {\n return 20000 <= kind && kind < 30000\n}\n\n/** Events are **addressable**, which means that, for each combination of `pubkey`, `kind` and the `d` tag, only the latest event is expected to be stored by relays, older versions are expected to be discarded. */\nexport function isAddressableKind(kind: number): boolean {\n return 30000 <= kind && kind < 40000\n}\n\n/** Classification of the event kind. */\nexport type KindClassification = 'regular' | 'replaceable' | 'ephemeral' | 'parameterized' | 'unknown'\n\n/** Determine the classification of this kind of event if known, or `unknown`. */\nexport function classifyKind(kind: number): KindClassification {\n if (isRegularKind(kind)) return 'regular'\n if (isReplaceableKind(kind)) return 'replaceable'\n if (isEphemeralKind(kind)) return 'ephemeral'\n if (isAddressableKind(kind)) return 'parameterized'\n return 'unknown'\n}\n\nexport function isKind<T extends number>(event: unknown, kind: T | Array<T>): event is NostrEvent & { kind: T } {\n const kindAsArray: number[] = kind instanceof Array ? kind : [kind]\n return (validateEvent(event) && kindAsArray.includes(event.kind)) || false\n}\n\nexport const Metadata = 0\nexport type Metadata = typeof Metadata\nexport const ShortTextNote = 1\nexport type ShortTextNote = typeof ShortTextNote\nexport const RecommendRelay = 2\nexport type RecommendRelay = typeof RecommendRelay\nexport const Contacts = 3\nexport type Contacts = typeof Contacts\nexport const EncryptedDirectMessage = 4\nexport type EncryptedDirectMessage = typeof EncryptedDirectMessage\nexport const EventDeletion = 5\nexport type EventDeletion = typeof EventDeletion\nexport const Repost = 6\nexport type Repost = typeof Repost\nexport const Reaction = 7\nexport type Reaction = typeof Reaction\nexport const BadgeAward = 8\nexport type BadgeAward = typeof BadgeAward\nexport const ChatMessage = 9\nexport type ChatMessage = typeof ChatMessage\nexport const ForumThread = 11\nexport type ForumThread = typeof ForumThread\nexport const Seal = 13\nexport type Seal = typeof Seal\nexport const PrivateDirectMessage = 14\nexport type PrivateDirectMessage = typeof PrivateDirectMessage\nexport const FileMessage = 15\nexport type FileMessage = typeof FileMessage\nexport const GenericRepost = 16\nexport type GenericRepost = typeof GenericRepost\nexport const Photo = 20\nexport type Photo = typeof Photo\nexport const NormalVideo = 21\nexport type NormalVideo = typeof NormalVideo\nexport const ShortVideo = 22\nexport type ShortVideo = typeof ShortVideo\nexport const ChannelCreation = 40\nexport type ChannelCreation = typeof ChannelCreation\nexport const ChannelMetadata = 41\nexport type ChannelMetadata = typeof ChannelMetadata\nexport const ChannelMessage = 42\nexport type ChannelMessage = typeof ChannelMessage\nexport const ChannelHideMessage = 43\nexport type ChannelHideMessage = typeof ChannelHideMessage\nexport const ChannelMuteUser = 44\nexport type ChannelMuteUser = typeof ChannelMuteUser\nexport const OpenTimestamps = 1040\nexport type OpenTimestamps = typeof OpenTimestamps\nexport const GiftWrap = 1059\nexport type GiftWrap = typeof GiftWrap\nexport const Poll = 1068\nexport type Poll = typeof Poll\nexport const FileMetadata = 1063\nexport type FileMetadata = typeof FileMetadata\nexport const Comment = 1111\nexport type Comment = typeof Comment\nexport const LiveChatMessage = 1311\nexport type LiveChatMessage = typeof LiveChatMessage\nexport const Voice = 1222\nexport type Voice = typeof Voice\nexport const VoiceComment = 1244\nexport type VoiceComment = typeof VoiceComment\nexport const ProblemTracker = 1971\nexport type ProblemTracker = typeof ProblemTracker\nexport const Report = 1984\nexport type Report = typeof Report\nexport const Reporting = 1984\nexport type Reporting = typeof Reporting\nexport const Label = 1985\nexport type Label = typeof Label\nexport const CommunityPostApproval = 4550\nexport type CommunityPostApproval = typeof CommunityPostApproval\nexport const JobRequest = 5999\nexport type JobRequest = typeof JobRequest\nexport const JobResult = 6999\nexport type JobResult = typeof JobResult\nexport const JobFeedback = 7000\nexport type JobFeedback = typeof JobFeedback\nexport const ZapGoal = 9041\nexport type ZapGoal = typeof ZapGoal\nexport const ZapRequest = 9734\nexport type ZapRequest = typeof ZapRequest\nexport const Zap = 9735\nexport type Zap = typeof Zap\nexport const Highlights = 9802\nexport type Highlights = typeof Highlights\nexport const PollResponse = 1018\nexport type PollResponse = typeof PollResponse\nexport const Mutelist = 10000\nexport type Mutelist = typeof Mutelist\nexport const Pinlist = 10001\nexport type Pinlist = typeof Pinlist\nexport const RelayList = 10002\nexport type RelayList = typeof RelayList\nexport const BookmarkList = 10003\nexport type BookmarkList = typeof BookmarkList\nexport const CommunitiesList = 10004\nexport type CommunitiesList = typeof CommunitiesList\nexport const PublicChatsList = 10005\nexport type PublicChatsList = typeof PublicChatsList\nexport const BlockedRelaysList = 10006\nexport type BlockedRelaysList = typeof BlockedRelaysList\nexport const SearchRelaysList = 10007\nexport type SearchRelaysList = typeof SearchRelaysList\nexport const FavoriteRelays = 10012\nexport type FavoriteRelays = typeof FavoriteRelays\nexport const InterestsList = 10015\nexport type InterestsList = typeof InterestsList\nexport const UserEmojiList = 10030\nexport type UserEmojiList = typeof UserEmojiList\nexport const DirectMessageRelaysList = 10050\nexport type DirectMessageRelaysList = typeof DirectMessageRelaysList\nexport const FileServerPreference = 10096\nexport type FileServerPreference = typeof FileServerPreference\nexport const BlossomServerList = 10063\nexport type BlossomServerList = typeof BlossomServerList\nexport const NWCWalletInfo = 13194\nexport type NWCWalletInfo = typeof NWCWalletInfo\nexport const LightningPubRPC = 21000\nexport type LightningPubRPC = typeof LightningPubRPC\nexport const ClientAuth = 22242\nexport type ClientAuth = typeof ClientAuth\nexport const NWCWalletRequest = 23194\nexport type NWCWalletRequest = typeof NWCWalletRequest\nexport const NWCWalletResponse = 23195\nexport type NWCWalletResponse = typeof NWCWalletResponse\nexport const NostrConnect = 24133\nexport type NostrConnect = typeof NostrConnect\nexport const HTTPAuth = 27235\nexport type HTTPAuth = typeof HTTPAuth\nexport const Followsets = 30000\nexport type Followsets = typeof Followsets\nexport const Genericlists = 30001\nexport type Genericlists = typeof Genericlists\nexport const Relaysets = 30002\nexport type Relaysets = typeof Relaysets\nexport const Bookmarksets = 30003\nexport type Bookmarksets = typeof Bookmarksets\nexport const Curationsets = 30004\nexport type Curationsets = typeof Curationsets\nexport const ProfileBadges = 30008\nexport type ProfileBadges = typeof ProfileBadges\nexport const BadgeDefinition = 30009\nexport type BadgeDefinition = typeof BadgeDefinition\nexport const Interestsets = 30015\nexport type Interestsets = typeof Interestsets\nexport const CreateOrUpdateStall = 30017\nexport type CreateOrUpdateStall = typeof CreateOrUpdateStall\nexport const CreateOrUpdateProduct = 30018\nexport type CreateOrUpdateProduct = typeof CreateOrUpdateProduct\nexport const LongFormArticle = 30023\nexport type LongFormArticle = typeof LongFormArticle\nexport const DraftLong = 30024\nexport type DraftLong = typeof DraftLong\nexport const Emojisets = 30030\nexport type Emojisets = typeof Emojisets\nexport const Application = 30078\nexport type Application = typeof Application\nexport const LiveEvent = 30311\nexport type LiveEvent = typeof LiveEvent\nexport const UserStatuses = 30315\nexport type UserStatuses = typeof UserStatuses\nexport const ClassifiedListing = 30402\nexport type ClassifiedListing = typeof ClassifiedListing\nexport const DraftClassifiedListing = 30403\nexport type DraftClassifiedListing = typeof DraftClassifiedListing\nexport const Date = 31922\nexport type Date = typeof Date\nexport const Time = 31923\nexport type Time = typeof Time\nexport const Calendar = 31924\nexport type Calendar = typeof Calendar\nexport const CalendarEventRSVP = 31925\nexport type CalendarEventRSVP = typeof CalendarEventRSVP\nexport const RelayReview = 31987\nexport type RelayReview = typeof RelayReview\nexport const Handlerrecommendation = 31989\nexport type Handlerrecommendation = typeof Handlerrecommendation\nexport const Handlerinformation = 31990\nexport type Handlerinformation = typeof Handlerinformation\nexport const CommunityDefinition = 34550\nexport type CommunityDefinition = typeof CommunityDefinition\nexport const GroupMetadata = 39000\nexport type GroupMetadata = typeof GroupMetadata\n", "import { chacha20 } from '@noble/ciphers/chacha.js'\nimport { equalBytes } from '@noble/ciphers/utils.js'\nimport { secp256k1 } from '@noble/curves/secp256k1.js'\nimport { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf.js'\nimport { hmac } from '@noble/hashes/hmac.js'\nimport { sha256 } from '@noble/hashes/sha2.js'\nimport { concatBytes, hexToBytes, randomBytes } from '@noble/hashes/utils.js'\nimport { base64 } from '@scure/base'\n\nimport { utf8Decoder, utf8Encoder } from './utils.ts'\n\nconst minPlaintextSize = 0x0001 // 1b msg => padded to 32b\nconst maxPlaintextSize = 0xffff // 65535 (64kb-1) => padded to 64kb\n\nexport function getConversationKey(privkeyA: Uint8Array, pubkeyB: string): Uint8Array {\n const sharedX = secp256k1.getSharedSecret(privkeyA, hexToBytes('02' + pubkeyB)).subarray(1, 33)\n return hkdf_extract(sha256, sharedX, utf8Encoder.encode('nip44-v2'))\n}\n\nfunction getMessageKeys(\n conversationKey: Uint8Array,\n nonce: Uint8Array,\n): { chacha_key: Uint8Array; chacha_nonce: Uint8Array; hmac_key: Uint8Array } {\n const keys = hkdf_expand(sha256, conversationKey, nonce, 76)\n return {\n chacha_key: keys.subarray(0, 32),\n chacha_nonce: keys.subarray(32, 44),\n hmac_key: keys.subarray(44, 76),\n }\n}\n\nfunction calcPaddedLen(len: number): number {\n if (!Number.isSafeInteger(len) || len < 1) throw new Error('expected positive integer')\n if (len <= 32) return 32\n const nextPower = 1 << (Math.floor(Math.log2(len - 1)) + 1)\n const chunk = nextPower <= 256 ? 32 : nextPower / 8\n return chunk * (Math.floor((len - 1) / chunk) + 1)\n}\n\nfunction writeU16BE(num: number): Uint8Array {\n if (!Number.isSafeInteger(num) || num < minPlaintextSize || num > maxPlaintextSize)\n throw new Error('invalid plaintext size: must be between 1 and 65535 bytes')\n const arr = new Uint8Array(2)\n new DataView(arr.buffer).setUint16(0, num, false)\n return arr\n}\n\nfunction pad(plaintext: string): Uint8Array {\n const unpadded = utf8Encoder.encode(plaintext)\n const unpaddedLen = unpadded.length\n const prefix = writeU16BE(unpaddedLen)\n const suffix = new Uint8Array(calcPaddedLen(unpaddedLen) - unpaddedLen)\n return concatBytes(prefix, unpadded, suffix)\n}\n\nfunction unpad(padded: Uint8Array): string {\n const unpaddedLen = new DataView(padded.buffer).getUint16(0)\n const unpadded = padded.subarray(2, 2 + unpaddedLen)\n if (\n unpaddedLen < minPlaintextSize ||\n unpaddedLen > maxPlaintextSize ||\n unpadded.length !== unpaddedLen ||\n padded.length !== 2 + calcPaddedLen(unpaddedLen)\n )\n throw new Error('invalid padding')\n return utf8Decoder.decode(unpadded)\n}\n\nfunction hmacAad(key: Uint8Array, message: Uint8Array, aad: Uint8Array): Uint8Array {\n if (aad.length !== 32) throw new Error('AAD associated data must be 32 bytes')\n const combined = concatBytes(aad, message)\n return hmac(sha256, key, combined)\n}\n\n// metadata: always 65b (version: 1b, nonce: 32b, max: 32b)\n// plaintext: 1b to 0xffff\n// padded plaintext: 32b to 0xffff\n// ciphertext: 32b+2 to 0xffff+2\n// raw payload: 99 (65+32+2) to 65603 (65+0xffff+2)\n// compressed payload (base64): 132b to 87472b\nfunction decodePayload(payload: string): { nonce: Uint8Array; ciphertext: Uint8Array; mac: Uint8Array } {\n if (typeof payload !== 'string') throw new Error('payload must be a valid string')\n const plen = payload.length\n if (plen < 132 || plen > 87472) throw new Error('invalid payload length: ' + plen)\n if (payload[0] === '#') throw new Error('unknown encryption version')\n let data: Uint8Array\n try {\n data = base64.decode(payload)\n } catch (error) {\n throw new Error('invalid base64: ' + (error as any).message)\n }\n const dlen = data.length\n if (dlen < 99 || dlen > 65603) throw new Error('invalid data length: ' + dlen)\n const vers = data[0]\n if (vers !== 2) throw new Error('unknown encryption version ' + vers)\n return {\n nonce: data.subarray(1, 33),\n ciphertext: data.subarray(33, -32),\n mac: data.subarray(-32),\n }\n}\n\nexport function encrypt(plaintext: string, conversationKey: Uint8Array, nonce: Uint8Array = randomBytes(32)): string {\n const { chacha_key, chacha_nonce, hmac_key } = getMessageKeys(conversationKey, nonce)\n const padded = pad(plaintext)\n const ciphertext = chacha20(chacha_key, chacha_nonce, padded)\n const mac = hmacAad(hmac_key, ciphertext, nonce)\n return base64.encode(concatBytes(new Uint8Array([2]), nonce, ciphertext, mac))\n}\n\nexport function decrypt(payload: string, conversationKey: Uint8Array): string {\n const { nonce, ciphertext, mac } = decodePayload(payload)\n const { chacha_key, chacha_nonce, hmac_key } = getMessageKeys(conversationKey, nonce)\n const calculatedMac = hmacAad(hmac_key, ciphertext, nonce)\n if (!equalBytes(calculatedMac, mac)) throw new Error('invalid MAC')\n const padded = chacha20(chacha_key, chacha_nonce, ciphertext)\n return unpad(padded)\n}\n\nexport const v2 = {\n utils: {\n getConversationKey,\n calcPaddedLen,\n },\n encrypt,\n decrypt,\n}\n", "import { EventTemplate, UnsignedEvent, NostrEvent } from './core.ts'\nimport { getConversationKey, decrypt, encrypt } from './nip44.ts'\nimport { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts'\nimport { Seal, GiftWrap } from './kinds.ts'\n\ntype Rumor = UnsignedEvent & { id: string }\n\nconst TWO_DAYS = 2 * 24 * 60 * 60\n\nconst now = () => Math.round(Date.now() / 1000)\nconst randomNow = () => Math.round(now() - Math.random() * TWO_DAYS)\n\nconst nip44ConversationKey = (privateKey: Uint8Array, publicKey: string) => getConversationKey(privateKey, publicKey)\n\nconst nip44Encrypt = (data: EventTemplate, privateKey: Uint8Array, publicKey: string) =>\n encrypt(JSON.stringify(data), nip44ConversationKey(privateKey, publicKey))\n\nconst nip44Decrypt = (data: NostrEvent, privateKey: Uint8Array) =>\n JSON.parse(decrypt(data.content, nip44ConversationKey(privateKey, data.pubkey)))\n\nexport function createRumor(event: Partial<UnsignedEvent>, privateKey: Uint8Array): Rumor {\n const rumor = {\n created_at: now(),\n content: '',\n tags: [],\n ...event,\n pubkey: getPublicKey(privateKey),\n } as any\n\n rumor.id = getEventHash(rumor)\n\n return rumor as Rumor\n}\n\nexport function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublicKey: string): NostrEvent {\n return finalizeEvent(\n {\n kind: Seal,\n content: nip44Encrypt(rumor, privateKey, recipientPublicKey),\n created_at: randomNow(),\n tags: [],\n },\n privateKey,\n )\n}\n\nexport function createWrap(seal: NostrEvent, recipientPublicKey: string): NostrEvent {\n const randomKey = generateSecretKey()\n\n return finalizeEvent(\n {\n kind: GiftWrap,\n content: nip44Encrypt(seal, randomKey, recipientPublicKey),\n created_at: randomNow(),\n tags: [['p', recipientPublicKey]],\n },\n randomKey,\n ) as NostrEvent\n}\n\nexport function wrapEvent(\n event: Partial<UnsignedEvent>,\n senderPrivateKey: Uint8Array,\n recipientPublicKey: string,\n): NostrEvent {\n const rumor = createRumor(event, senderPrivateKey)\n\n const seal = createSeal(rumor, senderPrivateKey, recipientPublicKey)\n return createWrap(seal, recipientPublicKey)\n}\n\nexport function wrapManyEvents(\n event: Partial<UnsignedEvent>,\n senderPrivateKey: Uint8Array,\n recipientsPublicKeys: string[],\n): NostrEvent[] {\n if (!recipientsPublicKeys || recipientsPublicKeys.length === 0) {\n throw new Error('At least one recipient is required.')\n }\n\n const senderPublicKey = getPublicKey(senderPrivateKey)\n\n const wrappeds = [wrapEvent(event, senderPrivateKey, senderPublicKey)]\n\n recipientsPublicKeys.forEach(recipientPublicKey => {\n wrappeds.push(wrapEvent(event, senderPrivateKey, recipientPublicKey))\n })\n\n return wrappeds\n}\n\nexport function unwrapEvent(wrap: NostrEvent, recipientPrivateKey: Uint8Array): Rumor {\n const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey)\n return nip44Decrypt(unwrappedSeal, recipientPrivateKey)\n}\n\nexport function unwrapManyEvents(wrappedEvents: NostrEvent[], recipientPrivateKey: Uint8Array): Rumor[] {\n let unwrappedEvents: Rumor[] = []\n\n wrappedEvents.forEach(e => {\n unwrappedEvents.push(unwrapEvent(e, recipientPrivateKey))\n })\n\n unwrappedEvents.sort((a, b) => a.created_at - b.created_at)\n\n return unwrappedEvents\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,qBAAAA;AAAA,EAAA,wBAAAC;AAAA,EAAA,iBAAAC;AAAA,EAAA;AAAA;AAAA;;;ACAA,uBAAwB;AACxB,IAAAC,gBAAuC;;;ACOhC,IAAM,iBAAiB,OAAO,UAAU;AAsB/C,IAAM,WAAW,CAAC,QAAiD,eAAe;AAE3E,SAAS,cAAiB,OAAsC;AACrE,MAAI,CAAC,SAAS,KAAK;AAAG,WAAO;AAC7B,MAAI,OAAO,MAAM,SAAS;AAAU,WAAO;AAC3C,MAAI,OAAO,MAAM,YAAY;AAAU,WAAO;AAC9C,MAAI,OAAO,MAAM,eAAe;AAAU,WAAO;AACjD,MAAI,OAAO,MAAM,WAAW;AAAU,WAAO;AAC7C,MAAI,CAAC,MAAM,OAAO,MAAM,gBAAgB;AAAG,WAAO;AAElD,MAAI,CAAC,MAAM,QAAQ,MAAM,IAAI;AAAG,WAAO;AACvC,WAASC,KAAI,GAAGA,KAAI,MAAM,KAAK,QAAQA,MAAK;AAC1C,QAAI,MAAM,MAAM,KAAKA;AACrB,QAAI,CAAC,MAAM,QAAQ,GAAG;AAAG,aAAO;AAChC,aAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACnC,UAAI,OAAO,IAAI,OAAO;AAAU,eAAO;AAAA,IACzC;AAAA,EACF;AAEA,SAAO;AACT;;;AD/CA,kBAAuB;;;AEEvB,mBAAuC;AAHhC,IAAM,cAA2B,IAAI,YAAY,OAAO;AACxD,IAAM,cAA2B,IAAI,YAAY;;;AFIxD,IAAM,KAAN,MAA0B;AAAA,EACxB,oBAAgC;AAC9B,WAAO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,EAAE;;;AGItB,IAAM,OAAO;AAEb,IAAM,uBAAuB;AAwB7B,IAAM,WAAW;;;ACvFxB,oBAAyB;AACzB,IAAAC,gBAA2B;AAC3B,IAAAC,oBAA0B;AAC1B,kBAA+D;AAC/D,kBAAqB;AACrB,IAAAC,eAAuB;AACvB,IAAAF,gBAAqD;AACrD,kBAAuB;AAIvB,IAAM,mBAAmB;AACzB,IAAM,mBAAmB;
|
|
4
|
+
"sourcesContent": ["import { PrivateDirectMessage } from './kinds.ts'\nimport { EventTemplate, NostrEvent, getPublicKey } from './pure.ts'\nimport * as nip59 from './nip59.ts'\n\ntype Recipient = {\n publicKey: string\n relayUrl?: string\n}\n\ntype ReplyTo = {\n eventId: string\n relayUrl?: string\n}\n\nfunction createEvent(\n recipients: Recipient | Recipient[],\n message: string,\n conversationTitle?: string,\n replyTo?: ReplyTo,\n): EventTemplate {\n const baseEvent: EventTemplate = {\n created_at: Math.ceil(Date.now() / 1000),\n kind: PrivateDirectMessage,\n tags: [],\n content: message,\n }\n\n const recipientsArray = Array.isArray(recipients) ? recipients : [recipients]\n\n recipientsArray.forEach(({ publicKey, relayUrl }) => {\n baseEvent.tags.push(relayUrl ? ['p', publicKey, relayUrl] : ['p', publicKey])\n })\n\n if (replyTo) {\n baseEvent.tags.push(['e', replyTo.eventId, replyTo.relayUrl || '', 'reply'])\n }\n\n if (conversationTitle) {\n baseEvent.tags.push(['subject', conversationTitle])\n }\n\n return baseEvent\n}\n\nexport function wrapEvent(\n senderPrivateKey: Uint8Array,\n recipient: Recipient,\n message: string,\n conversationTitle?: string,\n replyTo?: ReplyTo,\n): NostrEvent {\n const event = createEvent(recipient, message, conversationTitle, replyTo)\n return nip59.wrapEvent(event, senderPrivateKey, recipient.publicKey)\n}\n\nexport function wrapManyEvents(\n senderPrivateKey: Uint8Array,\n recipients: Recipient[],\n message: string,\n conversationTitle?: string,\n replyTo?: ReplyTo,\n): NostrEvent[] {\n if (!recipients || recipients.length === 0) {\n throw new Error('At least one recipient is required.')\n }\n\n const senderPublicKey = getPublicKey(senderPrivateKey)\n\n // wrap the event for the sender and then for each recipient\n return [{ publicKey: senderPublicKey }, ...recipients].map(recipient =>\n wrapEvent(senderPrivateKey, recipient, message, conversationTitle, replyTo),\n )\n}\n\nexport const unwrapEvent = nip59.unwrapEvent\n\nexport const unwrapManyEvents = nip59.unwrapManyEvents\n", "import { schnorr } from '@noble/curves/secp256k1.js'\nimport { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'\nimport { Nostr, Event, EventTemplate, UnsignedEvent, VerifiedEvent, verifiedSymbol, validateEvent } from './core.ts'\nimport { sha256 } from '@noble/hashes/sha2.js'\n\nimport { utf8Encoder } from './utils.ts'\n\nclass JS implements Nostr {\n generateSecretKey(): Uint8Array {\n return schnorr.utils.randomSecretKey()\n }\n getPublicKey(secretKey: Uint8Array): string {\n return bytesToHex(schnorr.getPublicKey(secretKey))\n }\n finalizeEvent(t: EventTemplate, secretKey: Uint8Array): VerifiedEvent {\n const event = t as VerifiedEvent\n event.pubkey = bytesToHex(schnorr.getPublicKey(secretKey))\n event.id = getEventHash(event)\n event.sig = bytesToHex(schnorr.sign(hexToBytes(getEventHash(event)), secretKey))\n event[verifiedSymbol] = true\n return event\n }\n verifyEvent(event: Event): event is VerifiedEvent {\n if (typeof event[verifiedSymbol] === 'boolean') return event[verifiedSymbol]\n\n try {\n const hash = getEventHash(event)\n if (hash !== event.id) {\n event[verifiedSymbol] = false\n return false\n }\n\n const valid = schnorr.verify(hexToBytes(event.sig), hexToBytes(hash), hexToBytes(event.pubkey))\n event[verifiedSymbol] = valid\n return valid\n } catch (err) {\n event[verifiedSymbol] = false\n return false\n }\n }\n}\n\nexport function serializeEvent(evt: UnsignedEvent): string {\n if (!validateEvent(evt)) throw new Error(\"can't serialize event with wrong or missing properties\")\n return JSON.stringify([0, evt.pubkey, evt.created_at, evt.kind, evt.tags, evt.content])\n}\n\nexport function getEventHash(event: UnsignedEvent): string {\n let eventHash = sha256(utf8Encoder.encode(serializeEvent(event)))\n return bytesToHex(eventHash)\n}\n\nconst i: JS = new JS()\n\nexport const generateSecretKey = i.generateSecretKey\nexport const getPublicKey = i.getPublicKey\nexport const finalizeEvent = i.finalizeEvent\nexport const verifyEvent = i.verifyEvent\nexport * from './core.ts'\n", "export interface Nostr {\n generateSecretKey(): Uint8Array\n getPublicKey(secretKey: Uint8Array): string\n finalizeEvent(event: EventTemplate, secretKey: Uint8Array): VerifiedEvent\n verifyEvent(event: Event): event is VerifiedEvent\n}\n\n/** Designates a verified event signature. */\nexport const verifiedSymbol = Symbol('verified')\n\nexport type NostrEvent = {\n kind: number\n tags: string[][]\n content: string\n created_at: number\n pubkey: string\n id: string\n sig: string\n [verifiedSymbol]?: boolean\n}\n\nexport type Event = NostrEvent\nexport type EventTemplate = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at'>\nexport type UnsignedEvent = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at' | 'pubkey'>\n\n/** An event whose signature has been verified. */\nexport interface VerifiedEvent extends Event {\n [verifiedSymbol]: true\n}\n\nconst isRecord = (obj: unknown): obj is Record<string, unknown> => obj instanceof Object\n\nexport function validateEvent<T>(event: T): event is T & UnsignedEvent {\n if (!isRecord(event)) return false\n if (typeof event.kind !== 'number') return false\n if (typeof event.content !== 'string') return false\n if (typeof event.created_at !== 'number') return false\n if (typeof event.pubkey !== 'string') return false\n if (!event.pubkey.match(/^[a-f0-9]{64}$/)) return false\n\n if (!Array.isArray(event.tags)) return false\n for (let i = 0; i < event.tags.length; i++) {\n let tag = event.tags[i]\n if (!Array.isArray(tag)) return false\n for (let j = 0; j < tag.length; j++) {\n if (typeof tag[j] !== 'string') return false\n }\n }\n\n return true\n}\n\n/**\n * Sort events in reverse-chronological order by the `created_at` timestamp,\n * and then by the event `id` (lexicographically) in case of ties.\n * This mutates the array.\n */\nexport function sortEvents(events: Event[]): Event[] {\n return events.sort((a: NostrEvent, b: NostrEvent): number => {\n if (a.created_at !== b.created_at) {\n return b.created_at - a.created_at\n }\n return a.id.localeCompare(b.id)\n })\n}\n", "import type { NostrEvent } from './core.ts'\n\nexport const utf8Decoder: TextDecoder = new TextDecoder('utf-8')\nexport const utf8Encoder: TextEncoder = new TextEncoder()\n\nexport { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'\n\nexport function normalizeURL(url: string): string {\n try {\n if (url.indexOf('://') === -1) url = 'wss://' + url\n let p = new URL(url)\n if (p.protocol === 'http:') p.protocol = 'ws:'\n else if (p.protocol === 'https:') p.protocol = 'wss:'\n p.pathname = p.pathname.replace(/\\/+/g, '/')\n if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)\n if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''\n p.searchParams.sort()\n p.hash = ''\n return p.toString()\n } catch (e) {\n throw new Error(`Invalid URL: ${url}`)\n }\n}\n\nexport function insertEventIntoDescendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {\n const [idx, found] = binarySearch(sortedArray, b => {\n if (event.id === b.id) return 0\n if (event.created_at === b.created_at) return -1\n return b.created_at - event.created_at\n })\n if (!found) {\n sortedArray.splice(idx, 0, event)\n }\n return sortedArray\n}\n\nexport function insertEventIntoAscendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {\n const [idx, found] = binarySearch(sortedArray, b => {\n if (event.id === b.id) return 0\n if (event.created_at === b.created_at) return -1\n return event.created_at - b.created_at\n })\n if (!found) {\n sortedArray.splice(idx, 0, event)\n }\n return sortedArray\n}\n\nexport function binarySearch<T>(arr: T[], compare: (b: T) => number): [number, boolean] {\n let start = 0\n let end = arr.length - 1\n\n while (start <= end) {\n const mid = Math.floor((start + end) / 2)\n const cmp = compare(arr[mid])\n\n if (cmp === 0) {\n return [mid, true]\n }\n\n if (cmp < 0) {\n end = mid - 1\n } else {\n start = mid + 1\n }\n }\n\n return [start, false]\n}\n\nexport function mergeReverseSortedLists(list1: NostrEvent[], list2: NostrEvent[]): NostrEvent[] {\n const result: NostrEvent[] = new Array(list1.length + list2.length)\n result.length = 0\n let i1 = 0\n let i2 = 0\n let sameTimestampIds: string[] = []\n\n while (i1 < list1.length && i2 < list2.length) {\n let next: NostrEvent\n if (list1[i1]?.created_at > list2[i2]?.created_at) {\n next = list1[i1]\n i1++\n } else {\n next = list2[i2]\n i2++\n }\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n while (i1 < list1.length) {\n const next = list1[i1]\n i1++\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n while (i2 < list2.length) {\n const next = list2[i2]\n i2++\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n return result\n}\n", "import { NostrEvent, validateEvent } from './pure.ts'\n\n/** Events are **regular**, which means they're all expected to be stored by relays. */\nexport function isRegularKind(kind: number): boolean {\n return kind < 10000 && kind !== 0 && kind !== 3\n}\n\n/** Events are **replaceable**, which means that, for each combination of `pubkey` and `kind`, only the latest event is expected to (SHOULD) be stored by relays, older versions are expected to be discarded. */\nexport function isReplaceableKind(kind: number): boolean {\n return kind === 0 || kind === 3 || (10000 <= kind && kind < 20000)\n}\n\n/** Events are **ephemeral**, which means they are not expected to be stored by relays. */\nexport function isEphemeralKind(kind: number): boolean {\n return 20000 <= kind && kind < 30000\n}\n\n/** Events are **addressable**, which means that, for each combination of `pubkey`, `kind` and the `d` tag, only the latest event is expected to be stored by relays, older versions are expected to be discarded. */\nexport function isAddressableKind(kind: number): boolean {\n return 30000 <= kind && kind < 40000\n}\n\n/** Classification of the event kind. */\nexport type KindClassification = 'regular' | 'replaceable' | 'ephemeral' | 'parameterized' | 'unknown'\n\n/** Determine the classification of this kind of event if known, or `unknown`. */\nexport function classifyKind(kind: number): KindClassification {\n if (isRegularKind(kind)) return 'regular'\n if (isReplaceableKind(kind)) return 'replaceable'\n if (isEphemeralKind(kind)) return 'ephemeral'\n if (isAddressableKind(kind)) return 'parameterized'\n return 'unknown'\n}\n\nexport function isKind<T extends number>(event: unknown, kind: T | Array<T>): event is NostrEvent & { kind: T } {\n const kindAsArray: number[] = kind instanceof Array ? kind : [kind]\n return (validateEvent(event) && kindAsArray.includes(event.kind)) || false\n}\n\nexport const Metadata = 0\nexport type Metadata = typeof Metadata\nexport const ShortTextNote = 1\nexport type ShortTextNote = typeof ShortTextNote\nexport const RecommendRelay = 2\nexport type RecommendRelay = typeof RecommendRelay\nexport const Contacts = 3\nexport type Contacts = typeof Contacts\nexport const EncryptedDirectMessage = 4\nexport type EncryptedDirectMessage = typeof EncryptedDirectMessage\nexport const EventDeletion = 5\nexport type EventDeletion = typeof EventDeletion\nexport const Repost = 6\nexport type Repost = typeof Repost\nexport const Reaction = 7\nexport type Reaction = typeof Reaction\nexport const BadgeAward = 8\nexport type BadgeAward = typeof BadgeAward\nexport const ChatMessage = 9\nexport type ChatMessage = typeof ChatMessage\nexport const ForumThread = 11\nexport type ForumThread = typeof ForumThread\nexport const Seal = 13\nexport type Seal = typeof Seal\nexport const PrivateDirectMessage = 14\nexport type PrivateDirectMessage = typeof PrivateDirectMessage\nexport const FileMessage = 15\nexport type FileMessage = typeof FileMessage\nexport const GenericRepost = 16\nexport type GenericRepost = typeof GenericRepost\nexport const Photo = 20\nexport type Photo = typeof Photo\nexport const NormalVideo = 21\nexport type NormalVideo = typeof NormalVideo\nexport const ShortVideo = 22\nexport type ShortVideo = typeof ShortVideo\nexport const ChannelCreation = 40\nexport type ChannelCreation = typeof ChannelCreation\nexport const ChannelMetadata = 41\nexport type ChannelMetadata = typeof ChannelMetadata\nexport const ChannelMessage = 42\nexport type ChannelMessage = typeof ChannelMessage\nexport const ChannelHideMessage = 43\nexport type ChannelHideMessage = typeof ChannelHideMessage\nexport const ChannelMuteUser = 44\nexport type ChannelMuteUser = typeof ChannelMuteUser\nexport const OpenTimestamps = 1040\nexport type OpenTimestamps = typeof OpenTimestamps\nexport const GiftWrap = 1059\nexport type GiftWrap = typeof GiftWrap\nexport const Poll = 1068\nexport type Poll = typeof Poll\nexport const FileMetadata = 1063\nexport type FileMetadata = typeof FileMetadata\nexport const Comment = 1111\nexport type Comment = typeof Comment\nexport const LiveChatMessage = 1311\nexport type LiveChatMessage = typeof LiveChatMessage\nexport const Voice = 1222\nexport type Voice = typeof Voice\nexport const VoiceComment = 1244\nexport type VoiceComment = typeof VoiceComment\nexport const ProblemTracker = 1971\nexport type ProblemTracker = typeof ProblemTracker\nexport const Report = 1984\nexport type Report = typeof Report\nexport const Reporting = 1984\nexport type Reporting = typeof Reporting\nexport const Label = 1985\nexport type Label = typeof Label\nexport const CommunityPostApproval = 4550\nexport type CommunityPostApproval = typeof CommunityPostApproval\nexport const JobRequest = 5999\nexport type JobRequest = typeof JobRequest\nexport const JobResult = 6999\nexport type JobResult = typeof JobResult\nexport const JobFeedback = 7000\nexport type JobFeedback = typeof JobFeedback\nexport const ZapGoal = 9041\nexport type ZapGoal = typeof ZapGoal\nexport const ZapRequest = 9734\nexport type ZapRequest = typeof ZapRequest\nexport const Zap = 9735\nexport type Zap = typeof Zap\nexport const Highlights = 9802\nexport type Highlights = typeof Highlights\nexport const PollResponse = 1018\nexport type PollResponse = typeof PollResponse\nexport const Mutelist = 10000\nexport type Mutelist = typeof Mutelist\nexport const Pinlist = 10001\nexport type Pinlist = typeof Pinlist\nexport const RelayList = 10002\nexport type RelayList = typeof RelayList\nexport const BookmarkList = 10003\nexport type BookmarkList = typeof BookmarkList\nexport const CommunitiesList = 10004\nexport type CommunitiesList = typeof CommunitiesList\nexport const PublicChatsList = 10005\nexport type PublicChatsList = typeof PublicChatsList\nexport const BlockedRelaysList = 10006\nexport type BlockedRelaysList = typeof BlockedRelaysList\nexport const SearchRelaysList = 10007\nexport type SearchRelaysList = typeof SearchRelaysList\nexport const FavoriteRelays = 10012\nexport type FavoriteRelays = typeof FavoriteRelays\nexport const InterestsList = 10015\nexport type InterestsList = typeof InterestsList\nexport const UserEmojiList = 10030\nexport type UserEmojiList = typeof UserEmojiList\nexport const DirectMessageRelaysList = 10050\nexport type DirectMessageRelaysList = typeof DirectMessageRelaysList\nexport const FileServerPreference = 10096\nexport type FileServerPreference = typeof FileServerPreference\nexport const BlossomServerList = 10063\nexport type BlossomServerList = typeof BlossomServerList\nexport const NWCWalletInfo = 13194\nexport type NWCWalletInfo = typeof NWCWalletInfo\nexport const LightningPubRPC = 21000\nexport type LightningPubRPC = typeof LightningPubRPC\nexport const ClientAuth = 22242\nexport type ClientAuth = typeof ClientAuth\nexport const NWCWalletRequest = 23194\nexport type NWCWalletRequest = typeof NWCWalletRequest\nexport const NWCWalletResponse = 23195\nexport type NWCWalletResponse = typeof NWCWalletResponse\nexport const NostrConnect = 24133\nexport type NostrConnect = typeof NostrConnect\nexport const HTTPAuth = 27235\nexport type HTTPAuth = typeof HTTPAuth\nexport const Followsets = 30000\nexport type Followsets = typeof Followsets\nexport const Genericlists = 30001\nexport type Genericlists = typeof Genericlists\nexport const Relaysets = 30002\nexport type Relaysets = typeof Relaysets\nexport const Bookmarksets = 30003\nexport type Bookmarksets = typeof Bookmarksets\nexport const Curationsets = 30004\nexport type Curationsets = typeof Curationsets\nexport const ProfileBadges = 30008\nexport type ProfileBadges = typeof ProfileBadges\nexport const BadgeDefinition = 30009\nexport type BadgeDefinition = typeof BadgeDefinition\nexport const Interestsets = 30015\nexport type Interestsets = typeof Interestsets\nexport const CreateOrUpdateStall = 30017\nexport type CreateOrUpdateStall = typeof CreateOrUpdateStall\nexport const CreateOrUpdateProduct = 30018\nexport type CreateOrUpdateProduct = typeof CreateOrUpdateProduct\nexport const LongFormArticle = 30023\nexport type LongFormArticle = typeof LongFormArticle\nexport const DraftLong = 30024\nexport type DraftLong = typeof DraftLong\nexport const Emojisets = 30030\nexport type Emojisets = typeof Emojisets\nexport const Application = 30078\nexport type Application = typeof Application\nexport const LiveEvent = 30311\nexport type LiveEvent = typeof LiveEvent\nexport const UserStatuses = 30315\nexport type UserStatuses = typeof UserStatuses\nexport const ClassifiedListing = 30402\nexport type ClassifiedListing = typeof ClassifiedListing\nexport const DraftClassifiedListing = 30403\nexport type DraftClassifiedListing = typeof DraftClassifiedListing\nexport const Date = 31922\nexport type Date = typeof Date\nexport const Time = 31923\nexport type Time = typeof Time\nexport const Calendar = 31924\nexport type Calendar = typeof Calendar\nexport const CalendarEventRSVP = 31925\nexport type CalendarEventRSVP = typeof CalendarEventRSVP\nexport const RelayReview = 31987\nexport type RelayReview = typeof RelayReview\nexport const Handlerrecommendation = 31989\nexport type Handlerrecommendation = typeof Handlerrecommendation\nexport const Handlerinformation = 31990\nexport type Handlerinformation = typeof Handlerinformation\nexport const CommunityDefinition = 34550\nexport type CommunityDefinition = typeof CommunityDefinition\nexport const GroupMetadata = 39000\nexport type GroupMetadata = typeof GroupMetadata\n", "import { chacha20 } from '@noble/ciphers/chacha.js'\nimport { equalBytes } from '@noble/ciphers/utils.js'\nimport { secp256k1 } from '@noble/curves/secp256k1.js'\nimport { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf.js'\nimport { hmac } from '@noble/hashes/hmac.js'\nimport { sha256 } from '@noble/hashes/sha2.js'\nimport { concatBytes, hexToBytes, randomBytes } from '@noble/hashes/utils.js'\nimport { base64 } from '@scure/base'\n\nimport { utf8Decoder, utf8Encoder } from './utils.ts'\n\nconst minPlaintextSize = 0x0001 // 1b msg => padded to 32b\nconst maxPlaintextSize = 0xffffffff // 4294967295 (2^32-1)\nconst extendedPrefixThreshold = 0x10000 // 65536: lengths below use 2-byte u16 prefix, at or above use 6-byte prefix\n\nexport function getConversationKey(privkeyA: Uint8Array, pubkeyB: string): Uint8Array {\n const sharedX = secp256k1.getSharedSecret(privkeyA, hexToBytes('02' + pubkeyB)).subarray(1, 33)\n return hkdf_extract(sha256, sharedX, utf8Encoder.encode('nip44-v2'))\n}\n\nfunction getMessageKeys(\n conversationKey: Uint8Array,\n nonce: Uint8Array,\n): { chacha_key: Uint8Array; chacha_nonce: Uint8Array; hmac_key: Uint8Array } {\n const keys = hkdf_expand(sha256, conversationKey, nonce, 76)\n return {\n chacha_key: keys.subarray(0, 32),\n chacha_nonce: keys.subarray(32, 44),\n hmac_key: keys.subarray(44, 76),\n }\n}\n\nfunction calcPaddedLen(len: number): number {\n if (!Number.isSafeInteger(len) || len < 1) throw new Error('expected positive integer')\n if (len <= 32) return 32\n const nextPower = 2 ** (Math.floor(Math.log2(len - 1)) + 1)\n const chunk = nextPower <= 256 ? 32 : nextPower / 8\n return chunk * (Math.floor((len - 1) / chunk) + 1)\n}\n\nfunction writeU16BE(num: number): Uint8Array {\n if (!Number.isSafeInteger(num) || num < minPlaintextSize || num > 0xffff)\n throw new Error('invalid plaintext size: must be between 1 and 65535 bytes')\n const arr = new Uint8Array(2)\n new DataView(arr.buffer).setUint16(0, num, false)\n return arr\n}\n\nfunction writeU32BE(num: number): Uint8Array {\n if (!Number.isSafeInteger(num) || num < extendedPrefixThreshold || num > maxPlaintextSize)\n throw new Error('invalid plaintext size: must be between 65536 and 4294967295 bytes')\n const arr = new Uint8Array(4)\n new DataView(arr.buffer).setUint32(0, num, false)\n return arr\n}\n\nfunction pad(plaintext: string): Uint8Array {\n const unpadded = utf8Encoder.encode(plaintext)\n const unpaddedLen = unpadded.length\n if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize)\n throw new Error('invalid plaintext size: must be between 1 and 4294967295 bytes')\n const prefix =\n unpaddedLen >= extendedPrefixThreshold\n ? concatBytes(new Uint8Array([0, 0]), writeU32BE(unpaddedLen)) // 6 bytes\n : writeU16BE(unpaddedLen) // 2 bytes\n const suffix = new Uint8Array(calcPaddedLen(unpaddedLen) - unpaddedLen)\n return concatBytes(prefix, unpadded, suffix)\n}\n\nfunction unpad(padded: Uint8Array): string {\n const dv = new DataView(padded.buffer, padded.byteOffset, padded.byteLength)\n const firstTwo = dv.getUint16(0)\n let unpaddedLen: number\n let prefixLen: number\n if (firstTwo === 0) {\n // Extended format: 2 zero bytes + 4-byte u32 length\n unpaddedLen = dv.getUint32(2)\n if (unpaddedLen < extendedPrefixThreshold) throw new Error('invalid padding')\n prefixLen = 6\n } else {\n unpaddedLen = firstTwo\n prefixLen = 2\n }\n const unpadded = padded.subarray(prefixLen, prefixLen + unpaddedLen)\n if (\n unpaddedLen < minPlaintextSize ||\n unpaddedLen > maxPlaintextSize ||\n unpadded.length !== unpaddedLen ||\n padded.length !== prefixLen + calcPaddedLen(unpaddedLen)\n )\n throw new Error('invalid padding')\n return utf8Decoder.decode(unpadded)\n}\n\nfunction hmacAad(key: Uint8Array, message: Uint8Array, aad: Uint8Array): Uint8Array {\n if (aad.length !== 32) throw new Error('AAD associated data must be 32 bytes')\n const combined = concatBytes(aad, message)\n return hmac(sha256, key, combined)\n}\n\n// metadata: always 65b (version: 1b, nonce: 32b, mac: 32b)\n// plaintext: 1b to 0xffffffff\n// padded plaintext (small, <65536): 32b to 0x10000, with 2b prefix -> 34b to 0x10000+2\n// padded plaintext (large, >=65536): 0x10000 to 0x100000000, with 6b prefix -> 0x10006 to 0x100000000+6\n// ciphertext: same as padded plaintext (chacha20 doesn't change length)\n// raw payload (small): 99 (65+34) to 65603 (65+0x10000+2)\n// raw payload (large): 65607 (65+0x10006) to 4294967367 (65+0x100000000+6)\nfunction decodePayload(payload: string): { nonce: Uint8Array; ciphertext: Uint8Array; mac: Uint8Array } {\n if (typeof payload !== 'string') throw new Error('payload must be a valid string')\n const plen = payload.length\n if (plen < 132) throw new Error('invalid payload length: ' + plen)\n if (payload[0] === '#') throw new Error('unknown encryption version')\n let data: Uint8Array\n try {\n data = base64.decode(payload)\n } catch (error) {\n throw new Error('invalid base64: ' + (error as any).message)\n }\n const dlen = data.length\n if (dlen < 99) throw new Error('invalid data length: ' + dlen)\n const vers = data[0]\n if (vers !== 2) throw new Error('unknown encryption version ' + vers)\n return {\n nonce: data.subarray(1, 33),\n ciphertext: data.subarray(33, -32),\n mac: data.subarray(-32),\n }\n}\n\nexport function encrypt(plaintext: string, conversationKey: Uint8Array, nonce: Uint8Array = randomBytes(32)): string {\n const { chacha_key, chacha_nonce, hmac_key } = getMessageKeys(conversationKey, nonce)\n const padded = pad(plaintext)\n const ciphertext = chacha20(chacha_key, chacha_nonce, padded)\n const mac = hmacAad(hmac_key, ciphertext, nonce)\n return base64.encode(concatBytes(new Uint8Array([2]), nonce, ciphertext, mac))\n}\n\n/** Callers should validate payload size before calling to prevent DoS from oversized inputs. */\nexport function decrypt(payload: string, conversationKey: Uint8Array): string {\n const { nonce, ciphertext, mac } = decodePayload(payload)\n const { chacha_key, chacha_nonce, hmac_key } = getMessageKeys(conversationKey, nonce)\n const calculatedMac = hmacAad(hmac_key, ciphertext, nonce)\n if (!equalBytes(calculatedMac, mac)) throw new Error('invalid MAC')\n const padded = chacha20(chacha_key, chacha_nonce, ciphertext)\n return unpad(padded)\n}\n\nexport const v2 = {\n utils: {\n getConversationKey,\n calcPaddedLen,\n pad,\n unpad,\n },\n encrypt,\n decrypt,\n}\n", "import { EventTemplate, UnsignedEvent, NostrEvent } from './core.ts'\nimport { getConversationKey, decrypt, encrypt } from './nip44.ts'\nimport { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts'\nimport { Seal, GiftWrap } from './kinds.ts'\n\ntype Rumor = UnsignedEvent & { id: string }\n\nconst TWO_DAYS = 2 * 24 * 60 * 60\n\nconst now = () => Math.round(Date.now() / 1000)\nconst randomNow = () => Math.round(now() - Math.random() * TWO_DAYS)\n\nconst nip44ConversationKey = (privateKey: Uint8Array, publicKey: string) => getConversationKey(privateKey, publicKey)\n\nconst nip44Encrypt = (data: EventTemplate, privateKey: Uint8Array, publicKey: string) =>\n encrypt(JSON.stringify(data), nip44ConversationKey(privateKey, publicKey))\n\nconst nip44Decrypt = (data: NostrEvent, privateKey: Uint8Array) =>\n JSON.parse(decrypt(data.content, nip44ConversationKey(privateKey, data.pubkey)))\n\nexport function createRumor(event: Partial<UnsignedEvent>, privateKey: Uint8Array): Rumor {\n const rumor = {\n created_at: now(),\n content: '',\n tags: [],\n ...event,\n pubkey: getPublicKey(privateKey),\n } as any\n\n rumor.id = getEventHash(rumor)\n\n return rumor as Rumor\n}\n\nexport function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublicKey: string): NostrEvent {\n return finalizeEvent(\n {\n kind: Seal,\n content: nip44Encrypt(rumor, privateKey, recipientPublicKey),\n created_at: randomNow(),\n tags: [],\n },\n privateKey,\n )\n}\n\nexport function createWrap(seal: NostrEvent, recipientPublicKey: string): NostrEvent {\n const randomKey = generateSecretKey()\n\n return finalizeEvent(\n {\n kind: GiftWrap,\n content: nip44Encrypt(seal, randomKey, recipientPublicKey),\n created_at: randomNow(),\n tags: [['p', recipientPublicKey]],\n },\n randomKey,\n ) as NostrEvent\n}\n\nexport function wrapEvent(\n event: Partial<UnsignedEvent>,\n senderPrivateKey: Uint8Array,\n recipientPublicKey: string,\n): NostrEvent {\n const rumor = createRumor(event, senderPrivateKey)\n\n const seal = createSeal(rumor, senderPrivateKey, recipientPublicKey)\n return createWrap(seal, recipientPublicKey)\n}\n\nexport function wrapManyEvents(\n event: Partial<UnsignedEvent>,\n senderPrivateKey: Uint8Array,\n recipientsPublicKeys: string[],\n): NostrEvent[] {\n if (!recipientsPublicKeys || recipientsPublicKeys.length === 0) {\n throw new Error('At least one recipient is required.')\n }\n\n const senderPublicKey = getPublicKey(senderPrivateKey)\n\n const wrappeds = [wrapEvent(event, senderPrivateKey, senderPublicKey)]\n\n recipientsPublicKeys.forEach(recipientPublicKey => {\n wrappeds.push(wrapEvent(event, senderPrivateKey, recipientPublicKey))\n })\n\n return wrappeds\n}\n\nexport function unwrapEvent(wrap: NostrEvent, recipientPrivateKey: Uint8Array): Rumor {\n const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey)\n return nip44Decrypt(unwrappedSeal, recipientPrivateKey)\n}\n\nexport function unwrapManyEvents(wrappedEvents: NostrEvent[], recipientPrivateKey: Uint8Array): Rumor[] {\n let unwrappedEvents: Rumor[] = []\n\n wrappedEvents.forEach(e => {\n unwrappedEvents.push(unwrapEvent(e, recipientPrivateKey))\n })\n\n unwrappedEvents.sort((a, b) => a.created_at - b.created_at)\n\n return unwrappedEvents\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,qBAAAA;AAAA,EAAA,wBAAAC;AAAA,EAAA,iBAAAC;AAAA,EAAA;AAAA;AAAA;;;ACAA,uBAAwB;AACxB,IAAAC,gBAAuC;;;ACOhC,IAAM,iBAAiB,OAAO,UAAU;AAsB/C,IAAM,WAAW,CAAC,QAAiD,eAAe;AAE3E,SAAS,cAAiB,OAAsC;AACrE,MAAI,CAAC,SAAS,KAAK;AAAG,WAAO;AAC7B,MAAI,OAAO,MAAM,SAAS;AAAU,WAAO;AAC3C,MAAI,OAAO,MAAM,YAAY;AAAU,WAAO;AAC9C,MAAI,OAAO,MAAM,eAAe;AAAU,WAAO;AACjD,MAAI,OAAO,MAAM,WAAW;AAAU,WAAO;AAC7C,MAAI,CAAC,MAAM,OAAO,MAAM,gBAAgB;AAAG,WAAO;AAElD,MAAI,CAAC,MAAM,QAAQ,MAAM,IAAI;AAAG,WAAO;AACvC,WAASC,KAAI,GAAGA,KAAI,MAAM,KAAK,QAAQA,MAAK;AAC1C,QAAI,MAAM,MAAM,KAAKA;AACrB,QAAI,CAAC,MAAM,QAAQ,GAAG;AAAG,aAAO;AAChC,aAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK;AACnC,UAAI,OAAO,IAAI,OAAO;AAAU,eAAO;AAAA,IACzC;AAAA,EACF;AAEA,SAAO;AACT;;;A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AC9E,CAAC;AAED,MAAI,SAAS;AACX,cAAU,KAAK,KAAK,CAAC,KAAK,QAAQ,SAAS,QAAQ,YAAY,IAAI,OAAO,CAAC;AAAA,EAC7E;AAEA,MAAI,mBAAmB;AACrB,cAAU,KAAK,KAAK,CAAC,WAAW,iBAAiB,CAAC;AAAA,EACpD;AAEA,SAAO;AACT;AAEO,SAASC,WACd,kBACA,WACA,SACA,mBACA,SACY;AACZ,QAAM,QAAQ,YAAY,WAAW,SAAS,mBAAmB,OAAO;AACxE,SAAa,UAAU,OAAO,kBAAkB,UAAU,SAAS;AACrE;AAEO,SAAS,eACd,kBACA,YACA,SACA,mBACA,SACc;AACd,MAAI,CAAC,cAAc,WAAW,WAAW,GAAG;AAC1C,UAAM,IAAI,MAAM,qCAAqC;AAAA,EACvD;AAEA,QAAM,kBAAkB,aAAa,gBAAgB;AAGrD,SAAO,CAAC,EAAE,WAAW,gBAAgB,GAAG,GAAG,UAAU,EAAE;AAAA,IAAI,eACzDA,WAAU,kBAAkB,WAAW,SAAS,mBAAmB,OAAO;AAAA,EAC5E;AACF;AAEO,IAAMC,eAAoB;AAE1B,IAAMC,oBAAyB;",
|
|
6
6
|
"names": ["unwrapEvent", "unwrapManyEvents", "wrapEvent", "import_utils", "i", "import_utils", "import_secp256k1", "import_sha2", "hkdf_extract", "hkdf_expand", "wrapEvent", "unwrapEvent", "unwrapManyEvents"]
|
|
7
7
|
}
|
package/lib/cjs/nip22.js
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// nip22.ts
|
|
21
|
+
var nip22_exports = {};
|
|
22
|
+
__export(nip22_exports, {
|
|
23
|
+
parse: () => parse
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(nip22_exports);
|
|
26
|
+
function parseKind(kind) {
|
|
27
|
+
if (!kind)
|
|
28
|
+
return void 0;
|
|
29
|
+
return /^\d+$/.test(kind) ? parseInt(kind, 10) : kind;
|
|
30
|
+
}
|
|
31
|
+
function parseAddressPointer(value, relayUrl) {
|
|
32
|
+
const idx = value.indexOf(":");
|
|
33
|
+
const idx2 = value.indexOf(":", idx + 1);
|
|
34
|
+
if (idx === -1 || idx2 === -1)
|
|
35
|
+
return void 0;
|
|
36
|
+
const kind = parseInt(value.slice(0, idx), 10);
|
|
37
|
+
if (Number.isNaN(kind))
|
|
38
|
+
return void 0;
|
|
39
|
+
return {
|
|
40
|
+
kind,
|
|
41
|
+
pubkey: value.slice(idx + 1, idx2),
|
|
42
|
+
identifier: value.slice(idx2 + 1),
|
|
43
|
+
relays: relayUrl ? [relayUrl] : []
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function parsePointer(tag) {
|
|
47
|
+
switch (tag[0]) {
|
|
48
|
+
case "E":
|
|
49
|
+
case "e":
|
|
50
|
+
if (!tag[1])
|
|
51
|
+
return void 0;
|
|
52
|
+
return {
|
|
53
|
+
id: tag[1],
|
|
54
|
+
relays: tag[2] ? [tag[2]] : [],
|
|
55
|
+
author: tag[3]
|
|
56
|
+
};
|
|
57
|
+
case "A":
|
|
58
|
+
case "a":
|
|
59
|
+
if (!tag[1])
|
|
60
|
+
return void 0;
|
|
61
|
+
return parseAddressPointer(tag[1], tag[2]);
|
|
62
|
+
case "I":
|
|
63
|
+
case "i":
|
|
64
|
+
if (!tag[1])
|
|
65
|
+
return void 0;
|
|
66
|
+
return {
|
|
67
|
+
value: tag[1],
|
|
68
|
+
hint: tag[2]
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function parseQuote(tag) {
|
|
73
|
+
if (!tag[1])
|
|
74
|
+
return void 0;
|
|
75
|
+
if (tag[1].includes(":")) {
|
|
76
|
+
return parseAddressPointer(tag[1], tag[2]);
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
id: tag[1],
|
|
80
|
+
relays: tag[2] ? [tag[2]] : [],
|
|
81
|
+
author: tag[3]
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function choosePointer(candidates) {
|
|
85
|
+
return candidates.findLast((candidate) => candidate.tagName === "A" || candidate.tagName === "a")?.pointer || candidates.findLast((candidate) => candidate.tagName === "I" || candidate.tagName === "i")?.pointer || candidates.findLast((candidate) => candidate.tagName === "E" || candidate.tagName === "e")?.pointer;
|
|
86
|
+
}
|
|
87
|
+
function inheritRelayHints(pointer, profiles) {
|
|
88
|
+
if (!pointer || !("id" in pointer) || !pointer.author)
|
|
89
|
+
return;
|
|
90
|
+
const author = profiles.find((profile) => profile.pubkey === pointer.author);
|
|
91
|
+
if (!author || !author.relays)
|
|
92
|
+
return;
|
|
93
|
+
if (!pointer.relays) {
|
|
94
|
+
pointer.relays = [];
|
|
95
|
+
}
|
|
96
|
+
author.relays.forEach((url) => {
|
|
97
|
+
if (pointer.relays.indexOf(url) === -1)
|
|
98
|
+
pointer.relays.push(url);
|
|
99
|
+
});
|
|
100
|
+
author.relays = pointer.relays;
|
|
101
|
+
}
|
|
102
|
+
function parse(event) {
|
|
103
|
+
const result = {
|
|
104
|
+
root: void 0,
|
|
105
|
+
rootKind: void 0,
|
|
106
|
+
reply: void 0,
|
|
107
|
+
replyKind: void 0,
|
|
108
|
+
mentions: [],
|
|
109
|
+
quotes: [],
|
|
110
|
+
profiles: []
|
|
111
|
+
};
|
|
112
|
+
const rootCandidates = [];
|
|
113
|
+
const replyCandidates = [];
|
|
114
|
+
for (const tag of event.tags) {
|
|
115
|
+
if ((tag[0] === "E" || tag[0] === "A" || tag[0] === "I") && tag[1]) {
|
|
116
|
+
const pointer = parsePointer(tag);
|
|
117
|
+
if (pointer)
|
|
118
|
+
rootCandidates.push({ tagName: tag[0], pointer });
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if ((tag[0] === "e" || tag[0] === "a" || tag[0] === "i") && tag[1]) {
|
|
122
|
+
const pointer = parsePointer(tag);
|
|
123
|
+
if (pointer)
|
|
124
|
+
replyCandidates.push({ tagName: tag[0], pointer });
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (tag[0] === "K") {
|
|
128
|
+
result.rootKind = parseKind(tag[1]);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (tag[0] === "k") {
|
|
132
|
+
result.replyKind = parseKind(tag[1]);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (tag[0] === "q") {
|
|
136
|
+
const pointer = parseQuote(tag);
|
|
137
|
+
if (pointer)
|
|
138
|
+
result.quotes.push(pointer);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if ((tag[0] === "P" || tag[0] === "p") && tag[1]) {
|
|
142
|
+
result.profiles.push({
|
|
143
|
+
pubkey: tag[1],
|
|
144
|
+
relays: tag[2] ? [tag[2]] : []
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
result.root = choosePointer(rootCandidates);
|
|
149
|
+
result.reply = choosePointer(replyCandidates);
|
|
150
|
+
inheritRelayHints(result.root, result.profiles);
|
|
151
|
+
inheritRelayHints(result.reply, result.profiles);
|
|
152
|
+
result.quotes.forEach((pointer) => inheritRelayHints(pointer, result.profiles));
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../nip22.ts"],
|
|
4
|
+
"sourcesContent": ["import type { Event } from './core.ts'\nimport type { AddressPointer, EventPointer, ProfilePointer } from './nip19.ts'\n\nexport type ExternalPointer = {\n value: string\n hint?: string\n}\n\nfunction parseKind(kind: string | undefined): number | string | undefined {\n if (!kind) return undefined\n return /^\\d+$/.test(kind) ? parseInt(kind, 10) : kind\n}\n\nfunction parseAddressPointer(value: string, relayUrl?: string): AddressPointer | undefined {\n const idx = value.indexOf(':')\n const idx2 = value.indexOf(':', idx + 1)\n if (idx === -1 || idx2 === -1) return undefined\n\n const kind = parseInt(value.slice(0, idx), 10)\n if (Number.isNaN(kind)) return undefined\n\n return {\n kind,\n pubkey: value.slice(idx + 1, idx2),\n identifier: value.slice(idx2 + 1),\n relays: relayUrl ? [relayUrl] : [],\n }\n}\n\nfunction parsePointer(tag: string[]): EventPointer | AddressPointer | ExternalPointer | undefined {\n switch (tag[0]) {\n case 'E':\n case 'e':\n if (!tag[1]) return undefined\n return {\n id: tag[1],\n relays: tag[2] ? [tag[2]] : [],\n author: tag[3],\n }\n case 'A':\n case 'a':\n if (!tag[1]) return undefined\n return parseAddressPointer(tag[1], tag[2])\n case 'I':\n case 'i':\n if (!tag[1]) return undefined\n return {\n value: tag[1],\n hint: tag[2],\n }\n }\n}\n\nfunction parseQuote(tag: string[]): EventPointer | AddressPointer | ExternalPointer | undefined {\n if (!tag[1]) return undefined\n\n if (tag[1].includes(':')) {\n return parseAddressPointer(tag[1], tag[2])\n }\n\n return {\n id: tag[1],\n relays: tag[2] ? [tag[2]] : [],\n author: tag[3],\n }\n}\n\nfunction choosePointer(\n candidates: Array<{ tagName: string; pointer: EventPointer | AddressPointer | ExternalPointer }>,\n): EventPointer | AddressPointer | ExternalPointer | undefined {\n return (\n candidates.findLast(candidate => candidate.tagName === 'A' || candidate.tagName === 'a')?.pointer ||\n candidates.findLast(candidate => candidate.tagName === 'I' || candidate.tagName === 'i')?.pointer ||\n candidates.findLast(candidate => candidate.tagName === 'E' || candidate.tagName === 'e')?.pointer\n )\n}\n\nfunction inheritRelayHints(\n pointer: EventPointer | AddressPointer | ExternalPointer | undefined,\n profiles: ProfilePointer[],\n) {\n if (!pointer || !('id' in pointer) || !pointer.author) return\n\n const author = profiles.find(profile => profile.pubkey === pointer.author)\n if (!author || !author.relays) return\n\n if (!pointer.relays) {\n pointer.relays = []\n }\n\n author.relays.forEach(url => {\n if (pointer.relays!.indexOf(url) === -1) pointer.relays!.push(url)\n })\n author.relays = pointer.relays\n}\n\nexport function parse(event: Pick<Event, 'tags'>): {\n /**\n * Pointer to root scope.\n */\n root: EventPointer | AddressPointer | ExternalPointer | undefined\n\n /**\n * Kind of root scope from `K` tag.\n */\n rootKind: number | string | undefined\n\n /**\n * Pointer to parent item being replied to.\n */\n reply: EventPointer | AddressPointer | ExternalPointer | undefined\n\n /**\n * Kind of parent item from `k` tag.\n */\n replyKind: number | string | undefined\n\n /**\n * Reserved for extra referenced items.\n */\n mentions: (EventPointer | AddressPointer | ExternalPointer)[]\n\n /**\n * Pointers directly quoted with `q` tags.\n */\n quotes: (EventPointer | AddressPointer | ExternalPointer)[]\n\n /**\n * Root and parent authors.\n */\n profiles: ProfilePointer[]\n} {\n const result: ReturnType<typeof parse> = {\n root: undefined,\n rootKind: undefined,\n reply: undefined,\n replyKind: undefined,\n mentions: [],\n quotes: [],\n profiles: [],\n }\n\n const rootCandidates: Array<{ tagName: string; pointer: EventPointer | AddressPointer | ExternalPointer }> = []\n const replyCandidates: Array<{ tagName: string; pointer: EventPointer | AddressPointer | ExternalPointer }> = []\n\n for (const tag of event.tags) {\n if ((tag[0] === 'E' || tag[0] === 'A' || tag[0] === 'I') && tag[1]) {\n const pointer = parsePointer(tag)\n if (pointer) rootCandidates.push({ tagName: tag[0], pointer })\n continue\n }\n\n if ((tag[0] === 'e' || tag[0] === 'a' || tag[0] === 'i') && tag[1]) {\n const pointer = parsePointer(tag)\n if (pointer) replyCandidates.push({ tagName: tag[0], pointer })\n continue\n }\n\n if (tag[0] === 'K') {\n result.rootKind = parseKind(tag[1])\n continue\n }\n\n if (tag[0] === 'k') {\n result.replyKind = parseKind(tag[1])\n continue\n }\n\n if (tag[0] === 'q') {\n const pointer = parseQuote(tag)\n if (pointer) result.quotes.push(pointer)\n continue\n }\n\n if ((tag[0] === 'P' || tag[0] === 'p') && tag[1]) {\n result.profiles.push({\n pubkey: tag[1],\n relays: tag[2] ? [tag[2]] : [],\n })\n }\n }\n\n result.root = choosePointer(rootCandidates)\n result.reply = choosePointer(replyCandidates)\n\n inheritRelayHints(result.root, result.profiles)\n inheritRelayHints(result.reply, result.profiles)\n result.quotes.forEach(pointer => inheritRelayHints(pointer, result.profiles))\n\n return result\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,SAAS,UAAU,MAAuD;AACxE,MAAI,CAAC;AAAM,WAAO;AAClB,SAAO,QAAQ,KAAK,IAAI,IAAI,SAAS,MAAM,EAAE,IAAI;AACnD;AAEA,SAAS,oBAAoB,OAAe,UAA+C;AACzF,QAAM,MAAM,MAAM,QAAQ,GAAG;AAC7B,QAAM,OAAO,MAAM,QAAQ,KAAK,MAAM,CAAC;AACvC,MAAI,QAAQ,MAAM,SAAS;AAAI,WAAO;AAEtC,QAAM,OAAO,SAAS,MAAM,MAAM,GAAG,GAAG,GAAG,EAAE;AAC7C,MAAI,OAAO,MAAM,IAAI;AAAG,WAAO;AAE/B,SAAO;AAAA,IACL;AAAA,IACA,QAAQ,MAAM,MAAM,MAAM,GAAG,IAAI;AAAA,IACjC,YAAY,MAAM,MAAM,OAAO,CAAC;AAA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I;AAAS,eAAO,OAAO,KAAK,OAAO;AACvC;AAAA,IACF;AAEA,SAAK,IAAI,OAAO,OAAO,IAAI,OAAO,QAAQ,IAAI,IAAI;AAChD,aAAO,SAAS,KAAK;AAAA,QACnB,QAAQ,IAAI;AAAA,QACZ,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC;AAAA,MAC/B,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO,OAAO,cAAc,cAAc;AAC1C,SAAO,QAAQ,cAAc,eAAe;AAE5C,oBAAkB,OAAO,MAAM,OAAO,QAAQ;AAC9C,oBAAkB,OAAO,OAAO,OAAO,QAAQ;AAC/C,SAAO,OAAO,QAAQ,aAAW,kBAAkB,SAAS,OAAO,QAAQ,CAAC;AAE5E,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/lib/cjs/nip44.js
CHANGED
|
@@ -42,7 +42,8 @@ var utf8Encoder = new TextEncoder();
|
|
|
42
42
|
|
|
43
43
|
// nip44.ts
|
|
44
44
|
var minPlaintextSize = 1;
|
|
45
|
-
var maxPlaintextSize =
|
|
45
|
+
var maxPlaintextSize = 4294967295;
|
|
46
|
+
var extendedPrefixThreshold = 65536;
|
|
46
47
|
function getConversationKey(privkeyA, pubkeyB) {
|
|
47
48
|
const sharedX = import_secp256k1.secp256k1.getSharedSecret(privkeyA, (0, import_utils3.hexToBytes)("02" + pubkeyB)).subarray(1, 33);
|
|
48
49
|
return (0, import_hkdf.extract)(import_sha2.sha256, sharedX, utf8Encoder.encode("nip44-v2"));
|
|
@@ -60,28 +61,49 @@ function calcPaddedLen(len) {
|
|
|
60
61
|
throw new Error("expected positive integer");
|
|
61
62
|
if (len <= 32)
|
|
62
63
|
return 32;
|
|
63
|
-
const nextPower =
|
|
64
|
+
const nextPower = 2 ** (Math.floor(Math.log2(len - 1)) + 1);
|
|
64
65
|
const chunk = nextPower <= 256 ? 32 : nextPower / 8;
|
|
65
66
|
return chunk * (Math.floor((len - 1) / chunk) + 1);
|
|
66
67
|
}
|
|
67
68
|
function writeU16BE(num) {
|
|
68
|
-
if (!Number.isSafeInteger(num) || num < minPlaintextSize || num >
|
|
69
|
+
if (!Number.isSafeInteger(num) || num < minPlaintextSize || num > 65535)
|
|
69
70
|
throw new Error("invalid plaintext size: must be between 1 and 65535 bytes");
|
|
70
71
|
const arr = new Uint8Array(2);
|
|
71
72
|
new DataView(arr.buffer).setUint16(0, num, false);
|
|
72
73
|
return arr;
|
|
73
74
|
}
|
|
75
|
+
function writeU32BE(num) {
|
|
76
|
+
if (!Number.isSafeInteger(num) || num < extendedPrefixThreshold || num > maxPlaintextSize)
|
|
77
|
+
throw new Error("invalid plaintext size: must be between 65536 and 4294967295 bytes");
|
|
78
|
+
const arr = new Uint8Array(4);
|
|
79
|
+
new DataView(arr.buffer).setUint32(0, num, false);
|
|
80
|
+
return arr;
|
|
81
|
+
}
|
|
74
82
|
function pad(plaintext) {
|
|
75
83
|
const unpadded = utf8Encoder.encode(plaintext);
|
|
76
84
|
const unpaddedLen = unpadded.length;
|
|
77
|
-
|
|
85
|
+
if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize)
|
|
86
|
+
throw new Error("invalid plaintext size: must be between 1 and 4294967295 bytes");
|
|
87
|
+
const prefix = unpaddedLen >= extendedPrefixThreshold ? (0, import_utils3.concatBytes)(new Uint8Array([0, 0]), writeU32BE(unpaddedLen)) : writeU16BE(unpaddedLen);
|
|
78
88
|
const suffix = new Uint8Array(calcPaddedLen(unpaddedLen) - unpaddedLen);
|
|
79
89
|
return (0, import_utils3.concatBytes)(prefix, unpadded, suffix);
|
|
80
90
|
}
|
|
81
91
|
function unpad(padded) {
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
|
|
92
|
+
const dv = new DataView(padded.buffer, padded.byteOffset, padded.byteLength);
|
|
93
|
+
const firstTwo = dv.getUint16(0);
|
|
94
|
+
let unpaddedLen;
|
|
95
|
+
let prefixLen;
|
|
96
|
+
if (firstTwo === 0) {
|
|
97
|
+
unpaddedLen = dv.getUint32(2);
|
|
98
|
+
if (unpaddedLen < extendedPrefixThreshold)
|
|
99
|
+
throw new Error("invalid padding");
|
|
100
|
+
prefixLen = 6;
|
|
101
|
+
} else {
|
|
102
|
+
unpaddedLen = firstTwo;
|
|
103
|
+
prefixLen = 2;
|
|
104
|
+
}
|
|
105
|
+
const unpadded = padded.subarray(prefixLen, prefixLen + unpaddedLen);
|
|
106
|
+
if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize || unpadded.length !== unpaddedLen || padded.length !== prefixLen + calcPaddedLen(unpaddedLen))
|
|
85
107
|
throw new Error("invalid padding");
|
|
86
108
|
return utf8Decoder.decode(unpadded);
|
|
87
109
|
}
|
|
@@ -95,7 +117,7 @@ function decodePayload(payload) {
|
|
|
95
117
|
if (typeof payload !== "string")
|
|
96
118
|
throw new Error("payload must be a valid string");
|
|
97
119
|
const plen = payload.length;
|
|
98
|
-
if (plen < 132
|
|
120
|
+
if (plen < 132)
|
|
99
121
|
throw new Error("invalid payload length: " + plen);
|
|
100
122
|
if (payload[0] === "#")
|
|
101
123
|
throw new Error("unknown encryption version");
|
|
@@ -106,7 +128,7 @@ function decodePayload(payload) {
|
|
|
106
128
|
throw new Error("invalid base64: " + error.message);
|
|
107
129
|
}
|
|
108
130
|
const dlen = data.length;
|
|
109
|
-
if (dlen < 99
|
|
131
|
+
if (dlen < 99)
|
|
110
132
|
throw new Error("invalid data length: " + dlen);
|
|
111
133
|
const vers = data[0];
|
|
112
134
|
if (vers !== 2)
|
|
@@ -136,7 +158,9 @@ function decrypt(payload, conversationKey) {
|
|
|
136
158
|
var v2 = {
|
|
137
159
|
utils: {
|
|
138
160
|
getConversationKey,
|
|
139
|
-
calcPaddedLen
|
|
161
|
+
calcPaddedLen,
|
|
162
|
+
pad,
|
|
163
|
+
unpad
|
|
140
164
|
},
|
|
141
165
|
encrypt,
|
|
142
166
|
decrypt
|
package/lib/cjs/nip44.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../nip44.ts", "../../utils.ts"],
|
|
4
|
-
"sourcesContent": ["import { chacha20 } from '@noble/ciphers/chacha.js'\nimport { equalBytes } from '@noble/ciphers/utils.js'\nimport { secp256k1 } from '@noble/curves/secp256k1.js'\nimport { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf.js'\nimport { hmac } from '@noble/hashes/hmac.js'\nimport { sha256 } from '@noble/hashes/sha2.js'\nimport { concatBytes, hexToBytes, randomBytes } from '@noble/hashes/utils.js'\nimport { base64 } from '@scure/base'\n\nimport { utf8Decoder, utf8Encoder } from './utils.ts'\n\nconst minPlaintextSize = 0x0001 // 1b msg => padded to 32b\nconst maxPlaintextSize = 0xffff // 65535 (64kb-1) => padded to 64kb\n\nexport function getConversationKey(privkeyA: Uint8Array, pubkeyB: string): Uint8Array {\n const sharedX = secp256k1.getSharedSecret(privkeyA, hexToBytes('02' + pubkeyB)).subarray(1, 33)\n return hkdf_extract(sha256, sharedX, utf8Encoder.encode('nip44-v2'))\n}\n\nfunction getMessageKeys(\n conversationKey: Uint8Array,\n nonce: Uint8Array,\n): { chacha_key: Uint8Array; chacha_nonce: Uint8Array; hmac_key: Uint8Array } {\n const keys = hkdf_expand(sha256, conversationKey, nonce, 76)\n return {\n chacha_key: keys.subarray(0, 32),\n chacha_nonce: keys.subarray(32, 44),\n hmac_key: keys.subarray(44, 76),\n }\n}\n\nfunction calcPaddedLen(len: number): number {\n if (!Number.isSafeInteger(len) || len < 1) throw new Error('expected positive integer')\n if (len <= 32) return 32\n const nextPower = 1 << (Math.floor(Math.log2(len - 1)) + 1)\n const chunk = nextPower <= 256 ? 32 : nextPower / 8\n return chunk * (Math.floor((len - 1) / chunk) + 1)\n}\n\nfunction writeU16BE(num: number): Uint8Array {\n if (!Number.isSafeInteger(num) || num < minPlaintextSize || num > maxPlaintextSize)\n throw new Error('invalid plaintext size: must be between 1 and 65535 bytes')\n const arr = new Uint8Array(2)\n new DataView(arr.buffer).setUint16(0, num, false)\n return arr\n}\n\nfunction pad(plaintext: string): Uint8Array {\n const unpadded = utf8Encoder.encode(plaintext)\n const unpaddedLen = unpadded.length\n const prefix = writeU16BE(unpaddedLen)\n const suffix = new Uint8Array(calcPaddedLen(unpaddedLen) - unpaddedLen)\n return concatBytes(prefix, unpadded, suffix)\n}\n\nfunction unpad(padded: Uint8Array): string {\n const unpaddedLen = new DataView(padded.buffer).getUint16(0)\n const unpadded = padded.subarray(2, 2 + unpaddedLen)\n if (\n unpaddedLen < minPlaintextSize ||\n unpaddedLen > maxPlaintextSize ||\n unpadded.length !== unpaddedLen ||\n padded.length !== 2 + calcPaddedLen(unpaddedLen)\n )\n throw new Error('invalid padding')\n return utf8Decoder.decode(unpadded)\n}\n\nfunction hmacAad(key: Uint8Array, message: Uint8Array, aad: Uint8Array): Uint8Array {\n if (aad.length !== 32) throw new Error('AAD associated data must be 32 bytes')\n const combined = concatBytes(aad, message)\n return hmac(sha256, key, combined)\n}\n\n// metadata: always 65b (version: 1b, nonce: 32b, max: 32b)\n// plaintext: 1b to 0xffff\n// padded plaintext: 32b to 0xffff\n// ciphertext: 32b+2 to 0xffff+2\n// raw payload: 99 (65+32+2) to 65603 (65+0xffff+2)\n// compressed payload (base64): 132b to 87472b\nfunction decodePayload(payload: string): { nonce: Uint8Array; ciphertext: Uint8Array; mac: Uint8Array } {\n if (typeof payload !== 'string') throw new Error('payload must be a valid string')\n const plen = payload.length\n if (plen < 132 || plen > 87472) throw new Error('invalid payload length: ' + plen)\n if (payload[0] === '#') throw new Error('unknown encryption version')\n let data: Uint8Array\n try {\n data = base64.decode(payload)\n } catch (error) {\n throw new Error('invalid base64: ' + (error as any).message)\n }\n const dlen = data.length\n if (dlen < 99 || dlen > 65603) throw new Error('invalid data length: ' + dlen)\n const vers = data[0]\n if (vers !== 2) throw new Error('unknown encryption version ' + vers)\n return {\n nonce: data.subarray(1, 33),\n ciphertext: data.subarray(33, -32),\n mac: data.subarray(-32),\n }\n}\n\nexport function encrypt(plaintext: string, conversationKey: Uint8Array, nonce: Uint8Array = randomBytes(32)): string {\n const { chacha_key, chacha_nonce, hmac_key } = getMessageKeys(conversationKey, nonce)\n const padded = pad(plaintext)\n const ciphertext = chacha20(chacha_key, chacha_nonce, padded)\n const mac = hmacAad(hmac_key, ciphertext, nonce)\n return base64.encode(concatBytes(new Uint8Array([2]), nonce, ciphertext, mac))\n}\n\nexport function decrypt(payload: string, conversationKey: Uint8Array): string {\n const { nonce, ciphertext, mac } = decodePayload(payload)\n const { chacha_key, chacha_nonce, hmac_key } = getMessageKeys(conversationKey, nonce)\n const calculatedMac = hmacAad(hmac_key, ciphertext, nonce)\n if (!equalBytes(calculatedMac, mac)) throw new Error('invalid MAC')\n const padded = chacha20(chacha_key, chacha_nonce, ciphertext)\n return unpad(padded)\n}\n\nexport const v2 = {\n utils: {\n getConversationKey,\n calcPaddedLen,\n },\n encrypt,\n decrypt,\n}\n", "import type { NostrEvent } from './core.ts'\n\nexport const utf8Decoder: TextDecoder = new TextDecoder('utf-8')\nexport const utf8Encoder: TextEncoder = new TextEncoder()\n\nexport { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'\n\nexport function normalizeURL(url: string): string {\n try {\n if (url.indexOf('://') === -1) url = 'wss://' + url\n let p = new URL(url)\n if (p.protocol === 'http:') p.protocol = 'ws:'\n else if (p.protocol === 'https:') p.protocol = 'wss:'\n p.pathname = p.pathname.replace(/\\/+/g, '/')\n if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)\n if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''\n p.searchParams.sort()\n p.hash = ''\n return p.toString()\n } catch (e) {\n throw new Error(`Invalid URL: ${url}`)\n }\n}\n\nexport function insertEventIntoDescendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {\n const [idx, found] = binarySearch(sortedArray, b => {\n if (event.id === b.id) return 0\n if (event.created_at === b.created_at) return -1\n return b.created_at - event.created_at\n })\n if (!found) {\n sortedArray.splice(idx, 0, event)\n }\n return sortedArray\n}\n\nexport function insertEventIntoAscendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {\n const [idx, found] = binarySearch(sortedArray, b => {\n if (event.id === b.id) return 0\n if (event.created_at === b.created_at) return -1\n return event.created_at - b.created_at\n })\n if (!found) {\n sortedArray.splice(idx, 0, event)\n }\n return sortedArray\n}\n\nexport function binarySearch<T>(arr: T[], compare: (b: T) => number): [number, boolean] {\n let start = 0\n let end = arr.length - 1\n\n while (start <= end) {\n const mid = Math.floor((start + end) / 2)\n const cmp = compare(arr[mid])\n\n if (cmp === 0) {\n return [mid, true]\n }\n\n if (cmp < 0) {\n end = mid - 1\n } else {\n start = mid + 1\n }\n }\n\n return [start, false]\n}\n\nexport function mergeReverseSortedLists(list1: NostrEvent[], list2: NostrEvent[]): NostrEvent[] {\n const result: NostrEvent[] = new Array(list1.length + list2.length)\n result.length = 0\n let i1 = 0\n let i2 = 0\n let sameTimestampIds: string[] = []\n\n while (i1 < list1.length && i2 < list2.length) {\n let next: NostrEvent\n if (list1[i1]?.created_at > list2[i2]?.created_at) {\n next = list1[i1]\n i1++\n } else {\n next = list2[i2]\n i2++\n }\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n while (i1 < list1.length) {\n const next = list1[i1]\n i1++\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n while (i2 < list2.length) {\n const next = list2[i2]\n i2++\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n return result\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAyB;AACzB,IAAAA,gBAA2B;AAC3B,uBAA0B;AAC1B,kBAA+D;AAC/D,kBAAqB;AACrB,kBAAuB;AACvB,IAAAA,gBAAqD;AACrD,kBAAuB;;;ACFvB,mBAAuC;AAHhC,IAAM,cAA2B,IAAI,YAAY,OAAO;AACxD,IAAM,cAA2B,IAAI,YAAY;;;ADQxD,IAAM,mBAAmB;AACzB,IAAM,mBAAmB;
|
|
4
|
+
"sourcesContent": ["import { chacha20 } from '@noble/ciphers/chacha.js'\nimport { equalBytes } from '@noble/ciphers/utils.js'\nimport { secp256k1 } from '@noble/curves/secp256k1.js'\nimport { extract as hkdf_extract, expand as hkdf_expand } from '@noble/hashes/hkdf.js'\nimport { hmac } from '@noble/hashes/hmac.js'\nimport { sha256 } from '@noble/hashes/sha2.js'\nimport { concatBytes, hexToBytes, randomBytes } from '@noble/hashes/utils.js'\nimport { base64 } from '@scure/base'\n\nimport { utf8Decoder, utf8Encoder } from './utils.ts'\n\nconst minPlaintextSize = 0x0001 // 1b msg => padded to 32b\nconst maxPlaintextSize = 0xffffffff // 4294967295 (2^32-1)\nconst extendedPrefixThreshold = 0x10000 // 65536: lengths below use 2-byte u16 prefix, at or above use 6-byte prefix\n\nexport function getConversationKey(privkeyA: Uint8Array, pubkeyB: string): Uint8Array {\n const sharedX = secp256k1.getSharedSecret(privkeyA, hexToBytes('02' + pubkeyB)).subarray(1, 33)\n return hkdf_extract(sha256, sharedX, utf8Encoder.encode('nip44-v2'))\n}\n\nfunction getMessageKeys(\n conversationKey: Uint8Array,\n nonce: Uint8Array,\n): { chacha_key: Uint8Array; chacha_nonce: Uint8Array; hmac_key: Uint8Array } {\n const keys = hkdf_expand(sha256, conversationKey, nonce, 76)\n return {\n chacha_key: keys.subarray(0, 32),\n chacha_nonce: keys.subarray(32, 44),\n hmac_key: keys.subarray(44, 76),\n }\n}\n\nfunction calcPaddedLen(len: number): number {\n if (!Number.isSafeInteger(len) || len < 1) throw new Error('expected positive integer')\n if (len <= 32) return 32\n const nextPower = 2 ** (Math.floor(Math.log2(len - 1)) + 1)\n const chunk = nextPower <= 256 ? 32 : nextPower / 8\n return chunk * (Math.floor((len - 1) / chunk) + 1)\n}\n\nfunction writeU16BE(num: number): Uint8Array {\n if (!Number.isSafeInteger(num) || num < minPlaintextSize || num > 0xffff)\n throw new Error('invalid plaintext size: must be between 1 and 65535 bytes')\n const arr = new Uint8Array(2)\n new DataView(arr.buffer).setUint16(0, num, false)\n return arr\n}\n\nfunction writeU32BE(num: number): Uint8Array {\n if (!Number.isSafeInteger(num) || num < extendedPrefixThreshold || num > maxPlaintextSize)\n throw new Error('invalid plaintext size: must be between 65536 and 4294967295 bytes')\n const arr = new Uint8Array(4)\n new DataView(arr.buffer).setUint32(0, num, false)\n return arr\n}\n\nfunction pad(plaintext: string): Uint8Array {\n const unpadded = utf8Encoder.encode(plaintext)\n const unpaddedLen = unpadded.length\n if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize)\n throw new Error('invalid plaintext size: must be between 1 and 4294967295 bytes')\n const prefix =\n unpaddedLen >= extendedPrefixThreshold\n ? concatBytes(new Uint8Array([0, 0]), writeU32BE(unpaddedLen)) // 6 bytes\n : writeU16BE(unpaddedLen) // 2 bytes\n const suffix = new Uint8Array(calcPaddedLen(unpaddedLen) - unpaddedLen)\n return concatBytes(prefix, unpadded, suffix)\n}\n\nfunction unpad(padded: Uint8Array): string {\n const dv = new DataView(padded.buffer, padded.byteOffset, padded.byteLength)\n const firstTwo = dv.getUint16(0)\n let unpaddedLen: number\n let prefixLen: number\n if (firstTwo === 0) {\n // Extended format: 2 zero bytes + 4-byte u32 length\n unpaddedLen = dv.getUint32(2)\n if (unpaddedLen < extendedPrefixThreshold) throw new Error('invalid padding')\n prefixLen = 6\n } else {\n unpaddedLen = firstTwo\n prefixLen = 2\n }\n const unpadded = padded.subarray(prefixLen, prefixLen + unpaddedLen)\n if (\n unpaddedLen < minPlaintextSize ||\n unpaddedLen > maxPlaintextSize ||\n unpadded.length !== unpaddedLen ||\n padded.length !== prefixLen + calcPaddedLen(unpaddedLen)\n )\n throw new Error('invalid padding')\n return utf8Decoder.decode(unpadded)\n}\n\nfunction hmacAad(key: Uint8Array, message: Uint8Array, aad: Uint8Array): Uint8Array {\n if (aad.length !== 32) throw new Error('AAD associated data must be 32 bytes')\n const combined = concatBytes(aad, message)\n return hmac(sha256, key, combined)\n}\n\n// metadata: always 65b (version: 1b, nonce: 32b, mac: 32b)\n// plaintext: 1b to 0xffffffff\n// padded plaintext (small, <65536): 32b to 0x10000, with 2b prefix -> 34b to 0x10000+2\n// padded plaintext (large, >=65536): 0x10000 to 0x100000000, with 6b prefix -> 0x10006 to 0x100000000+6\n// ciphertext: same as padded plaintext (chacha20 doesn't change length)\n// raw payload (small): 99 (65+34) to 65603 (65+0x10000+2)\n// raw payload (large): 65607 (65+0x10006) to 4294967367 (65+0x100000000+6)\nfunction decodePayload(payload: string): { nonce: Uint8Array; ciphertext: Uint8Array; mac: Uint8Array } {\n if (typeof payload !== 'string') throw new Error('payload must be a valid string')\n const plen = payload.length\n if (plen < 132) throw new Error('invalid payload length: ' + plen)\n if (payload[0] === '#') throw new Error('unknown encryption version')\n let data: Uint8Array\n try {\n data = base64.decode(payload)\n } catch (error) {\n throw new Error('invalid base64: ' + (error as any).message)\n }\n const dlen = data.length\n if (dlen < 99) throw new Error('invalid data length: ' + dlen)\n const vers = data[0]\n if (vers !== 2) throw new Error('unknown encryption version ' + vers)\n return {\n nonce: data.subarray(1, 33),\n ciphertext: data.subarray(33, -32),\n mac: data.subarray(-32),\n }\n}\n\nexport function encrypt(plaintext: string, conversationKey: Uint8Array, nonce: Uint8Array = randomBytes(32)): string {\n const { chacha_key, chacha_nonce, hmac_key } = getMessageKeys(conversationKey, nonce)\n const padded = pad(plaintext)\n const ciphertext = chacha20(chacha_key, chacha_nonce, padded)\n const mac = hmacAad(hmac_key, ciphertext, nonce)\n return base64.encode(concatBytes(new Uint8Array([2]), nonce, ciphertext, mac))\n}\n\n/** Callers should validate payload size before calling to prevent DoS from oversized inputs. */\nexport function decrypt(payload: string, conversationKey: Uint8Array): string {\n const { nonce, ciphertext, mac } = decodePayload(payload)\n const { chacha_key, chacha_nonce, hmac_key } = getMessageKeys(conversationKey, nonce)\n const calculatedMac = hmacAad(hmac_key, ciphertext, nonce)\n if (!equalBytes(calculatedMac, mac)) throw new Error('invalid MAC')\n const padded = chacha20(chacha_key, chacha_nonce, ciphertext)\n return unpad(padded)\n}\n\nexport const v2 = {\n utils: {\n getConversationKey,\n calcPaddedLen,\n pad,\n unpad,\n },\n encrypt,\n decrypt,\n}\n", "import type { NostrEvent } from './core.ts'\n\nexport const utf8Decoder: TextDecoder = new TextDecoder('utf-8')\nexport const utf8Encoder: TextEncoder = new TextEncoder()\n\nexport { bytesToHex, hexToBytes } from '@noble/hashes/utils.js'\n\nexport function normalizeURL(url: string): string {\n try {\n if (url.indexOf('://') === -1) url = 'wss://' + url\n let p = new URL(url)\n if (p.protocol === 'http:') p.protocol = 'ws:'\n else if (p.protocol === 'https:') p.protocol = 'wss:'\n p.pathname = p.pathname.replace(/\\/+/g, '/')\n if (p.pathname.endsWith('/')) p.pathname = p.pathname.slice(0, -1)\n if ((p.port === '80' && p.protocol === 'ws:') || (p.port === '443' && p.protocol === 'wss:')) p.port = ''\n p.searchParams.sort()\n p.hash = ''\n return p.toString()\n } catch (e) {\n throw new Error(`Invalid URL: ${url}`)\n }\n}\n\nexport function insertEventIntoDescendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {\n const [idx, found] = binarySearch(sortedArray, b => {\n if (event.id === b.id) return 0\n if (event.created_at === b.created_at) return -1\n return b.created_at - event.created_at\n })\n if (!found) {\n sortedArray.splice(idx, 0, event)\n }\n return sortedArray\n}\n\nexport function insertEventIntoAscendingList(sortedArray: NostrEvent[], event: NostrEvent): NostrEvent[] {\n const [idx, found] = binarySearch(sortedArray, b => {\n if (event.id === b.id) return 0\n if (event.created_at === b.created_at) return -1\n return event.created_at - b.created_at\n })\n if (!found) {\n sortedArray.splice(idx, 0, event)\n }\n return sortedArray\n}\n\nexport function binarySearch<T>(arr: T[], compare: (b: T) => number): [number, boolean] {\n let start = 0\n let end = arr.length - 1\n\n while (start <= end) {\n const mid = Math.floor((start + end) / 2)\n const cmp = compare(arr[mid])\n\n if (cmp === 0) {\n return [mid, true]\n }\n\n if (cmp < 0) {\n end = mid - 1\n } else {\n start = mid + 1\n }\n }\n\n return [start, false]\n}\n\nexport function mergeReverseSortedLists(list1: NostrEvent[], list2: NostrEvent[]): NostrEvent[] {\n const result: NostrEvent[] = new Array(list1.length + list2.length)\n result.length = 0\n let i1 = 0\n let i2 = 0\n let sameTimestampIds: string[] = []\n\n while (i1 < list1.length && i2 < list2.length) {\n let next: NostrEvent\n if (list1[i1]?.created_at > list2[i2]?.created_at) {\n next = list1[i1]\n i1++\n } else {\n next = list2[i2]\n i2++\n }\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n while (i1 < list1.length) {\n const next = list1[i1]\n i1++\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n while (i2 < list2.length) {\n const next = list2[i2]\n i2++\n\n if (result.length > 0 && result[result.length - 1].created_at === next.created_at) {\n if (sameTimestampIds.includes(next.id)) continue\n } else {\n sameTimestampIds.length = 0\n }\n result.push(next)\n sameTimestampIds.push(next.id)\n }\n\n return result\n}\n"],
|
|
5
|
+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