Compare commits

..

3 Commits

Author SHA1 Message Date
fiatjaf
29ecdfc5ec fix slow types so we can publish to jsr.io 2024-10-26 14:23:28 -03:00
fiatjaf
d3fc4734b4 export missing modules. 2024-10-26 13:10:26 -03:00
fiatjaf
66d0b8a4e1 nip46: export queryBunkerProfile() 2024-10-26 07:24:13 -03:00
6 changed files with 35 additions and 31 deletions

View File

@@ -23,7 +23,7 @@ export type SubscribeManyParams = Omit<SubscriptionParams, 'onclose' | 'id'> & {
} }
export class AbstractSimplePool { export class AbstractSimplePool {
protected relays = new Map<string, AbstractRelay>() protected relays: Map<string, AbstractRelay> = new Map()
public seenOn: Map<string, Set<AbstractRelay>> = new Map() public seenOn: Map<string, Set<AbstractRelay>> = new Map()
public trackRelays: boolean = false public trackRelays: boolean = false

View File

@@ -1,6 +1,6 @@
{ {
"name": "@nostr/tools", "name": "@nostr/tools",
"version": "2.3.2", "version": "2.9.4",
"exports": { "exports": {
".": "./index.ts", ".": "./index.ts",
"./core": "./core.ts", "./core": "./core.ts",
@@ -20,6 +20,7 @@
"./nip10": "./nip10.ts", "./nip10": "./nip10.ts",
"./nip11": "./nip11.ts", "./nip11": "./nip11.ts",
"./nip13": "./nip13.ts", "./nip13": "./nip13.ts",
"./nip17": "./nip17.ts",
"./nip18": "./nip18.ts", "./nip18": "./nip18.ts",
"./nip19": "./nip19.ts", "./nip19": "./nip19.ts",
"./nip21": "./nip21.ts", "./nip21": "./nip21.ts",
@@ -34,6 +35,8 @@
"./nip46": "./nip46.ts", "./nip46": "./nip46.ts",
"./nip49": "./nip49.ts", "./nip49": "./nip49.ts",
"./nip57": "./nip57.ts", "./nip57": "./nip57.ts",
"./nip58": "./nip58.ts",
"./nip59": "./nip59.ts",
"./nip75": "./nip75.ts", "./nip75": "./nip75.ts",
"./nip94": "./nip94.ts", "./nip94": "./nip94.ts",
"./nip96": "./nip96.ts", "./nip96": "./nip96.ts",
@@ -42,4 +45,4 @@
"./fakejson": "./fakejson.ts", "./fakejson": "./fakejson.ts",
"./utils": "./utils.ts" "./utils": "./utils.ts"
} }
} }

View File

@@ -1,5 +1,5 @@
import { PrivateDirectMessage } from './kinds.ts' import { PrivateDirectMessage } from './kinds.ts'
import { EventTemplate, getPublicKey } from './pure.ts' import { EventTemplate, NostrEvent, getPublicKey } from './pure.ts'
import * as nip59 from './nip59.ts' import * as nip59 from './nip59.ts'
type Recipient = { type Recipient = {
@@ -48,7 +48,7 @@ export function wrapEvent(
message: string, message: string,
conversationTitle?: string, conversationTitle?: string,
replyTo?: ReplyTo, replyTo?: ReplyTo,
) { ): NostrEvent {
const event = createEvent(recipient, message, conversationTitle, replyTo) const event = createEvent(recipient, message, conversationTitle, replyTo)
return nip59.wrapEvent(event, senderPrivateKey, recipient.publicKey) return nip59.wrapEvent(event, senderPrivateKey, recipient.publicKey)
} }
@@ -59,22 +59,17 @@ export function wrapManyEvents(
message: string, message: string,
conversationTitle?: string, conversationTitle?: string,
replyTo?: ReplyTo, replyTo?: ReplyTo,
) { ): NostrEvent[] {
if (!recipients || recipients.length === 0) { if (!recipients || recipients.length === 0) {
throw new Error('At least one recipient is required.') throw new Error('At least one recipient is required.')
} }
const senderPublicKey = getPublicKey(senderPrivateKey) const senderPublicKey = getPublicKey(senderPrivateKey)
// Initialize the wrappeds array with the sender's own wrapped event // wrap the event for the sender and then for each recipient
const wrappeds = [wrapEvent(senderPrivateKey, { publicKey: senderPublicKey }, message, conversationTitle, replyTo)] return [{ publicKey: senderPublicKey }, ...recipients].map(recipient =>
wrapEvent(senderPrivateKey, recipient, message, conversationTitle, replyTo),
// Wrap the event for each recipient )
recipients.forEach(recipient => {
wrappeds.push(wrapEvent(senderPrivateKey, recipient, message, conversationTitle, replyTo))
})
return wrappeds
} }
export const unwrapEvent = nip59.unwrapEvent export const unwrapEvent = nip59.unwrapEvent

View File

@@ -48,7 +48,7 @@ export async function parseBunkerInput(input: string): Promise<BunkerPointer | n
return queryBunkerProfile(input) return queryBunkerProfile(input)
} }
async function queryBunkerProfile(nip05: string): Promise<BunkerPointer | null> { export async function queryBunkerProfile(nip05: string): Promise<BunkerPointer | null> {
const match = nip05.match(NIP05_REGEX) const match = nip05.match(NIP05_REGEX)
if (!match) return null if (!match) return null
@@ -292,9 +292,6 @@ export async function createAccount(
return rpc return rpc
} }
// @deprecated use fetchBunkerProviders instead
export const fetchCustodialBunkers = fetchBunkerProviders
/** /**
* Fetches info on available providers that announce themselves using NIP-89 events. * Fetches info on available providers that announce themselves using NIP-89 events.
* @returns A promise that resolves to an array of available bunker objects. * @returns A promise that resolves to an array of available bunker objects.

View File

@@ -1,4 +1,4 @@
import { EventTemplate, UnsignedEvent, Event } from './core.ts' import { EventTemplate, UnsignedEvent, NostrEvent } from './core.ts'
import { getConversationKey, decrypt, encrypt } from './nip44.ts' import { getConversationKey, decrypt, encrypt } from './nip44.ts'
import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts' import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts'
import { Seal, GiftWrap } from './kinds.ts' import { Seal, GiftWrap } from './kinds.ts'
@@ -15,10 +15,10 @@ const nip44ConversationKey = (privateKey: Uint8Array, publicKey: string) => getC
const nip44Encrypt = (data: EventTemplate, privateKey: Uint8Array, publicKey: string) => const nip44Encrypt = (data: EventTemplate, privateKey: Uint8Array, publicKey: string) =>
encrypt(JSON.stringify(data), nip44ConversationKey(privateKey, publicKey)) encrypt(JSON.stringify(data), nip44ConversationKey(privateKey, publicKey))
const nip44Decrypt = (data: Event, privateKey: Uint8Array) => const nip44Decrypt = (data: NostrEvent, privateKey: Uint8Array) =>
JSON.parse(decrypt(data.content, nip44ConversationKey(privateKey, data.pubkey))) JSON.parse(decrypt(data.content, nip44ConversationKey(privateKey, data.pubkey)))
export function createRumor(event: Partial<UnsignedEvent>, privateKey: Uint8Array) { export function createRumor(event: Partial<UnsignedEvent>, privateKey: Uint8Array): Rumor {
const rumor = { const rumor = {
created_at: now(), created_at: now(),
content: '', content: '',
@@ -32,7 +32,7 @@ export function createRumor(event: Partial<UnsignedEvent>, privateKey: Uint8Arra
return rumor as Rumor return rumor as Rumor
} }
export function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublicKey: string) { export function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublicKey: string): NostrEvent {
return finalizeEvent( return finalizeEvent(
{ {
kind: Seal, kind: Seal,
@@ -41,10 +41,10 @@ export function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublic
tags: [], tags: [],
}, },
privateKey, privateKey,
) as Event )
} }
export function createWrap(seal: Event, recipientPublicKey: string) { export function createWrap(seal: NostrEvent, recipientPublicKey: string): NostrEvent {
const randomKey = generateSecretKey() const randomKey = generateSecretKey()
return finalizeEvent( return finalizeEvent(
@@ -55,10 +55,14 @@ export function createWrap(seal: Event, recipientPublicKey: string) {
tags: [['p', recipientPublicKey]], tags: [['p', recipientPublicKey]],
}, },
randomKey, randomKey,
) as Event ) as NostrEvent
} }
export function wrapEvent(event: Partial<UnsignedEvent>, senderPrivateKey: Uint8Array, recipientPublicKey: string) { export function wrapEvent(
event: Partial<UnsignedEvent>,
senderPrivateKey: Uint8Array,
recipientPublicKey: string,
): NostrEvent {
const rumor = createRumor(event, senderPrivateKey) const rumor = createRumor(event, senderPrivateKey)
const seal = createSeal(rumor, senderPrivateKey, recipientPublicKey) const seal = createSeal(rumor, senderPrivateKey, recipientPublicKey)
@@ -69,7 +73,7 @@ export function wrapManyEvents(
event: Partial<UnsignedEvent>, event: Partial<UnsignedEvent>,
senderPrivateKey: Uint8Array, senderPrivateKey: Uint8Array,
recipientsPublicKeys: string[], recipientsPublicKeys: string[],
) { ): NostrEvent[] {
if (!recipientsPublicKeys || recipientsPublicKeys.length === 0) { if (!recipientsPublicKeys || recipientsPublicKeys.length === 0) {
throw new Error('At least one recipient is required.') throw new Error('At least one recipient is required.')
} }
@@ -85,12 +89,12 @@ export function wrapManyEvents(
return wrappeds return wrappeds
} }
export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array): Rumor { export function unwrapEvent(wrap: NostrEvent, recipientPrivateKey: Uint8Array): Rumor {
const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey) const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey)
return nip44Decrypt(unwrappedSeal, recipientPrivateKey) return nip44Decrypt(unwrappedSeal, recipientPrivateKey)
} }
export function unwrapManyEvents(wrappedEvents: Event[], recipientPrivateKey: Uint8Array): Rumor[] { export function unwrapManyEvents(wrappedEvents: NostrEvent[], recipientPrivateKey: Uint8Array): Rumor[] {
let unwrappedEvents: Rumor[] = [] let unwrappedEvents: Rumor[] = []
wrappedEvents.forEach(e => { wrappedEvents.forEach(e => {

View File

@@ -1,7 +1,7 @@
{ {
"type": "module", "type": "module",
"name": "nostr-tools", "name": "nostr-tools",
"version": "2.9.2", "version": "2.9.4",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",
@@ -103,6 +103,11 @@
"require": "./lib/cjs/nip13.js", "require": "./lib/cjs/nip13.js",
"types": "./lib/types/nip13.d.ts" "types": "./lib/types/nip13.d.ts"
}, },
"./nip17": {
"import": "./lib/esm/nip17.js",
"require": "./lib/cjs/nip17.js",
"types": "./lib/types/nip17.d.ts"
},
"./nip18": { "./nip18": {
"import": "./lib/esm/nip18.js", "import": "./lib/esm/nip18.js",
"require": "./lib/cjs/nip18.js", "require": "./lib/cjs/nip18.js",