Compare commits

...

8 Commits

Author SHA1 Message Date
fiatjaf
7b79d6a899 nostr-wasm as optional and v2.1.3 2024-01-09 16:58:47 -03:00
Alex Gleason
c1efbbd919 Add NIP-40 module for event expiration 2024-01-09 16:16:43 -03:00
Akiomi Kamakura
7d58705e9a Fix typo 2024-01-08 13:50:48 -03:00
Akiomi Kamakura
f1d315632c Sort kinds 2024-01-08 13:50:36 -03:00
Alex Gleason
348d118ce4 Add getFilterLimit function 2024-01-04 09:56:02 -03:00
fiatjaf
498c1603b0 nip57: implement "P" tag for sender. 2024-01-01 11:39:22 -03:00
Shusui MOYATANI
4cfc67e294 fix yieldThread memory leak 2023-12-30 13:50:44 -03:00
fiatjaf
da51418f04 update readme example.
fixes https://github.com/nbd-wtf/nostr-tools/issues/337
2023-12-27 11:14:19 -03:00
11 changed files with 174 additions and 39 deletions

View File

@@ -66,18 +66,18 @@ const sub = relay.subscribe([
let sk = generateSecretKey()
let pk = getPublicKey(sk)
let sub = relay.sub([
relay.sub([
{
kinds: [1],
authors: [pk],
},
])
sub.on('event', event => {
console.log('got event:', event)
], {
onevent(event) {
console.log('got event:', event)
}
})
let event = {
let eventTemplate = {
kind: 1,
created_at: Math.floor(Date.now() / 1000),
tags: [],
@@ -85,14 +85,9 @@ let event = {
}
// this assigns the pubkey, calculates the event id and signs the event in a single step
const signedEvent = finalizeEvent(event, sk)
const signedEvent = finalizeEvent(eventTemplate, sk)
await relay.publish(signedEvent)
let events = await relay.list([{ kinds: [0, 1] }])
let event = await relay.get({
ids: ['44e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245'],
})
relay.close()
```

View File

@@ -1,5 +1,5 @@
import { describe, test, expect } from 'bun:test'
import { matchFilter, matchFilters, mergeFilters } from './filter.ts'
import { getFilterLimit, matchFilter, matchFilters, mergeFilters } from './filter.ts'
import { buildEvent } from './test-helpers.ts'
describe('Filter', () => {
@@ -241,4 +241,27 @@ describe('Filter', () => {
).toEqual({ kinds: [1, 7, 9, 10], since: 10, until: 30 })
})
})
describe('getFilterLimit', () => {
test('should handle ids', () => {
expect(getFilterLimit({ ids: ['123'] })).toEqual(1)
expect(getFilterLimit({ ids: ['123'], limit: 2 })).toEqual(1)
expect(getFilterLimit({ ids: ['123'], limit: 0 })).toEqual(0)
expect(getFilterLimit({ ids: ['123'], limit: -1 })).toEqual(0)
})
test('should count the authors times replaceable kinds', () => {
expect(getFilterLimit({ kinds: [0], authors: ['alex'] })).toEqual(1)
expect(getFilterLimit({ kinds: [0, 3], authors: ['alex'] })).toEqual(2)
expect(getFilterLimit({ kinds: [0, 3], authors: ['alex', 'fiatjaf'] })).toEqual(4)
})
test('should return Infinity for authors with regular kinds', () => {
expect(getFilterLimit({ kinds: [1], authors: ['alex'] })).toEqual(Infinity)
})
test('should return Infinity for empty filters', () => {
expect(getFilterLimit({})).toEqual(Infinity)
})
})
})

View File

@@ -1,4 +1,5 @@
import { Event } from './core.ts'
import { isReplaceableKind } from './kinds.ts'
export type Filter = {
ids?: string[]
@@ -70,3 +71,19 @@ export function mergeFilters(...filters: Filter[]): Filter {
return result
}
/** Calculate the intrinsic limit of a filter. This function may return `Infinity`. */
export function getFilterLimit(filter: Filter): number {
if (filter.ids && !filter.ids.length) return 0
if (filter.kinds && !filter.kinds.length) return 0
if (filter.authors && !filter.authors.length) return 0
return Math.min(
Math.max(0, filter.limit ?? Infinity),
filter.ids?.length ?? Infinity,
filter.authors?.length &&
filter.kinds?.every((kind) => isReplaceableKind(kind))
? filter.authors.length * filter.kinds.length
: Infinity,
)
}

View File

@@ -1,10 +1,15 @@
import { verifiedSymbol, type Event, type Nostr, VerifiedEvent } from './core.ts'
export async function yieldThread() {
return new Promise(resolve => {
return new Promise<void>(resolve => {
const ch = new MessageChannel()
const handler = () => {
// @ts-ignore (typescript thinks this property should be called `removeListener`, but in fact it's `removeEventListener`)
ch.port1.removeEventListener('message', handler)
resolve()
}
// @ts-ignore (typescript thinks this property should be called `addListener`, but in fact it's `addEventListener`)
ch.port1.addEventListener('message', resolve)
ch.port1.addEventListener('message', handler)
ch.port2.postMessage(0)
ch.port1.start()
})

View File

@@ -35,27 +35,22 @@ export const ShortTextNote = 1
export const RecommendRelay = 2
export const Contacts = 3
export const EncryptedDirectMessage = 4
export const EncryptedDirectMessages = 4
export const EventDeletion = 5
export const Repost = 6
export const Reaction = 7
export const BadgeAward = 8
export const GenericRepost = 16
export const ChannelCreation = 40
export const ChannelMetadata = 41
export const ChannelMessage = 42
export const ChannelHideMessage = 43
export const ChannelMuteUser = 44
export const Report = 1984
export const ZapRequest = 9734
export const Zap = 9735
export const RelayList = 10002
export const ClientAuth = 22242
export const BadgeDefinition = 30009
export const FileMetadata = 1063
export const EncryptedDirectMessages = 4
export const GenericRepost = 16
export const OpenTimestamps = 1040
export const FileMetadata = 1063
export const LiveChatMessage = 1311
export const ProblemTracker = 1971
export const Report = 1984
export const Reporting = 1984
export const Label = 1985
export const CommunityPostApproval = 4550
@@ -63,9 +58,12 @@ export const JobRequest = 5999
export const JobResult = 6999
export const JobFeedback = 7000
export const ZapGoal = 9041
export const ZapRequest = 9734
export const Zap = 9735
export const Highlights = 9802
export const Mutelist = 10000
export const Pinlist = 10001
export const RelayList = 10002
export const BookmarkList = 10003
export const CommunitiesList = 10004
export const PublicChatsList = 10005
@@ -75,6 +73,7 @@ export const InterestsList = 10015
export const UserEmojiList = 10030
export const NWCWalletInfo = 13194
export const LightningPubRPC = 21000
export const ClientAuth = 22242
export const NWCWalletRequest = 23194
export const NWCWalletResponse = 23195
export const NostrConnect = 24133
@@ -85,6 +84,7 @@ export const Relaysets = 30002
export const Bookmarksets = 30003
export const Curationsets = 30004
export const ProfileBadges = 30008
export const BadgeDefinition = 30009
export const Interestsets = 30015
export const CreateOrUpdateStall = 30017
export const CreateOrUpdateProduct = 30018

44
nip40.test.ts Normal file
View File

@@ -0,0 +1,44 @@
import { describe, test, expect, jest } from 'bun:test'
import { buildEvent } from './test-helpers.ts'
import { getExpiration, isEventExpired, waitForExpire, onExpire } from './nip40.ts'
describe('getExpiration', () => {
test('returns the expiration as a Date object', () => {
const event = buildEvent({ tags: [['expiration', '123']] })
const result = getExpiration(event)
expect(result).toEqual(new Date(123000))
})
})
describe('isEventExpired', () => {
test('returns true when the event has expired', () => {
const event = buildEvent({ tags: [['expiration', '123']] })
const result = isEventExpired(event)
expect(result).toEqual(true)
})
test('returns false when the event has not expired', () => {
const future = Math.floor(Date.now() / 1000) + 10
const event = buildEvent({ tags: [['expiration', future.toString()]] })
const result = isEventExpired(event)
expect(result).toEqual(false)
})
})
describe('waitForExpire', () => {
test('returns a promise that resolves when the event expires', async () => {
const event = buildEvent({ tags: [['expiration', '123']] })
const result = await waitForExpire(event)
expect(result).toEqual(event)
})
})
describe('onExpire', () => {
test('calls the callback when the event expires', async () => {
const event = buildEvent({ tags: [['expiration', '123']] })
const callback = jest.fn()
onExpire(event, callback)
await new Promise((resolve) => setTimeout(resolve, 200))
expect(callback).toHaveBeenCalled()
})
})

49
nip40.ts Normal file
View File

@@ -0,0 +1,49 @@
import { Event } from './core.ts'
/** Get the expiration of the event as a `Date` object, if any. */
function getExpiration(event: Event): Date | undefined {
const tag = event.tags.find(([name]) => name === 'expiration')
if (tag) {
return new Date(parseInt(tag[1]) * 1000)
}
}
/** Check if the event has expired. */
function isEventExpired(event: Event): boolean {
const expiration = getExpiration(event)
if (expiration) {
return Date.now() > expiration.getTime()
} else {
return false
}
}
/** Returns a promise that resolves when the event expires. */
async function waitForExpire(event: Event): Promise<Event> {
const expiration = getExpiration(event)
if (expiration) {
const diff = expiration.getTime() - Date.now()
if (diff > 0) {
await sleep(diff)
return event
} else {
return event
}
} else {
throw new Error('Event has no expiration')
}
}
/** Calls the callback when the event expires. */
function onExpire(event: Event, callback: (event: Event) => void): void {
waitForExpire(event)
.then(callback)
.catch(() => {})
}
/** Resolves when the given number of milliseconds have elapsed. */
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
export { getExpiration, isEventExpired, waitForExpire, onExpire }

View File

@@ -242,10 +242,11 @@ describe('validateZapRequest', () => {
})
describe('makeZapReceipt', () => {
test('returns a valid Zap receipt with a preimage', () => {
const privateKey = generateSecretKey()
const publicKey = getPublicKey(privateKey)
const privateKey = generateSecretKey()
const publicKey = getPublicKey(privateKey)
const target = 'efeb5d6e74ce6ffea6cae4094a9f29c26b5c56d7b44fae9f490f3410fd708c45'
test('returns a valid Zap receipt with a preimage', () => {
const zapRequest = JSON.stringify(
finalizeEvent(
{
@@ -253,7 +254,7 @@ describe('makeZapReceipt', () => {
created_at: Date.now() / 1000,
content: 'content',
tags: [
['p', publicKey],
['p', target],
['amount', '100'],
['relays', 'relay1', 'relay2'],
],
@@ -274,16 +275,14 @@ describe('makeZapReceipt', () => {
expect.arrayContaining([
['bolt11', bolt11],
['description', zapRequest],
['p', publicKey],
['p', target],
['P', publicKey],
['preimage', preimage],
]),
)
})
test('returns a valid Zap receipt without a preimage', () => {
const privateKey = generateSecretKey()
const publicKey = getPublicKey(privateKey)
const zapRequest = JSON.stringify(
finalizeEvent(
{
@@ -291,7 +290,7 @@ describe('makeZapReceipt', () => {
created_at: Date.now() / 1000,
content: 'content',
tags: [
['p', publicKey],
['p', target],
['amount', '100'],
['relays', 'relay1', 'relay2'],
],
@@ -311,7 +310,8 @@ describe('makeZapReceipt', () => {
expect.arrayContaining([
['bolt11', bolt11],
['description', zapRequest],
['p', publicKey],
['p', target],
['P', publicKey],
]),
)
expect(JSON.stringify(result.tags)).not.toContain('preimage')

View File

@@ -119,7 +119,7 @@ export function makeZapReceipt({
kind: 9735,
created_at: Math.round(paidAt.getTime() / 1000),
content: '',
tags: [...tagsFromZapRequest, ['bolt11', bolt11], ['description', zapRequest]],
tags: [...tagsFromZapRequest, ['P', zr.pubkey], ['bolt11', bolt11], ['description', zapRequest]],
}
if (preimage) {

View File

@@ -1,7 +1,7 @@
{
"type": "module",
"name": "nostr-tools",
"version": "2.1.1",
"version": "2.1.3",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",
@@ -173,8 +173,9 @@
"@noble/hashes": "1.3.1",
"@scure/base": "1.1.1",
"@scure/bip32": "1.3.1",
"@scure/bip39": "1.2.1",
"mitata": "^0.1.6",
"@scure/bip39": "1.2.1"
},
"optionalDependencies": {
"nostr-wasm": "v0.1.0"
},
"peerDependencies": {
@@ -205,6 +206,7 @@
"eslint-plugin-babel": "^5.3.1",
"esm-loader-typescript": "^1.0.3",
"events": "^3.3.0",
"mitata": "^0.1.6",
"node-fetch": "^2.6.9",
"prettier": "^3.0.3",
"tsd": "^0.22.0",

View File

@@ -19,7 +19,7 @@ test('removing duplicates when subscribing', async () => {
pool.subscribeMany(relays, [{ authors: [pub] }], {
onevent(event: Event) {
// this should be called only once even though we're listening
// to multiple relays because the events will be catched and
// to multiple relays because the events will be caught and
// deduplicated efficiently (without even being parsed)
received.push(event)
},